ponyc icon indicating copy to clipboard operation
ponyc copied to clipboard

No compiler error for "SomeClass.create() is SomeClass.create()"

Open adrianboyko opened this issue 3 years ago • 1 comments

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 refer and the change from C to C.create is in the expr pass after

The 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_typeref that is reference.c that is called from the expr pass.

So the valid_is_comparand doesn't handle the TK_CALL of C.create()

Then there is also this you would want to cover: C is C.create() and C.create() is C

adrianboyko avatar Jul 16 '22 19:07 adrianboyko

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 c1
  • P is P
  • P is P.create()

I'm not going to do anything about the "always true" cases, but wanted to note them here.

adrianboyko avatar Jul 20 '22 00:07 adrianboyko