ponyc icon indicating copy to clipboard operation
ponyc copied to clipboard

The compiler is too strict when checking consumed variables in try-else blocks

Open Praetonus opened this issue 9 years ago • 1 comments

Currently, a consumed variable in a try block can't be consumed in the associated else block, even if the variable isn't consumed in the erroring path of the try block. For example:

actor Main
  new create(env: Env) =>
    try
      partial()
      consume env
    else
      consume env
    end

    fun partial() ? => error

If we enter the else here, env wasn't consumed in the try, but the compiler says we can't use a consumed variable.

The compiler should find the last erroring operation in a try and ignore anything happening after it when checking consumed variables in the else.

Praetonus avatar May 22 '16 11:05 Praetonus

Thanks for the report @Praetonus This is a known issue and something that is remarkably hard to get right. There are a few issues in this general family.

The "simple" solution is:

actor Main
  new create(env: Env) =>
    try
      partial()
    end
    consume env

    fun partial() ? => error

but that might not work for all cases.

SeanTAllen avatar May 22 '16 11:05 SeanTAllen