Skip to content

Commit e83181d

Browse files
committed
Support local type synonyms natively
1 parent 3de6214 commit e83181d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+994
-155
lines changed

lib/purescript-cst/src/Language/PureScript/AST/Traversals.hs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -499,50 +499,56 @@ everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j
499499
k' s (ConditionGuard e) = ConditionGuard <$> g'' s e
500500
k' s (PatternGuard b e) = PatternGuard <$> h'' s b <*> g'' s e
501501

502-
data ScopedIdent = LocalIdent Ident | ToplevelIdent Ident
502+
data ScopedName = LocalName Name | ToplevelName Name
503503
deriving (Show, Eq, Ord)
504504

505-
inScope :: Ident -> S.Set ScopedIdent -> Bool
506-
inScope i s = (LocalIdent i `S.member` s) || (ToplevelIdent i `S.member` s)
505+
inScope' :: (a -> Name) -> a -> S.Set ScopedName -> Bool
506+
inScope' ctor i s = (LocalName (ctor i) `S.member` s) || (ToplevelName (ctor i) `S.member` s)
507+
508+
inScope :: Ident -> S.Set ScopedName -> Bool
509+
inScope = inScope' IdentName
510+
511+
typeInScope :: ProperName 'TypeName -> S.Set ScopedName -> Bool
512+
typeInScope = inScope' TyName
507513

