guppylang
guppylang copied to clipboard
Allow shadowing of extern variables
For example (see test in test_extern.py):
guppy.extern("x", ty="int", module=module)
@guppy(module)
def main(b: bool) -> int:
if b:
x = 4
return x
This currently crashes (KeyError: Variable.Id(name='x') in the linearity checker during [scope[x] for x in live_before[bb]]), but it would be nice if this worked.
Even if it can't work - the error should be a GuppyError not a crash
Added a temporary GuppyError for this case in #815
Reflecting on this, I think it's fine to keep the error since it actually matches Python's behaviour:
x = 0
def foo(b: bool):
if b:
x = 1
return x
foo(True) # Ok
foo(False) # UnboundLocalError: local variable 'x' referenced before assignment