Replace :! binding syntax with phase keywords and contextual defaults (#7479)

Implement the toolchain side of proposal #7254, removing the `:!`
binding
syntax for generic and template parameters in favor of the keywords
`generic`,
`template`, and `runtime` plus contextual defaults for phase.

For valid programs this is semantics-preserving: each binding resolves
to the
same phase, and produces the same SemIR, as it did under `:!`/`:`. The
parser
derives a binding's phase from its syntactic context plus any explicit
phase
keyword; new diagnostics and error recovery for misused keywords are
described
below.

Implementation details for each component:

- Lexer: remove the `:!` (`ColonExclaim`) token, move its virtual
parse-node
  budget onto `:`, and add the `generic` and `runtime` keywords.
- Parser: thread a `BindingContext` (`ExplicitParam`, `DeducedParam`, or
`CompileTimeEntityParam`) from declaration introducers down through
parameter
lists to each binding pattern, using a one-token lookahead to
distinguish a
name-qualifier parameter list from a declaration's own final list.
Parameters
of a compile-time entity (`class`, `interface`, `constraint`, `choice`,
`alias`, `export`, `namespace`) and deduced `[]` parameters default to
checked
generic; explicit function parameters and local bindings default to
runtime.
`HandleBindingPattern` resolves the phase from that context plus the
keyword: a
`generic` keyword needs no node of its own (the phase is carried by the
  binding's node kind), while a `runtime` keyword is preserved as a
`RuntimeBindingName` node so `check` can name it in a diagnostic. A
phase
keyword that is merely redundant with the contextual default is
diagnosed
  here, without invalidating the parse tree.
- Check: a phase keyword that is invalid for its context (for example
`runtime`
on a checked-generic parameter) is diagnosed here, and recovers by
building an
error binding that still introduces the name so that later uses of it do
not
  produce cascading errors.

The removed `:!` syntax is now rejected as an ordinary parse error.

The `form`/`:?`/`->?` ("extended types") portion of proposal #7254 is
left for a
separate change.

Assisted-by: Claude Code
This commit is contained in:
Chandler Carruth
2026-07-11 01:22:44 +00:00
committed by GitHub
parent bf106c3b4b
commit 8be274cf60
482 changed files with 16917 additions and 16226 deletions
+190 -62
View File
@@ -8,6 +8,66 @@
namespace Carbon::Parse {
// Determines whether a `:` binding is a generic binding, from its contextual
// default and any explicit phase keyword. `is_form` is true for a `:?` form
// binding, whose phase is fixed and unaffected by phase keywords.
//
// A keyword that is *redundant* with the contextual default is diagnosed here.
// A keyword that is *invalid* for the context (such as `runtime` on a
// compile-time entity's parameter) is intentionally not rejected here: the
// requested phase is honored, and `check` diagnoses the resulting phase as
// invalid for the context and recovers by building an error binding that still
// introduces the name. Either way no parse node is flagged as an error, so
// `check` never aborts on an invalid parse tree; the caller preserves an
// explicit `runtime` keyword as a `RuntimeBindingName` node so its token is
// accounted for and `check` can see the requested phase.
static auto ResolveBindingPhase(Context& context, Context::State& state,
bool is_form,
std::optional<Lex::TokenIndex> template_token,
std::optional<Lex::TokenIndex> generic_token,
std::optional<Lex::TokenIndex> runtime_token,
bool& redundant_modifier) -> bool {
// `template`/`generic` force a generic binding, `runtime` forces a runtime
// binding, and otherwise the context's default applies.
bool resolved_generic;
if (template_token || generic_token) {
resolved_generic = true;
} else if (runtime_token) {
resolved_generic = false;
} else {
resolved_generic = state.binding_context != BindingContext::ExplicitParam;
}
// A form binding's phase is fixed, so phase keywords don't apply to it, and
// there is no point diagnosing a redundant keyword once the binding is
// already in error.
// TODO: `:?` form bindings are temporary; see the TODO in
// `HandleBindingPattern`.
if (is_form || state.has_error) {
return resolved_generic;
}
// Diagnose a phase keyword that is redundant with the contextual default. A
// keyword that is invalid (rather than redundant) is left for `check`.
if (generic_token && state.binding_context != BindingContext::ExplicitParam) {
CARBON_DIAGNOSTIC(
RedundantGenericModifier, Error,
"`generic` is redundant here; this binding is a checked generic by "
"default");
context.emitter().Emit(*generic_token, RedundantGenericModifier);
redundant_modifier = true;
} else if (runtime_token &&
state.binding_context == BindingContext::ExplicitParam) {
CARBON_DIAGNOSTIC(
RedundantRuntimeModifier, Error,
"`runtime` is redundant here; this binding is runtime by default");
context.emitter().Emit(*runtime_token, RedundantRuntimeModifier);
redundant_modifier = true;
}
return resolved_generic;
}
auto HandleBindingPattern(Context& context) -> void {
auto state = context.PopState();
@@ -16,7 +76,7 @@ auto HandleBindingPattern(Context& context) -> void {
if (!state.has_error) {
CARBON_DIAGNOSTIC(
ExpectedBindingPattern, Error,
"expected {0:name|`:`, `:!`, or `:?`} in binding pattern"
"expected {0:name|`:` or `:?`} in binding pattern"
"{1:; prefix reserved word with `r#` to form a valid identifier|}",
Diagnostics::BoolAsSelect, Diagnostics::BoolAsSelect);
context.emitter().Emit(*context.position(), ExpectedBindingPattern,
@@ -25,8 +85,10 @@ auto HandleBindingPattern(Context& context) -> void {
}
};
// A `template` keyword may precede the name.
// Phase keywords and `ref` may precede the name.
auto template_token = context.ConsumeIf(Lex::TokenKind::Template);
auto generic_token = context.ConsumeIf(Lex::TokenKind::Generic);
auto runtime_token = context.ConsumeIf(Lex::TokenKind::Runtime);
auto ref_token = context.ConsumeIf(Lex::TokenKind::Ref);
if (ref_token && state.in_var_pattern) {
CARBON_DIAGNOSTIC(RefInsideVar, Error, "found `ref` inside `var` pattern");
@@ -34,6 +96,23 @@ auto HandleBindingPattern(Context& context) -> void {
state.has_error = true;
}
// Recover from `unused` written after a phase keyword or `ref` by consuming
// it and wrapping the binding in `unused`, as if it had been written first.
// The misordering is diagnosed later, once we know the modifier is itself
// valid; a redundant or invalid modifier is diagnosed on its own, and we
// don't stack the ordering error on top of it.
std::optional<Lex::TokenIndex> misordered_unused_token;
Lex::TokenKind misordered_unused_modifier = Lex::TokenKind::Unused;
if ((template_token || generic_token || runtime_token || ref_token) &&
context.PositionIs(Lex::TokenKind::Unused)) {
misordered_unused_modifier = template_token ? Lex::TokenKind::Template
: generic_token ? Lex::TokenKind::Generic
: runtime_token ? Lex::TokenKind::Runtime
: Lex::TokenKind::Ref;
context.PushState(StateKind::FinishUnusedPattern);
misordered_unused_token = context.ConsumeChecked(Lex::TokenKind::Unused);
}
// The first item should be an identifier, the placeholder `_`, or `self`.
std::optional<Lex::TokenIndex> self_token;
if (auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier)) {
@@ -67,74 +146,123 @@ auto HandleBindingPattern(Context& context) -> void {
on_error(/*expected_name=*/true);
}
if (auto token_kind = context.PositionKind();
token_kind.is_binding_pattern_operator()) {
// Add the wrapper node for the `template` keyword if present.
if (template_token) {
if (token_kind != Lex::TokenKind::ColonExclaim && !state.has_error) {
CARBON_DIAGNOSTIC(ExpectedGenericBindingPatternAfterTemplate, Error,
"expected `:!` binding after `template`");
context.emitter().Emit(*template_token,
ExpectedGenericBindingPatternAfterTemplate);
state.has_error = true;
auto token_kind = context.PositionKind();
if (!token_kind.is_binding_pattern_operator()) {
if (self_token && !template_token && !generic_token && !runtime_token) {
// A `self` binding may omit its type; checking supplies the implicit
// `Self` type. There is no type node, so this produces a
// `SelfBindingPattern` rather than a `LetBindingPattern`.
if (ref_token) {
context.AddNode(NodeKind::RefBindingName, *ref_token, state.has_error);
}
context.AddNode(NodeKind::TemplateBindingName, *template_token,
context.AddNode(NodeKind::SelfBindingPattern, *self_token,
state.has_error);
}
if (ref_token) {
if (token_kind != Lex::TokenKind::Colon && !state.has_error) {
CARBON_DIAGNOSTIC(ExpectedRuntimeBindingPatternAfterRef, Error,
"expected `:` binding after `ref`");
context.emitter().Emit(*ref_token,
ExpectedRuntimeBindingPatternAfterRef);
state.has_error = true;
if (state.has_error) {
context.ReturnErrorOnState();
}
context.AddNode(NodeKind::RefBindingName, *ref_token, state.has_error);
return;
}
switch (token_kind) {
case Lex::TokenKind::Colon:
state.kind = StateKind::BindingPatternFinishAsRegular;
break;
case Lex::TokenKind::ColonExclaim:
state.kind = StateKind::BindingPatternFinishAsGeneric;
break;
case Lex::TokenKind::ColonQuestion:
state.kind = StateKind::BindingPatternFinishAsForm;
break;
default:
CARBON_FATAL("Unexpected token kind");
}
// Use the `:`, `:!`, or `:?` for the root node.
state.token = context.Consume();
if (token_kind == Lex::TokenKind::ColonExclaim) {
// Add a virtual node before the compile time binding's type
// expression.
context.AddNode(NodeKind::CompileTimeBindingPatternStart, state.token,
state.has_error);
}
context.PushState(state);
context.PushStateForExpr(PrecedenceGroup::ForType());
} else if (self_token && !template_token) {
// A `self` binding may omit its type; checking supplies the implicit `Self`
// type. There is no type node, so this produces a `SelfBindingPattern`
// rather than a `LetBindingPattern`.
if (ref_token) {
context.AddNode(NodeKind::RefBindingName, *ref_token, state.has_error);
}
context.AddNode(NodeKind::SelfBindingPattern, *self_token, state.has_error);
if (state.has_error) {
context.ReturnErrorOnState();
}
} else {
on_error(/*expected_name=*/false);
// Add a substitute for a type node.
context.AddInvalidParse(*context.position());
context.PushState(state, StateKind::BindingPatternFinishAsRegular);
return;
}
// TODO: `:?` introduces a form binding, from pending proposal #5389; proposal
// #7254 suggests replacing it with a `fwd` binding modifier. Until then form
// bindings are handled inline here, and are unaffected by phase keywords and
// contextual defaults.
bool is_form = token_kind == Lex::TokenKind::ColonQuestion;
bool redundant_modifier = false;
bool resolved_generic =
ResolveBindingPhase(context, state, is_form, template_token,
generic_token, runtime_token, redundant_modifier);
// `self` is always a runtime receiver binding; its phase never comes from the
// enclosing context's default. Forcing runtime here means that a misplaced
// `self` (in a deduced `[]` list or a compile-time entity's parameters, where
// the default would otherwise be generic) is reported by `check` as a
// misplaced `self` — the relevant error — rather than also producing a
// `ref`-on-generic error from that default.
if (self_token) {
resolved_generic = false;
}
// `template` and `ref` wrap the binding name, and each is only meaningful on
// a particular kind of binding: `template` on a generic binding, and `ref` on
// a runtime `:` binding. Using one elsewhere is diagnosed and marks the
// binding as errored; we skip its wrapper node rather than attach it to a
// binding that can't hold it, which would leave the parse tree malformed.
if (template_token) {
if (is_form || !resolved_generic) {
if (!state.has_error) {
CARBON_DIAGNOSTIC(ExpectedGenericBindingPatternAfterTemplate, Error,
"`template` is only allowed on a generic binding");
context.emitter().Emit(*template_token,
ExpectedGenericBindingPatternAfterTemplate);
}
state.has_error = true;
} else {
context.AddNode(NodeKind::TemplateBindingName, *template_token,
state.has_error);
}
}
if (ref_token) {
if (is_form || resolved_generic) {
if (!state.has_error) {
CARBON_DIAGNOSTIC(ExpectedRuntimeBindingPatternAfterRef, Error,
"`ref` is only allowed on a runtime binding");
context.emitter().Emit(*ref_token,
ExpectedRuntimeBindingPatternAfterRef);
}
state.has_error = true;
} else {
context.AddNode(NodeKind::RefBindingName, *ref_token, state.has_error);
}
}
// Preserve an explicit `runtime` keyword as a node wrapping the binding name,
// so its token is accounted for and `check` sees that the phase was written.
// It applies only to a runtime `:` binding; on a generic or form binding the
// keyword doesn't set the phase and any misuse is diagnosed separately, so no
// node is added there.
if (runtime_token && !is_form && !resolved_generic) {
context.AddNode(NodeKind::RuntimeBindingName, *runtime_token,
state.has_error);
}
// Now diagnose a misordered `unused` (recovered above), but only for an
// otherwise-valid modifier: a redundant modifier sets `redundant_modifier`,
// and an invalid `template`/`ref` sets `has_error`. Mark the binding in error
// once diagnosed, since recovery reordered the tokens the user wrote.
if (misordered_unused_token && !redundant_modifier && !state.has_error) {
CARBON_DIAGNOSTIC(UnusedAfterBindingModifier, Error,
"`unused` must be written before `{0}`", Lex::TokenKind);
context.emitter().Emit(*misordered_unused_token, UnusedAfterBindingModifier,
misordered_unused_modifier);
state.has_error = true;
}
if (is_form) {
state.kind = StateKind::BindingPatternFinishAsForm;
} else if (resolved_generic) {
state.kind = StateKind::BindingPatternFinishAsGeneric;
} else {
state.kind = StateKind::BindingPatternFinishAsRegular;
}
// Use the `:` or `:?` for the root node.
state.token = context.Consume();
if (!is_form && resolved_generic) {
// Add a virtual node before the compile time binding's type expression.
context.AddNode(NodeKind::CompileTimeBindingPatternStart, state.token,
state.has_error);
}
context.PushState(state);
context.PushStateForExpr(PrecedenceGroup::ForType());
}
// Handles BindingPatternFinishAs(Generic|Regular|Form).
@@ -147,7 +275,7 @@ static auto HandleBindingPatternFinish(Context& context, StateKind finish_kind)
node_kind = NodeKind::VarBindingPattern;
if (finish_kind != StateKind::BindingPatternFinishAsRegular) {
CARBON_DIAGNOSTIC(NonRegularBindingInVarDecl, Error,
"found {0:`:!`|`:?`} pattern inside `var` pattern",
"found {0:generic|`:?`} binding inside `var` pattern",
Diagnostics::BoolAsSelect);
context.emitter().Emit(
*context.position(), NonRegularBindingInVarDecl,