Skip to content

Commit 1131c45

Browse files
committed
Addressing comments
1 parent 64f9ce9 commit 1131c45

File tree

11 files changed

+219
-198
lines changed

11 files changed

+219
-198
lines changed

toolchain/check/convert.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,9 @@ static auto ConvertStructToStructOrClass(Context& context,
416416
StructInitElementCountMismatch, Error,
417417
"cannot initialize {0:class|struct} with {1} field(s) from struct "
418418
"with {2} field(s).",
419-
FormatBool, size_t, size_t);
419+
BoolAsSelect, size_t, size_t);
420420
context.emitter().Emit(value_loc_id, StructInitElementCountMismatch,
421-
{.value = ToClass}, dest_elem_fields.size(),
421+
ToClass, dest_elem_fields.size(),
422422
src_elem_fields.size());
423423
return SemIR::InstId::BuiltinError;
424424
}
@@ -1163,9 +1163,8 @@ static auto ConvertSelf(Context& context, SemIR::LocId call_loc_id,
11631163
CARBON_DIAGNOSTIC(InCallToFunctionSelf, Note,
11641164
"initializing `{0:addr self|self}` parameter of "
11651165
"method declared here",
1166-
FormatBool);
1167-
builder.Note(self_param_id, InCallToFunctionSelf,
1168-
{.value = addr_pattern});
1166+
BoolAsSelect);
1167+
builder.Note(self_param_id, InCallToFunctionSelf, addr_pattern);
11691168
});
11701169

