Skip to content

Commit 9f1c90a

Browse files
authored
[mypyc] Don't crash on non-inlinable final local reads (#15719)
Fixes mypyc/mypyc#852. Fixes mypyc/mypyc#990.
1 parent 162c74d commit 9f1c90a

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

mypyc/irbuild/builder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
UnionType,
6161
get_proper_type,
6262
)
63-
from mypy.util import split_target
63+
from mypy.util import module_prefix, split_target
6464
from mypy.visitor import ExpressionVisitor, StatementVisitor
6565
from mypyc.common import BITMAP_BITS, SELF_NAME, TEMP_ATTR_NAME
6666
from mypyc.crash import catch_errors
@@ -1023,7 +1023,7 @@ def emit_load_final(
10231023
"""
10241024
if final_var.final_value is not None: # this is safe even for non-native names
10251025
return self.load_literal_value(final_var.final_value)
1026-
elif native:
1026+
elif native and module_prefix(self.graph, fullname):
10271027
return self.load_final_static(fullname, self.mapper.type_to_rtype(typ), line, name)
10281028
else:
10291029
return None

mypyc/test-data/irbuild-basic.test

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3337,6 +3337,32 @@ def foo(z):
33373337
L0:
33383338
return 1
33393339

3340+
[case testFinalLocals]
3341+
from typing import Final
3342+
3343+
def inlined() -> str:
3344+
# XXX: the final type must be declared explicitly for Var.final_value to be set.
3345+
const: Final[str] = "Oppenheimer"
3346+
return const
3347+
3348+
def local() -> str:
3349+
const: Final[str] = inlined()
3350+
return const
3351+
[out]
3352+
def inlined():
3353+
r0, const, r1 :: str
3354+
L0:
3355+
r0 = 'Oppenheimer'
3356+
const = r0
3357+
r1 = 'Oppenheimer'
3358+
return r1
3359+
def local():
3360+
r0, const :: str
3361+
L0:
3362+
r0 = inlined()
3363+
const = r0
3364+
return const
3365+
33403366
[case testDirectlyCall__bool__]
33413367
class A:
33423368
def __bool__(self) -> bool:

0 commit comments

Comments
 (0)