Commits

Brian McKenna committed b31e3f4

Unify non-row-polymorphic objects

  • Participants
  • Parent commits 938789c
  • Branches constraints

Comments (0)

Files changed (1)

File src/typeinference.js

 }
 
 function mostGeneralUnifier(a, b, node) {
+    var row,
+        props,
+        keys;
+
     function typeError() {
         throw new Error('Type error on line ' + node.lineno + ': ' + b.toString() + ' is not ' + a.toString());
     }
     } else if(a instanceof t.RowObjectType && b instanceof t.RowObjectType) {
         return mostGeneralUnifier(a, b.row, node);
     } else if(a instanceof t.RowObjectType && b instanceof t.ObjectType) {
-        return (function(row, props, keys) {
-            while(row instanceof t.RowObjectType) {
-                props.push(row.props);
-                keys = keys.concat(_.keys(row.props));
-                row = row.row;
-            }
+        row = a;
+        props = [];
+        keys = [];
+
+        while(row instanceof t.RowObjectType) {
+            props.push(row.props);
+            keys = keys.concat(_.keys(row.props));
+            row = row.row;
+        }
 
-            if(_.difference(keys, _.keys(b.props)).length)
-                typeError();
+        if(_.difference(keys, _.keys(b.props)).length)
+            typeError();
 
-            return _.reduce(props, function(accum, prop) {
-                return _.reduce(_.keys(prop), function(accum, key) {
-                    var c = typeSubstitute(accum, prop[key]),
-                        d = typeSubstitute(accum, b.props[key]);
+        return _.reduce(props, function(accum, prop) {
+            return _.reduce(_.keys(prop), function(accum, key) {
+                var c = typeSubstitute(accum, prop[key]),
+                    d = typeSubstitute(accum, b.props[key]);
 
-                    return _.extend(accum, mostGeneralUnifier(c, d, node));
-                }, accum);
-            }, {});
-        })(a, [], []);
+                return _.extend(accum, mostGeneralUnifier(c, d, node));
+            }, accum);
+        }, {});
     } else if(a instanceof t.ObjectType && b instanceof t.RowObjectType) {
         return mostGeneralUnifier(b, a);
+    } else if(a instanceof t.ObjectType && b instanceof t.ObjectType) {
+        if(_.difference(_.keys(a.props), _.keys(b.props)).length)
+            typeError();
+
+        return _.reduce(_.keys(a.props), function(accum, key) {
+            var c = typeSubstitute(accum, a.props[key]),
+                d = typeSubstitute(accum, b.props[key]);
+
+            return _.extend(accum, mostGeneralUnifier(c, d, node));
+        }, {});
     } else if(a instanceof t.ArrayType && b instanceof t.ArrayType) {
         return mostGeneralUnifier(a.type, b.type);
     } else if(a instanceof t.TagType && b instanceof t.TagType && a.name == b.name && a.vars.length == b.vars.length) {