diff --git a/toolchain/check/cpp/type_mapping.cpp b/toolchain/check/cpp/type_mapping.cpp index 6b5da01ab821..49fbedbec808 100644 --- a/toolchain/check/cpp/type_mapping.cpp +++ b/toolchain/check/cpp/type_mapping.cpp @@ -110,22 +110,26 @@ static auto TryMapClassType(Context& context, SemIR::ClassType class_type) break; } case SemIR::TypeLiteralInfo::Numeric: { + // Carbon supports large bit width beyond C++ builtins; we don't need to + // translate those. + if (!literal.numeric.bit_width_id.is_embedded_value()) { + return clang::QualType(); + } + int bit_width = literal.numeric.bit_width_id.AsValue(); + switch (literal.numeric.kind) { case SemIR::NumericTypeLiteralInfo::None: { CARBON_FATAL("Unexpected invalid numeric type literal"); } case SemIR::NumericTypeLiteralInfo::Float: { return context.ast_context().getRealTypeForBitwidth( - literal.numeric.bit_width_id.AsValue(), - clang::FloatModeKind::NoFloat); + bit_width, clang::FloatModeKind::NoFloat); } case SemIR::NumericTypeLiteralInfo::Int: { - return context.ast_context().getIntTypeForBitwidth( - literal.numeric.bit_width_id.AsValue(), true); + return context.ast_context().getIntTypeForBitwidth(bit_width, true); } case SemIR::NumericTypeLiteralInfo::UInt: { - return context.ast_context().getIntTypeForBitwidth( - literal.numeric.bit_width_id.AsValue(), false); + return context.ast_context().getIntTypeForBitwidth(bit_width, false); } } } diff --git a/toolchain/check/testdata/interop/cpp/function/param_unsupported.carbon b/toolchain/check/testdata/interop/cpp/function/param_unsupported.carbon index 134dfc9568ea..57a22a81a2a1 100644 --- a/toolchain/check/testdata/interop/cpp/function/param_unsupported.carbon +++ b/toolchain/check/testdata/interop/cpp/function/param_unsupported.carbon @@ -58,6 +58,44 @@ fn F() { //@dump-sem-ir-end } +// ============================================================================ +// Unconvertible integer argument. +// ============================================================================ + +// --- int_param.h + +auto foo(int a) -> void; + +// --- fail_large_int.carbon + +library "[[@TEST_NAME]]"; + +import Cpp library "int_param.h"; + +// This doesn't have a direct type mapping in `getIntTypeForBitwidth`. +fn F(x: i512) { + // CHECK:STDERR: fail_large_int.carbon:[[@LINE+4]]:11: error: call argument of type `i512` is not supported [CppCallArgTypeNotSupported] + // CHECK:STDERR: Cpp.foo(x); + // CHECK:STDERR: ^ + // CHECK:STDERR: + Cpp.foo(x); +} + +// --- fail_very_large_int.carbon + +library "[[@TEST_NAME]]"; + +import Cpp library "int_param.h"; + +// This doesn't fit as an embedded `IntId` value. +fn F(x: i8388608) { + // CHECK:STDERR: fail_very_large_int.carbon:[[@LINE+4]]:11: error: call argument of type `i8388608` is not supported [CppCallArgTypeNotSupported] + // CHECK:STDERR: Cpp.foo(x); + // CHECK:STDERR: ^ + // CHECK:STDERR: + Cpp.foo(x); +} + // CHECK:STDOUT: --- fail_todo_import_unsupported_primitive_type.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants {