@@ -685,7 +685,7 @@ renderTypeError e env src = case e of
685685 Type. Var' (TypeVar. Existential {}) -> mempty
686686 _ -> Pr. wrap $ " It should be of type " <> Pr. group (style Type1 (renderType' env expectedType) <> " ." )
687687 UnknownTerm {.. } ->
688- let (correct, rightNameWrongTypes, wrongNameRightTypes, similarNameRightTypes, similarNameWrongTypes ) =
688+ let (suggestionsRightNameRightType, suggestionsRightNameWrongType, suggestionsWrongNameRightType, suggestionsSimilarNameRightType, suggestionsSimilarNameWrongType ) =
689689 foldr
690690 sep
691691 id
@@ -700,16 +700,15 @@ renderTypeError e env src = case e of
700700 C. SimilarNameWrongType -> (_5 %~ (s : )) . r
701701 undefinedSymbolHelp =
702702 mconcat
703- [ ( case expectedType of
704- Type. Var' (TypeVar. Existential {}) ->
705- Pr. wrap " I also don't know what type it should be."
706- _ ->
707- mconcat
708- [ Pr. wrap " I think its type should be:" ,
709- " \n\n " ,
710- Pr. indentN 4 (style Type1 (renderType' env expectedType))
711- ]
712- ),
703+ [ case expectedType of
704+ Type. Var' (TypeVar. Existential {}) ->
705+ Pr. wrap " I also don't know what type it should be."
706+ _ ->
707+ mconcat
708+ [ Pr. wrap " I think its type should be:" ,
709+ " \n\n " ,
710+ Pr. indentN 4 (style Type1 (renderType' env expectedType))
711+ ],
713712 " \n\n " ,
714713 Pr. hang
715714 " Some common causes of this error include:"
@@ -720,89 +719,83 @@ renderTypeError e env src = case e of
720719 ]
721720 )
722721 ]
722+
723+ -- Handle various types of transitions
724+ -- The suggestions should be ordered such that handling the first set of suggestions that is non-empty is sufficient
725+ handleSuggestions (suggestionsRightNameRightType, _, _, _, _)
726+ | not $ null suggestionsRightNameRightType =
727+ mconcat
728+ [ Pr. wrap
729+ ( mconcat
730+ [ mconcat
731+ [ " The name " ,
732+ style Identifier (Var. nameStr unknownTermV),
733+ " is ambiguous. "
734+ ],
735+ case expectedType of
736+ Type. Var' (TypeVar. Existential {}) -> " I couldn't narrow it down by type, as any type would work here."
737+ _ ->
738+ " Its type should be:\n\n "
739+ <> Pr. indentN 4 (style Type1 (renderType' env expectedType))
740+ ]
741+ ),
742+ " \n\n " ,
743+ Pr. wrap " I found some terms in scope that have matching names and types. Maybe you meant one of these:" ,
744+ " \n\n " ,
745+ intercalateMap " \n " (renderSuggestion env) suggestionsRightNameRightType
746+ ]
747+ handleSuggestions (_, suggestionsRightNameWrongType, _, _, _)
748+ | not $ null suggestionsRightNameWrongType =
749+ let helpMeOut =
750+ Pr. wrap
751+ ( mconcat
752+ [ " Help me out by" ,
753+ Pr. bold " using a more specific name here" ,
754+ " or" ,
755+ Pr. bold " adding a type annotation."
756+ ]
757+ )
758+ in Pr. wrap
759+ ( " The name "
760+ <> style Identifier (Var. nameStr unknownTermV)
761+ <> " is ambiguous. I tried to resolve it by type but"
762+ )
763+ <> " "
764+ <> case expectedType of
765+ Type. Var' (TypeVar. Existential {}) -> Pr. wrap (" its type could be anything." <> helpMeOut) <> " \n "
766+ _ ->
767+ mconcat
768+ [ Pr. wrap $
769+ mconcat
770+ [ " no term with that name would pass typechecking." ,
771+ " I think its type should be:"
772+ ],
773+ " \n\n " ,
774+ Pr. indentN 4 (style Type1 (renderType' env expectedType)),
775+ " \n\n " ,
776+ Pr. wrap
777+ ( mconcat
778+ [ " If that's not what you expected, you may have a type error somewhere else in your code." ,
779+ helpMeOut
780+ ]
781+ )
782+ ]
783+ <> " \n\n "
784+ <> formatWrongs preambleRightNameWrongType suggestionsRightNameWrongType
785+ handleSuggestions (_, _, suggestionsSimilarNameRightType, _, _)
786+ | not $ null suggestionsSimilarNameRightType =
787+ formatWrongs preambleSimilarNameRightType suggestionsSimilarNameRightType
788+ handleSuggestions (_, _, _, suggestionsWrongNameRightType, suggestionsSimilarNameWrongType)
789+ | not $ null (suggestionsSimilarNameWrongType ++ suggestionsWrongNameRightType) =
790+ formatWrongs preambleDifferentNameWrongType (suggestionsSimilarNameWrongType ++ suggestionsWrongNameRightType)
791+ handleSuggestions (_, _, _, _, _) = undefinedSymbolHelp
723792 in mconcat
724793 [ " I couldn't figure out what " ,
725794 style ErrorSite (Var. nameStr unknownTermV),
726795 " refers to here:\n\n " ,
727796 annotatedAsErrorSite src termSite,
728797 " \n " ,
729- case correct of
730- [] -> case rightNameWrongTypes of
731- [] -> case similarNameRightTypes of
732- [] ->
733- -- If available, show any 'WrongNameRightType' or 'SimilarNameWrongType' suggestions
734- -- Otherwise if no suggestions are available show 'undefinedSymbolHelp'
735- if null wrongNameRightTypes && null similarNameWrongTypes
736- then undefinedSymbolHelp
737- else
738- mconcat
739- [ if null similarNameWrongTypes
740- then " "
741- else formatWrongs similarNameWrongTypeText similarNameWrongTypes,
742- if null wrongNameRightTypes
743- then " "
744- else formatWrongs wrongNameRightTypeText wrongNameRightTypes
745- ]
746- similarNameRightTypes -> formatWrongs similarNameRightTypeText similarNameRightTypes
747- rightNameWrongTypes ->
748- let helpMeOut =
749- Pr. wrap
750- ( mconcat
751- [ " Help me out by" ,
752- Pr. bold " using a more specific name here" ,
753- " or" ,
754- Pr. bold " adding a type annotation."
755- ]
756- )
757- in Pr. wrap
758- ( " The name "
759- <> style Identifier (Var. nameStr unknownTermV)
760- <> " is ambiguous. I tried to resolve it by type but"
761- )
762- <> " "
763- <> case expectedType of
764- Type. Var' (TypeVar. Existential {}) -> Pr. wrap (" its type could be anything." <> helpMeOut) <> " \n "
765- _ ->
766- mconcat
767- [ ( Pr. wrap $
768- mconcat
769- [ " no term with that name would pass typechecking." ,
770- " I think its type should be:"
771- ]
772- ),
773- " \n\n " ,
774- Pr. indentN 4 (style Type1 (renderType' env expectedType)),
775- " \n\n " ,
776- Pr. wrap
777- ( mconcat
778- [ " If that's not what you expected, you may have a type error somewhere else in your code." ,
779- helpMeOut
780- ]
781- )
782- ]
783- <> " \n\n "
784- <> formatWrongs rightNameWrongTypeText rightNameWrongTypes
785- suggs ->
786- mconcat
787- [ Pr. wrap
788- ( mconcat
789- [ mconcat
790- [ " The name " ,
791- style Identifier (Var. nameStr unknownTermV),
792- " is ambiguous. "
793- ],
794- case expectedType of
795- Type. Var' (TypeVar. Existential {}) -> " I couldn't narrow it down by type, as any type would work here."
796- _ ->
797- " Its type should be:\n\n "
798- <> Pr. indentN 4 (style Type1 (renderType' env expectedType))
799- ]
800- ),
801- " \n\n " ,
802- Pr. wrap " I found some terms in scope that have matching names and types. Maybe you meant one of these:" ,
803- " \n\n " ,
804- intercalateMap " \n " (renderSuggestion env) suggs
805- ]
798+ handleSuggestions (suggestionsRightNameRightType, suggestionsRightNameWrongType, suggestionsWrongNameRightType, suggestionsSimilarNameRightType, suggestionsSimilarNameWrongType)
806799 ]
807800 DuplicateDefinitions {.. } ->
808801 mconcat
@@ -862,46 +855,64 @@ renderTypeError e env src = case e of
862855 summary note
863856 ]
864857 where
865- rightNameWrongTypeText _ =
866- mconcat
867- [ " I found one or more terms in scope with the " ,
868- Pr. bold " right names " ,
869- " but the " ,
870- Pr. bold " wrong types." ,
871- " \n " ,
872- " If you meant to use one of these, try using it with its full name and then adjusting types" ,
873- " :\n\n "
874- ]
875- similarNameRightTypeText _ =
876- mconcat
877- [ " I found one or more terms in scope with " ,
878- Pr. bold " similar names " ,
879- " and the " ,
880- Pr. bold " right types." ,
881- " \n " ,
882- " If you meant to use one of these, try using it instead" ,
883- " :\n\n "
884- ]
885- similarNameWrongTypeText _ =
886- mconcat
887- [ " I found one or more terms in scope with " ,
888- Pr. bold " similar names " ,
889- " but the " ,
890- Pr. bold " wrong types." ,
891- " \n " ,
892- " If you meant to use one of these, try using it instead and then adjusting types" ,
893- " :\n\n "
894- ]
895- wrongNameRightTypeText _ =
896- mconcat
897- [ " I found one or more terms in scope with the " ,
898- Pr. bold " wrong names " ,
899- " but the " ,
900- Pr. bold " right types." ,
901- " \n " ,
902- " If you meant to use one of these, try using it instead" ,
903- " :\n\n "
904- ]
858+ preambleRightNameWrongType pl =
859+ Pr. paragraphyText
860+ ( mconcat
861+ [ " I found " ,
862+ pl " a term" " some terms" ,
863+ " in scope with " ,
864+ pl " a " " " ,
865+ " matching name" ,
866+ pl " " " s" ,
867+ " but " ,
868+ pl " a " " " ,
869+ " different type" ,
870+ pl " " " s" ,
871+ " . " ,
872+ " If " ,
873+ pl " this" " one of these" ,
874+ " is what you meant, try using its full name:"
875+ ]
876+ )
877+ <> " \n\n "
878+ preambleSimilarNameRightType pl =
879+ Pr. paragraphyText
880+ ( mconcat
881+ [ " I found " ,
882+ pl " a term" " some terms" ,
883+ " in scope with " ,
884+ pl " a " " " ,
885+ " matching type" ,
886+ pl " " " s" ,
887+ " but " ,
888+ pl " a " " " ,
889+ " different name" ,
890+ pl " " " s" ,
891+ " . " ,
892+ " Maybe you meant " ,
893+ pl " this" " one of these" ,
894+ " :\n\n "
895+ ]
896+ )
897+ preambleDifferentNameWrongType pl =
898+ Pr. paragraphyText
899+ ( mconcat
900+ [ " I found " ,
901+ pl " a term" " some terms" ,
902+ " in scope with " ,
903+ pl " a " " " ,
904+ " similar name" ,
905+ pl " " " s" ,
906+ " but " ,
907+ pl " a " " " ,
908+ " different type" ,
909+ pl " " " s" ,
910+ " . " ,
911+ " Maybe you meant " ,
912+ pl " this" " one of these" ,
913+ " :\n\n "
914+ ]
915+ )
905916 formatWrongs txt wrongs =
906917 let sz = length wrongs
907918 pl a b = if sz == 1 then a else b
0 commit comments