Skip to content

Commit 58acfae

Browse files
committed
Merge pull request purescript#1140 from purescript/warn-on-class-operator
Warn about declaring operators in classes
2 parents c3342ee + 76e1087 commit 58acfae

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Language/PureScript/Errors.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ data SimpleErrorMessage
119119
| TransitiveExportError DeclarationRef [DeclarationRef]
120120
| ShadowedName Ident
121121
| WildcardInferredType Type
122+
| ClassOperator ProperName Ident
122123
deriving (Show)
123124

124125
-- |
@@ -226,6 +227,7 @@ errorCode em = case unwrapErrorMessage em of
226227
(TransitiveExportError _ _) -> "TransitiveExportError"
227228
(ShadowedName _) -> "ShadowedName"
228229
(WildcardInferredType _) -> "WildcardInferredType"
230+
(ClassOperator _ _) -> "ClassOperator"
229231

230232
-- |
231233
-- A stack trace for an error
@@ -543,6 +545,11 @@ prettyPrintSingleError full e = prettyPrintErrorMessage <$> onTypesInErrorMessag
543545
: map (line . prettyPrintExport) ys
544546
goSimple (ShadowedName nm) =
545547
line $ "Name '" ++ show nm ++ "' was shadowed."
548+
goSimple (ClassOperator className opName) =
549+
paras [ line $ "Class '" ++ show className ++ "' declares operator " ++ show opName ++ "."
550+
, indent $ line $ "This may be disallowed in the future - consider declaring a named member in the class and making the operator an alias:"
551+
, indent $ line $ show opName ++ " = someMember"
552+
]
546553
goSimple (WildcardInferredType ty) =
547554
line $ "The wildcard type definition has the inferred type " ++ prettyPrintType ty
548555
go (NotYetDefined names err) =

src/Language/PureScript/Linter.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,20 @@ lint (Module _ mn ds _) = censor (onErrorMessages (ErrorInModule mn)) $ mapM_ li
5151

5252
lintDeclaration :: Declaration -> m ()
5353
lintDeclaration d =
54-
let (f, _, _, _, _) = everythingWithContextOnValues moduleNames mempty mappend def stepE stepB def def
54+
let (f, _, _, _, _) = everythingWithContextOnValues moduleNames mempty mappend stepD stepE stepB def def
5555
in tell (f d)
5656
where
5757
def s _ = (s, mempty)
5858

59+
stepD :: S.Set Ident -> Declaration -> (S.Set Ident, MultipleErrors)
60+
stepD s (TypeClassDeclaration name _ _ decls) = (s, foldr go mempty decls)
61+
where
62+
go :: Declaration -> MultipleErrors -> MultipleErrors
63+
go (PositionedDeclaration _ _ d') errs = go d' errs
64+
go (TypeDeclaration op@(Op _) _) errs = errorMessage (ClassOperator name op) <> errs
65+
go _ errs = errs
66+
stepD s _ = (s, mempty)
67+
5968
stepE :: S.Set Ident -> Expr -> (S.Set Ident, MultipleErrors)
6069
stepE s (Abs (Left name) _) = bind s name
6170
stepE s (Let ds' _) =

0 commit comments

Comments
 (0)