ponyc
ponyc copied to clipboard
The compiler is too strict when checking consumed variables in try-else blocks
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.
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.