Skip to content
Open
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
15 changes: 10 additions & 5 deletions mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<length><item codes>". 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"
Expand All @@ -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":
Expand Down
48 changes: 24 additions & 24 deletions mypyc/lib-rt/vecs/librt_vecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions mypyc/test-data/run-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 13 additions & 4 deletions mypyc/test/test_tuplename.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
RTuple,
RUnion,
bool_rprimitive,
int8_rprimitive,
int16_rprimitive,
int32_rprimitive,
int64_rprimitive,
Expand All @@ -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"
Expand All @@ -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"
Loading