on master help sign in
Make Localizer available for Reporting.Error.Main

It can happen sometimes. I’m not 100% certain about relying on lazy evaluation in this case, and it may be better for GC to just do it strictly.

commitb93555a411dd2696218a042daf73634af44d0e77 authorEvan Czaplicki <evancz@users.noreply.github.com> dateWed 18 Apr 2018, 16:25 (9 years ago) parent538992b
6 files changed+40−40
compiler/src/Compile.hs+6−10 compiler/src/Reporting/Error.hs+3−3 compiler/src/Reporting/Error/Main.hs+4−3 compiler/src/Reporting/Render/Type.hs+17−16 compiler/src/Reporting/Render/Type/Localizer.hs+6−5 compiler/src/Reporting/Warning.hs+4−3
compiler/src/Compile.hs+6−10
@@ -10,8 +10,6 @@ module Compile
1010import qualified Data.ByteString as BS
1111import qualified Data.Map as Map
1212
13−import qualified AST.Source as Src
14−import qualified AST.Valid as Valid
1513import qualified AST.Canonical as Can
1614import qualified AST.Optimized as Opt
1715import qualified AST.Module.Name as ModuleName
@@ -62,13 +60,15 @@ compile flag pkg importDict interfaces source =
6260 canonical <- Result.mapError Error.Canonicalize $
6361 Canonicalize.canonicalize pkg importDict interfaces valid
6462
63+ let localizer = L.fromModule valid -- TODO should this be strict for GC?
64+
6565 annotations <-
66− runTypeInference (Valid._imports valid) canonical
66+ runTypeInference localizer canonical
6767
6868 () <-
6969 exhaustivenessCheck canonical
7070
71− graph <- Result.mapError Error.Main $
71+ graph <- Result.mapError (Error.Main localizer) $
7272 Optimize.optimize annotations canonical
7373
7474 documentation <-
@@ -86,17 +86,13 @@ compile flag pkg importDict interfaces source =
8686-- TYPE INFERENCE
8787
8888
89−runTypeInference :: [Src.Import] -> Can.Module -> Result i (Map.Map N.Name Can.Annotation)
90−runTypeInference imports canonical =
89+runTypeInference :: L.Localizer -> Can.Module -> Result i (Map.Map N.Name Can.Annotation)
90+runTypeInference localizer canonical =
9191 case unsafePerformIO (Type.run =<< Type.constrain canonical) of
9292 Right annotations ->
9393 Result.ok annotations
9494
9595 Left errors ->
96− let
97− localizer =
98− L.fromImports (ModuleName._module (Can._name canonical)) imports
99− in
10096 Result.throw (Error.Type localizer errors)
10197
10298
compiler/src/Reporting/Error.hs+3−3
@@ -26,7 +26,7 @@ data Error
2626 = Syntax Syntax.Error
2727 | Canonicalize Canonicalize.Error
2828 | Type L.Localizer [Type.Error]
29− | Main Main.Error
29+ | Main L.Localizer Main.Error
3030 | Pattern [Pattern.Error]
3131 | Docs Docs.Error
3232
@@ -47,8 +47,8 @@ toReports source err =
4747 Type localizer typeErrors ->
4848 map (Type.toReport source localizer) typeErrors
4949
50− Main mainError ->
51− [Main.toReport source mainError]
50+ Main localizer mainError ->
51+ [Main.toReport localizer source mainError]
5252
5353 Pattern patternErrors ->
5454 map (Pattern.toReport source) patternErrors
compiler/src/Reporting/Error/Main.hs+4−3
@@ -14,6 +14,7 @@ import qualified Reporting.Error.Canonicalize as E
1414import qualified Reporting.Region as R
1515import qualified Reporting.Render.Code as Code
1616import qualified Reporting.Render.Type as RT
17+import qualified Reporting.Render.Type.Localizer as L
1718import qualified Reporting.Report as Report
1819
1920
@@ -31,8 +32,8 @@ data Error
3132-- TO REPORT
3233
3334
34−toReport :: Code.Source -> Error -> Report.Report
35−toReport source err =
35+toReport :: L.Localizer -> Code.Source -> Error -> Report.Report
36+toReport localizer source err =
3637 case err of
3738 BadType region tipe ->
3839 Report.Report "BAD MAIN TYPE" region [] $
@@ -42,7 +43,7 @@ toReport source err =
4243 ,
4344 D.stack
4445 [ "The type of `main` value I am seeing is:"
45− , D.indent 4 $ D.dullyellow $ RT.canToDoc RT.None tipe
46+ , D.indent 4 $ D.dullyellow $ RT.canToDoc localizer RT.None tipe
4647 , D.reflow $
4748 "I only know how to handle Html, Svg, and Programs\
4849 \ though. Modify `main` to be one of those types of values!"
compiler/src/Reporting/Render/Type.hs+17−16
@@ -23,6 +23,7 @@ import qualified Elm.Name as N
2323import qualified Reporting.Annotation as A
2424import qualified Reporting.Doc as D
2525import Reporting.Doc ( Doc, (<+>), (<>) )
26+import qualified Reporting.Render.Type.Localizer as L
2627
2728
2829
@@ -177,29 +178,29 @@ collectSrcArgs tipe =
177178-- CANONICAL TYPE TO DOC
178179
179180
180−canToDoc :: Context -> Can.Type -> Doc
181−canToDoc context tipe =
181+canToDoc :: L.Localizer -> Context -> Can.Type -> Doc
182+canToDoc localizer context tipe =
182183 case tipe of
183184 Can.TLambda arg1 result ->
184185 let
185186 (arg2, rest) = collectArgs result
186187 in
187188 lambda context
188− (canToDoc Func arg1)
189− (canToDoc Func arg2)
190− (map (canToDoc Func) rest)
189+ (canToDoc localizer Func arg1)
190+ (canToDoc localizer Func arg2)
191+ (map (canToDoc localizer Func) rest)
191192
192193 Can.TVar name ->
193194 D.fromName name
194195
195196 Can.TType (ModuleName.Canonical _ home) name args ->
196197 apply context
197− (D.fromName home <> "." <> D.fromName name)
198− (map (canToDoc App) args)
198+ (L.toDoc localizer home name)
199+ (map (canToDoc localizer App) args)
199200
200201 Can.TRecord fields ext ->
201202 record
202− (map entryToDocs (Map.toList fields))
203+ (map (entryToDocs localizer) (Map.toList fields))
203204 (fmap D.fromName ext)
204205
205206 Can.TUnit ->
@@ -207,19 +208,19 @@ canToDoc context tipe =
207208
208209 Can.TTuple a b maybeC ->
209210 tuple
210− (canToDoc None a)
211− (canToDoc None b)
212− (map (canToDoc None) (Maybe.maybeToList maybeC))
211+ (canToDoc localizer None a)
212+ (canToDoc localizer None b)
213+ (map (canToDoc localizer None) (Maybe.maybeToList maybeC))
213214
214215 Can.TAlias (ModuleName.Canonical _ home) name args _ ->
215216 apply context
216− (D.fromName home <> "." <> D.fromName name)
217− (map (canToDoc App . snd) args)
217+ (L.toDoc localizer home name)
218+ (map (canToDoc localizer App . snd) args)
218219
219220
220−entryToDocs :: (N.Name, Can.Type) -> (Doc, Doc)
221−entryToDocs (name, tipe) =
222− (D.fromName name, canToDoc None tipe)
221+entryToDocs :: L.Localizer -> (N.Name, Can.Type) -> (Doc, Doc)
222+entryToDocs localizer (name, tipe) =
223+ (D.fromName name, canToDoc localizer None tipe)
223224
224225
225226collectArgs :: Can.Type -> (Can.Type, [Can.Type])
compiler/src/Reporting/Render/Type/Localizer.hs+6−5
@@ -5,7 +5,7 @@ module Reporting.Render.Type.Localizer
55 , toDoc
66 , empty
77 , fromNames
8− , fromImports
8+ , fromModule
99 )
1010 where
1111
@@ -14,6 +14,7 @@ import qualified Data.Map as Map
1414import qualified Data.Set as Set
1515
1616import qualified AST.Source as Src
17+import qualified AST.Valid as Valid
1718import qualified Elm.Name as N
1819import Reporting.Doc ((<>))
1920import qualified Reporting.Doc as D
@@ -77,13 +78,13 @@ fromNames names =
7778
7879
7980
80−-- FROM IMPORTS
81+-- FROM MODULE
8182
8283
83−fromImports :: N.Name -> [Src.Import] -> Localizer
84−fromImports self imports =
84+fromModule :: Valid.Module -> Localizer
85+fromModule (Valid.Module name _ _ _ imports _ _ _ _ _) =
8586 Localizer $ Map.fromList $
86− (self, Import Nothing All) : map toPair imports
87+ (name, Import Nothing All) : map toPair imports
8788
8889
8990toPair :: Src.Import -> (N.Name, Import)
compiler/src/Reporting/Warning.hs+4−3
@@ -18,6 +18,7 @@ import qualified Reporting.Region as R
1818import qualified Reporting.Report as Report
1919import qualified Reporting.Render.Code as Code
2020import qualified Reporting.Render.Type as RT
21+import qualified Reporting.Render.Type.Localizer as L
2122
2223
2324
@@ -37,8 +38,8 @@ data Context = Def | Pattern
3738-- TO REPORT
3839
3940
40−toReport :: Code.Source -> Warning -> Report.Report
41−toReport source warning =
41+toReport :: L.Localizer -> Code.Source -> Warning -> Report.Report
42+toReport localizer source warning =
4243 case warning of
4344 UnusedImport region moduleName ->
4445 Report.Report "unused import" region [] $
@@ -89,7 +90,7 @@ toReport source warning =
8990 [ "I inferred the type annotation myself though! You can copy it into your code:"
9091 , D.green $ D.hang 4 $ D.sep $
9192 [ D.fromName name <> " :"
92− , RT.canToDoc RT.None inferredType
93+ , RT.canToDoc localizer RT.None inferredType
9394 ]
9495 ]
9596 )