Files
Dana Jansens 2d38978756 Diagnose explicit Self in extend in the parse node handler (Refactor Impl construction 1/7) (#6465)
We encode the state of looking that the parent scope is a Class into a
type so that we can avoid extra lookups. Then use that in refactoring
where diagnostics are generated for an explicit `Self` in an `extend
impl` declaration.

We add tests that we don't double-diagnose the Self type when it's
already an error, and make the behaviour of `extend require` match that
of `extend impl as`.

This avoids some fragile/complex parse-node lookups (such as
`context.parse_tree_and_subtrees().ExtractAs<Parse::ImplTypeAs>`) by
using the parse node at the point where we are handling it instead of
much later.

This is part of #6420 which is being split up into a chain of smaller
PRs.
2025-12-09 20:38:50 +00:00

26 lines
770 B
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/name_scope.h"
namespace Carbon::Check {
auto TryAsClassScope(Context& context, SemIR::NameScopeId scope_id)
-> std::optional<ClassScope> {
if (!scope_id.has_value()) {
return std::nullopt;
}
auto& scope = context.name_scopes().Get(scope_id);
if (!scope.inst_id().has_value()) {
return std::nullopt;
}
auto class_decl = context.insts().TryGetAs<SemIR::ClassDecl>(scope.inst_id());
if (!class_decl) {
return std::nullopt;
}
return {{.class_decl = *class_decl, .name_scope = &scope}};
}
} // namespace Carbon::Check