@@ -19,7 +19,7 @@ import Control.Monad.Error.Class (MonadError(..))
19
19
import Control.Monad.State.Lazy
20
20
import Control.Monad.Writer (MonadWriter (.. ))
21
21
22
- import Data.Maybe (fromMaybe , mapMaybe )
22
+ import Data.Maybe (fromMaybe , isNothing , mapMaybe )
23
23
import qualified Data.Map as M
24
24
import qualified Data.Set as S
25
25
@@ -35,6 +35,11 @@ import Language.PureScript.Sugar.Names.Imports
35
35
import Language.PureScript.Traversals
36
36
import Language.PureScript.Types
37
37
38
+ data BoundNames = BoundNames
39
+ { boundValues :: [Ident ]
40
+ , boundTypes :: [ProperName 'TypeName]
41
+ }
42
+
38
43
-- |
39
44
-- Replaces all local names with qualified names within a list of modules. The
40
45
-- modules should be topologically sorted beforehand.
@@ -187,57 +192,57 @@ renameInModule imports (Module modSS coms mn decls exps) =
187
192
188
193
(go, _, _, _, _) =
189
194
everywhereWithContextOnValuesM
190
- (modSS, [] )
195
+ (modSS, BoundNames [] [] )
191
196
(\ (_, bound) d -> (\ (bound', d') -> ((declSourceSpan d', bound'), d')) <$> updateDecl bound d)
192
197
updateValue
193
198
updateBinder
194
199
updateCase
195
200
defS
196
201
197
202
updateDecl
198
- :: [ Ident ]
203
+ :: BoundNames
199
204
-> Declaration
200
- -> m ([ Ident ] , Declaration )
205
+ -> m (BoundNames , Declaration )
201
206
updateDecl bound (DataDeclaration sa dtype name args dctors) =
202
207
fmap (bound,) $
203
208
DataDeclaration sa dtype name
204
- <$> updateTypeArguments args
205
- <*> traverse (traverseDataCtorFields (traverse (sndM updateTypesEverywhere))) dctors
209
+ <$> updateTypeArguments bound args
210
+ <*> traverse (traverseDataCtorFields (traverse (sndM ( updateTypesEverywhere bound) ))) dctors
206
211
updateDecl bound (TypeSynonymDeclaration sa name ps ty) =
207
- fmap (bound,) $
212
+ fmap (bound{ boundTypes = name : boundTypes bound } ,) $
208
213
TypeSynonymDeclaration sa name
209
- <$> updateTypeArguments ps
210
- <*> updateTypesEverywhere ty
214
+ <$> updateTypeArguments bound ps
215
+ <*> updateTypesEverywhere bound ty
211
216
updateDecl bound (TypeClassDeclaration sa@ (ss, _) className args implies deps ds) =
212
217
fmap (bound,) $
213
218
TypeClassDeclaration sa className
214
- <$> updateTypeArguments args
215
- <*> updateConstraints ss implies
219
+ <$> updateTypeArguments bound args
220
+ <*> updateConstraints bound ss implies
216
221
<*> pure deps
217
222
<*> pure ds
218
223
updateDecl bound (TypeInstanceDeclaration sa@ (ss, _) ch idx name cs cn ts ds) =
219
224
fmap (bound,) $
220
225
TypeInstanceDeclaration sa ch idx name
221
- <$> updateConstraints ss cs
226
+ <$> updateConstraints bound ss cs
222
227
<*> updateClassName cn ss
223
- <*> traverse updateTypesEverywhere ts
228
+ <*> traverse ( updateTypesEverywhere bound) ts
224
229
<*> pure ds
225
230
updateDecl bound (KindDeclaration sa kindFor name ty) =
226
231
fmap (bound,) $
227
232
KindDeclaration sa kindFor name
228
- <$> updateTypesEverywhere ty
233
+ <$> updateTypesEverywhere bound ty
229
234
updateDecl bound (TypeDeclaration (TypeDeclarationData sa name ty)) =
230
235
fmap (bound,) $
231
236
TypeDeclaration . TypeDeclarationData sa name
232
- <$> updateTypesEverywhere ty
237
+ <$> updateTypesEverywhere bound ty
233
238
updateDecl bound (ExternDeclaration sa name ty) =
234
- fmap (name : bound,) $
239
+ fmap (bound{ boundValues = name : boundValues bound } ,) $
235
240
ExternDeclaration sa name
236
- <$> updateTypesEverywhere ty
241
+ <$> updateTypesEverywhere bound ty
237
242
updateDecl bound (ExternDataDeclaration sa name ki) =
238
243
fmap (bound,) $
239
244
ExternDataDeclaration sa name
240
- <$> updateTypesEverywhere ki
245
+ <$> updateTypesEverywhere bound ki
241
246
updateDecl bound (TypeFixityDeclaration sa@ (ss, _) fixity alias op) =
242
247
fmap (bound,) $
243
248
TypeFixityDeclaration sa fixity
@@ -257,52 +262,53 @@ renameInModule imports (Module modSS coms mn decls exps) =
257
262
return (b, d)
258
263
259
264
updateValue
260
- :: (SourceSpan , [ Ident ] )
265
+ :: (SourceSpan , BoundNames )
261
266
-> Expr
262
- -> m ((SourceSpan , [ Ident ] ), Expr )
267
+ -> m ((SourceSpan , BoundNames ), Expr )
263
268
updateValue (_, bound) v@ (PositionedValue pos' _ _) =
264
269
return ((pos', bound), v)
265
270
updateValue (pos, bound) (Abs (VarBinder ss arg) val') =
266
- return ((pos, arg : bound), Abs (VarBinder ss arg) val')
271
+ return ((pos, bound{ boundValues = arg : boundValues bound } ), Abs (VarBinder ss arg) val')
267
272
updateValue (pos, bound) (Let w ds val') = do
268
273
let args = mapMaybe letBoundVariable ds
274
+ let syns = mapMaybe letBoundTypeSynonym ds
269
275
unless (length (ordNub args) == length args) .
270
276
throwError . errorMessage' pos $ OverlappingNamesInLet
271
- return ((pos, args ++ bound), Let w ds val')
272
- updateValue (_, bound) (Var ss name'@ (Qualified Nothing ident)) | ident `notElem` bound =
277
+ return ((pos, bound{ boundValues = args ++ boundValues bound, boundTypes = syns ++ boundTypes bound } ), Let w ds val')
278
+ updateValue (_, bound) (Var ss name'@ (Qualified Nothing ident)) | ident `notElem` boundValues bound =
273
279
(,) (ss, bound) <$> (Var ss <$> updateValueName name' ss)
274
280
updateValue (_, bound) (Var ss name'@ (Qualified (Just _) _)) =
275
281
(,) (ss, bound) <$> (Var ss <$> updateValueName name' ss)
276
282
updateValue (_, bound) (Op ss op) =
277
283
(,) (ss, bound) <$> (Op ss <$> updateValueOpName op ss)
278
284
updateValue (_, bound) (Constructor ss name) =
279
285
(,) (ss, bound) <$> (Constructor ss <$> updateDataConstructorName name ss)
280
- updateValue s (TypedValue check val ty) =
281
- (,) s <$> (TypedValue check val <$> updateTypesEverywhere ty)
286
+ updateValue s@ (_, bound) (TypedValue check val ty) =
287
+ (,) s <$> (TypedValue check val <$> updateTypesEverywhere bound ty)
282
288
updateValue s v = return (s, v)
283
289
284
290
updateBinder
285
- :: (SourceSpan , [ Ident ] )
291
+ :: (SourceSpan , BoundNames )
286
292
-> Binder
287
- -> m ((SourceSpan , [ Ident ] ), Binder )
293
+ -> m ((SourceSpan , BoundNames ), Binder )
288
294
updateBinder (_, bound) v@ (PositionedBinder pos _ _) =
289
295
return ((pos, bound), v)
290
296
updateBinder (_, bound) (ConstructorBinder ss name b) =
291
297
(,) (ss, bound) <$> (ConstructorBinder ss <$> updateDataConstructorName name ss <*> pure b)
292
298
updateBinder (_, bound) (OpBinder ss op) =
293
299
(,) (ss, bound) <$> (OpBinder ss <$> updateValueOpName op ss)
294
- updateBinder s (TypedBinder t b) = do
295
- t' <- updateTypesEverywhere t
300
+ updateBinder s@ (_, bound) (TypedBinder t b) = do
301
+ t' <- updateTypesEverywhere bound t
296
302
return (s, TypedBinder t' b)
297
303
updateBinder s v =
298
304
return (s, v)
299
305
300
306
updateCase
301
- :: (SourceSpan , [ Ident ] )
307
+ :: (SourceSpan , BoundNames )
302
308
-> CaseAlternative
303
- -> m ((SourceSpan , [ Ident ] ), CaseAlternative )
309
+ -> m ((SourceSpan , BoundNames ), CaseAlternative )
304
310
updateCase (pos, bound) c@ (CaseAlternative bs gs) =
305
- return ((pos, concatMap binderNames bs ++ updateGuard gs ++ bound), c)
311
+ return ((pos, bound{ boundValues = concatMap binderNames bs ++ updateGuard gs ++ boundValues bound } ), c)
306
312
where
307
313
updateGuard :: [GuardedExpr ] -> [Ident ]
308
314
updateGuard [] = []
@@ -315,29 +321,37 @@ renameInModule imports (Module modSS coms mn decls exps) =
315
321
letBoundVariable :: Declaration -> Maybe Ident
316
322
letBoundVariable = fmap valdeclIdent . getValueDeclaration
317
323
324
+ letBoundTypeSynonym :: Declaration -> Maybe (ProperName 'TypeName)
325
+ letBoundTypeSynonym (TypeSynonymDeclaration _ name _ _) = Just name
326
+ letBoundTypeSynonym _ = Nothing
327
+
318
328
updateTypeArguments
319
329
:: (Traversable f , Traversable g )
320
- => f (a , g SourceType ) -> m (f (a , g SourceType ))
321
- updateTypeArguments = traverse (sndM (traverse updateTypesEverywhere))
330
+ => BoundNames -> f (a , g SourceType ) -> m (f (a , g SourceType ))
331
+ updateTypeArguments bound = traverse (sndM (traverse ( updateTypesEverywhere bound) ))
322
332
323
- updateTypesEverywhere :: SourceType -> m SourceType
324
- updateTypesEverywhere = everywhereOnTypesM updateType
333
+ updateTypesEverywhere :: BoundNames -> SourceType -> m SourceType
334
+ updateTypesEverywhere bound = everywhereOnTypesM updateType
325
335
where
326
336
updateType :: SourceType -> m SourceType
327
337
updateType (TypeOp ann@ (ss, _) name) = TypeOp ann <$> updateTypeOpName name ss
328
- updateType (TypeConstructor ann@ (ss, _) name) = TypeConstructor ann <$> updateTypeName name ss
338
+ updateType (TypeConstructor ann@ (ss, _) qname@ (Qualified mn' name)) =
339
+ TypeConstructor ann <$>
340
+ if isNothing mn' && name `elem` boundTypes bound
341
+ then return qname
342
+ else updateTypeName qname ss
329
343
updateType (ConstrainedType ann c t) = ConstrainedType ann <$> updateInConstraint c <*> pure t
330
344
updateType t = return t
331
345
updateInConstraint :: SourceConstraint -> m SourceConstraint
332
346
updateInConstraint (Constraint ann@ (ss, _) name ks ts info) =
333
347
Constraint ann <$> updateClassName name ss <*> pure ks <*> pure ts <*> pure info
334
348
335
- updateConstraints :: SourceSpan -> [SourceConstraint ] -> m [SourceConstraint ]
336
- updateConstraints pos = traverse $ \ (Constraint ann name ks ts info) ->
349
+ updateConstraints :: BoundNames -> SourceSpan -> [SourceConstraint ] -> m [SourceConstraint ]
350
+ updateConstraints bound pos = traverse $ \ (Constraint ann name ks ts info) ->
337
351
Constraint ann
338
352
<$> updateClassName name pos
339
- <*> traverse updateTypesEverywhere ks
340
- <*> traverse updateTypesEverywhere ts
353
+ <*> traverse ( updateTypesEverywhere bound) ks
354
+ <*> traverse ( updateTypesEverywhere bound) ts
341
355
<*> pure info
342
356
343
357
updateTypeName
0 commit comments