From 5cd08872343c9e2e0e4148216a21b29628e818c9 Mon Sep 17 00:00:00 2001 From: Richard Si Date: Wed, 19 Jul 2023 16:07:57 -0400 Subject: [PATCH] [mypyc] Don't crash on non-inlinable final local reads --- mypyc/irbuild/builder.py | 4 ++-- mypyc/test-data/irbuild-basic.test | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/mypyc/irbuild/builder.py b/mypyc/irbuild/builder.py index 8c68f91bf633..42e84bf6292c 100644 --- a/mypyc/irbuild/builder.py +++ b/mypyc/irbuild/builder.py @@ -59,7 +59,7 @@ UnionType, get_proper_type, ) -from mypy.util import split_target +from mypy.util import module_prefix, split_target from mypy.visitor import ExpressionVisitor, StatementVisitor from mypyc.common import BITMAP_BITS, SELF_NAME, TEMP_ATTR_NAME from mypyc.crash import catch_errors @@ -1015,7 +1015,7 @@ def emit_load_final( """ if final_var.final_value is not None: # this is safe even for non-native names return self.load_literal_value(final_var.final_value) - elif native: + elif native and module_prefix(self.graph, fullname): return self.load_final_static(fullname, self.mapper.type_to_rtype(typ), line, name) else: return None diff --git a/mypyc/test-data/irbuild-basic.test b/mypyc/test-data/irbuild-basic.test index 33fc8cfaa83b..cab517df73f5 100644 --- a/mypyc/test-data/irbuild-basic.test +++ b/mypyc/test-data/irbuild-basic.test @@ -3354,6 +3354,32 @@ def foo(z): L0: return 1 +[case testFinalLocals] +from typing import Final + +def inlined() -> str: + # XXX: the final type must be declared explicitly for Var.final_value to be set. + const: Final[str] = "Oppenheimer" + return const + +def local() -> str: + const: Final[str] = inlined() + return const +[out] +def inlined(): + r0, const, r1 :: str +L0: + r0 = 'Oppenheimer' + const = r0 + r1 = 'Oppenheimer' + return r1 +def local(): + r0, const :: str +L0: + r0 = inlined() + const = r0 + return const + [case testDirectlyCall__bool__] class A: def __bool__(self) -> bool: