@@ -282,16 +282,18 @@ declarationsToPatches moduleName decls =
282282 (Maybe.catMaybes maybeNodes, concat patchLists)
283283
284284
285−-- When canonicalizing, all _values should be Local, but all _adts and _patterns
286−-- should be fully namespaced. With _adts, they may appear in types that can
287−-- escape the module.
285+-- When canonicalizing, it is important that:
286+-- _adts are fully namespaced (Var.Module ...)
287+-- _patterns are fully namespaced (Var.Module ...)
288+-- _values that are defined as top-level declarations are (Var.TopLevel ...)
289+-- all other _values are local (Var.Local)
288290declToPatches
289291 :: Module.Name
290292 -> D.ValidDecl
291293 -> (Maybe Node, [Env.Patch])
292294declToPatches moduleName (A.A (region,_) decl) =
293− let local mkPatch x =
294− mkPatch x (Var.local x)
295+ let topLevel mkPatch x =
296+ mkPatch x (Var.topLevel moduleName x )
295297
296298 namespaced mkPatch x =
297299 mkPatch x (Var.fromModule moduleName x)
@@ -302,7 +304,7 @@ declToPatches moduleName (A.A (region,_) decl) =
302304 case decl of
303305 D.Definition (Valid.Definition pattern _ _) ->
304306 ( Nothing
305− , map (local Env.Value) (P.boundVarList pattern)
307+ , map (topLevel Env.Value) (P.boundVarList pattern)
306308 )
307309
308310 D.Datatype name _ ctors ->
@@ -310,7 +312,7 @@ declToPatches moduleName (A.A (region,_) decl) =
310312 in
311313 ( Nothing
312314 , namespaced Env.Union name
313− : map (local Env.Value) ctorNames
315+ : map (topLevel Env.Value) ctorNames
314316 ++ map patternPatch ctors
315317 )
316318
@@ -318,14 +320,14 @@ declToPatches moduleName (A.A (region,_) decl) =
318320 ( Just (node region name tvars alias)
319321 , case alias of
320322 A.A _ (Type.RRecord _ _) ->
321− [local Env.Value name]
323+ [topLevel Env.Value name]
322324 _ ->
323325 []
324326 )
325327
326328 D.Port port ->
327329 ( Nothing
328− , [local Env.Value (D.validPortName port)]
330+ , [topLevel Env.Value (D.validPortName port)]
329331 )
330332
331333 D.Fixity _ _ _ ->