diff --git a/mypy/typeanal.py b/mypy/typeanal.py index bd6f97cc94ce..545bf3c049f5 100644 --- a/mypy/typeanal.py +++ b/mypy/typeanal.py @@ -2915,6 +2915,16 @@ def visit_type_alias_type(self, t: TypeAliasType) -> Type: return t +# Native int types that librt.vecs supports as vec item types (there is a specialized +# vec type for each of these) +VEC_NATIVE_INT_ITEM_NAMES: Final = ( + "mypy_extensions.i64", + "mypy_extensions.i32", + "mypy_extensions.i16", + "mypy_extensions.u8", +) + + def check_vec_type_args( args: tuple[Type, ...] | list[Type], ctx: Context, api: SemanticAnalyzerCoreInterface ) -> bool: @@ -2931,6 +2941,13 @@ def check_vec_type_args( if arg.type.fullname == "builtins.int": # A fixed-width integer such as 'i64' must be used instead of plain 'int' ok = False + elif ( + arg.type.fullname in MYPYC_NATIVE_INT_NAMES + and arg.type.fullname not in VEC_NATIVE_INT_ITEM_NAMES + ): + # librt.vecs has no specialized vec for this native int type, and + # a generic vec can't hold its values + ok = False elif isinstance(arg, UnionType): non_optional = None items = [get_proper_type(item) for item in arg.items] diff --git a/mypyc/doc/librt_vecs.rst b/mypyc/doc/librt_vecs.rst index a7f5bc3ffbd9..5fb3a39e810b 100644 --- a/mypyc/doc/librt_vecs.rst +++ b/mypyc/doc/librt_vecs.rst @@ -38,6 +38,8 @@ for these *value item types*: ``int`` is not a valid item type, since it has an arbitrary precision, and vec is an efficiency-focused type. Use one of the fixed-length integer types instead. +The other native integer types (``i8``, ``u16``, ``u32`` and ``u64``) aren't supported +as item types yet. Class item types (e.g. ``str`` or ``MyNativeClass``) are represented as regular object references. Optional class item types (e.g. ``str | None``) are supported for convenience, but arbitrary diff --git a/mypyc/test-data/irbuild-vec-t.test b/mypyc/test-data/irbuild-vec-t.test index e720c2d14651..d4a3304407ee 100644 --- a/mypyc/test-data/irbuild-vec-t.test +++ b/mypyc/test-data/irbuild-vec-t.test @@ -403,6 +403,27 @@ main:5: error: Invalid item type for "vec" main:6: error: Invalid item type for "vec" main:7: error: Invalid item type for "vec" +[case testVecTCheckNativeIntItemType] +from librt.vecs import vec +from mypy_extensions import i64, i32, i16, i8, u64, u32, u16, u8 + +def bad1(v: vec[i8]) -> None: pass +def bad2(v: vec[u16]) -> None: pass +def bad3(v: vec[u32]) -> None: pass +def bad4(v: vec[u64]) -> None: pass +def bad5(v: vec[vec[u64]]) -> None: pass +def bad6() -> None: + vec[u64]() + +def ok1(v: vec[i64], v2: vec[i32], v3: vec[i16], v4: vec[u8]) -> None: pass +[out] +main:4: error: Invalid item type for "vec" +main:5: error: Invalid item type for "vec" +main:6: error: Invalid item type for "vec" +main:7: error: Invalid item type for "vec" +main:8: error: Invalid item type for "vec" +main:10: error: Invalid item type for "vec" + [case testVecTCreateWithCap_64bit] from librt.vecs import vec from mypy_extensions import i64 diff --git a/test-data/unit/check-vec.test b/test-data/unit/check-vec.test index e087a8b823be..64816e4aedb8 100644 --- a/test-data/unit/check-vec.test +++ b/test-data/unit/check-vec.test @@ -3,7 +3,7 @@ from types import EllipsisType from typing import Optional, Any, TypeVar from librt.vecs import vec -from mypy_extensions import i64, i32, i16, u8 +from mypy_extensions import i64, i32, i16, i8, u64, u32, u16, u8 def f(v: vec[i64]) -> None: x: i64 = v[0] @@ -38,6 +38,12 @@ vec_bad_optional1: vec[int | None] # E: Invalid item type for "vec" vec_bad_optional2: vec[i64 | None] # E: Invalid item type for "vec" vec_bad_optional3: vec[bool | None] # E: Invalid item type for "vec" vec_bad_optional4: vec[float | None] # E: Invalid item type for "vec" +vec_bad_i8: vec[i8] # E: Invalid item type for "vec" +vec_bad_u16: vec[u16] # E: Invalid item type for "vec" +vec_bad_u32: vec[u32] # E: Invalid item type for "vec" +vec_bad_u64: vec[u64] # E: Invalid item type for "vec" +vec_bad_nested: vec[vec[u64]] # E: Invalid item type for "vec" +vec_bad_optional5: vec[u64 | None] # E: Invalid item type for "vec" T = TypeVar("T")