guppylang icon indicating copy to clipboard operation
guppylang copied to clipboard

Allow shadowing of extern variables

Open tatiana-s opened this issue 10 months ago • 3 comments

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.

tatiana-s avatar Mar 03 '25 11:03 tatiana-s

Even if it can't work - the error should be a GuppyError not a crash

ss2165 avatar Mar 03 '25 11:03 ss2165

Added a temporary GuppyError for this case in #815

tatiana-s avatar Mar 04 '25 12:03 tatiana-s

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

mark-koch avatar Mar 17 '25 14:03 mark-koch