git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
2374: Add error message for ambiguous type variables in inferred contexts (#2410)
5ed9384bbqbaron
CONTRIBUTORS.md+1
@@ -83,6 +83,7 @@ This file lists the contributors to the PureScript compiler project, and the ter
83- [@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.
83- [@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.
84- [@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).
84- [@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).
85- [@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).
85- [@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).
86
87
87### Companies
88### Companies
88
89
examples/failing/ConstraintFailure.pursnew file+13
@@ -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+1−1
@@ -1,4 +1,4 @@
1−-- @shouldFailWith NoInstanceFound
1+-- @shouldFailWith AmbiguousTypeVariables
2
2
3module Main where
3module Main where
4
4
src/Language/PureScript/AST/Declarations.hs+1
@@ -83,6 +83,7 @@ data SimpleErrorMessage
83 | ConstrainedTypeUnified Type Type
83 | ConstrainedTypeUnified Type Type
84 | OverlappingInstances (Qualified (ProperName 'ClassName)) [Type] [Qualified Ident]
84 | OverlappingInstances (Qualified (ProperName 'ClassName)) [Type] [Qualified Ident]
85 | NoInstanceFound Constraint
85 | NoInstanceFound Constraint
86+ | AmbiguousTypeVariables Type Constraint
86 | UnknownClass (Qualified (ProperName 'ClassName))
87 | UnknownClass (Qualified (ProperName 'ClassName))
87 | PossiblyInfiniteInstance (Qualified (ProperName 'ClassName)) [Type]
88 | PossiblyInfiniteInstance (Qualified (ProperName 'ClassName)) [Type]
88 | CannotDerive (Qualified (ProperName 'ClassName)) [Type]
89 | CannotDerive (Qualified (ProperName 'ClassName)) [Type]
src/Language/PureScript/Errors.hs+7
@@ -118,6 +118,7 @@ errorCode em = case unwrapErrorMessage em of
118 ConstrainedTypeUnified{} -> "ConstrainedTypeUnified"
118 ConstrainedTypeUnified{} -> "ConstrainedTypeUnified"
119 OverlappingInstances{} -> "OverlappingInstances"
119 OverlappingInstances{} -> "OverlappingInstances"
120 NoInstanceFound{} -> "NoInstanceFound"
120 NoInstanceFound{} -> "NoInstanceFound"
121+ AmbiguousTypeVariables{} -> "AmbiguousTypeVariables"
121 UnknownClass{} -> "UnknownClass"
122 UnknownClass{} -> "UnknownClass"
122 PossiblyInfiniteInstance{} -> "PossiblyInfiniteInstance"
123 PossiblyInfiniteInstance{} -> "PossiblyInfiniteInstance"
123 CannotDerive{} -> "CannotDerive"
124 CannotDerive{} -> "CannotDerive"
@@ -261,6 +262,7 @@ onTypesInErrorMessageM f (ErrorMessage hints simple) = ErrorMessage <$> traverse
261 gSimple (ExprDoesNotHaveType e t) = ExprDoesNotHaveType e <$> f t
262 gSimple (ExprDoesNotHaveType e t) = ExprDoesNotHaveType e <$> f t
262 gSimple (InvalidInstanceHead t) = InvalidInstanceHead <$> f t
263 gSimple (InvalidInstanceHead t) = InvalidInstanceHead <$> f t
263 gSimple (NoInstanceFound con) = NoInstanceFound <$> overConstraintArgs (traverse f) con
264 gSimple (NoInstanceFound con) = NoInstanceFound <$> overConstraintArgs (traverse f) con
265+ gSimple (AmbiguousTypeVariables t con) = AmbiguousTypeVariables <$> (f t) <*> pure con
264 gSimple (OverlappingInstances cl ts insts) = OverlappingInstances cl <$> traverse f ts <*> pure insts
266 gSimple (OverlappingInstances cl ts insts) = OverlappingInstances cl <$> traverse f ts <*> pure insts
265 gSimple (PossiblyInfiniteInstance cl ts) = PossiblyInfiniteInstance cl <$> traverse f ts
267 gSimple (PossiblyInfiniteInstance cl ts) = PossiblyInfiniteInstance cl <$> traverse f ts
266 gSimple (CannotDerive cl ts) = CannotDerive cl <$> traverse f ts
268 gSimple (CannotDerive cl ts) = CannotDerive cl <$> traverse f ts
@@ -636,6 +638,11 @@ prettyPrintSingleError (PPEOptions codeColor full level showWiki) e = flip evalS
636 where
638 where
637 go TUnknown{} = True
639 go TUnknown{} = True
638 go _ = False
640 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+ ]
639 renderSimpleErrorMessage (PossiblyInfiniteInstance nm ts) =
646 renderSimpleErrorMessage (PossiblyInfiniteInstance nm ts) =
640 paras [ line "Type class instance for"
647 paras [ line "Type class instance for"
641 , markCodeBox $ indent $ Box.hsep 1 Box.left
648 , markCodeBox $ indent $ Box.hsep 1 Box.left
src/Language/PureScript/TypeChecker/Types.hs+1−1
@@ -111,7 +111,7 @@ typesOf bindingGroupType moduleName vals = withFreshSubstitution $ do
111 let solved = foldMap (S.fromList . fdDetermined) typeClassDependencies
111 let solved = foldMap (S.fromList . fdDetermined) typeClassDependencies
112 let constraintTypeVars = nub . foldMap (unknownsInType . fst) . filter ((`notElem` solved) . snd) $ zip (constraintArgs con) [0..]
112 let constraintTypeVars = nub . foldMap (unknownsInType . fst) . filter ((`notElem` solved) . snd) $ zip (constraintArgs con) [0..]
113 when (any (`notElem` unsolvedTypeVars) constraintTypeVars) $ do
113 when (any (`notElem` unsolvedTypeVars) constraintTypeVars) $ do
114− throwError . onErrorMessages (replaceTypes currentSubst) . errorMessage $ NoInstanceFound con
114+ throwError . onErrorMessages (replaceTypes currentSubst) . errorMessage $ AmbiguousTypeVariables generalized con
115
115
116 -- Check skolem variables did not escape their scope
116 -- Check skolem variables did not escape their scope
117 skolemEscapeCheck val'
117 skolemEscapeCheck val'