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
@@ -58,14 +58,14 @@ interface Y {
}
// T does not need to impl Z yet.
impl forall [T:! type] T as Y;
impl forall [T: type] T as Y;
// T needs to impl Z at the start of the definition.
// CHECK:STDERR: fail_missing_extend_require_for_symbolic_type.carbon:[[@LINE+4]]:1: error: interface `Y` being implemented requires that `T` implements `Z` [RequireImplsNotImplemented]
// CHECK:STDERR: impl forall [T:! type] T as Y {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: impl forall [T: type] T as Y {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
impl forall [T:! type] T as Y {}
impl forall [T: type] T as Y {}
// --- fail_missing_require_for_symbolic_type.carbon
library "[[@TEST_NAME]]";
@@ -77,25 +77,25 @@ interface Y {
}
// T does not need to impl Z yet.
impl forall [T:! type] T as Y;
impl forall [T: type] T as Y;
// T needs to impl Z at the start of the definition.
// CHECK:STDERR: fail_missing_require_for_symbolic_type.carbon:[[@LINE+4]]:1: error: interface `Y` being implemented requires that `T` implements `Z` [RequireImplsNotImplemented]
// CHECK:STDERR: impl forall [T:! type] T as Y {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: impl forall [T: type] T as Y {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
impl forall [T:! type] T as Y {}
impl forall [T: type] T as Y {}
// --- fail_missing_extend_require_double.carbon
library "[[@TEST_NAME]]";
interface Z1(T:! type) {}
interface Z2(T:! type) {}
interface Z1(T: type) {}
interface Z2(T: type) {}
class A;
class B;
interface Y(U:! type) {
interface Y(U: type) {
extend require impls Z1(U) & Z2(B);
}
@@ -108,13 +108,13 @@ impl () as Y(A) {}
// --- fail_missing_require_double.carbon
library "[[@TEST_NAME]]";
interface Z1(T:! type) {}
interface Z2(T:! type) {}
interface Z1(T: type) {}
interface Z2(T: type) {}
class A;
class B;
interface Y(U:! type) {
interface Y(U: type) {
require impls Z1(U) & Z2(B);
}
@@ -160,17 +160,17 @@ interface Y {
}
// T does not need to impl Z1/Z2 yet.
impl forall [T:! type] T as Y;
impl forall [T: type] T as Y;
// Now we know T impls Z1 and Z2.
impl forall [T:! type] T as Z1;
impl forall [T:! type] T as Z2;
impl forall [T: type] T as Z1;
impl forall [T: type] T as Z2;
// So no error here.
impl forall [T:! type] T as Y {}
impl forall [T: type] T as Y {}
impl forall [T:! type] T as Z1 {}
impl forall [T:! type] T as Z2 {}
impl forall [T: type] T as Z1 {}
impl forall [T: type] T as Z2 {}
// --- require_for_symbolic_type_impl_matches_same_interface.carbon
library "[[@TEST_NAME]]";
@@ -184,18 +184,18 @@ interface Y {
}
// This only matches T that impl Z1 and Z2 so no error here.
impl forall [T:! Z1 & Z2] T as Y {}
impl forall [T: Z1 & Z2] T as Y {}
// --- require_for_generic_interfaces.carbon
library "[[@TEST_NAME]]";
interface Z1(T:! type) {}
interface Z2(T:! type) {}
interface Z1(T: type) {}
interface Z2(T: type) {}
class A;
class B;
interface Y(U:! type) {
interface Y(U: type) {
extend require impls Z1(U);
require impls Z2(B);
}
@@ -216,11 +216,11 @@ impl () as Z2(B) {}
// --- fail_require_non_self_missing.carbon
library "[[@TEST_NAME]]";
interface Z(T:! type) {}
interface Z(T: type) {}
class C;
interface Y(U:! type) {
interface Y(U: type) {
// `Self` must appear somewhere in the require decl, so it's in the interface
// specific because we want to test a different self-type.
require C impls Z(Self);
@@ -236,11 +236,11 @@ impl () as Y(()) {}
// --- require_non_self.carbon
library "[[@TEST_NAME]]";
interface Z(T:! type) {}
interface Z(T: type) {}
class C;
interface Y(U:! type) {
interface Y(U: type) {
// `Self` must appear somewhere in the require decl, so it's in the interface
// specific because we want to test a different self-type.
require C impls Z(Self);
@@ -264,15 +264,15 @@ interface C {
require impls B;
}
impl forall [T:! B] T as A { fn AA() {} }
impl forall [T: B] T as A { fn AA() {} }
// When we get to the definition we check that anything B requires is satisfied.
// - The interface B requires A, so we must check T impls A.
// - The impl for A requires T impls B.
// - This impl is what provides T impls B.
impl forall [T:! C] T as B {}
impl forall [T: C] T as B {}
fn F(T:! C) {
fn F(generic T: C) {
// If things go wrong, we find the impl `T as B`, but inside its definition is
// a lookup for `T as A`, which (through deducing `T`) includes a lookup for
// `T as B`. This creates a cycle of evaluating `T as B` recursively.
@@ -286,25 +286,25 @@ fn F(T:! C) {
// --- fail_impl_requires_itself_cycle_with_monomorphization_error.carbon
library "[[@TEST_NAME]]";
class W(T:! type) {
class W(T: type) {
adapt {};
}
interface A(N:! Core.IntLiteral) {
interface A(N: Core.IntLiteral) {
fn AA() -> W(array((), N));
}
interface B(N:! Core.IntLiteral) {
interface B(N: Core.IntLiteral) {
require impls A(N);
}
interface C(N:! Core.IntLiteral) {
interface C(N: Core.IntLiteral) {
require impls B(N);
}
impl forall [N:! Core.IntLiteral, T:! B(N)] T as A(N) {
impl forall [N: Core.IntLiteral, T: B(N)] T as A(N) {
fn AA() -> W(array((), N)) { return {} as W(array((), N)); }
}
impl forall [N:! Core.IntLiteral, T:! C(N)] T as B(N) {
impl forall [N: Core.IntLiteral, T: C(N)] T as B(N) {
// The definition here does not contain the lookups verifying that C(N) impls
// `A(N)`, so they do not get re-evaluated for a specific `N`. That doesn't
// prevent us from producing a reasonable diagnostic when that `N` causes an
@@ -314,7 +314,7 @@ impl forall [N:! Core.IntLiteral, T:! C(N)] T as B(N) {
// `T as B(N)` implies `T as A(N)`.
}
fn F(T:! C(-1)) {
fn F(generic T: C(-1)) {
// CHECK:STDERR: fail_impl_requires_itself_cycle_with_monomorphization_error.carbon:[[@LINE+7]]:3: error: unable to monomorphize specific `AA()` [ResolvingSpecificHere]
// CHECK:STDERR: T.(A(-1).AA)();
// CHECK:STDERR: ^~~~~~~~~~~~~~