on master help sign in
Track info for better shadowing error messages

Primarily this starts tracking the location of any given local definition. That way, if there is any shadowing, I can show the two-part code snippet where it’s like “I see one definition <HERE> and another one <THERE>, so which is it?” It also removes some info from errors when tuples are too large. I think it makes sense to underline the whole tuple, not just the “extra” entries. The change is going to be switching to records, so it is not about removing some entries.

commit732b5d1f98252ed1c6a4536aca7f9a88ad190f9b authorEvan Czaplicki <evancz@users.noreply.github.com> dateThu 21 Dec 2017, 13:35 (9 years ago) parent0667191
5 files changed+33−41
src/Canonicalize/Environment.hs+11−12 src/Canonicalize/Environment/Local.hs+15−18 src/Canonicalize/Expression.hs+4−5 src/Canonicalize/Pattern.hs+2−3 src/Canonicalize/Type.hs+1−3
src/Canonicalize/Environment.hs+11−12
@@ -73,8 +73,8 @@ data VarHomes =
7373
7474
7575data UnqualifiedVarHome
76− = Local
77− | TopLevel
76+ = Local R.Region
77+ | TopLevel R.Region
7878 | Foreign (Bag.Bag ForeignVarHome)
7979
8080
@@ -129,7 +129,7 @@ addLocals :: Map.Map N.Name R.Region -> Env -> Result i w Env
129129addLocals names (Env home vars types patterns binops) =
130130 do newVars <-
131131 Map.mergeA
132− (Map.mapMissing (\_ _ -> localVarHomes))
132+ (Map.mapMissing addLocalLeft)
133133 (Map.mapMissing (\_ homes -> homes))
134134 (Map.zipWithAMatched addLocalBoth)
135135 names
@@ -137,23 +137,22 @@ addLocals names (Env home vars types patterns binops) =
137137 Result.ok $ Env home newVars types patterns binops
138138
139139
140−{-# NOINLINE localVarHomes #-}
141−localVarHomes :: VarHomes
142−localVarHomes =
143− VarHomes Local Map.empty
140+addLocalLeft :: N.Name -> R.Region -> VarHomes
141+addLocalLeft _ region =
142+ VarHomes (Local region) Map.empty
144143
145144
146145addLocalBoth :: N.Name -> R.Region -> VarHomes -> Result i w VarHomes
147146addLocalBoth name region (VarHomes unqualified qualified) =
148147 case unqualified of
149148 Foreign _ ->
150− Result.ok (VarHomes Local qualified)
149+ Result.ok (VarHomes (Local region) qualified)
151150
152− Local ->
153− Result.throw (Error.Shadowing name region)
151+ Local parentRegion ->
152+ Result.throw (Error.Shadowing name parentRegion region)
154153
155− TopLevel ->
156− Result.throw (Error.Shadowing name region)
154+ TopLevel parentRegion ->
155+ Result.throw (Error.Shadowing name parentRegion region)
157156
158157
159158
src/Canonicalize/Environment/Local.hs+15−18
@@ -118,29 +118,26 @@ addVars module_ (Env.Env home vars types patterns binops) =
118118 return $ Env.Env home newVars types patterns binops
119119
120120
121−addVarsHelp :: Map.Map N.Name () -> Map.Map N.Name Env.VarHomes -> Map.Map N.Name Env.VarHomes
121+addVarsHelp :: Map.Map N.Name R.Region -> Map.Map N.Name Env.VarHomes -> Map.Map N.Name Env.VarHomes
122122addVarsHelp localVars vars =
123123 let
124− varHomes =
125− Env.VarHomes Env.TopLevel Map.empty
124+ addLeft _ region =
125+ Env.VarHomes (Env.TopLevel region) Map.empty
126126
127− addLeft _ _ =
128− varHomes
129−
130− addBoth _ _ (Env.VarHomes _ qualified) =
131− Env.VarHomes Env.TopLevel qualified
127+ addBoth _ region (Env.VarHomes _ qualified) =
128+ Env.VarHomes (Env.TopLevel region) qualified
132129 in
133130 merge addLeft addBoth localVars vars
134131
135132
136−collectLowerVars :: Valid.Module -> Result i (Map.Map N.Name ())
133+collectLowerVars :: Valid.Module -> Result i (Map.Map N.Name R.Region)
137134collectLowerVars (Valid.Module _ _ _ _ _ decls _ _ _ effects) =
138135 let
139136 addDecl (A.At _ (Valid.Decl (A.At region name) _ _ _)) dict =
140− Dups.insert name region () dict
137+ Dups.insert name region region dict
141138
142139 addPort (Valid.Port (A.At region name) _) dict =
143− Dups.insert name region () dict
140+ Dups.insert name region region dict
144141
145142 effectDict =
146143 case effects of
@@ -153,32 +150,32 @@ collectLowerVars (Valid.Module _ _ _ _ _ decls _ _ _ effects) =
153150 Valid.Manager _ manager ->
154151 case manager of
155152 Valid.Cmd (A.At region _) ->
156− Dups.one "command" region ()
153+ Dups.one "command" region region
157154
158155 Valid.Sub (A.At region _) ->
159− Dups.one "subscription" region ()
156+ Dups.one "subscription" region region
160157
161158 Valid.Fx (A.At regionCmd _) (A.At regionSub _) ->
162159 Dups.union
163− (Dups.one "command" regionCmd ())
164− (Dups.one "subscription" regionSub ())
160+ (Dups.one "command" regionCmd regionCmd)
161+ (Dups.one "subscription" regionSub regionSub)
165162 in
166163 Dups.detect Error.DuplicateDecl $ foldr addDecl effectDict decls
167164
168165
169−collectUpperVars :: Valid.Module -> Result i (Map.Map N.Name ())
166+collectUpperVars :: Valid.Module -> Result i (Map.Map N.Name R.Region)
170167collectUpperVars (Valid.Module _ _ _ _ _ _ unions aliases _ _) =
171168 let
172169 addUnion (Valid.Union _ _ ctors) dict =
173170 foldr addCtor dict ctors
174171
175172 addCtor (A.At region name, _args) dict =
176− Dups.insert name region () dict
173+ Dups.insert name region region dict
177174
178175 addAlias (Valid.Alias (A.At region name) _ (A.At _ tipe)) dict =
179176 case tipe of
180177 Src.TRecord _ Nothing ->
181− Dups.insert name region () dict
178+ Dups.insert name region region dict
182179
183180 _ ->
184181 dict
src/Canonicalize/Expression.hs+4−5
@@ -164,9 +164,8 @@ canonicalizeTupleExtras region env extras =
164164 [three] ->
165165 Just <$> canonicalize env three
166166
167− _ : others ->
168− let (A.At r1 _, A.At r2 _) = (head others, last others) in
169− Result.throw (Error.TupleLargerThanThree region (R.merge r1 r2))
167+ _ ->
168+ Result.throw (Error.TupleLargerThanThree region)
170169
171170
172171
@@ -653,10 +652,10 @@ findVar region (Env.Env localHome vars _ _ _) maybePrefix name =
653652 case maybePrefix of
654653 Nothing ->
655654 case unqualified of
656− Env.Local ->
655+ Env.Local _ ->
657656 logVar name (Can.VarLocal name)
658657
659− Env.TopLevel ->
658+ Env.TopLevel _ ->
660659 logVar name (Can.VarTopLevel localHome name)
661660
662661 Env.Foreign bag ->
src/Canonicalize/Pattern.hs+2−3
@@ -130,9 +130,8 @@ canonicalizeTuple tupleRegion env extras =
130130 [three] ->
131131 Just <$> canonicalize env three
132132
133− _ : others ->
134− let (A.At r1 _, A.At r2 _) = (head others, last others) in
135− Result.throw (Error.TupleLargerThanThree tupleRegion (R.merge r1 r2))
133+ _ ->
134+ Result.throw $ Error.TupleLargerThanThree tupleRegion
136135
137136
138137canonicalizeList :: Env.Env -> [Src.Pattern] -> Result DupsDict w [Can.Pattern]
src/Canonicalize/Type.hs+1−3
@@ -87,9 +87,7 @@ canonicalize env (A.At typeRegion tipe) =
8787 Just <$> canonicalize env c
8888
8989 _ ->
90− let (A.At start _, A.At end _) = (head cs, last cs) in
91− Result.throw $
92− Error.TupleLargerThanThree typeRegion (R.merge start end)
90+ Result.throw $ Error.TupleLargerThanThree typeRegion
9391
9492
9593