From 606057487a23b0149dfe184c19dfeb89553cda6f Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sat, 14 Nov 2026 16:02:13 +0500 Subject: [PATCH] [mypyc] Make tuple struct names unambiguous RTuple C struct names are built as "T", but signed native ints used the digit codes "8", "4", "2" and "1". A digit item code right after the length makes the name ambiguous, so two different tuple types could map to the same struct name. For example, both Tuple[Tuple[i8], Tuple[int, ...11 ints]] and Tuple[Tuple[Tuple[i8], ...10 ints], int] were named "tuple_T2T11T11IIIIIIIIIII", which caused a C compile error. The same could happen with i16, i32 and i64 items. Use "S8", "S4", "S2" and "S1" for signed native ints, mirroring the existing "U8" etc. codes for unsigned ints. Now no item code starts with a digit, so the length is always delimited. Names of tuples without signed native ints are unchanged. Update the vec pop result struct names in librt_vecs.h accordingly. --- mypyc/ir/rtypes.py | 15 +++++++++----- mypyc/lib-rt/vecs/librt_vecs.h | 36 ++++++++++++++++----------------- mypyc/test-data/run-tuples.test | 21 +++++++++++++++++++ mypyc/test/test_tuplename.py | 17 ++++++++++++---- 4 files changed, 62 insertions(+), 27 deletions(-) diff --git a/mypyc/ir/rtypes.py b/mypyc/ir/rtypes.py index 18b37f0fcb641..1cb187afbb504 100644 --- a/mypyc/ir/rtypes.py +++ b/mypyc/ir/rtypes.py @@ -756,7 +756,12 @@ def is_immutable_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]: class TupleNameVisitor(RTypeVisitor[str]): - """Produce a tuple name based on the concrete representations of types.""" + """Produce a tuple name based on the concrete representations of types. + + A tuple is encoded as "T". No item code may start with + a digit, as otherwise the name could be ambiguous (e.g. "T11..." could mean + a 1-tuple or an 11-tuple), and distinct tuple types could share a C struct. + """ def visit_rinstance(self, t: RInstance) -> str: return "O" @@ -776,13 +781,13 @@ def visit_rprimitive(self, t: RPrimitive) -> str: elif t._ctype == "char": return "C" elif t._ctype == "int64_t": - return "8" # "8 byte integer" + return "S8" # "8 byte signed integer" elif t._ctype == "int32_t": - return "4" # "4 byte integer" + return "S4" # "4 byte signed integer" elif t._ctype == "int16_t": - return "2" # "2 byte integer" + return "S2" # "2 byte signed integer" elif t._ctype == "int8_t": - return "1" # "1 byte integer" + return "S1" # "1 byte signed integer" elif t._ctype == "uint64_t": return "U8" elif t._ctype == "uint32_t": diff --git a/mypyc/lib-rt/vecs/librt_vecs.h b/mypyc/lib-rt/vecs/librt_vecs.h index 66aa4087b306b..23206fcdb4f43 100644 --- a/mypyc/lib-rt/vecs/librt_vecs.h +++ b/mypyc/lib-rt/vecs/librt_vecs.h @@ -264,31 +264,31 @@ typedef struct _VecNestedObject { } VecNestedObject; -#ifndef MYPYC_DECLARED_tuple_T2V88 -#define MYPYC_DECLARED_tuple_T2V88 -typedef struct tuple_T2V88 { +#ifndef MYPYC_DECLARED_tuple_T2VS8S8 +#define MYPYC_DECLARED_tuple_T2VS8S8 +typedef struct tuple_T2VS8S8 { VecI64 f0; int64_t f1; -} tuple_T2V88; -static tuple_T2V88 tuple_undefined_T2V88 = { { -1, NULL } , 0 }; +} tuple_T2VS8S8; +static tuple_T2VS8S8 tuple_undefined_T2VS8S8 = { { -1, NULL } , 0 }; #endif -#ifndef MYPYC_DECLARED_tuple_T2V44 -#define MYPYC_DECLARED_tuple_T2V44 -typedef struct tuple_T2V44 { +#ifndef MYPYC_DECLARED_tuple_T2VS4S4 +#define MYPYC_DECLARED_tuple_T2VS4S4 +typedef struct tuple_T2VS4S4 { VecI32 f0; int32_t f1; -} tuple_T2V44; -static tuple_T2V44 tuple_undefined_T2V44 = { { -1, NULL } , 0 }; +} tuple_T2VS4S4; +static tuple_T2VS4S4 tuple_undefined_T2VS4S4 = { { -1, NULL } , 0 }; #endif -#ifndef MYPYC_DECLARED_tuple_T2V22 -#define MYPYC_DECLARED_tuple_T2V22 -typedef struct tuple_T2V22 { +#ifndef MYPYC_DECLARED_tuple_T2VS2S2 +#define MYPYC_DECLARED_tuple_T2VS2S2 +typedef struct tuple_T2VS2S2 { VecI16 f0; int16_t f1; -} tuple_T2V22; -static tuple_T2V22 tuple_undefined_T2V22 = { { -1, NULL } , 0 }; +} tuple_T2VS2S2; +static tuple_T2VS2S2 tuple_undefined_T2VS2S2 = { { -1, NULL } , 0 }; #endif #ifndef MYPYC_DECLARED_tuple_T2VU1U1 @@ -318,9 +318,9 @@ typedef struct tuple_T2VCC { static tuple_T2VCC tuple_undefined_T2VCC = { { -1, NULL } , 0 }; #endif -typedef tuple_T2V88 VecI64PopResult; -typedef tuple_T2V44 VecI32PopResult; -typedef tuple_T2V22 VecI16PopResult; +typedef tuple_T2VS8S8 VecI64PopResult; +typedef tuple_T2VS4S4 VecI32PopResult; +typedef tuple_T2VS2S2 VecI16PopResult; typedef tuple_T2VU1U1 VecU8PopResult; typedef tuple_T2VFF VecFloatPopResult; typedef tuple_T2VCC VecBoolPopResult; diff --git a/mypyc/test-data/run-tuples.test b/mypyc/test-data/run-tuples.test index d9bcd577abe61..1d5fa987d8e6e 100644 --- a/mypyc/test-data/run-tuples.test +++ b/mypyc/test-data/run-tuples.test @@ -586,3 +586,24 @@ def test_unhashable_item() -> None: hash((a, 1)) with assertRaises(TypeError, "unhashable type: 'list'"): hash((1, (2, a))) + +[case testTupleStructNamesWithNativeInts] +from typing import Tuple + +from mypy_extensions import i8, i64 + +# These used to get the same C struct name, causing a C compile error +def f() -> Tuple[Tuple[i8], Tuple[int, int, int, int, int, int, int, int, int, int, int]]: + return ((i8(5),), (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)) + +def g() -> Tuple[Tuple[Tuple[i8], int, int, int, int, int, int, int, int, int, int], int]: + return (((i8(6),), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 99) + +def test_tuple_struct_names() -> None: + a = f() + b = g() + assert a[0][0] == 5 + assert a[1][10] == 11 + assert b[0][0][0] == 6 + assert b[0][10] == 10 + assert b[1] == 99 diff --git a/mypyc/test/test_tuplename.py b/mypyc/test/test_tuplename.py index 0fbc3994b011c..59eb3854a15ed 100644 --- a/mypyc/test/test_tuplename.py +++ b/mypyc/test/test_tuplename.py @@ -8,6 +8,7 @@ RTuple, RUnion, bool_rprimitive, + int8_rprimitive, int16_rprimitive, int32_rprimitive, int64_rprimitive, @@ -28,10 +29,10 @@ def setUp(self) -> None: def test_names(self) -> None: assert RTuple([int_rprimitive, int_rprimitive]).unique_id == "T2II" - assert RTuple([int64_rprimitive, int32_rprimitive]).unique_id == "T284" - assert RTuple([int32_rprimitive, uint32_rprimitive]).unique_id == "T24U4" - assert RTuple([int64_rprimitive, uint64_rprimitive]).unique_id == "T28U8" - assert RTuple([int16_rprimitive, uint16_rprimitive]).unique_id == "T22U2" + assert RTuple([int64_rprimitive, int32_rprimitive]).unique_id == "T2S8S4" + assert RTuple([int32_rprimitive, uint32_rprimitive]).unique_id == "T2S4U4" + assert RTuple([int64_rprimitive, uint64_rprimitive]).unique_id == "T2S8U8" + assert RTuple([int16_rprimitive, uint16_rprimitive]).unique_id == "T2S2U2" assert RTuple([uint8_rprimitive]).unique_id == "T1U1" assert RTuple([list_rprimitive, object_rprimitive, self.inst_a]).unique_id == "T3OOO" assert RTuple([list_rprimitive, object_rprimitive, self.inst_b]).unique_id == "T3OOO" @@ -43,3 +44,11 @@ def test_names(self) -> None: RTuple([bool_rprimitive, RUnion([bool_rprimitive, int_rprimitive])]).unique_id == "T2CO" ) + + def test_names_unambiguous(self) -> None: + # Previously both of these were named "T2T11T11IIIIIIIIIII", since the + # i8 item code "1" was indistinguishable from the tuple length. + t1 = RTuple([RTuple([int8_rprimitive]), RTuple([int_rprimitive] * 11)]) + t2 = RTuple([RTuple([RTuple([int8_rprimitive])] + [int_rprimitive] * 10), int_rprimitive]) + assert t1.unique_id == "T2T1S1T11IIIIIIIIIII" + assert t2.unique_id == "T2T11T1S1IIIIIIIIIII"