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
+1 -1
View File
@@ -10,7 +10,7 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/function/generic/call.carbon
fn F[T:! type](_: T) {
fn F[T: type](_: T) {
}
class C {}
@@ -16,16 +16,16 @@ class C {
}
}
fn F[T:! type](_: T) {
fn F[T: type](_: T) {
}
fn H[T:! Core.Copy](x: T) -> T {
fn H[T: Core.Copy](x: T) -> T {
return x;
}
// Simple stress test for single depth of calls with change in types.
// Check definitions are emitted for each specific.
fn G[T:! Core.Copy & Core.Destroy](x: T) -> T {
fn G[T: Core.Copy & Core.Destroy](x: T) -> T {
H(x);
H(i32);
H(G(x));
@@ -13,15 +13,15 @@
// Builds on `call_basic.carbon`. Checks definitions are emitted with deeper
// call stack, i.e., when functions can be type checked without looking at the
// specific calling context.
fn F[T:! type](_: T) {
fn F[T: type](_: T) {
}
fn H[T:! Core.Copy](x: T) -> T {
fn H[T: Core.Copy](x: T) -> T {
F(x);
return x;
}
fn G[T:! Core.Copy & Core.Destroy](x: T) -> T {
fn G[T: Core.Copy & Core.Destroy](x: T) -> T {
H(x);
F(x);
return x;
@@ -10,7 +10,7 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/function/generic/call_dedup_ptr.carbon
fn F[T:! Core.Copy](x: T) -> T {
fn F[T: Core.Copy](x: T) -> T {
return x;
}
@@ -15,19 +15,19 @@
// conclude "equivalent" for all the call graph. This is to account for
// switching to a work-queue from recursion.
fn A[U:! Core.Copy](x: U) -> U {
fn A[U: Core.Copy](x: U) -> U {
return x;
}
fn D[U:! Core.Copy](x: U) -> U {
fn D[U: Core.Copy](x: U) -> U {
return x;
}
fn B[U:! Core.Copy & Core.Destroy](x: U*) {
fn B[U: Core.Copy & Core.Destroy](x: U*) {
D(*x);
}
fn F[T:! Core.Copy & Core.Destroy](x: T*) {
fn F[T: Core.Copy & Core.Destroy](x: T*) {
A(i32);
B(x);
}
@@ -14,12 +14,12 @@
// Check different functions are still emitted when trying to deduplicate emitted definitons.
interface I {
let T:! type;
let T: type;
// TODO: An interface with multiple associated entities is not yet
// implemented in mangling.
}
fn G(U:! I) {
fn G(generic U: I) {
var _: U.T*;
}
@@ -26,7 +26,7 @@ impl Y as I {
// Cannot coalesce the lowering for G specifics, as they call different functions.
// Check different functions are still emitted when trying to deduplicate emitted definitons.
fn G(T:! I) { T.F(); }
fn G(generic T: I) { T.F(); }
fn Run() {
G(X);
@@ -105,9 +105,9 @@ fn Run() {
// CHECK:STDOUT: !17 = !DILocation(line: 33, column: 3, scope: !12)
// CHECK:STDOUT: !18 = !DILocation(line: 31, column: 1, scope: !12)
// CHECK:STDOUT: !19 = distinct !DISubprogram(name: "G", linkageName: "_CG.Main.c81631f865470482", scope: null, file: !1, line: 29, type: !5, spFlags: DISPFlagDefinition, unit: !0)
// CHECK:STDOUT: !20 = !DILocation(line: 29, column: 15, scope: !19)
// CHECK:STDOUT: !20 = !DILocation(line: 29, column: 22, scope: !19)
// CHECK:STDOUT: !21 = !DILocation(line: 29, column: 1, scope: !19)
// CHECK:STDOUT: !22 = distinct !DISubprogram(name: "G", linkageName: "_CG.Main.0b241ec84a68877b", scope: null, file: !1, line: 29, type: !5, spFlags: DISPFlagDefinition, unit: !0)
// CHECK:STDOUT: !23 = !DILocation(line: 29, column: 15, scope: !22)
// CHECK:STDOUT: !23 = !DILocation(line: 29, column: 22, scope: !22)
// CHECK:STDOUT: !24 = !DILocation(line: 29, column: 1, scope: !22)
// CHECK:STDOUT:
@@ -13,7 +13,7 @@
import Core library "io";
interface I {
let T:! Core.Destroy;
let T: Core.Destroy;
fn F() -> T;
}
class X {}
@@ -33,7 +33,7 @@ impl Y as I where .T = i32 {
// Cannot coalesce the lowering for G specifics, as they call different functions.
// Check different functions are still emitted when trying to deduplicate emitted definitons.
fn G(U:! I) {
fn G(generic U: I) {
let _: U.T = U.F();
}
@@ -22,19 +22,19 @@
// specifics, where the pointer values are different, and the calls to the
// two D specifics are to functions with different function types.
fn A[U:! Core.Copy](x: U) -> U {
fn A[U: Core.Copy](x: U) -> U {
return x;
}
fn D[U:! Core.Copy](x: U) -> U {
fn D[U: Core.Copy](x: U) -> U {
return x;
}
fn B[U:! Core.Copy & Core.Destroy](x: U*) {
fn B[U: Core.Copy & Core.Destroy](x: U*) {
D(*x);
}
fn F[T:! Core.Copy & Core.Destroy](x: T*) {
fn F[T: Core.Copy & Core.Destroy](x: T*) {
A(i32);
B(x);
}
@@ -11,7 +11,7 @@
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/function/generic/call_impl_function.carbon
interface SomeInterface(T:! type) {
interface SomeInterface(T: type) {
fn F(x: T) -> T;
}
@@ -32,7 +32,7 @@ impl ImplsSomeInterface as SomeInterface(i32) {
// }
fn CallGenericMethod(T:! type, U:! SomeInterface(T), x: T) -> T {
fn CallGenericMethod(generic T: type, generic U: SomeInterface(T), x: T) -> T {
return U.F(x);
}
@@ -11,7 +11,7 @@
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/function/generic/call_method.carbon
class C {
fn F[T:! Core.Copy](unused self, x: T) -> T {
fn F[T: Core.Copy](unused self, x: T) -> T {
return x;
}
}
@@ -18,7 +18,7 @@ class C {
// The two specifics for recursive function F, when T is a pointer, could be
// deduplicated.
fn F[T:! Core.Copy](x: T, count: i32) -> T {
fn F[T: Core.Copy](x: T, count: i32) -> T {
if (count > 0) {
return x;
}
@@ -11,14 +11,14 @@
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/function/generic/call_recursive_diamond.carbon
import Core library "io";
fn A[T:! Core.Copy](x: T, count: i32) -> T;
fn B[T:! Core.Copy](x: T, count: i32) -> T;
fn C[T:! Core.Copy](x: T, count: i32) -> T;
fn D[T:! Core.Copy](x: T, count: i32) -> T;
fn A[T: Core.Copy](x: T, count: i32) -> T;
fn B[T: Core.Copy](x: T, count: i32) -> T;
fn C[T: Core.Copy](x: T, count: i32) -> T;
fn D[T: Core.Copy](x: T, count: i32) -> T;
// Builds on `call_recursive_mutual.carbon`. The two specifics for each of:
// A, B, C, D with a pointer type could be deduplicated.
fn A[T:! Core.Copy](x: T, count: i32) -> T {
fn A[T: Core.Copy](x: T, count: i32) -> T {
if (count > 4) {
return x;
}
@@ -30,17 +30,17 @@ fn A[T:! Core.Copy](x: T, count: i32) -> T {
}
// B and C are not equivalent in the diamond: different generics and different function bodies.
fn B[T:! Core.Copy](x: T, count: i32) -> T {
fn B[T: Core.Copy](x: T, count: i32) -> T {
Core.Print(1);
return D(x, count);
}
fn C[T:! Core.Copy](x: T, count: i32) -> T {
fn C[T: Core.Copy](x: T, count: i32) -> T {
Core.Print(2);
return D(x, count);
}
fn D[T:! Core.Copy](x: T, count: i32) -> T {
fn D[T: Core.Copy](x: T, count: i32) -> T {
return A(x, count + 1);
}
@@ -27,7 +27,7 @@ impl Y as I {
// Builds on "call_different_impls.carbon", cannot lower a single G due to
// different calls to F. Additionally, add recursion so the function
// fingerprint is dependent on itself.
fn G(T:! I, count: i32) -> i32 {
fn G(generic T: I, count: i32) -> i32 {
T.F();
if (count > 0) {
@@ -13,16 +13,16 @@
// Builds on `call_recursive_basic.carbon`.
// The two specifics for mutual recursive functions F and G,
// when T is a pointer, could be deduplicated.
fn G[T:! Core.Copy](x: T, count: i32) -> T;
fn G[T: Core.Copy](x: T, count: i32) -> T;
fn F[T:! Core.Copy](x: T, count: i32) -> T {
fn F[T: Core.Copy](x: T, count: i32) -> T {
if (count > 3) {
return x;
}
return G(x, count + 1);
}
fn G[T:! Core.Copy](x: T, count: i32) -> T {
fn G[T: Core.Copy](x: T, count: i32) -> T {
if (count > 4) {
return x;
}
@@ -12,7 +12,7 @@
// Builds on `call_recursive_basic.carbon`.
fn F[T:! type, U:! type](x: T, y: U, count: i32) -> i32 {
fn F[T: type, U: type](x: T, y: U, count: i32) -> i32 {
if (count > 2) {
return count;
}
@@ -12,7 +12,7 @@
// Builds on `call_recursive_reorder.carbon`.
fn F[T:! type, U:! type, V:! type](x: T, y: U, z: V, count: i32) -> i32 {
fn F[T: type, U: type, V: type](x: T, y: U, z: V, count: i32) -> i32 {
if (count > 2) {
return count;
}
@@ -11,12 +11,12 @@
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/function/generic/call_recursive_sccs_deep.carbon
import Core library "io";
fn A[T:! Core.Copy](x: T, count: i32) -> T;
fn B[T:! Core.Copy](x: T, count: i32);
fn C[T:! Core.Copy](x: T, count: i32);
fn D[T:! Core.Copy](x: T, count: i32) -> T;
fn E[T:! Core.Copy](x: T, count: i32) -> T;
fn F[T:! Core.Copy](x: T, count: i32) -> T;
fn A[T: Core.Copy](x: T, count: i32) -> T;
fn B[T: Core.Copy](x: T, count: i32);
fn C[T: Core.Copy](x: T, count: i32);
fn D[T: Core.Copy](x: T, count: i32) -> T;
fn E[T: Core.Copy](x: T, count: i32) -> T;
fn F[T: Core.Copy](x: T, count: i32) -> T;
// Builds on `call_recursive_mutual.carbon` and `call_recursive_diamond.carbon`
// B-C form a mutually recursive SCC (strongly connected component), D-E-F-G
@@ -28,23 +28,23 @@ fn F[T:! Core.Copy](x: T, count: i32) -> T;
// optimization here, if the function fingerprint may infer deduplication
// when not including the two different generic_ids.
fn A[T:! Core.Copy](x: T, count: i32) -> T {
fn A[T: Core.Copy](x: T, count: i32) -> T {
B(x, count);
return D(x, count);
}
fn B[T:! Core.Copy](x: T, count: i32) {
fn B[T: Core.Copy](x: T, count: i32) {
C(x, count);
}
fn C[T:! Core.Copy](x: T, count: i32) {
fn C[T: Core.Copy](x: T, count: i32) {
if (count <= 2) {
Core.Print(count);
B(x, count + 1);
}
}
fn D[T:! Core.Copy](x: T, count: i32) -> T {
fn D[T: Core.Copy](x: T, count: i32) -> T {
if (count > 4) {
return x;
}
@@ -55,17 +55,17 @@ fn D[T:! Core.Copy](x: T, count: i32) -> T {
}
}
fn G[T:! Core.Copy](x: T, count: i32) -> T;
fn G[T: Core.Copy](x: T, count: i32) -> T;
fn E[T:! Core.Copy](x: T, count: i32) -> T {
fn E[T: Core.Copy](x: T, count: i32) -> T {
return G(x, count);
}
fn F[T:! Core.Copy](x: T, count: i32) -> T {
fn F[T: Core.Copy](x: T, count: i32) -> T {
return G(x, count);
}
fn G[T:! Core.Copy](x: T, count: i32) -> T {
fn G[T: Core.Copy](x: T, count: i32) -> T {
return D(x, count + 1);
}
@@ -15,17 +15,17 @@
// generic function for an object, from a generic context.
// The specifics with a pointer type could be deduplicated here.
class C {
fn Cfn[T:! Core.Copy](unused self, x: T) -> T {
fn Cfn[T: Core.Copy](unused self, x: T) -> T {
return x;
}
}
fn G[T:! Core.Copy](x: T) -> T {
fn G[T: Core.Copy](x: T) -> T {
var c: C;
return c.Cfn(x);
}
fn F[T:! Core.Copy](x: T) -> T {
fn F[T: Core.Copy](x: T) -> T {
return G(x);
}
@@ -14,11 +14,11 @@
library "[[@TEST_NAME]]";
private fn F[T:! Core.Copy](a: T, _: T) -> T {
private fn F[T: Core.Copy](a: T, _: T) -> T {
return a;
}
fn Lib1CallF[T:! Core.Copy](a: T, b: T) -> T {
fn Lib1CallF[T: Core.Copy](a: T, b: T) -> T {
return F(a, b);
}
@@ -29,11 +29,11 @@ library "[[@TEST_NAME]]";
// Duplicate function name in different files. Shouldn't be a name conflict
// because of `private`. However, we currently use the same mangling for both
// functions.
private fn F[T:! Core.Copy](_: T, b: T) -> T {
private fn F[T: Core.Copy](_: T, b: T) -> T {
return b;
}
fn Lib2CallF[T:! Core.Copy](a: T, b: T) -> T {
fn Lib2CallF[T: Core.Copy](a: T, b: T) -> T {
return F(a, b);
}
+2 -2
View File
@@ -22,7 +22,7 @@ fn IndirectDeclared();
fn IndirectDefined() {}
fn IndirectGeneric(T:! type, x: T*) -> T* {
fn IndirectGeneric(generic T: type, x: T*) -> T* {
IndirectDeclared();
IndirectDefined();
return x;
@@ -38,7 +38,7 @@ fn DirectDeclared();
fn DirectDefined() {}
fn DirectGeneric(T:! type, y: T*) -> T* {
fn DirectGeneric(generic T: type, y: T*) -> T* {
DirectDeclared();
DirectDefined();
return IndirectGeneric(T, y);
@@ -14,7 +14,7 @@
library "[[@TEST_NAME]]";
fn F(x:! i32) {
fn F(generic x: i32) {
// This generates a witness for the destructor of `y` which is reused by the
// instantiation for the call in `main.carbon`.
var unused y: i32 = x;
@@ -27,7 +27,7 @@ impl Foo as Core.Copy {
}
}
fn GenericF[T:! type](unused x: T) -> Foo {
fn GenericF[T: type](unused x: T) -> Foo {
var f: Foo = (0 as i32) as Foo;
return f;
}
@@ -14,7 +14,7 @@
// This previously crashed due to the local function not having a parent name
// scope.
fn F(T:! Core.Copy, y: T) -> T {
fn F(generic T: Core.Copy, y: T) -> T {
fn G(x: T) -> T { return x; }
return G(y);
}
@@ -12,32 +12,32 @@
class C1 {}
fn first[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V);
fn second[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V);
fn third[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V);
fn fourth[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V);
fn first[T: type, U: type, V: type](arg1: T, arg2: U, arg3: V);
fn second[T: type, U: type, V: type](arg1: T, arg2: U, arg3: V);
fn third[T: type, U: type, V: type](arg1: T, arg2: U, arg3: V);
fn fourth[T: type, U: type, V: type](arg1: T, arg2: U, arg3: V);
fn first[T:! type, U:! type, V:! type](unused arg1: T, arg2: U, unused arg3: V) {
fn first[T: type, U: type, V: type](unused arg1: T, arg2: U, unused arg3: V) {
var local_name: C1*;
second(arg2, arg2, local_name);
}
fn second[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V) {
fn second[T: type, U: type, V: type](arg1: T, arg2: U, arg3: V) {
var local_name: C1*;
first(arg3, arg3, arg1);
third(arg2, arg2, local_name);
}
fn third[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V) {
fn third[T: type, U: type, V: type](arg1: T, arg2: U, arg3: V) {
var local_name: C1**;
fourth(local_name, local_name, arg1);
fourth(arg3, arg3, arg2);
}
fn fourth[T:! type, U:! type, V:! type](unused arg1: T, unused arg2: U, unused arg3: V) {
fn fourth[T: type, U: type, V: type](unused arg1: T, unused arg2: U, unused arg3: V) {
}
fn Main() {
@@ -24,15 +24,15 @@ class class_name4 {}
class class_name5 {}
class class_name6 {}
fn third_function[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V);
fn third_function[T: type, U: type, V: type](arg1: T, arg2: U, arg3: V);
fn fourth_function[T:! type](arg: T);
fn fourth_function[T: type](arg: T);
fn first_function[T:! type](arg: T);
fn first_function[T: type](arg: T);
fn second_function[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V);
fn second_function[T: type, U: type, V: type](arg1: T, arg2: U, arg3: V);
fn third_function[T:! type, U:! type, V:! type](arg1: T, arg2: U, unused arg3: V) {
fn third_function[T: type, U: type, V: type](arg1: T, arg2: U, unused arg3: V) {
var local1: class_name2*;
var local2: class_name2*;
@@ -40,13 +40,13 @@ fn third_function[T:! type, U:! type, V:! type](arg1: T, arg2: U, unused arg3: V
fourth_function(local1);
}
fn unused_1[T:! type, U:! type, V:! type](arg1: T, unused arg2: U, arg3: V) {
fn unused_1[T: type, U: type, V: type](arg1: T, unused arg2: U, arg3: V) {
var local1: class_name6*;
third_function(arg1, arg3, local1);
}
fn fourth_function[T:! type](unused arg: T) {
fn fourth_function[T: type](unused arg: T) {
var local1: class_name5**;
var local2: class_name6*;
@@ -54,20 +54,20 @@ fn fourth_function[T:! type](unused arg: T) {
first_function(local2);
}
fn first_function[T:! type](arg: T) {
fn first_function[T: type](arg: T) {
var local1: class_name4*;
var local2: class_name3*;
second_function(local2, local1, arg);
}
fn unused_2[T:! type, U:! type](arg1: T, arg2: U) {
fn unused_2[T: type, U: type](arg1: T, arg2: U) {
var local1: class_name6*;
second_function(arg1, arg2, local1);
}
fn second_function[T:! type, U:! type, V:! type](arg1: T, unused arg2: U, arg3: V) {
fn second_function[T: type, U: type, V: type](arg1: T, unused arg2: U, arg3: V) {
var local1: class_name3*;
var local2: class_name4*;
var local3: class_name2*;
@@ -10,7 +10,7 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/function/generic/type_param.carbon
fn F(T:! type) {
fn F(generic T: type) {
var p: T*;
let _: T = *p;
}
@@ -14,7 +14,7 @@ interface Copy {
fn Op(self) -> Self;
}
fn F[T:! Copy & Core.Destroy](a: T) -> T {
fn F[T: Copy & Core.Destroy](a: T) -> T {
var _: T = a.(Copy.Op)();
return a.(Copy.Op)();
}