Improve diagnostics for missing qualified names. (#4638)

Mention the scope in which the name wasn't found.

---------

Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Richard Smith
2024-12-09 23:48:46 +00:00
committed by GitHub
co-authored by Geoff Romer
parent 4e3b8c9775
commit e75ef34591
23 changed files with 1243 additions and 25 deletions
+54 -1
View File
@@ -222,6 +222,54 @@ auto Context::DiagnoseNameNotFound(SemIRLoc loc, SemIR::NameId name_id)
emitter_->Emit(loc, NameNotFound, name_id);
}
// Given an instruction associated with a scope and a `SpecificId` for that
// scope, returns an instruction that describes the specific scope.
static auto GetInstForSpecificScope(Context& context, SemIR::InstId inst_id,
SemIR::SpecificId specific_id)
-> SemIR::InstId {
if (!specific_id.is_valid()) {
return inst_id;
}
auto inst = context.insts().Get(inst_id);
CARBON_KIND_SWITCH(inst) {
case CARBON_KIND(SemIR::ClassDecl class_decl): {
return context.types().GetInstId(
context.GetClassType(class_decl.class_id, specific_id));
}
case CARBON_KIND(SemIR::InterfaceDecl interface_decl): {
return context.types().GetInstId(
context.GetInterfaceType(interface_decl.interface_id, specific_id));
}
default: {
// Don't know how to form a specific for this generic scope.
// TODO: Handle more cases.
return SemIR::InstId::Invalid;
}
}
}
auto Context::DiagnoseMemberNameNotFound(
SemIRLoc loc, SemIR::NameId name_id,
llvm::ArrayRef<LookupScope> lookup_scopes) -> void {
if (lookup_scopes.size() == 1 &&
lookup_scopes.front().name_scope_id.is_valid()) {
const auto& scope = name_scopes().Get(lookup_scopes.front().name_scope_id);
if (auto specific_inst_id = GetInstForSpecificScope(
*this, scope.inst_id(), lookup_scopes.front().specific_id);
specific_inst_id.is_valid()) {
CARBON_DIAGNOSTIC(MemberNameNotFoundInScope, Error,
"member name `{0}` not found in {1}", SemIR::NameId,
InstIdAsType);
emitter_->Emit(loc, MemberNameNotFoundInScope, name_id, specific_inst_id);
return;
}
}
CARBON_DIAGNOSTIC(MemberNameNotFound, Error, "member name `{0}` not found",
SemIR::NameId);
emitter_->Emit(loc, MemberNameNotFound, name_id);
}
auto Context::NoteAbstractClass(SemIR::ClassId class_id,
DiagnosticBuilder& builder) -> void {
const auto& class_info = classes().Get(class_id);
@@ -590,7 +638,7 @@ auto Context::LookupQualifiedName(SemIRLoc loc, SemIR::NameId name_id,
if (required && !result.inst_id.is_valid()) {
if (!has_error) {
if (prohibited_accesses.empty()) {
DiagnoseNameNotFound(loc, name_id);
DiagnoseMemberNameNotFound(loc, name_id, lookup_scopes);
} else {
// TODO: We should report multiple prohibited accesses in case we don't
// find a valid lookup. Reporting the last one should suffice for now.
@@ -1361,6 +1409,11 @@ auto Context::GetSingletonType(SemIR::InstId singleton_id) -> SemIR::TypeId {
return type_id;
}
auto Context::GetClassType(SemIR::ClassId class_id,
SemIR::SpecificId specific_id) -> SemIR::TypeId {
return GetCompleteTypeImpl<SemIR::ClassType>(*this, class_id, specific_id);
}
auto Context::GetFunctionType(SemIR::FunctionId fn_id,
SemIR::SpecificId specific_id) -> SemIR::TypeId {
return GetCompleteTypeImpl<SemIR::FunctionType>(*this, fn_id, specific_id);