Skip to content

Commit 2a36ff6

Browse files
authored
Remove a couple std::string uses in diagnostics. (#4421)
We can't completely remove std::string from diagnostics because it's probably better to provide a string than something like a StringLiteralValueId or NameId (because those may be opaque for someone trying to present the diagnostic). So this change is just playing whackamole on another couple things that could easily use the new format providers.
1 parent 7c22348 commit 2a36ff6

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

toolchain/check/handle_literal.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "toolchain/check/call.h"
66
#include "toolchain/check/context.h"
77
#include "toolchain/check/handle.h"
8+
#include "toolchain/diagnostics/format_providers.h"
89
#include "toolchain/sem_ir/typed_insts.h"
910

1011
namespace Carbon::Check {
@@ -127,10 +128,10 @@ static auto HandleIntOrUnsignedIntTypeLiteral(Context& context,
127128
if (!(context.ints().Get(size_id) & 3).isZero()) {
128129
CARBON_DIAGNOSTIC(IntWidthNotMultipleOf8, Error,
129130
"bit width of integer type literal must be a multiple of "
130-
"8; use `Core.{0}({1})` instead",
131-
std::string, llvm::APSInt);
131+
"8; use `Core.{0:Int|UInt}({1})` instead",
132+
BoolAsSelect, llvm::APSInt);
132133
context.emitter().Emit(
133-
node_id, IntWidthNotMultipleOf8, int_kind.is_signed() ? "Int" : "UInt",
134+
node_id, IntWidthNotMultipleOf8, int_kind.is_signed(),
134135
llvm::APSInt(context.ints().Get(size_id), /*isUnsigned=*/true));
135136
}
136137
auto width_id = MakeI32Literal(context, node_id, size_id);

toolchain/check/handle_struct.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "toolchain/check/context.h"
77
#include "toolchain/check/convert.h"
88
#include "toolchain/check/handle.h"
9+
#include "toolchain/diagnostics/format_providers.h"
910

1011
namespace Carbon::Check {
1112

@@ -70,7 +71,7 @@ auto HandleParseNode(Context& context, Parse::StructTypeFieldId node_id)
7071

7172
static auto DiagnoseDuplicateNames(Context& context,
7273
SemIR::InstBlockId type_block_id,
73-
llvm::StringRef construct) -> bool {
74+
bool is_struct_type_literal) -> bool {
7475
auto& sem_ir = context.sem_ir();
7576
auto fields = sem_ir.inst_blocks().Get(type_block_id);
7677
Map<SemIR::NameId, SemIR::InstId> names;
@@ -80,12 +81,13 @@ static auto DiagnoseDuplicateNames(Context& context,
8081
auto result = names.Insert(field_inst.name_id, field_inst_id);
8182
if (!result.is_inserted()) {
8283
CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
83-
"duplicated field name `{1}` in {0}", std::string,
84-
SemIR::NameId);
84+
"duplicated field name `{1}` in "
85+
"{0:struct type literal|struct literal}",
86+
BoolAsSelect, SemIR::NameId);
8587
CARBON_DIAGNOSTIC(StructNamePrevious, Note,
8688
"field with the same name here");
8789
context.emitter()
88-
.Build(field_inst_id, StructNameDuplicate, construct.str(),
90+
.Build(field_inst_id, StructNameDuplicate, is_struct_type_literal,
8991
field_inst.name_id)
9092
.Note(result.value(), StructNamePrevious)
9193
.Emit();
@@ -103,7 +105,8 @@ auto HandleParseNode(Context& context, Parse::StructLiteralId node_id) -> bool {
103105
context.node_stack()
104106
.PopAndDiscardSoloNodeId<Parse::NodeKind::StructLiteralStart>();
105107
auto type_block_id = context.args_type_info_stack().Pop();
106-
if (DiagnoseDuplicateNames(context, type_block_id, "struct literal")) {
108+
if (DiagnoseDuplicateNames(context, type_block_id,
109+
/*is_struct_type_literal=*/false)) {
107110
context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
108111
return true;
109112
}
@@ -128,7 +131,8 @@ auto HandleParseNode(Context& context, Parse::StructTypeLiteralId node_id)
128131
CARBON_CHECK(refs_id != SemIR::InstBlockId::Empty,
129132
"{{}} is handled by StructLiteral.");
130133

131-
if (DiagnoseDuplicateNames(context, refs_id, "struct type literal")) {
134+
if (DiagnoseDuplicateNames(context, refs_id,
135+
/*is_struct_type_literal=*/true)) {
132136
context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
133137
return true;
134138
}

toolchain/source/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ cc_library(
1313
deps = [
1414
"//common:error",
1515
"//toolchain/diagnostics:diagnostic_emitter",
16+
"//toolchain/diagnostics:format_providers",
1617
"@llvm-project//llvm:Support",
1718
],
1819
)

0 commit comments

Comments
 (0)