Decouple associated constants from let (#5973)

Decouples associated constants from being special cased in let handlers.
Enforces associated constant grammar restrictions in parsing instead of
checking.

Closes #5411
This commit is contained in:
Elliott Kalt
2025-09-02 23:15:26 +00:00
committed by GitHub
parent 0e6dd7e701
commit 58de34e534
43 changed files with 755 additions and 677 deletions
+26 -8
View File
@@ -46,9 +46,10 @@ static auto ApplyIntroducer(Context& context, Context::State state,
namespace {
// The kind of context in which a declaration appears.
enum DeclContextKind : int8_t {
NonClassContext = 0,
RegularContext = 0,
ClassContext = 1,
MaxDeclContextKind = ClassContext,
InterfaceContext = 2,
MaxDeclContextKind = InterfaceContext,
};
// The kind of declaration introduced by an introducer keyword.
@@ -72,6 +73,9 @@ static constexpr auto DeclIntroducers = [] {
{{{.introducer_kind = DeclIntroducerKind::Unrecognized, \
.node_kind = NodeKind::InvalidParse, \
.state_kind = StateKind::Invalid}, \
{.introducer_kind = DeclIntroducerKind::Unrecognized, \
.node_kind = NodeKind::InvalidParse, \
.state_kind = StateKind::Invalid}, \
{.introducer_kind = DeclIntroducerKind::Unrecognized, \
.node_kind = NodeKind::InvalidParse, \
.state_kind = StateKind::Invalid}}},
@@ -126,8 +130,14 @@ static constexpr auto DeclIntroducers = [] {
StateKind::TypeAfterIntroducerAsInterface);
set(Lex::TokenKind::Namespace, NodeKind::NamespaceStart,
StateKind::Namespace);
set(Lex::TokenKind::Let, NodeKind::LetIntroducer, StateKind::Let);
set_contextual(Lex::TokenKind::Var, NonClassContext,
set_contextual(Lex::TokenKind::Let, RegularContext, NodeKind::LetIntroducer,
StateKind::Let);
set_contextual(Lex::TokenKind::Let, ClassContext, NodeKind::LetIntroducer,
StateKind::Let);
set_contextual(Lex::TokenKind::Let, InterfaceContext,
NodeKind::AssociatedConstantIntroducer,
StateKind::AssociatedConstant);
set_contextual(Lex::TokenKind::Var, RegularContext,
NodeKind::VariableIntroducer, StateKind::VarAsRegular);
set_contextual(Lex::TokenKind::Var, ClassContext, NodeKind::FieldIntroducer,
StateKind::FieldDecl);
@@ -294,8 +304,12 @@ auto HandleDeclAsClass(Context& context) -> void {
HandleDecl(context, ClassContext);
}
auto HandleDeclAsNonClass(Context& context) -> void {
HandleDecl(context, NonClassContext);
auto HandleDeclAsInterface(Context& context) -> void {
HandleDecl(context, InterfaceContext);
}
auto HandleDeclAsRegular(Context& context) -> void {
HandleDecl(context, RegularContext);
}
static auto HandleDeclScopeLoop(Context& context, StateKind decl_state_kind)
@@ -315,8 +329,12 @@ auto HandleDeclScopeLoopAsClass(Context& context) -> void {
HandleDeclScopeLoop(context, StateKind::DeclAsClass);
}
auto HandleDeclScopeLoopAsNonClass(Context& context) -> void {
HandleDeclScopeLoop(context, StateKind::DeclAsNonClass);
auto HandleDeclScopeLoopAsInterface(Context& context) -> void {
HandleDeclScopeLoop(context, StateKind::DeclAsInterface);
}
auto HandleDeclScopeLoopAsRegular(Context& context) -> void {
HandleDeclScopeLoop(context, StateKind::DeclAsRegular);
}
} // namespace Carbon::Parse