on master help sign in
Merge pull request #1074 from elm-lang/dev

Fixes from getting core working again

commit719f335a056fc071ad17ccc3749903bb4e184e5c authorEvan Czaplicki <info@elm-lang.org> dateThu 1 Oct 2015, 18:38 (11 years ago) parent255f9df79fa633
9 files changed+33−26
elm-compiler.cabal+1 src/Generate/JavaScript/Expression.hs+7−18 src/Optimize/Patterns/DecisionTree.hs+3 src/Reporting/Error/Syntax.hs+7 src/Validate.hs+11−2 tests/test-files/good-expected-js/AliasSubstitution.elm.js+1−3 tests/test-files/good-expected-js/Ports.elm.js+1−1 tests/test-files/good-expected-js/PortsWithTypeAliases.elm.js+1−1 tests/test-files/good-expected-js/Unify/NonHomogeneousRecords.elm.js+1−1
elm-compiler.cabal+1
@@ -78,6 +78,7 @@ Library
7878 Elm.Compiler.Version,
7979 Generate.JavaScript,
8080 Generate.JavaScript.BuiltIn,
81+ Generate.JavaScript.Expression,
8182 Generate.JavaScript.Helpers,
8283 Generate.JavaScript.Literal,
8384 Generate.JavaScript.Port,
src/Generate/JavaScript/Expression.hs+7−18
@@ -127,24 +127,13 @@ generateCode expr =
127127 jsExpr $ BuiltIn.recordUpdate jsRecord jsFields
128128
129129 Record fields ->
130− do fields' <-
131− forM fields $ \(field, e) ->
132− (,) (Var.safe field) <$> generateJsExpr e
133−
134− let fieldMap =
135− List.foldl' combine Map.empty fields'
136−
137− jsExpr $ ObjectLit () $ (prop "_", hidden fieldMap) : visible fieldMap
138− where
139− combine record (field, value) =
140− Map.insertWith (++) field [value] record
141−
142− hidden fs =
143− ObjectLit () . map (prop *** ArrayLit ()) $
144− Map.toList (Map.filter (not . null) (Map.map tail fs))
145−
146− visible fs =
147− map (first prop) (Map.toList (Map.map head fs))
130+ let
131+ toField (field, value) =
132+ do jsValue <- generateJsExpr value
133+ return (prop (Var.safe field), jsValue)
134+ in
135+ do jsFields <- mapM toField fields
136+ jsExpr $ ObjectLit () jsFields
148137
149138 Binop op leftExpr rightExpr ->
150139 binop op leftExpr rightExpr
src/Optimize/Patterns/DecisionTree.hs+3
@@ -159,6 +159,9 @@ toDecisionTree variantDict rawBranches =
159159 (_, []) ->
160160 Decision path decisionEdges Nothing
161161
162+ ([], _ : _) ->
163+ toDecisionTree variantDict fallback
164+
162165 (_, _) ->
163166 Decision path decisionEdges (Just (toDecisionTree variantDict fallback))
164167
src/Reporting/Error/Syntax.hs+7
@@ -21,6 +21,7 @@ data Error
2121 | TypeWithoutDefinition String
2222 | PortWithoutAnnotation String
2323 | UnexpectedPort
24+ | DuplicateFieldName String
2425 | DuplicateValueDeclaration String
2526 | DuplicateTypeDeclaration String
2627 | DuplicateDefinition String
@@ -97,6 +98,12 @@ toReport dealiaser err =
9798 ++ "if the module is imported twice, do we send values out the port twice?"
9899 )
99100
101+ DuplicateFieldName name ->
102+ Report.simple
103+ "DUPLICATE FIELD"
104+ ("This record has more than one field named `" ++ name ++ "`.")
105+ "There can only be one. Do some renaming to make sure the names are distinct!"
106+
100107 DuplicateValueDeclaration name ->
101108 Report.simple
102109 "DUPLICATE DEFINITION"
src/Validate.hs+11−2
@@ -1,6 +1,7 @@
11{-# OPTIONS_GHC -Wall #-}
22module Validate (declarations) where
33
4+import Control.Monad (foldM)
45import qualified Data.Map as Map
56import qualified Data.Set as Set
67import qualified Data.Foldable as F
@@ -253,8 +254,16 @@ expression (A.A ann sourceExpression) =
253254 <*> T.traverse second fields
254255
255256 Record fields ->
256− Record
257− <$> T.traverse second fields
257+ let
258+ checkDups seenFields (field,_) =
259+ if Set.member field seenFields then
260+ Result.throw ann (Error.DuplicateFieldName field)
261+
262+ else
263+ return (Set.insert field seenFields)
264+ in
265+ do _ <- foldM checkDups Set.empty fields
266+ Record <$> T.traverse second fields
258267
259268 Let defs body ->
260269 Let
tests/test-files/good-expected-js/AliasSubstitution.elm.js+1−3
@@ -4,8 +4,6 @@ Elm.Main.make = function (_elm) {
44 _elm.Main = _elm.Main || {};
55 if (_elm.Main.values) return _elm.Main.values;
66 var _op = {};
7− var extractVec = function (v) {
8− return {_: {},x: v.x,y: v.y};
9− };
7+ var extractVec = function (v) { return {x: v.x,y: v.y};};
108 return _elm.Main.values = {_op: _op,extractVec: extractVec};
119};
tests/test-files/good-expected-js/Ports.elm.js+1−1
@@ -10,7 +10,7 @@ Elm.Main.make = function (_elm) {
1010 return {name: v.name,age: v.age};
1111 });
1212 },
13− _utils.list([{_: {},age: 42,name: "Tom"}]));
13+ _utils.list([{name: "Tom",age: 42}]));
1414 var time = Elm.Native.Port.make(_elm).outbound("time",
1515 function (v) {
1616 return v;
tests/test-files/good-expected-js/PortsWithTypeAliases.elm.js+1−1
@@ -21,6 +21,6 @@ Elm.Main.make = function (_elm) {
2121 ,y: typeof v.y === "number" ? v.y : _U.badPort("a number",
2222 v.y)} : _U.badPort("an object with fields `x`, `y`",v);
2323 });
24− var Point = F2(function (a,b) { return {_: {},x: a,y: b};});
24+ var Point = F2(function (a,b) { return {x: a,y: b};});
2525 return _elm.Main.values = {_op: _op,Point: Point};
2626};
tests/test-files/good-expected-js/Unify/NonHomogeneousRecords.elm.js+1−1
@@ -5,6 +5,6 @@ Elm.Main.make = function (_elm) {
55 if (_elm.Main.values) return _elm.Main.values;
66 var _op = {};
77 var f = function (t) { var y = t.y;var x = t.x;return t;};
8− var Thing = F2(function (a,b) { return {_: {},x: a,y: b};});
8+ var Thing = F2(function (a,b) { return {x: a,y: b};});
99 return _elm.Main.values = {_op: _op,Thing: Thing,f: f};
1010};