[Oberon] Type compatibility rules for Pointers
andreas_pirklbauer at yahoo.com
Mon Jun 15 22:04:23 CEST 2020
> R = RECORD i : INTEGER END;
> P1 = POINTER TO R;
> P2 = POINTER TO R;
> VAR p1 : P1; p2 : P2;
> I am a bit confused that p1 := p2 compiles without errors.
The two types p1 and p2 are compatible because the *base* types (which
in this example is R in both cases) are compatible.
Section 6.4. on p.5. of the Oberon-07 language report it states: "Pointer types
inherit the extension relation of their base types, if there is any.” See [*] below.
So far so good. So this part of the language report clearly defines P2 to
be an extension of P1 and therefore IF p1 IS P2 THEN (* true *) END,
as you have correctly noted.
But it looks like exception 3 in ch. 9.1. on page 9 should perhaps be modified
“3. In the case of records, the type of the source must be an extension
of the type of the destination."
“3. In the case of records or pointers, the type of the source must
be an extension of the type of the destination."
That should do the trick - BECAUSE we also have the rule "Pointer types
inherit the extension relation of their base types, if there is any.”
[*] PS: You can easily trace this case in the compiler as well:
When you write p1 := p2, two items x and y get created in StatSequence:
item x for p1 with x.type = P1
item y for p2 with y.type = P2
Now CompTypes(P1, P2, FALSE) is called in StatSequence.
Since P1 # P2, CompTypes now calls IsExtension(P1.base, P2.base)
which is of course IsExtension(R, R) which in turn is obviously TRUE.
See the code of CompTypes:
PROCEDURE CompTypes(t0, t1: ORB.Type; varpar: BOOLEAN): BOOLEAN;
RETURN (t0 = t1)
OR ~varpar &
((t0.form = ORB.Pointer) & (t1.form = ORB.Pointer) & IsExtension(t0.base, t1.base)
More information about the Oberon