Skip to content

Commit f622319

Browse files
committed
fix(name resolution): revert to friendlier plurality-gnostic error msgs
1 parent d82cd19 commit f622319

File tree

7 files changed

+172
-158
lines changed

7 files changed

+172
-158
lines changed

parser-typechecker/src/Unison/PrintError.hs

Lines changed: 139 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -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

parser-typechecker/src/Unison/Typechecker.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,8 @@ typeDirectedNameResolution ppe oldNotes oldType env = do
388388
(TypeVar.liftType foundType)
389389
replace
390390
if not fuzzyNameMatch
391-
then
392-
if typeMatches then Context.Exact else Context.RightNameWrongType
393-
else
394-
if typeMatches then Context.SimilarNameRightType else Context.SimilarNameWrongType
391+
then if typeMatches then Context.Exact else Context.RightNameWrongType
392+
else if typeMatches then Context.SimilarNameRightType else Context.SimilarNameWrongType
395393

396394
-- | Check whether a term matches a type, using a
397395
-- function to resolve the type of @Ref@ constructors

unison-src/transcripts-manual/rewrites.output.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,8 @@ scratch/main> load
338338
339339
19 | bar21
340340
341-
I found one or more terms in scope with similar names and the right types.
342-
If you meant to use one of these, try using it instead:
341+
I found a term in scope with a similar name but a different
342+
type. Maybe you meant this:
343343
344344
bar1 : Nat
345345
```
@@ -391,8 +391,8 @@ scratch/main> load
391391
392392
6 | a1
393393
394-
I found one or more terms in scope with similar names and the right types.
395-
If you meant to use one of these, try using it instead:
394+
I found some terms in scope with similar names but different
395+
types. Maybe you meant one of these:
396396
397397
(<|) : (i ->{g} o) -> i ->{g} o
398398
Bytes.at : Nat -> Bytes -> Optional Nat

unison-src/transcripts/idempotent/destructuring-binds.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ ex4 =
8989
9090
2 | (a,b) = (a Nat.+ b, 19)
9191
92-
I found one or more terms in scope with similar names but the wrong types.
93-
If you meant to use one of these, try using it instead and then adjusting types:
92+
I found some terms in scope with similar names but different
93+
types. Maybe you meant one of these:
9494
9595
(Float.*) : Float -> Float -> Float
9696
(Int.*) : Int -> Int -> Int

unison-src/transcripts/idempotent/fix845.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ Now, typecheck a file with a reference to `Blah.zonk` (which doesn't exist in th
4242
4343
2 | > Blah.zonk [1,2,3]
4444
45-
I found one or more terms in scope with similar names and the right types.
46-
If you meant to use one of these, try using it instead:
45+
I found some terms in scope with similar names but different
46+
types. Maybe you meant one of these:
4747
48+
Text.zonk : Text -> Text
4849
List.zonk : [a] -> [a]
4950
```
5051

unison-src/transcripts/idempotent/formatter.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ brokenDoc = {{ hello }} + 1
194194
Help me out by using a more specific name here or adding a
195195
type annotation.
196196
197-
I found one or more terms in scope with the right names but the wrong types.
198-
If you meant to use one of these, try using it with its full name and then adjusting types:
197+
I found some terms in scope with matching names but different
198+
types. If one of these is what you meant, try using its full
199+
name:
199200
200201
(Float.+) : Float -> Float -> Float
201202
(Int.+) : Int -> Int -> Int

0 commit comments

Comments
 (0)