mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 14:22:44 +00:00
This is the final step in landing the _structural_ updates to diagnostic rendering. The key idea here is that we want the primary diagnostic to have a message clearly explaining the problem and attached to the code that has that problem. This should be the starting point for readers working to address the issue, but is rendered attached to the code snippet in question so errors are always "in context" of the code with the error. Everything else in a diagnostic is an additional label attached to code to explain or help the reader understand the problem in the primary message. Previously, `ContextScope` resulted in some confusion here, as diagnostic consumers turned messages into labels and made the message in the context scope the message. In essence, this isn't a scope with context _for_ a diagnostic (with two exceptions that should probably have been an `AnnotationScope`), but a context within `check` where we have a preferred message for a diagnostic emitted by general purpose code we call into. For example, "class was forward declared here" starts as an error, and then consumers render it as a label to make the message from the context scope the error. We also grew an `AnnotationScope` to handle the contextual information we want to show for a diagnostic (what I would have assumed `ContextScope` actually did). Now we have a `Diagnoser`: code in `check` that can better explain the problem than shared logic it is calling into passes a callback that builds a caller-specific diagnostic. The callee then can attach relevant labels and emit it. Diagnostics no longer store `contexts` and consumers no longer need the logic to restructure things. We move all (but two) of the old contexts to be normal messages emitted in this way, and we move some diagnostics to become labels. Each of the new normal messages corresponds to a new diagnostic kind as usual. Common code following this pattern now _requires_ the `Diagnoser` to be passed (or emit nothing), and this uncovered three places where an empty context was used that made the rendered form miss key information. All three are fixed with new diagnostics, one already covered by tests, and two with TODOs to add test coverage. We also completely remove the old `ContextScope` and `CARBON_DIAGNOSTIC_CONTEXT` and rename `AnnotationScope` to `ContextScope`. It is only used to attach information about when/where a diagnostic occurred. Labels also no longer have categories. What were `Primary` are now `ExtraRange`s on the diagnostic rather than labels. Labels are now always informational. `DeclaredName` now narrows the range of a label when the range is a declaration of a named entity to only be the range of that name. For entities that don't have a declared name, the entire declaration remains the range. The code building and emitting these diagnostics can skip them if the declared location isn't distinct from the diagnostic location. Wording is largely unchanged here so we can just review the _structural_ change. #7991 iterates using this to improve locations and adds missing labels. A subsequent PR will iterate on the wording to increasingly fit within this structural model. This is separated out as a way to review incrementally, and so that subsequently we don't have to review wording improvements that only make sense in a new structure that you cannot see yet. It seems easier to look at the structure here even with awkward wording. Assisted-by: Claude Code
254 lines
9.6 KiB
C++
254 lines
9.6 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/check/context.h"
|
|
#include "toolchain/check/generic.h"
|
|
#include "toolchain/check/handle.h"
|
|
#include "toolchain/check/inst.h"
|
|
#include "toolchain/check/member_access.h"
|
|
#include "toolchain/check/name_component.h"
|
|
#include "toolchain/check/name_lookup.h"
|
|
#include "toolchain/check/pointer_dereference.h"
|
|
#include "toolchain/check/type.h"
|
|
#include "toolchain/lex/token_kind.h"
|
|
#include "toolchain/sem_ir/inst.h"
|
|
#include "toolchain/sem_ir/typed_insts.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto HandleParseNode(Context& context, Parse::MemberAccessExprId node_id)
|
|
-> bool {
|
|
auto node_kind = context.node_stack().PeekNodeKind();
|
|
|
|
if (node_kind == Parse::NodeKind::ParenExpr) {
|
|
auto member_expr_id = context.node_stack().PopExpr();
|
|
auto base_id = context.node_stack().PopExpr();
|
|
auto member_id =
|
|
PerformCompoundMemberAccess(context, node_id, base_id, member_expr_id);
|
|
context.node_stack().Push(node_id, member_id);
|
|
} else if (node_kind == Parse::NodeKind::IntLiteral) {
|
|
auto index_inst_id = context.node_stack().PopExpr();
|
|
auto tuple_inst_id = context.node_stack().PopExpr();
|
|
|
|
auto tuple_value_inst_id =
|
|
PerformTupleAccess(context, node_id, tuple_inst_id, index_inst_id);
|
|
|
|
context.node_stack().Push(node_id, tuple_value_inst_id);
|
|
} else {
|
|
SemIR::NameId name_id = context.node_stack().PopName();
|
|
auto base_id = context.node_stack().PopExpr();
|
|
auto member_id = PerformMemberAccess(context, node_id, base_id, name_id);
|
|
context.node_stack().Push(node_id, member_id);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::PointerMemberAccessExprId node_id)
|
|
-> bool {
|
|
auto diagnose_not_pointer = [&context,
|
|
&node_id](SemIR::TypeId not_pointer_type_id) {
|
|
// TODO: Pass in the expression we're trying to dereference to produce a
|
|
// better diagnostic.
|
|
CARBON_DIAGNOSTIC(ArrowOperatorOfNonPointer, Error,
|
|
"cannot apply `->` operator to non-pointer type {0}",
|
|
SemIR::TypeId);
|
|
|
|
auto builder =
|
|
context.emitter().Build(LocIdForDiagnostics::TokenOnly(node_id),
|
|
ArrowOperatorOfNonPointer, not_pointer_type_id);
|
|
builder.Emit();
|
|
};
|
|
|
|
auto node_kind = context.node_stack().PeekNodeKind();
|
|
|
|
if (node_kind == Parse::NodeKind::ParenExpr) {
|
|
auto member_expr_id = context.node_stack().PopExpr();
|
|
auto base_id = context.node_stack().PopExpr();
|
|
auto deref_base_id = PerformPointerDereference(context, node_id, base_id,
|
|
diagnose_not_pointer);
|
|
auto member_id = PerformCompoundMemberAccess(context, node_id,
|
|
deref_base_id, member_expr_id);
|
|
context.node_stack().Push(node_id, member_id);
|
|
} else if (node_kind == Parse::NodeKind::IntLiteral) {
|
|
auto index_inst_id = context.node_stack().PopExpr();
|
|
auto tuple_pointer_inst_id = context.node_stack().PopExpr();
|
|
auto tuple_inst_id = PerformPointerDereference(
|
|
context, node_id, tuple_pointer_inst_id, diagnose_not_pointer);
|
|
auto tuple_value_inst_id =
|
|
PerformTupleAccess(context, node_id, tuple_inst_id, index_inst_id);
|
|
|
|
context.node_stack().Push(node_id, tuple_value_inst_id);
|
|
} else {
|
|
SemIR::NameId name_id = context.node_stack().PopName();
|
|
auto base_id = context.node_stack().PopExpr();
|
|
auto deref_base_id = PerformPointerDereference(context, node_id, base_id,
|
|
diagnose_not_pointer);
|
|
auto member_id =
|
|
PerformMemberAccess(context, node_id, deref_base_id, name_id);
|
|
context.node_stack().Push(node_id, member_id);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Returns the `NameId` for an identifier node.
|
|
static auto GetIdentifierAsNameId(
|
|
Context& context,
|
|
Parse::NodeIdOneOf<Parse::IdentifierNameNotBeforeSignatureId,
|
|
Parse::IdentifierNameMaybeBeforeSignatureId,
|
|
Parse::IdentifierNameExprId>
|
|
node_id) -> SemIR::NameId {
|
|
CARBON_CHECK(!context.parse_tree().node_has_error(node_id),
|
|
"TODO: Support checking error parse nodes");
|
|
auto token = context.parse_tree().node_token(node_id);
|
|
return SemIR::NameId::ForIdentifier(context.tokens().GetIdentifier(token));
|
|
}
|
|
|
|
// Handle a name that is used as an expression by performing unqualified name
|
|
// lookup.
|
|
static auto HandleNameAsExpr(Context& context, Parse::NodeId node_id,
|
|
SemIR::NameId name_id) -> SemIR::InstId {
|
|
auto result = LookupUnqualifiedName(context, node_id, name_id);
|
|
if (!TryGetLookupResultInCurrentScope(context, node_id, name_id, result)) {
|
|
return SemIR::ErrorInst::InstId;
|
|
}
|
|
return BuildNameRef(context, node_id, name_id,
|
|
result.scope_result.target_inst_id(), result.specific_id);
|
|
}
|
|
|
|
auto HandleParseNode(Context& context,
|
|
Parse::IdentifierNameNotBeforeSignatureId node_id)
|
|
-> bool {
|
|
// The parent is responsible for binding the name.
|
|
context.node_stack().Push(node_id, GetIdentifierAsNameId(context, node_id));
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context,
|
|
Parse::IdentifierNameMaybeBeforeSignatureId node_id)
|
|
-> bool {
|
|
// Push a pattern block stack entry to handle the parameter pattern.
|
|
context.pattern_block_stack().Push();
|
|
context.full_pattern_stack().PushParameterizedDecl();
|
|
// The parent is responsible for binding the name.
|
|
context.node_stack().Push(node_id, GetIdentifierAsNameId(context, node_id));
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::IdentifierNameExprId node_id)
|
|
-> bool {
|
|
auto name_id = GetIdentifierAsNameId(context, node_id);
|
|
context.node_stack().Push(node_id,
|
|
HandleNameAsExpr(context, node_id, name_id));
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::BaseNameId node_id) -> bool {
|
|
context.node_stack().Push(node_id, SemIR::NameId::Base);
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::SelfTypeNameId node_id) -> bool {
|
|
context.node_stack().Push(node_id, SemIR::NameId::SelfType);
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::SelfTypeNameExprId node_id)
|
|
-> bool {
|
|
context.node_stack().Push(
|
|
node_id, HandleNameAsExpr(context, node_id, SemIR::NameId::SelfType));
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::SelfValueNameId node_id) -> bool {
|
|
context.node_stack().Push(node_id, SemIR::NameId::SelfValue);
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::SelfValueNameExprId node_id)
|
|
-> bool {
|
|
context.node_stack().Push(
|
|
node_id, HandleNameAsExpr(context, node_id, SemIR::NameId::SelfValue));
|
|
return true;
|
|
}
|
|
|
|
// Common logic for name qualifiers.
|
|
static auto ApplyNameQualifier(Context& context) -> bool {
|
|
context.decl_name_stack().ApplyNameQualifier(PopNameComponent(context));
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context,
|
|
Parse::IdentifierNameQualifierWithParamsId /*node_id*/)
|
|
-> bool {
|
|
return ApplyNameQualifier(context);
|
|
}
|
|
|
|
auto HandleParseNode(Context& context,
|
|
Parse::IdentifierNameQualifierWithoutParamsId /*node_id*/)
|
|
-> bool {
|
|
return ApplyNameQualifier(context);
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::DesignatorExprId node_id)
|
|
-> bool {
|
|
SemIR::NameId name_id = context.node_stack().PopName();
|
|
|
|
if (name_id == SemIR::NameId::SelfType) {
|
|
// Look up `.Self`.
|
|
SemIR::InstId period_self_id =
|
|
HandleNameAsExpr(context, node_id, SemIR::NameId::PeriodSelf);
|
|
context.node_stack().Push(node_id, period_self_id);
|
|
} else {
|
|
// Otherwise this is `.Member`, so look up `.Self` and then `Member` in
|
|
// `.Self`.
|
|
SemIR::InstId period_self_id = SemIR::InstId::None;
|
|
{
|
|
// TODO: Pass a diagnoser to `HandleNameAsExpr`, so that this can emit
|
|
// "name `.Self` implicitly referenced by designated expression, but not
|
|
// found" in place of labeling the "name `.Self` not found" diagnostic.
|
|
Diagnostics::ContextScope diagnostic_context(
|
|
&context.emitter(), [&](auto& builder) {
|
|
CARBON_DIAGNOSTIC_LABEL(
|
|
NoPeriodSelfForDesignator,
|
|
"designator may only be used when `.Self` is in scope");
|
|
builder.Attach(SemIR::LocId::None, NoPeriodSelfForDesignator);
|
|
});
|
|
period_self_id =
|
|
HandleNameAsExpr(context, node_id, SemIR::NameId::PeriodSelf);
|
|
}
|
|
auto member_id =
|
|
PerformMemberAccess(context, node_id, period_self_id, name_id);
|
|
context.node_stack().Push(node_id, member_id);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::PackageExprId node_id) -> bool {
|
|
AddInstAndPush<SemIR::NameRef>(
|
|
context, node_id,
|
|
{.type_id = GetSingletonType(context, SemIR::NamespaceType::TypeInstId),
|
|
.name_id = SemIR::NameId::PackageKeyword,
|
|
.value_id = SemIR::Namespace::PackageInstId});
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::CoreNameExprId node_id) -> bool {
|
|
// TODO: Unqualified lookup will never find anything; perform lookup directly
|
|
// into file scope.
|
|
context.node_stack().Push(
|
|
node_id, HandleNameAsExpr(context, node_id, SemIR::NameId::Core));
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::CppNameExprId node_id) -> bool {
|
|
// TODO: Unqualified lookup will never find anything; perform lookup directly
|
|
// into file scope.
|
|
context.node_stack().Push(
|
|
node_id, HandleNameAsExpr(context, node_id, SemIR::NameId::Cpp));
|
|
return true;
|
|
}
|
|
|
|
} // namespace Carbon::Check
|