ponyc
ponyc copied to clipboard
No compiler error for "SomeClass.create() is SomeClass.create()"
Consider the following program:
class C
primitive P
actor Main
new create(env: Env) =>
env.out.print(if C is C then "C is C" else "C is NOT C" end)
env.out.print(if P is P then "P is P" else "P is NOT P" end)
As expected, it fails with identity comparison with a new object will always be false. But then try the following program that manually desugars the creations:
class C
primitive P
actor Main
new create(env: Env) =>
env.out.print(if C.create() is C.create() then "C is C" else "C is NOT C" end)
env.out.print(if P.create() is P.create() then "P is P" else "P is NOT P" end)
This manually desugared version compiles successfully despite the fact that C.create() is C.create() is equivalent to C is C and should be treated the same by the compiler.
In response to the above, @SeanTAllen said:
Yes, that check is in
referand the change from C to C.create is in the expr pass afterThe refer pass check could be improved to catch that case as well. Its certainly more complicated then what is currently there (a simple id to id check) but it is doable and would be a welcome addition.
In particular in case anyone is interested, the C becomes a C.create in
expr_typerefthat is reference.c that is called from the expr pass.So the
valid_is_comparanddoesn't handle the TK_CALL of C.create()Then there is also this you would want to cover:
C is C.create()andC.create() is C
The current valid_is_comparand examines individual comparands, in isolation. As a result, it can only detect "always false" is-expressions and is unable to detect "always true" is-expressions such as:
c1 is c1P is PP is P.create()
I'm not going to do anything about the "always true" cases, but wanted to note them here.