Преглед изворни кода

Fix pointer types in ternary operator.

Vladislav Folts пре 9 година
родитељ
комит
3297347b49

+ 1 - 1
src/eberon/EberonContextDesignator.ob

@@ -168,7 +168,7 @@ BEGIN
     RETURN result;
 END;
 
-PROCEDURE makePointer(type: Types.PStorageType): Record.PPointer;
+PROCEDURE makePointer*(type: Types.PStorageType): Record.PPointer;
 BEGIN
     typeId <- NEW TypeId.Type(type);
     RETURN NEW Record.Pointer("", typeId);

+ 2 - 2
src/eberon/EberonContextExpression.ob

@@ -187,7 +187,7 @@ BEGIN
     IF result = NIL THEN
         result := SELF.condition;
 
-        IF SELF.typePromotion # NIL THEN
+        IF (SELF.typePromotion # NIL) & (SELF.currentNode.parentTerm = NIL) THEN
             msg <- EberonContextDesignator.TransferPromotedTypesMsg(SELF.typePromotion);
             void <- SELF.parent().handleMessage(msg);
         END;
@@ -197,7 +197,7 @@ BEGIN
         IF (firstType IS Record.PType) & (secondType IS Record.PType) THEN
             resultType := findCommonBase(firstType, secondType);
         ELSIF (firstType IS Record.PPointer) & (secondType IS Record.PPointer) THEN
-            resultType := findCommonBase(Record.pointerBase(firstType^), Record.pointerBase(secondType^));
+            resultType := EberonContextDesignator.makePointer(findCommonBase(Record.pointerBase(firstType^), Record.pointerBase(secondType^)));
         END;
 
         IF resultType = NIL THEN 

+ 12 - 8
test/test_unit_eberon.js

@@ -1516,13 +1516,14 @@ exports.suite = {
     },
 "ternary operator": testWithContext(
         context(grammar.expression,
-                "TYPE Base = RECORD END; Derived = RECORD(Base) END;"
+                "TYPE Base = RECORD END; PBase = POINTER TO Base; Derived = RECORD(Base) END;"
                     + "Derived2 = RECORD(Base) END; Derived3 = RECORD(Base) END;" 
                 + "VAR b: BOOLEAN; i1, i2: INTEGER; s: STRING; byte: BYTE;"
                 + "rb: Base; rd: Derived; rd2: Derived2; rd3: Derived3;"
-                + "pb: POINTER TO Base; pd: POINTER TO Derived; pd2: POINTER TO Derived2; pd3: POINTER TO Derived3;"
+                + "pb: PBase; pd: POINTER TO Derived; pd2: POINTER TO Derived2; pd3: POINTER TO Derived3;"
                 + "PROCEDURE passBase(b: Base): BOOLEAN; RETURN TRUE END;"
                 + "PROCEDURE passDerived(d: Derived): BOOLEAN; RETURN TRUE END;"
+                + "PROCEDURE passPBase(p: PBase): BOOLEAN; RETURN TRUE END;"
                 ),
         pass("b ? i1 : i2",
              "(b ? i1 : i2) # 0",
@@ -1531,20 +1532,23 @@ exports.suite = {
              "b ? byte : i1",
              "b ? pb : pd",
              "b ? pd : pb",
-             "passBase(b ? pb : pd)",
-             "passBase(b ? pd : pb)",
-             "passBase(b ? pd2 : pd3)",
+             "passBase(b ? pb^ : pd^)",
+             "passBase(b ? pd^ : pb^)",
+             "passBase(b ? pd2^ : pd3^)",
              "passBase(b ? rb : rd)",
              "passBase(b ? rd : rb)",
              "passBase(b ? rd2 : rd3)",
-             "b ? i1 : b ? i1 : i2"
+             "b ? i1 : b ? i1 : i2",
+             "passPBase(b ? pb : pd)",
+             "passPBase(b ? pd : pb)"
              ),
         fail(["b ?", "not parsed"],
              ["b ? i1", "expected \":\" after \"?\" in ternary operator"],
              ["b ? i1 :", "expression is expected after \":\" in ternary operator"],
              ["b ? i1 : s", "incompatible types in ternary operator: 'INTEGER' and 'STRING'"],
-             ["passDerived(b ? pb : pd)", "type mismatch for argument 1: 'Base' cannot be converted to 'Derived'"],
-             ["passDerived(b ? pd : pb)", "type mismatch for argument 1: 'Base' cannot be converted to 'Derived'"],
+             ["passBase(b ? pb^ : pd)", "incompatible types in ternary operator: 'Base' and 'POINTER TO Derived'"],
+             ["passDerived(b ? pb : pd)", "type mismatch for argument 1: 'POINTER TO Base' cannot be converted to 'Derived'"],
+             ["passDerived(b ? pd : pb)", "type mismatch for argument 1: 'POINTER TO Base' cannot be converted to 'Derived'"],
              ["b ? b ? i1 : i2 : i1", "expected \":\" after \"?\" in ternary operator"]
              )
     )