Skip to content

Commit 4711b89

Browse files
Apply review suggestions
1 parent 3c7b715 commit 4711b89

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

toolchain/check/cpp/import.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,8 +2235,9 @@ static auto MapConstant(Context& context, SemIR::LocId loc_id,
22352235

22362236
SemIR::TypeId type_id = MapType(context, loc_id, expr->getType()).type_id;
22372237
if (!type_id.has_value()) {
2238-
context.TODO(
2239-
loc_id, "Unsupported: constant type: " + expr->getType().getAsString());
2238+
context.TODO(loc_id, llvm::formatv("Unsupported: C++ literal's type `{0}` "
2239+
"could not be mapped to a Carbon type",
2240+
expr->getType().getAsString()));
22402241
return SemIR::ErrorInst::InstId;
22412242
}
22422243

@@ -2258,8 +2259,9 @@ static auto MapConstant(Context& context, SemIR::LocId loc_id,
22582259
context, imported_loc_id,
22592260
{.type_id = type_id, .float_id = float_id}));
22602261
} else {
2261-
context.TODO(
2262-
loc_id, "Unsupported: constant type: " + expr->getType().getAsString());
2262+
context.TODO(loc_id, llvm::formatv(
2263+
"Unsupported: C++ constant expression type: '{0}'",
2264+
expr->getType().getAsString()));
22632265
return SemIR::ErrorInst::InstId;
22642266
}
22652267

toolchain/check/cpp/macros.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,24 @@ auto TryEvaluateMacroToConstant(Context& context, SemIR::LocId loc_id,
6262
}
6363

6464
clang::Expr::EvalResult evaluated_result;
65-
if (!result_expr->EvaluateAsConstantExpr(evaluated_result,
66-
sema.getASTContext())) {
67-
context.TODO(loc_id,
68-
"failed to evaluate macro to a valid constant expression");
69-
return nullptr;
70-
}
65+
CARBON_CHECK(result_expr->EvaluateAsConstantExpr(evaluated_result,
66+
sema.getASTContext()));
7167
clang::APValue ap_value = evaluated_result.Val;
72-
if (ap_value.isInt()) {
73-
return clang::IntegerLiteral::Create(
74-
sema.getASTContext(), ap_value.getInt(), result_expr->getType(),
75-
result_expr->getExprLoc());
76-
}
77-
if (ap_value.isFloat()) {
78-
return clang::FloatingLiteral::Create(
79-
sema.getASTContext(), ap_value.getFloat(),
80-
/*isExact=*/true, result_expr->getType(), result_expr->getExprLoc());
68+
switch (ap_value.getKind()) {
69+
case clang::APValue::Int:
70+
return clang::IntegerLiteral::Create(
71+
sema.getASTContext(), ap_value.getInt(), result_expr->getType(),
72+
result_expr->getExprLoc());
73+
case clang::APValue::Float:
74+
return clang::FloatingLiteral::Create(
75+
sema.getASTContext(), ap_value.getFloat(),
76+
/*isExact=*/true, result_expr->getType(), result_expr->getExprLoc());
77+
default:
78+
context.TODO(loc_id,
79+
"Unsupported: macro evaluated to a constant of type: " +
80+
result_expr->getType().getAsString());
81+
return nullptr;
8182
}
82-
context.TODO(loc_id, "Unsupported: constant type:" +
83-
result_expr->getType().getAsString());
84-
return nullptr;
8583
}
8684

8785
} // namespace Carbon::Check

toolchain/check/testdata/interop/cpp/macros.carbon

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn F() {
8989
// CHECK:STDERR: Cpp.CONFIG_VALUE;
9090
// CHECK:STDERR: ^~~~~~~~~~~~~~~~
9191
// CHECK:STDERR:
92-
// CHECK:STDERR: fail_import_integer_literal_too_big.carbon:[[@LINE+11]]:3: error: semantics TODO: `Unsupported: constant type: unsigned long long` [SemanticsTodo]
92+
// CHECK:STDERR: fail_import_integer_literal_too_big.carbon:[[@LINE+11]]:3: error: semantics TODO: `Unsupported: C++ literal's type `unsigned long long` could not be mapped to a Carbon type` [SemanticsTodo]
9393
// CHECK:STDERR: Cpp.CONFIG_VALUE;
9494
// CHECK:STDERR: ^~~~~~~~~~~~~~~~
9595
// CHECK:STDERR: fail_import_integer_literal_too_big.carbon:[[@LINE+8]]:3: note: in `Cpp` name lookup for `CONFIG_VALUE` [InCppNameLookup]
@@ -264,7 +264,7 @@ import Cpp library "string_literal_object_like_macro.h";
264264

265265
fn F() {
266266
// TODO: Get rid of the second error.
267-
// CHECK:STDERR: fail_todo_import_string_literal_object_like_macro.carbon:[[@LINE+11]]:3: error: semantics TODO: `Unsupported: constant type:const char[4]` [SemanticsTodo]
267+
// CHECK:STDERR: fail_todo_import_string_literal_object_like_macro.carbon:[[@LINE+11]]:3: error: semantics TODO: `Unsupported: macro evaluated to a constant of type: const char[4]` [SemanticsTodo]
268268
// CHECK:STDERR: Cpp.CONFIG_VALUE;
269269
// CHECK:STDERR: ^~~~~~~~~~~~~~~~
270270
// CHECK:STDERR: fail_todo_import_string_literal_object_like_macro.carbon:[[@LINE+8]]:3: note: in `Cpp` name lookup for `CONFIG_VALUE` [InCppNameLookup]
@@ -307,7 +307,7 @@ library "[[@TEST_NAME]]";
307307
import Cpp library "unsupported_floating_point_literal_macro.h";
308308

309309
fn F() {
310-
// CHECK:STDERR: fail_import_unsupported_floating_point_literal_macro.carbon:[[@LINE+11]]:3: error: semantics TODO: `Unsupported: constant type: long double` [SemanticsTodo]
310+
// CHECK:STDERR: fail_import_unsupported_floating_point_literal_macro.carbon:[[@LINE+11]]:3: error: semantics TODO: `Unsupported: C++ literal's type `long double` could not be mapped to a Carbon type` [SemanticsTodo]
311311
// CHECK:STDERR: Cpp.MyLongDouble;
312312
// CHECK:STDERR: ^~~~~~~~~~~~~~~~
313313
// CHECK:STDERR: fail_import_unsupported_floating_point_literal_macro.carbon:[[@LINE+8]]:3: note: in `Cpp` name lookup for `MyLongDouble` [InCppNameLookup]
@@ -335,6 +335,19 @@ fn F() {
335335
Cpp.MyDouble = 1.0;
336336
}
337337

338+
// --- lambda.carbon
339+
340+
library "[[@TEST_NAME]]";
341+
342+
import Cpp inline '''
343+
#define MyIntLambda (([] { return 7; })())
344+
''';
345+
346+
fn F() {
347+
let i: i32 = Cpp.MyIntLambda;
348+
}
349+
350+
338351
// --- macro_undefined.h
339352

340353
#define CONFIG_VALUE 1

0 commit comments

Comments
 (0)