From ce61d116bdb848071fe71e189c2f62b2e5d3fe9b Mon Sep 17 00:00:00 2001 From: Valentin Stanciu <250871+svalentin@users.noreply.github.com> Date: Tue, 14 Jan 2025 15:15:52 +0000 Subject: [PATCH] [mypyc] Report error for nested class instead of crashing (#18460) fixes https://github.com/mypyc/mypyc/issues/864 --- mypyc/irbuild/classdef.py | 4 ++++ mypyc/test-data/irbuild-classes.test | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/mypyc/irbuild/classdef.py b/mypyc/irbuild/classdef.py index 84dd493c6d15..dda8f31fd893 100644 --- a/mypyc/irbuild/classdef.py +++ b/mypyc/irbuild/classdef.py @@ -97,6 +97,10 @@ def transform_class_def(builder: IRBuilder, cdef: ClassDef) -> None: This is the main entry point to this module. """ + if cdef.info not in builder.mapper.type_to_ir: + builder.error("Nested class definitions not supported", cdef.line) + return + ir = builder.mapper.type_to_ir[cdef.info] # We do this check here because the base field of parent diff --git a/mypyc/test-data/irbuild-classes.test b/mypyc/test-data/irbuild-classes.test index dbc1f8927669..e0f7dfe6514f 100644 --- a/mypyc/test-data/irbuild-classes.test +++ b/mypyc/test-data/irbuild-classes.test @@ -1300,3 +1300,15 @@ class T: class E(T): y: str # E: Type of "y" is incompatible with definition in trait "T" + + +[case testNestedClasses] +def outer(): + class Inner: # E: Nested class definitions not supported + pass + + return Inner + +if True: + class OtherInner: # E: Nested class definitions not supported + pass