@@ -187,7 +187,7 @@ data SplitBindingGroup = SplitBindingGroup
187187-- This function also generates fresh unification variables for the types of
188188-- declarations without type annotations, returned in the 'UntypedData' structure.
189189typeDictionaryForBindingGroup
190− :: (MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m)
190+ :: (MonadState CheckState m, MonadWriter MultipleErrors m)
191191 => Maybe ModuleName
192192 -> [(Ident, Expr)]
193193 -> m SplitBindingGroup
@@ -197,7 +197,7 @@ typeDictionaryForBindingGroup moduleName vals = do
197197 -- fully expanded types.
198198 let (untyped, typed) = partitionEithers (map splitTypeAnnotation vals)
199199 (typedDict, typed') <- fmap unzip . for typed $ \(ident, (expr, ty, checkType)) -> do
200− ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
200+ ty' <- replaceTypeWildcards ty
201201 return ((ident, ty'), (ident, (expr, ty', checkType)))
202202 -- Create fresh unification variables for the types of untyped declarations
203203 (untypedDict, untyped') <- fmap unzip . for untyped $ \(ident, expr) -> do
@@ -233,11 +233,14 @@ checkTypedBindingGroupElement mn (ident, (val, ty, checkType)) dict = do
233233 -- Kind check
234234 (kind, args) <- kindOfWithScopedVars ty
235235 checkTypeKind ty kind
236+ -- We replace type synonyms _after_ kind-checking, since we don't want type
237+ -- synonym expansion to bring type variables into scope. See #2542.
238+ ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
236239 -- Check the type with the new names in scope
237240 val' <- if checkType
238− then withScopedTypeVars mn args $ bindNames dict $ TypedValue True <$> check val ty <*> pure ty
239− else return (TypedValue False val ty)
240− return (ident, (val', ty))
241+ then withScopedTypeVars mn args $ bindNames dict $ TypedValue True <$> check val ty' <*> pure ty'
242+ else return (TypedValue False val ty')
243+ return (ident, (val', ty'))
241244
242245-- | Infer a type for a value in a binding group which lacks an annotation.
243246typeForBindingGroupElement