Skip to content

Commit

Permalink
Add diagnostic support to pass in NameId. (#3696)
Browse files Browse the repository at this point in the history
Builds on #3695 to provide equivalent support for NameId.
  • Loading branch information
jonmeow authored Feb 9, 2024
1 parent 1bf4dc5 commit 7e7e870
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 26 deletions.
4 changes: 4 additions & 0 deletions toolchain/check/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class SemIRLocationTranslator
auto TranslateArg(DiagnosticTypeTranslation translation, llvm::Any arg) const
-> llvm::Any override {
switch (translation) {
case DiagnosticTypeTranslation::NameId: {
auto name_id = llvm::any_cast<SemIR::NameId>(arg);
return sem_ir_->names().GetFormatted(name_id).str();
}
case DiagnosticTypeTranslation::TypeId: {
auto type_id = llvm::any_cast<SemIR::TypeId>(arg);
return sem_ir_->StringifyType(type_id);
Expand Down
10 changes: 5 additions & 5 deletions toolchain/check/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ auto Context::DiagnoseDuplicateName(SemIR::InstId dup_def_id,

auto Context::DiagnoseNameNotFound(Parse::NodeId parse_node,
SemIR::NameId name_id) -> void {
CARBON_DIAGNOSTIC(NameNotFound, Error, "Name `{0}` not found.", std::string);
emitter_->Emit(parse_node, NameNotFound, names().GetFormatted(name_id).str());
CARBON_DIAGNOSTIC(NameNotFound, Error, "Name `{0}` not found.",
SemIR::NameId);
emitter_->Emit(parse_node, NameNotFound, name_id);
}

auto Context::NoteIncompleteClass(SemIR::ClassId class_id,
Expand Down Expand Up @@ -344,9 +345,8 @@ auto Context::LookupQualifiedName(Parse::NodeId parse_node,
CARBON_DIAGNOSTIC(
NameAmbiguousDueToExtend, Error,
"Ambiguous use of name `{0}` found in multiple extended scopes.",
std::string);
emitter_->Emit(parse_node, NameAmbiguousDueToExtend,
names().GetFormatted(name_id).str());
SemIR::NameId);
emitter_->Emit(parse_node, NameAmbiguousDueToExtend, name_id);
// TODO: Add notes pointing to the scopes.
return SemIR::InstId::BuiltinError;
}
Expand Down
13 changes: 6 additions & 7 deletions toolchain/check/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,19 +477,18 @@ static auto ConvertStructToStructOrClass(Context& context,
CARBON_DIAGNOSTIC(
StructInitMissingFieldInLiteral, Error,
"Missing value for field `{0}` in struct initialization.",
std::string);
context.emitter().Emit(
value_parse_node, StructInitMissingFieldInLiteral,
sem_ir.names().GetFormatted(dest_field.name_id).str());
SemIR::NameId);
context.emitter().Emit(value_parse_node,
StructInitMissingFieldInLiteral,
dest_field.name_id);
} else {
CARBON_DIAGNOSTIC(StructInitMissingFieldInConversion, Error,
"Cannot convert from struct type `{0}` to `{1}`: "
"missing field `{2}` in source type.",
SemIR::TypeId, SemIR::TypeId, std::string);
SemIR::TypeId, SemIR::TypeId, SemIR::NameId);
context.emitter().Emit(
value_parse_node, StructInitMissingFieldInConversion,
value.type_id(), target.type_id,
sem_ir.names().GetFormatted(dest_field.name_id).str());
value.type_id(), target.type_id, dest_field.name_id);
}
return SemIR::InstId::BuiltinError;
}
Expand Down
5 changes: 2 additions & 3 deletions toolchain/check/handle_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,11 @@ auto HandleClassDefinitionStart(Context& context,
// Track that this declaration is the definition.
if (class_info.definition_id.is_valid()) {
CARBON_DIAGNOSTIC(ClassRedefinition, Error, "Redefinition of class {0}.",
std::string);
SemIR::NameId);
CARBON_DIAGNOSTIC(ClassPreviousDefinition, Note,
"Previous definition was here.");
context.emitter()
.Build(parse_node, ClassRedefinition,
context.names().GetFormatted(class_info.name_id).str())
.Build(parse_node, ClassRedefinition, class_info.name_id)
.Note(class_info.definition_id, ClassPreviousDefinition)
.Emit();
} else {
Expand Down
5 changes: 2 additions & 3 deletions toolchain/check/handle_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,11 @@ auto HandleFunctionDefinitionStart(Context& context,
// Track that this declaration is the definition.
if (function.definition_id.is_valid()) {
CARBON_DIAGNOSTIC(FunctionRedefinition, Error,
"Redefinition of function {0}.", std::string);
"Redefinition of function {0}.", SemIR::NameId);
CARBON_DIAGNOSTIC(FunctionPreviousDefinition, Note,
"Previous definition was here.");
context.emitter()
.Build(parse_node, FunctionRedefinition,
context.names().GetFormatted(function.name_id).str())
.Build(parse_node, FunctionRedefinition, function.name_id)
.Note(function.definition_id, FunctionPreviousDefinition)
.Emit();
} else {
Expand Down
5 changes: 2 additions & 3 deletions toolchain/check/handle_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,11 @@ auto HandleInterfaceDefinitionStart(
// Track that this declaration is the definition.
if (interface_info.definition_id.is_valid()) {
CARBON_DIAGNOSTIC(InterfaceRedefinition, Error,
"Redefinition of interface {0}.", std::string);
"Redefinition of interface {0}.", SemIR::NameId);
CARBON_DIAGNOSTIC(InterfacePreviousDefinition, Note,
"Previous definition was here.");
context.emitter()
.Build(parse_node, InterfaceRedefinition,
context.names().GetFormatted(interface_info.name_id).str())
.Build(parse_node, InterfaceRedefinition, interface_info.name_id)
.Note(interface_info.definition_id, InterfacePreviousDefinition)
.Emit();
} else {
Expand Down
5 changes: 2 additions & 3 deletions toolchain/check/handle_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,9 @@ auto HandleMemberAccessExpr(Context& context,
}
CARBON_DIAGNOSTIC(QualifiedExprNameNotFound, Error,
"Type `{0}` does not have a member `{1}`.",
SemIR::TypeId, std::string);
SemIR::TypeId, SemIR::NameId);
context.emitter().Emit(parse_node, QualifiedExprNameNotFound,
base_type_id,
context.names().GetFormatted(name_id).str());
base_type_id, name_id);
break;
}
// TODO: `ConstType` should support member access just like the
Expand Down
4 changes: 2 additions & 2 deletions toolchain/check/handle_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ static auto DiagnoseDuplicateNames(Context& context,
if (!added) {
CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
"Duplicated field name `{1}` in {0}.", std::string,
std::string);
SemIR::NameId);
CARBON_DIAGNOSTIC(StructNamePrevious, Note,
"Field with the same name here.");
context.emitter()
.Build(field_inst_id, StructNameDuplicate, construct.str(),
sem_ir.names().GetFormatted(field_inst.name_id).str())
field_inst.name_id)
.Note(it->second, StructNamePrevious)
.Emit();
return true;
Expand Down
1 change: 1 addition & 0 deletions toolchain/diagnostics/diagnostic_emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class DiagnosticConsumer {
// diagnostic.
enum class DiagnosticTypeTranslation : int8_t {
None,
NameId,
TypeId,
};

Expand Down
4 changes: 4 additions & 0 deletions toolchain/sem_ir/ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ constexpr BoolValue BoolValue::True = BoolValue(1);
// The ID of a name. A name is either a string or a special name such as
// `self`, `Self`, or `base`.
struct NameId : public IdBase, public Printable<NameId> {
// names().GetFormatted() is used for diagnostics.
using DiagnosticType =
DiagnosticTypeInfo<std::string, DiagnosticTypeTranslation::NameId>;

// An explicitly invalid ID.
static const NameId Invalid;
// The name of `self`.
Expand Down

0 comments on commit 7e7e870

Please sign in to comment.