508514
everythingWithScope
509515
:: forall r
510516
. (Monoid r)
511-
=> (S.Set ScopedIdent -> Declaration -> r)
512-
-> (S.Set ScopedIdent -> Expr -> r)
513-
-> (S.Set ScopedIdent -> Binder -> r)
514-
-> (S.Set ScopedIdent -> CaseAlternative -> r)
515-
-> (S.Set ScopedIdent -> DoNotationElement -> r)
516-
-> ( S.Set ScopedIdent -> Declaration -> r
517-
, S.Set ScopedIdent -> Expr -> r
518-
, S.Set ScopedIdent -> Binder -> r
519-
, S.Set ScopedIdent -> CaseAlternative -> r
520-
, S.Set ScopedIdent -> DoNotationElement -> r
517+
=> (S.Set ScopedName -> Declaration -> r)
518+
-> (S.Set ScopedName -> Expr -> r)
519+
-> (S.Set ScopedName -> Binder -> r)
520+
-> (S.Set ScopedName -> CaseAlternative -> r)
521+
-> (S.Set ScopedName -> DoNotationElement -> r)
522+
-> ( S.Set ScopedName -> Declaration -> r
523+
, S.Set ScopedName -> Expr -> r
524+
, S.Set ScopedName -> Binder -> r
525+
, S.Set ScopedName -> CaseAlternative -> r
526+
, S.Set ScopedName -> DoNotationElement -> r
521527
)
522528
everythingWithScope f g h i j = (f'', g'', h'', i'', \s -> snd . j'' s)
523529
where
524-
f'' :: S.Set ScopedIdent -> Declaration -> r
530+
f'' :: S.Set ScopedName -> Declaration -> r
525531
f'' s a = f s a <> f' s a
526532

527-
f' :: S.Set ScopedIdent -> Declaration -> r
533+
f' :: S.Set ScopedName -> Declaration -> r
528534
f' s (DataBindingGroupDeclaration ds) =
529-
let s' = S.union s (S.fromList (map ToplevelIdent (mapMaybe getDeclIdent (NEL.toList ds))))
535+
let s' = S.union s (S.fromList (map ToplevelName (mapMaybe declName (NEL.toList ds))))
530536
in foldMap (f'' s') ds
531537
f' s (ValueDecl _ name _ bs val) =
532-
let s' = S.insert (ToplevelIdent name) s
538+
let s' = S.insert (ToplevelName (IdentName name)) s
533539
s'' = S.union s' (S.fromList (concatMap localBinderNames bs))
534540
in foldMap (h'' s') bs <> foldMap (l' s'') val
535541
f' s (BindingGroupDeclaration ds) =
536-
let s' = S.union s (S.fromList (NEL.toList (fmap (\((_, name), _, _) -> ToplevelIdent name) ds)))
542+
let s' = S.union s (S.fromList (NEL.toList (fmap (\((_, name), _, _) -> ToplevelName (IdentName name)) ds)))
537543
in foldMap (\(_, _, val) -> g'' s' val) ds
538544
f' s (TypeClassDeclaration _ _ _ _ _ ds) = foldMap (f'' s) ds
539545
f' s (TypeInstanceDeclaration _ _ _ _ _ _ _ (ExplicitInstance ds)) = foldMap (f'' s) ds
540546
f' _ _ = mempty
541547

542-
g'' :: S.Set ScopedIdent -> Expr -> r
548+
g'' :: S.Set ScopedName -> Expr -> r
543549
g'' s a = g s a <> g' s a
544550

545-
g' :: S.Set ScopedIdent -> Expr -> r
551+
g' :: S.Set ScopedName -> Expr -> r
546552
g' s (Literal _ l) = lit g'' s l
547553
g' s (UnaryMinus _ v1) = g'' s v1
548554
g' s (BinaryNoParens op v1 v2) = g'' s op <> g'' s v1 <> g'' s v2
@@ -560,7 +566,7 @@ everythingWithScope f g h i j = (f'', g'', h'', i'', \s -> snd . j'' s)
560566
g' s (Case vs alts) = foldMap (g'' s) vs <> foldMap (i'' s) alts
561567
g' s (TypedValue _ v1 _) = g'' s v1
562568
g' s (Let _ ds v1) =
563-
let s' = S.union s (S.fromList (map LocalIdent (mapMaybe getDeclIdent ds)))
569+
let s' = S.union s (S.fromList (map LocalName (mapMaybe declName ds)))
564570
in foldMap (f'' s') ds <> g'' s' v1
565571
g' s (Do _ es) = fold . snd . mapAccumL j'' s $ es
566572
g' s (Ado _ es v1) =
@@ -569,46 +575,46 @@ everythingWithScope f g h i j = (f'', g'', h'', i'', \s -> snd . j'' s)
569575
g' s (PositionedValue _ _ v1) = g'' s v1
570576
g' _ _ = mempty
571577

572-
h'' :: S.Set ScopedIdent -> Binder -> r
578+
h'' :: S.Set ScopedName -> Binder -> r
573579
h'' s a = h s a <> h' s a
574580

575-
h' :: S.Set ScopedIdent -> Binder -> r
581+
h' :: S.Set ScopedName -> Binder -> r
576582
h' s (LiteralBinder _ l) = lit h'' s l
577583
h' s (ConstructorBinder _ _ bs) = foldMap (h'' s) bs
578584
h' s (BinaryNoParensBinder b1 b2 b3) = foldMap (h'' s) [b1, b2, b3]
579585
h' s (ParensInBinder b) = h'' s b
580-
h' s (NamedBinder _ name b1) = h'' (S.insert (LocalIdent name) s) b1
586+
h' s (NamedBinder _ name b1) = h'' (S.insert (LocalName (IdentName name)) s) b1
581587
h' s (PositionedBinder _ _ b1) = h'' s b1
582588
h' s (TypedBinder _ b1) = h'' s b1
583589
h' _ _ = mempty
584590

585-
lit :: (S.Set ScopedIdent -> a -> r) -> S.Set ScopedIdent -> Literal a -> r
591+
lit :: (S.Set ScopedName -> a -> r) -> S.Set ScopedName -> Literal a -> r
586592
lit go s (ArrayLiteral as) = foldMap (go s) as
587593
lit go s (ObjectLiteral as) = foldMap (go s . snd) as
588594
lit _ _ _ = mempty
589595

590-
i'' :: S.Set ScopedIdent -> CaseAlternative -> r
596+
i'' :: S.Set ScopedName -> CaseAlternative -> r
591597
i'' s a = i s a <> i' s a
592598

593-
i' :: S.Set ScopedIdent -> CaseAlternative -> r
599+
i' :: S.Set ScopedName -> CaseAlternative -> r
594600
i' s (CaseAlternative bs gs) =
595601
let s' = S.union s (S.fromList (concatMap localBinderNames bs))
596602
in foldMap (h'' s) bs <> foldMap (l' s') gs
597603

598-
j'' :: S.Set ScopedIdent -> DoNotationElement -> (S.Set ScopedIdent, r)
604+
j'' :: S.Set ScopedName -> DoNotationElement -> (S.Set ScopedName, r)
599605
j'' s a = let (s', r) = j' s a in (s', j s a <> r)
600606

601-
j' :: S.Set ScopedIdent -> DoNotationElement -> (S.Set ScopedIdent, r)
607+
j' :: S.Set ScopedName -> DoNotationElement -> (S.Set ScopedName, r)
602608
j' s (DoNotationValue v) = (s, g'' s v)
603609
j' s (DoNotationBind b v) =
604610
let s' = S.union (S.fromList (localBinderNames b)) s
605611
in (s', h'' s b <> g'' s v)
606612
j' s (DoNotationLet ds) =
607-
let s' = S.union s (S.fromList (map LocalIdent (mapMaybe getDeclIdent ds)))
613+
let s' = S.union s (S.fromList (map LocalName (mapMaybe declName ds)))
608614
in (s', foldMap (f'' s') ds)
609615
j' s (PositionedDoNotationElement _ _ e1) = j'' s e1
610616

611-
k' :: S.Set ScopedIdent -> Guard -> (S.Set ScopedIdent, r)
617+
k' :: S.Set ScopedName -> Guard -> (S.Set ScopedName, r)
612618
k' s (ConditionGuard e) = (s, g'' s e)
613619
k' s (PatternGuard b e) =
614620
let s' = S.union (S.fromList (localBinderNames b)) s
@@ -619,12 +625,7 @@ everythingWithScope f g h i j = (f'', g'', h'', i'', \s -> snd . j'' s)
619625
let (s', r) = k' s grd
620626
in r <> l' s' (GuardedExpr gs e)
621627

622-
getDeclIdent :: Declaration -> Maybe Ident
623-
getDeclIdent (ValueDeclaration vd) = Just (valdeclIdent vd)
624-
getDeclIdent (TypeDeclaration td) = Just (tydeclIdent td)
625-
getDeclIdent _ = Nothing
626-
627-
localBinderNames = map LocalIdent . binderNames
628+
localBinderNames = map (LocalName . IdentName) . binderNames
628629

629630
accumTypes
630631
:: (Monoid r)

lib/purescript-cst/src/Language/PureScript/CST/Convert.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ convertLetBinding fileName = \case
225225
binding@(LetBindingPattern _ a _ b) -> do
226226
let ann = uncurry (sourceAnnCommented fileName) $ letBindingRange binding
227227
AST.BoundValueDeclaration ann (convertBinder fileName a) (convertWhere fileName b)
228+
binding@(LetBindingType _ (DataHead _ a vars) _ bd) -> do
229+
let ann = uncurry (sourceAnnCommented fileName) $ letBindingRange binding
230+
AST.TypeSynonymDeclaration ann (nameValue a) (goTypeVar <$> vars) (convertType fileName bd)
231+
binding@(LetBindingKindSignature _ _ (Labeled name _ ty)) -> do
232+
let ann = uncurry (sourceAnnCommented fileName) $ letBindingRange binding
233+
AST.KindDeclaration ann AST.TypeSynonymSig (nameValue name) $ convertType fileName ty
234+
where
235+
goTypeVar = \case
236+
TypeVarKinded (Wrapped _ (Labeled x _ y) _) -> (getIdent $ nameValue x, Just $ convertType fileName y)
237+
TypeVarName x -> (getIdent $ nameValue x, Nothing)
228238

229239
convertExpr :: forall a. String -> Expr a -> AST.Expr
230240
convertExpr fileName = go

lib/purescript-cst/src/Language/PureScript/CST/Flatten.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ flattenLetBinding = \case
163163
LetBindingSignature _ a -> flattenLabeled flattenName flattenType a
164164
LetBindingName _ a -> flattenValueBindingFields a
165165
LetBindingPattern _ a b c -> flattenBinder a <> pure b <> flattenWhere c
166+
LetBindingType _ a b c -> flattenDataHead a <> pure b <> flattenType c
167+
LetBindingKindSignature _ a b -> pure a <> flattenLabeled flattenName flattenType b
166168

167169
flattenWhere :: Where a -> DList SourceToken
168170
flattenWhere (Where a b) =

lib/purescript-cst/src/Language/PureScript/CST/Parser.y

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ letBinding :: { LetBinding () }
457457
| ident guardedDecl { LetBindingName () (ValueBindingFields $1 [] $2) }
458458
| ident many(binderAtom) guardedDecl { LetBindingName () (ValueBindingFields $1 (NE.toList $2) $3) }
459459
| binder1 '=' exprWhere { LetBindingPattern () $1 $2 $3 }
460+
| typeHead '=' type {% checkNoWildcards $3 *> pure (LetBindingType () $1 $2 $3) }
461+
| 'type' properName '::' type {% checkNoWildcards $4 *> pure (LetBindingKindSignature () $1 (Labeled (getProperName $2) $3 $4)) }
460462
461463
caseBranch :: { (Separated (Binder ()), Guarded ()) }
462464
: sep(binder1, ',') guardedCase { ($1, $2) }

lib/purescript-cst/src/Language/PureScript/CST/Positions.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ letBindingRange = \case
307307
LetBindingSignature _ (Labeled a _ b) -> (nameTok a, snd $ typeRange b)
308308
LetBindingName _ a -> valueBindingFieldsRange a
309309
LetBindingPattern _ a _ b -> (fst $ binderRange a, snd $ whereRange b)
310+
LetBindingType _ a _ b -> (fst $ dataHeadRange a, snd $ typeRange b)
311+
LetBindingKindSignature _ a (Labeled _ _ b) -> (a, snd $ typeRange b)
310312

311313
doStatementRange :: DoStatement a -> TokenRange
312314
doStatementRange = \case

lib/purescript-cst/src/Language/PureScript/CST/Types.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ data LetBinding a
401401
= LetBindingSignature a (Labeled (Name Ident) (Type a))
402402
| LetBindingName a (ValueBindingFields a)
403403
| LetBindingPattern a (Binder a) SourceToken (Where a)
404+
| LetBindingType a (DataHead a) SourceToken (Type a)
405+
| LetBindingKindSignature a SourceToken (Labeled (Name (N.ProperName 'N.TypeName)) (Type a))
404406
deriving (Show, Eq, Ord, Functor, Foldable, Traversable, Generic)
405407

406408
data DoBlock a = DoBlock

src/Language/PureScript/Errors.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ data SimpleErrorMessage
133133
| TransitiveDctorExportError DeclarationRef [ProperName 'ConstructorName]
134134
| HiddenConstructors DeclarationRef (Qualified (ProperName 'ClassName))
135135
| ShadowedName Ident
136+
| ShadowedTypeName (ProperName 'TypeName)
136137
| ShadowedTypeVar Text
137138
| UnusedTypeVar Text
138-
| UnusedName Ident
139+
| UnusedName Name
139140
| UnusedDeclaration Ident
140141
| WildcardInferredType SourceType Context
141142
| HoleInferredType Text SourceType Context (Maybe TypeSearch)
@@ -306,6 +307,7 @@ errorCode em = case unwrapErrorMessage em of
306307
ShadowedName{} -> "ShadowedName"
307308
UnusedName{} -> "UnusedName"
308309
UnusedDeclaration{} -> "UnusedDeclaration"
310+
ShadowedTypeName{} -> "ShadowedTypeName"
309311
ShadowedTypeVar{} -> "ShadowedTypeVar"
310312
UnusedTypeVar{} -> "UnusedTypeVar"
311313
WildcardInferredType{} -> "WildcardInferredType"
@@ -1055,10 +1057,12 @@ prettyPrintSingleError (PPEOptions codeColor full level showDocs relPath) e = fl
10551057
]
10561058
renderSimpleErrorMessage (ShadowedName nm) =
10571059
line $ "Name " <> markCode (showIdent nm) <> " was shadowed."
1060+
renderSimpleErrorMessage (ShadowedTypeName nm) =
1061+
line $ "Type " <> markCode (runProperName nm) <> " was shadowed."
10581062
renderSimpleErrorMessage (ShadowedTypeVar tv) =
10591063
line $ "Type variable " <> markCode tv <> " was shadowed."
10601064
renderSimpleErrorMessage (UnusedName nm) =
1061-
line $ "Name " <> markCode (showIdent nm) <> " was introduced but not used."
1065+
line $ "Name " <> markCode (runName (Qualified Nothing nm)) <> " was introduced but not used."
10621066
renderSimpleErrorMessage (UnusedDeclaration nm) =
10631067
line $ "Declaration " <> markCode (showIdent nm) <> " was not used, and is not exported."
10641068
renderSimpleErrorMessage (UnusedTypeVar tv) =

src/Language/PureScript/Ide/Usage.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ applySearch module_ search =
141141
P.Var sp i
142142
| Just ideValue <- preview _IdeDeclValue (P.disqualify search)
143143
, P.isQualified search
144-
|| not (P.LocalIdent (_ideValueIdent ideValue) `Set.member` scope) ->
144+
|| not (P.LocalName (P.IdentName (_ideValueIdent ideValue)) `Set.member` scope) ->
145145
[sp | map P.runIdent i == map identifierFromIdeDeclaration search]
146146
P.Constructor sp name
147147
| Just ideDtor <- traverse (preview _IdeDeclDataConstructor) search ->

0 commit comments

Comments
 (0)