diff --git a/mypyc/ir/rtypes.py b/mypyc/ir/rtypes.py index 2764c7ca182a..98f234c85d9a 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 cedc77433179..1ffae864a265 100644 --- a/mypyc/lib-rt/vecs/librt_vecs.h +++ b/mypyc/lib-rt/vecs/librt_vecs.h @@ -356,40 +356,40 @@ 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_T2V11 -#define MYPYC_DECLARED_tuple_T2V11 -typedef struct tuple_T2V11 { +#ifndef MYPYC_DECLARED_tuple_T2VS1S1 +#define MYPYC_DECLARED_tuple_T2VS1S1 +typedef struct tuple_T2VS1S1 { VecI8 f0; int8_t f1; -} tuple_T2V11; -static tuple_T2V11 tuple_undefined_T2V11 = { { -1, NULL } , 0 }; +} tuple_T2VS1S1; +static tuple_T2VS1S1 tuple_undefined_T2VS1S1 = { { -1, NULL } , 0 }; #endif #ifndef MYPYC_DECLARED_tuple_T2VU8U8 @@ -446,10 +446,10 @@ 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_T2V11 VecI8PopResult; +typedef tuple_T2VS8S8 VecI64PopResult; +typedef tuple_T2VS4S4 VecI32PopResult; +typedef tuple_T2VS2S2 VecI16PopResult; +typedef tuple_T2VS1S1 VecI8PopResult; typedef tuple_T2VU8U8 VecU64PopResult; typedef tuple_T2VU4U4 VecU32PopResult; typedef tuple_T2VU2U2 VecU16PopResult; diff --git a/mypyc/test-data/run-tuples.test b/mypyc/test-data/run-tuples.test index d31b67eb62ba..d29f607d7522 100644 --- a/mypyc/test-data/run-tuples.test +++ b/mypyc/test-data/run-tuples.test @@ -751,3 +751,24 @@ class TupleNeOverride(tuple[Any, ...]): class NT(NamedTuple): x: int y: int + +[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 0fbc3994b011..59eb3854a15e 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"