on master help sign in
Throw errors when you have ports in a submodule
commitfb1a6d26f09f3e43fc45df9df82ec548bbb276e7 authorEvan Czaplicki <info@elm-lang.org> dateTue 12 May 2015, 19:34 (11 years ago) parentcaa8283
5 files changed+29−12
src/Compile.hs+3−2 src/Elm/Compiler.hs+3−2 src/Parse/Parse.hs+3−2 src/Reporting/Error/Syntax.hs+11 src/Validate.hs+9−6
src/Compile.hs+3−2
@@ -17,11 +17,12 @@ import qualified Type.Inference as TI
1717compile
1818 :: String
1919 -> String
20+ -> Bool
2021 -> Module.Interfaces
2122 -> String
2223 -> Result.Result Warning.Warning Error.Error Module.CanonicalModule
2324
24−compile user projectName interfaces source =
25+compile user projectName isRoot interfaces source =
2526 do
2627 -- determine if default imports should be added
2728 -- only elm-lang/core is exempt
@@ -31,7 +32,7 @@ compile user projectName interfaces source =
3132 -- Parse the source code
3233 validModule <-
3334 Result.mapError Error.Syntax $
34− Parse.program needsDefaults (getOpTable interfaces) source
35+ Parse.program needsDefaults isRoot (getOpTable interfaces) source
3536
3637 -- Canonicalize all variables, pinning down where they came from.
3738 canonicalModule <-
src/Elm/Compiler.hs+3−2
@@ -61,15 +61,16 @@ parseDependencies sourceCode =
6161compile
6262 :: String
6363 -> String
64+ -> Bool
6465 -> String
6566 -> Map.Map PublicModule.Name PublicModule.Interface
6667 -> ([Warning], Either [Error] (PublicModule.Interface, String))
67−compile user packageName source interfaces =
68+compile user packageName isRoot source interfaces =
6869 let unwrappedInterfaces =
6970 Map.mapKeysMonotonic (\(PublicModule.Name name) -> name) interfaces
7071
7172 (Result.Result warnings rawResult) =
72− Compile.compile user packageName unwrappedInterfaces source
73+ Compile.compile user packageName isRoot unwrappedInterfaces source
7374 in
7475 (,) (map Warning warnings) $
7576 case rawResult of
src/Parse/Parse.hs+3−2
@@ -21,14 +21,15 @@ import qualified Validate
2121
2222program
2323 :: Bool
24+ -> Bool
2425 -> OpTable
2526 -> String
2627 -> Result.Result wrn Error.Error M.ValidModule
27−program needsDefaults table src =
28+program needsDefaults isRoot table src =
2829 do (M.Module names filePath exports imports sourceDecls) <-
2930 parseWithTable table src programParser
3031
31− validDecls <- Validate.declarations sourceDecls
32+ validDecls <- Validate.declarations isRoot sourceDecls
3233
3334 let ammendedImports =
3435 (if needsDefaults then Imports.defaults else [], imports)
src/Reporting/Error/Syntax.hs+11
@@ -19,6 +19,7 @@ data Error
1919 | InfixDuplicate String
2020 | TypeWithoutDefinition String
2121 | PortWithoutAnnotation String
22+ | UnexpectedPort
2223 | DuplicateValueDeclaration String
2324 | DuplicateTypeDeclaration String
2425 | DuplicateDefinition String
@@ -65,6 +66,16 @@ toReport err =
6566 ++ " port " ++ portName ++ " : Signal Int"
6667 )
6768
69+
70+ UnexpectedPort ->
71+ Report.simple
72+ "PORT ERROR"
73+ "This module has ports, but ports can only appear in the main module."
74+ ( "Ports in library code would create hidden dependencies where importing a\n"
75+ ++ "module could bring in constraints not captured in the public API. Furthermore,\n"
76+ ++ "if the module is imported twice, do we send values out the port twice?"
77+ )
78+
6879 DuplicateValueDeclaration name ->
6980 Report.simple
7081 "DUPLICATE DEFINITION"
src/Validate.hs+9−6
@@ -23,13 +23,14 @@ import qualified Reporting.Result as Result
2323-- VALIDATE DECLARATIONS
2424
2525declarations
26− :: [D.SourceDecl]
26+ :: Bool
27+ -> [D.SourceDecl]
2728 -> Result.Result wrn Error.Error [D.ValidDecl]
28−declarations sourceDecls =
29+declarations isRoot sourceDecls =
2930 do validDecls <- validateDecls sourceDecls
3031 (\_ _ -> validDecls)
3132 <$> declDuplicates validDecls
32− <*> T.traverse checkDecl validDecls
33+ <*> T.traverse (checkDecl isRoot) validDecls
3334
3435
3536validateDecls
@@ -339,8 +340,8 @@ extractValues (A.A region decl) =
339340
340341-- UNBOUND TYPE VARIABLES
341342
342−checkDecl :: D.ValidDecl -> Result.Result wrn Error.Error ()
343−checkDecl (A.A region decl) =
343+checkDecl :: Bool -> D.ValidDecl -> Result.Result wrn Error.Error ()
344+checkDecl isRoot (A.A region decl) =
344345 case decl of
345346 D.Definition _ ->
346347 return ()
@@ -372,7 +373,9 @@ checkDecl (A.A region decl) =
372373 (Error.UnboundTypeVarsInAlias name boundVars v vs tipe)
373374
374375 D.Port _ ->
375− return ()
376+ if isRoot
377+ then return ()
378+ else Result.throw region Error.UnexpectedPort
376379
377380 D.Fixity _ _ _ ->
378381 return ()