on master help sign in
Disambiguate when different types (and type aliases) in errors

Localization may permit two different types to have the same name, so be fully explicit when that happens.

commiteb39dd7bfff216e6eb225f98d58a5edcd04b788e authorEvan Czaplicki <evancz@users.noreply.github.com> dateFri 1 Jun 2018, 17:54 (8 years ago) parent10fa496
4 files changed+69−34
compiler/src/Reporting/Doc.hs+1−1 compiler/src/Reporting/Render/Type.hs+2−3 compiler/src/Reporting/Render/Type/Localizer.hs+13−6 compiler/src/Type/Error.hs+53−24
compiler/src/Reporting/Doc.hs+1−1
@@ -5,7 +5,7 @@ module Reporting.Doc
55 , (P.<+>), (<>)
66 , P.align, P.cat, P.empty, P.fill, P.fillSep, P.hang
77 , P.hcat, P.hsep, P.indent, P.sep, P.vcat
8− , P.red, P.cyan, P.magenta, P.green, P.blue, P.black
8+ , P.red, P.cyan, P.magenta, P.green, P.blue, P.black, P.yellow
99 , P.dullred, P.dullcyan, P.dullyellow
1010
1111 , fromString, fromText, fromName, fromInt
compiler/src/Reporting/Render/Type.hs+2−3
@@ -18,7 +18,6 @@ import qualified Data.Maybe as Maybe
1818
1919import qualified AST.Source as Src
2020import qualified AST.Canonical as Can
21−import qualified AST.Module.Name as ModuleName
2221import qualified Elm.Name as N
2322import qualified Reporting.Annotation as A
2423import qualified Reporting.Doc as D
@@ -213,7 +212,7 @@ canToDoc localizer context tipe =
213212 Can.TVar name ->
214213 D.fromName name
215214
216− Can.TType (ModuleName.Canonical _ home) name args ->
215+ Can.TType home name args ->
217216 apply context
218217 (L.toDoc localizer home name)
219218 (map (canToDoc localizer App) args)
@@ -232,7 +231,7 @@ canToDoc localizer context tipe =
232231 (canToDoc localizer None b)
233232 (map (canToDoc localizer None) (Maybe.maybeToList maybeC))
234233
235− Can.TAlias (ModuleName.Canonical _ home) name args _ ->
234+ Can.TAlias home name args _ ->
236235 apply context
237236 (L.toDoc localizer home name)
238237 (map (canToDoc localizer App . snd) args)
compiler/src/Reporting/Render/Type/Localizer.hs+13−6
@@ -3,6 +3,7 @@
33module Reporting.Render.Type.Localizer
44 ( Localizer
55 , toDoc
6+ , toString
67 , empty
78 , fromNames
89 , fromModule
@@ -16,6 +17,7 @@ import qualified Data.Map as Map
1617import qualified Data.Set as Set
1718
1819import qualified AST.Source as Src
20+import qualified AST.Module.Name as ModuleName
1921import qualified AST.Valid as Valid
2022import qualified Elm.Compiler.Imports as Imports
2123import qualified Elm.Name as N
@@ -54,22 +56,27 @@ empty =
5456-- LOCALIZE
5557
5658
57−toDoc :: Localizer -> N.Name -> N.Name -> D.Doc
58−toDoc (Localizer localizer) home name =
59+toDoc :: Localizer -> ModuleName.Canonical -> N.Name -> D.Doc
60+toDoc localizer home name =
61+ D.fromString (toString localizer home name)
62+
63+
64+toString :: Localizer -> ModuleName.Canonical -> N.Name -> String
65+toString (Localizer localizer) (ModuleName.Canonical _ home) name =
5966 case Map.lookup home localizer of
6067 Nothing ->
61− D.fromName home <> "." <> D.fromName name
68+ N.toString home <> "." <> N.toString name
6269
6370 Just (Import alias exposing) ->
6471 case exposing of
6572 All ->
66− D.fromName name
73+ N.toString name
6774
6875 Only set ->
6976 if Set.member name set then
70− D.fromName name
77+ N.toString name
7178 else
72− D.fromName (maybe home id alias) <> "." <> D.fromName name
79+ N.toString (maybe home id alias) <> "." <> N.toString name
7380
7481
7582
compiler/src/Type/Error.hs+53−24
@@ -19,6 +19,7 @@ module Type.Error
1919
2020import qualified Data.Map as Map
2121import qualified Data.Maybe as Maybe
22+import Data.Monoid ((<>))
2223
2324import qualified AST.Module.Name as ModuleName
2425import qualified Data.Bag as Bag
@@ -75,11 +76,6 @@ iteratedDealias tipe =
7576-- TO DOC
7677
7778
78−nameToDoc :: L.Localizer -> ModuleName.Canonical -> N.Name -> D.Doc
79−nameToDoc localizer (ModuleName.Canonical _ home) name =
80− L.toDoc localizer home name
81−
82−
8379toDoc :: L.Localizer -> RT.Context -> Type -> D.Doc
8480toDoc localizer ctx tipe =
8581 case tipe of
@@ -109,7 +105,7 @@ toDoc localizer ctx tipe =
109105
110106 Type home name args ->
111107 RT.apply ctx
112− (nameToDoc localizer home name)
108+ (L.toDoc localizer home name)
113109 (map (toDoc localizer RT.App) args)
114110
115111 Record fields ext ->
@@ -131,7 +127,7 @@ toDoc localizer ctx tipe =
131127aliasToDoc :: L.Localizer -> RT.Context -> ModuleName.Canonical -> N.Name -> [(N.Name, Type)] -> D.Doc
132128aliasToDoc localizer ctx home name args =
133129 RT.apply ctx
134− (nameToDoc localizer home name)
130+ (L.toDoc localizer home name)
135131 (map (toDoc localizer RT.App . snd) args)
136132
137133
@@ -273,48 +269,70 @@ toDiff localizer ctx tipe1 tipe2 =
273269 diffRecord localizer fields1 ext1 fields2 ext2
274270
275271 (Type home1 name1 args1, Type home2 name2 args2) | home1 == home2 && name1 == name2 ->
276− RT.apply ctx (nameToDoc localizer home1 name1)
272+ RT.apply ctx (L.toDoc localizer home1 name1)
277273 <$> sequenceA (zipWith (toDiff localizer RT.App) args1 args2)
278274
279275 (Alias home1 name1 args1 _, Alias home2 name2 args2 _) | home1 == home2 && name1 == name2 ->
280− RT.apply ctx (nameToDoc localizer home1 name1)
276+ RT.apply ctx (L.toDoc localizer home1 name1)
281277 <$> sequenceA (zipWith (toDiff localizer RT.App) (map snd args1) (map snd args2))
282278
283279 -- start trying to find specific problems
284280
281+ (Type home1 name1 args1, Type home2 name2 args2) | L.toString localizer home1 name1 == L.toString localizer home2 name2 ->
282+ different
283+ (nameClashToDoc ctx localizer home1 name1 args1)
284+ (nameClashToDoc ctx localizer home2 name2 args2)
285+ Bag.empty
286+
285287 (Type home name [t1], t2) | isMaybe home name && isSimilar (toDiff localizer ctx t1 t2) ->
286288 different
287− (RT.apply ctx (D.dullyellow (nameToDoc localizer home name)) [toDoc localizer RT.App t1])
289+ (RT.apply ctx (D.dullyellow (L.toDoc localizer home name)) [toDoc localizer RT.App t1])
288290 (toDoc localizer ctx t2)
289291 (Bag.one AnythingFromMaybe)
290292
291293 (t1, Type home name [t2]) | isList home name && isSimilar (toDiff localizer ctx t1 t2) ->
292294 different
293295 (toDoc localizer ctx t1)
294− (RT.apply ctx (D.dullyellow (nameToDoc localizer home name)) [toDoc localizer RT.App t2])
296+ (RT.apply ctx (D.dullyellow (L.toDoc localizer home name)) [toDoc localizer RT.App t2])
295297 (Bag.one AnythingToList)
296298
297− (Alias home name args t1, t2) ->
299+ (Alias home1 name1 args1 t1, t2) ->
298300 case diffAliasedRecord localizer t1 t2 of
299301 Just (Diff _ doc2 status) ->
300− Diff (D.dullyellow (aliasToDoc localizer ctx home name args)) doc2 status
302+ Diff (D.dullyellow (aliasToDoc localizer ctx home1 name1 args1)) doc2 status
301303
302304 Nothing ->
303− different
304− (D.dullyellow (toDoc localizer ctx tipe1))
305− (D.dullyellow (toDoc localizer ctx tipe2))
306− Bag.empty
307−
308− (t1, Alias home name args t2) ->
305+ case t2 of
306+ Type home2 name2 args2 | L.toString localizer home1 name1 == L.toString localizer home2 name2 ->
307+ different
308+ (nameClashToDoc ctx localizer home1 name1 (map snd args1))
309+ (nameClashToDoc ctx localizer home2 name2 args2)
310+ Bag.empty
311+
312+ _ ->
313+ different
314+ (D.dullyellow (toDoc localizer ctx tipe1))
315+ (D.dullyellow (toDoc localizer ctx tipe2))
316+ Bag.empty
317+
318+ (t1, Alias home2 name2 args2 t2) ->
309319 case diffAliasedRecord localizer t1 t2 of
310320 Just (Diff doc1 _ status) ->
311− Diff doc1 (D.dullyellow (aliasToDoc localizer ctx home name args)) status
321+ Diff doc1 (D.dullyellow (aliasToDoc localizer ctx home2 name2 args2)) status
312322
313323 Nothing ->
314− different
315− (D.dullyellow (toDoc localizer ctx tipe1))
316− (D.dullyellow (toDoc localizer ctx tipe2))
317− Bag.empty
324+ case t1 of
325+ Type home1 name1 args1 | L.toString localizer home1 name1 == L.toString localizer home2 name2 ->
326+ different
327+ (nameClashToDoc ctx localizer home1 name1 args1)
328+ (nameClashToDoc ctx localizer home2 name2 (map snd args2))
329+ Bag.empty
330+
331+ _ ->
332+ different
333+ (D.dullyellow (toDoc localizer ctx tipe1))
334+ (D.dullyellow (toDoc localizer ctx tipe2))
335+ Bag.empty
318336
319337 pair ->
320338 let
@@ -430,6 +448,17 @@ isSuper super tipe =
430448
431449
432450
451+-- NAME CLASH
452+
453+
454+nameClashToDoc :: RT.Context -> L.Localizer -> ModuleName.Canonical -> N.Name -> [Type] -> D.Doc
455+nameClashToDoc ctx localizer (ModuleName.Canonical _ home) name args =
456+ RT.apply ctx
457+ (D.yellow (D.fromName home) <> D.dullyellow ("." <> D.fromName name))
458+ (map (toDoc localizer RT.App) args)
459+
460+
461+
433462-- DIFF ALIASED RECORD
434463
435464