Skip to content

Commit 5ed9384

Browse files
bbqbaronpaf31
authored andcommitted
2374: Add error message for ambiguous type variables in inferred contexts (purescript#2410)
* 2374: Add error message for ambiguous type variables in inferred contexts * 2323 add contribution entry. remove Ident from AmbiguousTypeVariables since it's redundant with the enclosing compiler error's ident * 2323 update error message * 2374 multiline error message and print type, not type atom
1 parent 2b04f5a commit 5ed9384

6 files changed

Lines changed: 24 additions & 2 deletions

File tree

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ This file lists the contributors to the PureScript compiler project, and the ter
8383
- [@vkorablin](https://github.com/vkorablin) (Vladimir Korablin) - My existing contributions and all future contributions until further notice are Copyright Vladimir Korablin, and are licensed to the owners and users of the PureScript compiler project under the terms of the MIT license.
8484
- [@zudov](https://github.com/zudov) (Konstantin Zudov) My existing contributions and all future contributions until further notice are Copyright Konstantin Zudov, and are licensed to the owners and users of the PureScript compiler project under the terms of the [MIT license](http://opensource.org/licenses/MIT).
8585
- [@brandonhamilton](https://github.com/brandonhamilton) (Brandon Hamilton) My existing contributions and all future contributions until further notice are Copyright Brandon Hamilton, and are licensed to the owners and users of the PureScript compiler project under the terms of the [MIT license](http://opensource.org/licenses/MIT).
86+
- [@bbqbaron](https://github.com/bbqbaron) (Eric Loren) My existing contributions and all future contributions until further notice are Copyright Eric Loren, and are licensed to the owners and users of the PureScript compiler project under the terms of the [MIT license](http://opensource.org/licenses/MIT).
8687

8788
### Companies
8889

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- @shouldFailWith NoInstanceFound
2+
3+
module Main where
4+
5+
import Prelude
6+
7+
data Foo = Bar
8+
9+
spin :: forall a. a -> Foo
10+
spin x = Bar
11+
12+
main = show <<< spin
13+

examples/failing/ConstraintInference.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- @shouldFailWith NoInstanceFound
1+
-- @shouldFailWith AmbiguousTypeVariables
22

33
module Main where
44

src/Language/PureScript/AST/Declarations.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ data SimpleErrorMessage
8383
| ConstrainedTypeUnified Type Type
8484
| OverlappingInstances (Qualified (ProperName 'ClassName)) [Type] [Qualified Ident]
8585
| NoInstanceFound Constraint
86+
| AmbiguousTypeVariables Type Constraint
8687
| UnknownClass (Qualified (ProperName 'ClassName))
8788
| PossiblyInfiniteInstance (Qualified (ProperName 'ClassName)) [Type]
8889
| CannotDerive (Qualified (ProperName 'ClassName)) [Type]

src/Language/PureScript/Errors.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ errorCode em = case unwrapErrorMessage em of
118118
ConstrainedTypeUnified{} -> "ConstrainedTypeUnified"
119119
OverlappingInstances{} -> "OverlappingInstances"
120120
NoInstanceFound{} -> "NoInstanceFound"
121+
AmbiguousTypeVariables{} -> "AmbiguousTypeVariables"
121122
UnknownClass{} -> "UnknownClass"
122123
PossiblyInfiniteInstance{} -> "PossiblyInfiniteInstance"
123124
CannotDerive{} -> "CannotDerive"
@@ -261,6 +262,7 @@ onTypesInErrorMessageM f (ErrorMessage hints simple) = ErrorMessage <$> traverse
261262
gSimple (ExprDoesNotHaveType e t) = ExprDoesNotHaveType e <$> f t
262263
gSimple (InvalidInstanceHead t) = InvalidInstanceHead <$> f t
263264
gSimple (NoInstanceFound con) = NoInstanceFound <$> overConstraintArgs (traverse f) con
265+
gSimple (AmbiguousTypeVariables t con) = AmbiguousTypeVariables <$> (f t) <*> pure con
264266
gSimple (OverlappingInstances cl ts insts) = OverlappingInstances cl <$> traverse f ts <*> pure insts
265267
gSimple (PossiblyInfiniteInstance cl ts) = PossiblyInfiniteInstance cl <$> traverse f ts
266268
gSimple (CannotDerive cl ts) = CannotDerive cl <$> traverse f ts
@@ -636,6 +638,11 @@ prettyPrintSingleError (PPEOptions codeColor full level showWiki) e = flip evalS
636638
where
637639
go TUnknown{} = True
638640
go _ = False
641+
renderSimpleErrorMessage (AmbiguousTypeVariables t _) =
642+
paras [ line "The inferred type"
643+
, indent $ line $ markCode $ prettyPrintType t
644+
, line "has type variables which are not mentioned in the body of the type. Consider adding a type annotation."
645+
]
639646
renderSimpleErrorMessage (PossiblyInfiniteInstance nm ts) =
640647
paras [ line "Type class instance for"
641648
, markCodeBox $ indent $ Box.hsep 1 Box.left

src/Language/PureScript/TypeChecker/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ typesOf bindingGroupType moduleName vals = withFreshSubstitution $ do
111111
let solved = foldMap (S.fromList . fdDetermined) typeClassDependencies
112112
let constraintTypeVars = nub . foldMap (unknownsInType . fst) . filter ((`notElem` solved) . snd) $ zip (constraintArgs con) [0..]
113113
when (any (`notElem` unsolvedTypeVars) constraintTypeVars) $ do
114-
throwError . onErrorMessages (replaceTypes currentSubst) . errorMessage $ NoInstanceFound con
114+
throwError . onErrorMessages (replaceTypes currentSubst) . errorMessage $ AmbiguousTypeVariables generalized con
115115

116116
-- Check skolem variables did not escape their scope
117117
skolemEscapeCheck val'

0 commit comments

Comments
 (0)