mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 07:24:42 +01:00
Add diagnostics for extend impl misuse. (#3721)
To make the implementation simpler, make `PopWithParseNodeIf` return `pair<NodeId, optional<value>>` rather than `optional<pair<NodeId, value>>`. While wrapping the whole result in `optional` seems more principled, it's significantly harder to work with.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "toolchain/check/convert.h"
|
||||
#include "toolchain/check/decl_name_stack.h"
|
||||
#include "toolchain/check/modifiers.h"
|
||||
#include "toolchain/parse/typed_nodes.h"
|
||||
#include "toolchain/sem_ir/ids.h"
|
||||
#include "toolchain/sem_ir/typed_insts.h"
|
||||
|
||||
@@ -63,8 +64,7 @@ static auto TryAsClassScope(Context& context, SemIR::NameScopeId scope_id)
|
||||
return context.insts().TryGetAs<SemIR::ClassDecl>(scope.inst_id);
|
||||
}
|
||||
|
||||
static auto GetDefaultSelfType(Context& context, Parse::NodeId parse_node)
|
||||
-> SemIR::TypeId {
|
||||
static auto GetDefaultSelfType(Context& context) -> SemIR::TypeId {
|
||||
auto enclosing_scope_id = context.decl_name_stack().PeekTargetScope();
|
||||
|
||||
if (auto class_decl = TryAsClassScope(context, enclosing_scope_id)) {
|
||||
@@ -73,24 +73,32 @@ static auto GetDefaultSelfType(Context& context, Parse::NodeId parse_node)
|
||||
|
||||
// TODO: This is also valid in a mixin.
|
||||
|
||||
CARBON_DIAGNOSTIC(ImplAsOutsideClass, Error,
|
||||
"`impl as` can only be used in a class.");
|
||||
context.emitter().Emit(parse_node, ImplAsOutsideClass);
|
||||
return SemIR::TypeId::Error;
|
||||
return SemIR::TypeId::Invalid;
|
||||
}
|
||||
|
||||
auto HandleDefaultSelfImplAs(Context& context,
|
||||
Parse::DefaultSelfImplAsId parse_node) -> bool {
|
||||
auto self_type_id = GetDefaultSelfType(context, parse_node);
|
||||
auto self_type_id = GetDefaultSelfType(context);
|
||||
if (!self_type_id.is_valid()) {
|
||||
CARBON_DIAGNOSTIC(ImplAsOutsideClass, Error,
|
||||
"`impl as` can only be used in a class.");
|
||||
context.emitter().Emit(parse_node, ImplAsOutsideClass);
|
||||
self_type_id = SemIR::TypeId::Error;
|
||||
}
|
||||
|
||||
context.node_stack().Push(parse_node, self_type_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Process an `extend impl` declaration by extending the impl scope with the
|
||||
// `impl`'s scope.
|
||||
static auto ExtendImpl(Context& context, Parse::AnyImplDeclId parse_node,
|
||||
SemIR::TypeId constraint_id) -> void {
|
||||
static auto ExtendImpl(Context& context, Parse::NodeId extend_node,
|
||||
Parse::AnyImplDeclId parse_node,
|
||||
Parse::NodeId self_type_node, SemIR::TypeId self_type_id,
|
||||
Parse::NodeId params_node, SemIR::TypeId constraint_id)
|
||||
-> void {
|
||||
auto enclosing_scope_id = context.decl_name_stack().PeekTargetScope();
|
||||
auto& enclosing_scope = context.name_scopes().Get(enclosing_scope_id);
|
||||
|
||||
// TODO: This is also valid in a mixin.
|
||||
if (!TryAsClassScope(context, enclosing_scope_id)) {
|
||||
@@ -99,7 +107,38 @@ static auto ExtendImpl(Context& context, Parse::AnyImplDeclId parse_node,
|
||||
context.emitter().Emit(parse_node, ExtendImplOutsideClass);
|
||||
return;
|
||||
}
|
||||
auto& enclosing_scope = context.name_scopes().Get(enclosing_scope_id);
|
||||
|
||||
if (params_node.is_valid()) {
|
||||
CARBON_DIAGNOSTIC(ExtendImplForall, Error,
|
||||
"Cannot `extend` a parameterized `impl`.");
|
||||
context.emitter().Emit(extend_node, ExtendImplForall);
|
||||
enclosing_scope.has_error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (context.parse_tree().node_kind(self_type_node) ==
|
||||
Parse::NodeKind::TypeImplAs) {
|
||||
CARBON_DIAGNOSTIC(ExtendImplSelfAs, Error,
|
||||
"Cannot `extend` an `impl` with an explicit self type.");
|
||||
auto diag = context.emitter().Build(extend_node, ExtendImplSelfAs);
|
||||
|
||||
// If the explicit self type is not the default, just bail out.
|
||||
if (self_type_id != GetDefaultSelfType(context)) {
|
||||
diag.Emit();
|
||||
enclosing_scope.has_error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// The explicit self type is the same as the default self type, so suggest
|
||||
// removing it and recover as if it were not present.
|
||||
if (auto self_as =
|
||||
context.parse_tree().ExtractAs<Parse::TypeImplAs>(self_type_node)) {
|
||||
CARBON_DIAGNOSTIC(ExtendImplSelfAsDefault, Note,
|
||||
"Remove the explicit `Self` type here.");
|
||||
diag.Note(self_as->type_expr, ExtendImplSelfAsDefault);
|
||||
}
|
||||
diag.Emit();
|
||||
}
|
||||
|
||||
auto interface_type =
|
||||
context.types().TryGetAs<SemIR::InterfaceType>(constraint_id);
|
||||
@@ -132,9 +171,10 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId parse_node)
|
||||
-> std::pair<SemIR::ImplId, SemIR::InstId> {
|
||||
auto [constraint_node, constraint_id] =
|
||||
context.node_stack().PopExprWithParseNode();
|
||||
auto self_type_id = context.node_stack().Pop<Parse::NodeCategory::ImplAs>();
|
||||
|
||||
auto params_id = context.node_stack().PopIf<Parse::NodeKind::ImplForall>();
|
||||
auto [self_type_node, self_type_id] =
|
||||
context.node_stack().PopWithParseNode<Parse::NodeCategory::ImplAs>();
|
||||
auto [params_node, params_id] =
|
||||
context.node_stack().PopWithParseNodeIf<Parse::NodeKind::ImplForall>();
|
||||
auto decl_block_id = context.inst_block_stack().Pop();
|
||||
context.node_stack().PopForSoloParseNode<Parse::NodeKind::ImplIntroducer>();
|
||||
|
||||
@@ -168,9 +208,9 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId parse_node)
|
||||
// For an `extend impl` declaration, mark the impl as extending this `impl`.
|
||||
if (!!(context.decl_state_stack().innermost().modifier_set &
|
||||
KeywordModifierSet::Extend)) {
|
||||
// TODO: Diagnose combining `extend` with `forall`.
|
||||
// TODO: Diagnose combining `extend` with an explicit self type.
|
||||
ExtendImpl(context, parse_node, constraint_type_id);
|
||||
auto extend_node = context.decl_state_stack().innermost().saw_decl_modifier;
|
||||
ExtendImpl(context, extend_node, parse_node, self_type_node, self_type_id,
|
||||
params_node, constraint_type_id);
|
||||
}
|
||||
|
||||
context.decl_state_stack().Pop(DeclState::Impl);
|
||||
|
||||
Reference in New Issue
Block a user