ponylang / ponyc

Pony is an open-source, actor-model, capabilities-secure, high performance programming language
http://www.ponylang.io
BSD 2-Clause "Simplified" License
5.68k stars 409 forks source link

Compiler error on trying to assign a value to a cast object's property #3604

Open adri326 opened 4 years ago

adri326 commented 4 years ago

Demonstration

When casting an object using the as operator and trying to assign a value to one of its properties ((object as Something).property = value), the compiler errors out with a failed assertion:

/build/ponyc/src/ponyc-0.35.1/src/libponyc/pass/refer.c:142: generate_multi_dot_name: Assertion `0` failed.

The error also appears if there are intermediary methods ((object as Something).some_method().property = value).

adri326 commented 4 years ago

I should also note that one can currently work around the bug using a match expression.

jemc commented 4 years ago

Given the assert location, this regression was probably introduced in #3304

adri326 commented 4 years ago

My old build from january, 2020 was still affected

ergl commented 3 years ago

The linked code no longer fails with an assertion error; it now emits the following error:

can't reassign to a consumed identifier in a try expression if there is a partial call involved
      (a as Wumpus).hunger = 1
                           ^

which definitely points to #3304 as the source of this. It's not clear to me why the AST_FLAG_CNSM_REASGN is assigned here, since no field is being consumed here, afaik.

ergl commented 2 years ago

The updated Pony code from OP should now be:

class Wumpus
  var hunger: USize = 0

actor Main
  new create(env: Env) =>
    let a: (Wumpus | None) = Wumpus
    try
      (a as Wumpus).hunger = 1
    end

The error message notes "can't reassign to a consumed identifier", although there's no visible consume in the code. The issue comes from the fact that as desugars to a consume: https://github.com/ponylang/ponyc/blob/deb1494a4ba423b5c46743062b00ccf5f4bf7942/src/libponyc/ast/parser.c#L1012-L1021

There are a few possibilities to go forward: