Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypyc] Don't crash on non-inlinable final local reads #15719

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions mypyc/test-data/irbuild-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down