11711170
return CallerPatternMatch(context, callee_specific_id, self_param_id,

toolchain/check/eval.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,11 @@ static auto PerformBuiltinBinaryIntOp(Context& context, SemIRLoc loc,
751751
CARBON_DIAGNOSTIC(
752752
CompileTimeShiftOutOfRange, Error,
753753
"shift distance not in range [0, {0}) in {1} {2:<<|>>} {3}",
754-
unsigned, TypedInt, FormatBool, TypedInt);
754+
unsigned, TypedInt, BoolAsSelect, TypedInt);
755755
context.emitter().Emit(
756756
loc, CompileTimeShiftOutOfRange, lhs_val.getBitWidth(),
757757
{.type = lhs.type_id, .value = lhs_val},
758-
{.value = builtin_kind == SemIR::BuiltinFunctionKind::IntLeftShift},
758+
builtin_kind == SemIR::BuiltinFunctionKind::IntLeftShift,
759759
{.type = rhs.type_id, .value = rhs_val});
760760
// TODO: Is it useful to recover by returning 0 or -1?
761761
return SemIR::ConstantId::Error;

toolchain/check/handle_binding_pattern.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,18 @@ static auto HandleAnyBindingPattern(Context& context, Parse::NodeId node_id,
106106
[&] {
107107
CARBON_DIAGNOSTIC(IncompleteTypeInVarDecl, Error,
108108
"{0:field|variable} has incomplete type {1}",
109-
FormatBool, SemIR::TypeId);
110-
return context.emitter().Build(
111-
type_node, IncompleteTypeInVarDecl,
112-
{.value = parent_class_decl.has_value()}, cast_type_id);
109+
BoolAsSelect, SemIR::TypeId);
110+
return context.emitter().Build(type_node, IncompleteTypeInVarDecl,
111+
parent_class_decl.has_value(),
112+
cast_type_id);
113113
},
114114
[&] {
115115
CARBON_DIAGNOSTIC(AbstractTypeInVarDecl, Error,
116116
"{0:field|variable} has abstract type {1}",
117-
FormatBool, SemIR::TypeId);
118-
return context.emitter().Build(
119-
type_node, AbstractTypeInVarDecl,
120-
{.value = parent_class_decl.has_value()}, cast_type_id);
117+
BoolAsSelect, SemIR::TypeId);
118+
return context.emitter().Build(type_node, AbstractTypeInVarDecl,
119+
parent_class_decl.has_value(),
120+
cast_type_id);
121121
});
122122
if (parent_class_decl) {
123123
CARBON_CHECK(context_node_kind == Parse::NodeKind::VariableIntroducer,

toolchain/check/merge.cpp

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,6 @@
1212

1313
namespace Carbon::Check {
1414

15-
// Explicit or implicit parameters. Used in diagnostics.
16-
enum class ParamKind : uint8_t {
17-
Explicit,
18-
Implicit,
19-
};
20-
21-
} // namespace Carbon::Check
22-
23-
// `ParamKind` is formatted in diagnostics as `{0}parameters`, and we only add
24-
// text for implicit paramseters.
25-
template <>
26-
struct llvm::format_provider<Carbon::Check::ParamKind> {
27-
using ParamKind = Carbon::Check::ParamKind;
28-
static void format(const ParamKind& kind, raw_ostream& out,
29-
StringRef /*style*/) {
30-
if (kind == ParamKind::Implicit) {
31-
out << "implicit ";
32-
}
33-
}
34-
};
35-
36-
namespace Carbon::Check {
37-
3815
CARBON_DIAGNOSTIC(RedeclPrevDecl, Note, "previously declared here");
3916

4017
// Diagnoses a redeclaration which is redundant.
@@ -217,7 +194,7 @@ static auto EntityHasParamError(Context& context, const DeclParams& info)
217194

218195
// Returns false if a param differs for a redeclaration. The caller is expected
219196
// to provide a diagnostic.
220-
static auto CheckRedeclParam(Context& context, ParamKind param_kind,
197+
static auto CheckRedeclParam(Context& context, bool is_implicit_param,
221198
int32_t param_index,
222199
SemIR::InstId new_param_pattern_id,
223200
SemIR::InstId prev_param_pattern_id,
@@ -231,15 +208,16 @@ static auto CheckRedeclParam(Context& context, ParamKind param_kind,
231208
return;
232209
}
233210
CARBON_DIAGNOSTIC(RedeclParamDiffers, Error,
234-
"redeclaration differs at {0}parameter {1}", ParamKind,
235-
int32_t);
236-
CARBON_DIAGNOSTIC(RedeclParamPrevious, Note,
237-
"previous declaration's corresponding {0}parameter here",
238-
ParamKind);
211+
"redeclaration differs at {0:implicit |}parameter {1}",
212+
BoolAsSelect, int32_t);
213+
CARBON_DIAGNOSTIC(
214+
RedeclParamPrevious, Note,
215+
"previous declaration's corresponding {0:implicit |}parameter here",
216+
BoolAsSelect);
239217
context.emitter()
240-
.Build(new_param_pattern_id, RedeclParamDiffers, param_kind,
218+
.Build(new_param_pattern_id, RedeclParamDiffers, is_implicit_param,
241219
param_index + 1)
242-
.Note(prev_param_pattern_id, RedeclParamPrevious, param_kind)
220+
.Note(prev_param_pattern_id, RedeclParamPrevious, is_implicit_param)
243221
.Emit();
244222
};
245223

@@ -291,7 +269,7 @@ static auto CheckRedeclParams(Context& context, SemIRLoc new_decl_loc,
291269
SemIR::InstBlockId new_param_patterns_id,
292270
SemIRLoc prev_decl_loc,
293271
SemIR::InstBlockId prev_param_patterns_id,
294-
ParamKind param_kind,
272+
bool is_implicit_param,
295273
SemIR::SpecificId prev_specific_id, bool diagnose)
296274
-> bool {
297275
// This will often occur for empty params.
@@ -304,18 +282,19 @@ static auto CheckRedeclParams(Context& context, SemIRLoc new_decl_loc,
304282
if (!diagnose) {
305283
return false;
306284
}
307-
CARBON_DIAGNOSTIC(
308-
RedeclParamListDiffers, Error,
309-
"redeclaration differs because of {1:'|missing '}{0}parameter list",
310-
ParamKind, FormatBool);
285+
CARBON_DIAGNOSTIC(RedeclParamListDiffers, Error,
286+
"redeclaration differs because of "
287+
"{1:'|missing '}{0:implicit |}parameter list",
288+
BoolAsSelect, BoolAsSelect);
311289
CARBON_DIAGNOSTIC(RedeclParamListPrevious, Note,
312-
"previously declared {1:with|without} {0}parameter list",
313-
ParamKind, FormatBool);
290+
"previously declared "
291+
"{1:with|without} {0:implicit |}parameter list",
292+
BoolAsSelect, BoolAsSelect);
314293
context.emitter()
315-
.Build(new_decl_loc, RedeclParamListDiffers, param_kind,
316-
{.value = new_param_patterns_id.is_valid()})
317-
.Note(prev_decl_loc, RedeclParamListPrevious, param_kind,
318-
{.value = prev_param_patterns_id.is_valid()})
294+
.Build(new_decl_loc, RedeclParamListDiffers, is_implicit_param,
295+
new_param_patterns_id.is_valid())
296+
.Note(prev_decl_loc, RedeclParamListPrevious, is_implicit_param,
297+
prev_param_patterns_id.is_valid())
319298
.Emit();
320299
return false;
321300
}
@@ -332,23 +311,25 @@ static auto CheckRedeclParams(Context& context, SemIRLoc new_decl_loc,
332311
}
333312
CARBON_DIAGNOSTIC(
334313
RedeclParamCountDiffers, Error,
335-
"redeclaration differs because of {0}parameter count of {1}", ParamKind,
336-
int32_t);
337-
CARBON_DIAGNOSTIC(RedeclParamCountPrevious, Note,
338-
"previously declared with {0}parameter count of {1}",
339-
ParamKind, int32_t);
314+
"redeclaration differs because of {0:implicit |}parameter count of {1}",
315+
BoolAsSelect, int32_t);
316+
CARBON_DIAGNOSTIC(
317+
RedeclParamCountPrevious, Note,
318+
"previously declared with {0:implicit |}parameter count of {1}",
319+
BoolAsSelect, int32_t);
340320
context.emitter()
341-
.Build(new_decl_loc, RedeclParamCountDiffers, param_kind,
321+
.Build(new_decl_loc, RedeclParamCountDiffers, is_implicit_param,
342322
new_param_pattern_ids.size())
343-
.Note(prev_decl_loc, RedeclParamCountPrevious, param_kind,
323+
.Note(prev_decl_loc, RedeclParamCountPrevious, is_implicit_param,
344324
prev_param_pattern_ids.size())
345325
.Emit();
346326
return false;
347327
}
348328
for (auto [index, new_param_pattern_id, prev_param_pattern_id] :
349329
llvm::enumerate(new_param_pattern_ids, prev_param_pattern_ids)) {
350-
if (!CheckRedeclParam(context, param_kind, index, new_param_pattern_id,
351-
prev_param_pattern_id, prev_specific_id, diagnose)) {
330+
if (!CheckRedeclParam(context, is_implicit_param, index,
331+
new_param_pattern_id, prev_param_pattern_id,
332+
prev_specific_id, diagnose)) {
352333
return false;
353334
}
354335
}
@@ -434,15 +415,16 @@ auto CheckRedeclParamsMatch(Context& context, const DeclParams& new_entity,
434415
EntityHasParamError(context, prev_entity)) {
435416
return false;
436417
}
437-
if (!CheckRedeclParams(context, new_entity.loc,
438-
new_entity.implicit_param_patterns_id, prev_entity.loc,
439-
prev_entity.implicit_param_patterns_id,
440-
ParamKind::Implicit, prev_specific_id, diagnose)) {
418+
if (!CheckRedeclParams(
419+
context, new_entity.loc, new_entity.implicit_param_patterns_id,
420+
prev_entity.loc, prev_entity.implicit_param_patterns_id,
421+
/*is_implicit_param=*/true, prev_specific_id, diagnose)) {
441422
return false;
442423
}
443424
if (!CheckRedeclParams(context, new_entity.loc, new_entity.param_patterns_id,
444425
prev_entity.loc, prev_entity.param_patterns_id,
445-
ParamKind::Explicit, prev_specific_id, diagnose)) {
426+
/*is_implicit_param=*/false, prev_specific_id,
427+
diagnose)) {
446428
return false;
447429
}
448430
if (check_syntax &&

toolchain/diagnostics/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ cc_library(
5959

6060
cc_library(
6161
name = "format_providers",
62+
srcs = ["format_providers.cpp"],
6263
hdrs = ["format_providers.h"],
6364
deps = [
6465
"//common:check",
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
#include "toolchain/diagnostics/format_providers.h"
6+
7+
#include "common/check.h"
8+
#include "llvm/ADT/StringExtras.h"
9+
10+
auto llvm::format_provider<Carbon::BoolAsSelect>::format(
11+
const Carbon::BoolAsSelect& wrapper, raw_ostream& out, StringRef style)
12+
-> void {
13+
if (style.empty()) {
14+
llvm::format_provider<bool>::format(wrapper.value, out, style);
15+
return;
16+
}
17+
18+
// Remove wrapping quotes if present.
19+
if (style.starts_with('\'') && style.ends_with('\'')) {
20+
style = style.drop_front().drop_back();
21+
}
22+
23+
auto sep = style.find('|');
24+
CARBON_CHECK(
25+
sep != llvm::StringRef::npos,
26+
"BoolAsSelect requires a `|` separating true and false results: `{0}`",
27+
style);
28+
CARBON_CHECK(style.find('|', sep + 1) == llvm::StringRef::npos,
29+
"BoolAsSelect only allows one `|`: `{0}`", style);
30+
31+
if (wrapper.value) {
32+
out << style.take_front(sep);
33+
} else {
34+
out << style.drop_front(sep + 1);
35+
}
36+
}
37+
38+
auto llvm::format_provider<Carbon::IntAsSelect>::format(
39+
const Carbon::IntAsSelect& wrapper, raw_ostream& out, StringRef style)
40+
-> void {
41+
if (style.empty()) {
42+
llvm::format_provider<int>::format(wrapper.value, out, style);
43+
return;
44+
}
45+
46+
// Remove wrapping quotes if present.
47+
if (style.starts_with('\'') && style.ends_with('\'')) {
48+
style = style.drop_front().drop_back();
49+
}
50+
51+
auto cursor = style;
52+
while (!cursor.empty()) {
53+
auto case_sep = cursor.find("|");
54+
auto token = cursor.substr(0, case_sep);
55+
if (case_sep == llvm::StringRef::npos) {
56+
cursor = llvm::StringRef();
57+
} else {
58+
cursor = cursor.drop_front(case_sep + 1);
59+
}
60+
61+
auto pair_sep = token.find(':');
62+
CARBON_CHECK(pair_sep != llvm::StringRef::npos,
63+
"IntAsSelect requires a `:` separating each comparison and "
64+
"output string: `{0}`",
65+
style);
66+
67+
auto comp = token.take_front(pair_sep);
68+
auto output_string = token.drop_front(pair_sep + 1);
69+
70+
if (comp.empty()) {
71+
// Default case.
72+
CARBON_CHECK(cursor.empty(),
73+
"IntAsSelect requires the default case be last: `{0}`",
74+
style);
75+
out << output_string;
76+
return;
77+
} else if (comp.consume_front("=")) {
78+
// Equality comparison.
79+
int value;
80+
CARBON_CHECK(to_integer(comp, value),
81+
"IntAsSelect has invalid value in comparison: `{0}`", style);
82+
if (value == wrapper.value) {
83+
out << output_string;
84+
return;
85+
}
86+
} else {
87+
CARBON_FATAL("IntAsSelect has unrecognized comparison: `{0}`", style);
88+
}
89+
}
90+
91+
CARBON_FATAL("IntAsSelect doesn't handle `{0}`: `{1}`", wrapper.value, style);
92+
}

0 commit comments

Comments
 (0)