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
+17 -17
View File
@@ -39,7 +39,7 @@ var array_index: array(i32, 1) = (0,) as array(i32, ((5, 7, 1, 9) as array(i32,
library "[[@TEST_NAME]]";
// Check that we propagate the `symbolic` tag through evaluations.
fn F(T:! Core.Destroy) {
fn F(generic T: Core.Destroy) {
//@dump-sem-ir-begin
var unused u: (T*, const T);
var unused v: {.a: T};
@@ -47,7 +47,7 @@ fn F(T:! Core.Destroy) {
//@dump-sem-ir-end
}
fn G(N:! i32) {
fn G(generic N: i32) {
//@dump-sem-ir-begin
var unused k: array(i32, N);
//@dump-sem-ir-end
@@ -609,11 +609,11 @@ fn G(N:! i32) {
// CHECK:STDOUT: %ImplicitAs.impl_witness_table.b8b = impl_witness_table (%Core.import_ref.32e), @Int.as.ImplicitAs.impl [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic fn @F(%T.loc4_7.2: %Destroy.type) {
// CHECK:STDOUT: generic fn @F(%T.loc4_15.2: %Destroy.type) {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %T.as_type.loc6_19.2: type = facet_access_type %T.loc4_7.1 [symbolic = %T.as_type.loc6_19.2 (constants.%T.as_type.700)]
// CHECK:STDOUT: %T.as_type.loc6_19.2: type = facet_access_type %T.loc4_15.1 [symbolic = %T.as_type.loc6_19.2 (constants.%T.as_type.700)]
// CHECK:STDOUT: %ptr.loc6_19.2: type = ptr_type %T.as_type.loc6_19.2 [symbolic = %ptr.loc6_19.2 (constants.%ptr.b37)]
// CHECK:STDOUT: %const.loc6_22.2: type = const_type %T.as_type.loc6_19.2 [symbolic = %const.loc6_22.2 (constants.%const)]
// CHECK:STDOUT: %tuple: %tuple.type.24b = tuple_value (%ptr.loc6_19.2, %const.loc6_22.2) [symbolic = %tuple (constants.%tuple)]
@@ -685,11 +685,11 @@ fn G(N:! i32) {
// CHECK:STDOUT: %DefaultOrUnformed.WithSelf.Op.call.loc6: init @F.%tuple.type (%tuple.type.cd5) to %.loc6_3.1 = call %specific_impl_fn.loc6_30.1()
// CHECK:STDOUT: assign %u.var, %DefaultOrUnformed.WithSelf.Op.call.loc6
// CHECK:STDOUT: %.loc6_29.1: type = splice_block %.loc6_29.3 [symbolic = %tuple.type (constants.%tuple.type.cd5)] {
// CHECK:STDOUT: %T.ref.loc6_18: %Destroy.type = name_ref T, %T.loc4_7.2 [symbolic = %T.loc4_7.1 (constants.%T.0e7)]
// CHECK:STDOUT: %T.ref.loc6_18: %Destroy.type = name_ref T, %T.loc4_15.2 [symbolic = %T.loc4_15.1 (constants.%T.0e7)]
// CHECK:STDOUT: %T.as_type.loc6_19.1: type = facet_access_type %T.ref.loc6_18 [symbolic = %T.as_type.loc6_19.2 (constants.%T.as_type.700)]
// CHECK:STDOUT: %.loc6_19: type = converted %T.ref.loc6_18, %T.as_type.loc6_19.1 [symbolic = %T.as_type.loc6_19.2 (constants.%T.as_type.700)]
// CHECK:STDOUT: %ptr.loc6_19.1: type = ptr_type %.loc6_19 [symbolic = %ptr.loc6_19.2 (constants.%ptr.b37)]
// CHECK:STDOUT: %T.ref.loc6_28: %Destroy.type = name_ref T, %T.loc4_7.2 [symbolic = %T.loc4_7.1 (constants.%T.0e7)]
// CHECK:STDOUT: %T.ref.loc6_28: %Destroy.type = name_ref T, %T.loc4_15.2 [symbolic = %T.loc4_15.1 (constants.%T.0e7)]
// CHECK:STDOUT: %T.as_type.loc6_22: type = facet_access_type %T.ref.loc6_28 [symbolic = %T.as_type.loc6_19.2 (constants.%T.as_type.700)]
// CHECK:STDOUT: %.loc6_22: type = converted %T.ref.loc6_28, %T.as_type.loc6_22 [symbolic = %T.as_type.loc6_19.2 (constants.%T.as_type.700)]
// CHECK:STDOUT: %const.loc6_22.1: type = const_type %.loc6_22 [symbolic = %const.loc6_22.2 (constants.%const)]
@@ -711,7 +711,7 @@ fn G(N:! i32) {
// CHECK:STDOUT: %DefaultOrUnformed.WithSelf.Op.call.loc7: init @F.%struct_type.a.loc7_23.2 (%struct_type.a) = call %specific_impl_fn.loc7_24.1()
// CHECK:STDOUT: assign %v.var, %DefaultOrUnformed.WithSelf.Op.call.loc7
// CHECK:STDOUT: %.loc7_23: type = splice_block %struct_type.a.loc7_23.1 [symbolic = %struct_type.a.loc7_23.2 (constants.%struct_type.a)] {
// CHECK:STDOUT: %T.ref.loc7: %Destroy.type = name_ref T, %T.loc4_7.2 [symbolic = %T.loc4_7.1 (constants.%T.0e7)]
// CHECK:STDOUT: %T.ref.loc7: %Destroy.type = name_ref T, %T.loc4_15.2 [symbolic = %T.loc4_15.1 (constants.%T.0e7)]
// CHECK:STDOUT: %T.as_type.loc7: type = facet_access_type %T.ref.loc7 [symbolic = %T.as_type.loc6_19.2 (constants.%T.as_type.700)]
// CHECK:STDOUT: %.loc7_22: type = converted %T.ref.loc7, %T.as_type.loc7 [symbolic = %T.as_type.loc6_19.2 (constants.%T.as_type.700)]
// CHECK:STDOUT: %struct_type.a.loc7_23.1: type = struct_type {.a: @F.%T.as_type.loc6_19.2 (%T.as_type.700)} [symbolic = %struct_type.a.loc7_23.2 (constants.%struct_type.a)]
@@ -732,7 +732,7 @@ fn G(N:! i32) {
// CHECK:STDOUT: %DefaultOrUnformed.WithSelf.Op.call.loc8: init @F.%array_type.loc8_27.2 (%array_type.50f) to %.loc8_3.1 = call %specific_impl_fn.loc8_28.1()
// CHECK:STDOUT: assign %w.var, %DefaultOrUnformed.WithSelf.Op.call.loc8
// CHECK:STDOUT: %.loc8_27: type = splice_block %array_type.loc8_27.1 [symbolic = %array_type.loc8_27.2 (constants.%array_type.50f)] {
// CHECK:STDOUT: %T.ref.loc8: %Destroy.type = name_ref T, %T.loc4_7.2 [symbolic = %T.loc4_7.1 (constants.%T.0e7)]
// CHECK:STDOUT: %T.ref.loc8: %Destroy.type = name_ref T, %T.loc4_15.2 [symbolic = %T.loc4_15.1 (constants.%T.0e7)]
// CHECK:STDOUT: %int_5: Core.IntLiteral = int_value 5 [concrete = constants.%int_5]
// CHECK:STDOUT: %T.as_type.loc8: type = facet_access_type %T.ref.loc8 [symbolic = %T.as_type.loc6_19.2 (constants.%T.as_type.700)]
// CHECK:STDOUT: %.loc8_23: type = converted %T.ref.loc8, %T.as_type.loc8 [symbolic = %T.as_type.loc6_19.2 (constants.%T.as_type.700)]
@@ -776,13 +776,13 @@ fn G(N:! i32) {
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic fn @G(%N.loc12_7.2: %i32) {
// CHECK:STDOUT: generic fn @G(%N.loc12_15.2: %i32) {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %Int.as.ImplicitAs.impl.Convert.bound: <bound method> = bound_method %N.loc12_7.1, constants.%Int.as.ImplicitAs.impl.Convert.69e [symbolic = %Int.as.ImplicitAs.impl.Convert.bound (constants.%Int.as.ImplicitAs.impl.Convert.bound)]
// CHECK:STDOUT: %bound_method.loc14_28.3: <bound method> = bound_method %N.loc12_7.1, constants.%Int.as.ImplicitAs.impl.Convert.specific_fn [symbolic = %bound_method.loc14_28.3 (constants.%bound_method)]
// CHECK:STDOUT: %Int.as.ImplicitAs.impl.Convert.call.loc14_28.2: init Core.IntLiteral = call %bound_method.loc14_28.3(%N.loc12_7.1) [symbolic = %Int.as.ImplicitAs.impl.Convert.call.loc14_28.2 (constants.%Int.as.ImplicitAs.impl.Convert.call)]
// CHECK:STDOUT: %Int.as.ImplicitAs.impl.Convert.bound: <bound method> = bound_method %N.loc12_15.1, constants.%Int.as.ImplicitAs.impl.Convert.69e [symbolic = %Int.as.ImplicitAs.impl.Convert.bound (constants.%Int.as.ImplicitAs.impl.Convert.bound)]
// CHECK:STDOUT: %bound_method.loc14_28.3: <bound method> = bound_method %N.loc12_15.1, constants.%Int.as.ImplicitAs.impl.Convert.specific_fn [symbolic = %bound_method.loc14_28.3 (constants.%bound_method)]
// CHECK:STDOUT: %Int.as.ImplicitAs.impl.Convert.call.loc14_28.2: init Core.IntLiteral = call %bound_method.loc14_28.3(%N.loc12_15.1) [symbolic = %Int.as.ImplicitAs.impl.Convert.call.loc14_28.2 (constants.%Int.as.ImplicitAs.impl.Convert.call)]
// CHECK:STDOUT: %array_type.loc14_29.2: type = array_type %Int.as.ImplicitAs.impl.Convert.call.loc14_28.2, constants.%i32 [symbolic = %array_type.loc14_29.2 (constants.%array_type.359)]
// CHECK:STDOUT: %require_complete: <witness> = require_complete_type %array_type.loc14_29.2 [symbolic = %require_complete (constants.%require_complete.f47)]
// CHECK:STDOUT: %pattern_type: type = pattern_type %array_type.loc14_29.2 [symbolic = %pattern_type (constants.%pattern_type.486)]
@@ -816,7 +816,7 @@ fn G(N:! i32) {
// CHECK:STDOUT: assign %k.var, %DefaultOrUnformed.WithSelf.Op.call
// CHECK:STDOUT: %.loc14_29: type = splice_block %array_type.loc14_29.1 [symbolic = %array_type.loc14_29.2 (constants.%array_type.359)] {
// CHECK:STDOUT: %i32.loc14: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: %N.ref: %i32 = name_ref N, %N.loc12_7.2 [symbolic = %N.loc12_7.1 (constants.%N.37f)]
// CHECK:STDOUT: %N.ref: %i32 = name_ref N, %N.loc12_15.2 [symbolic = %N.loc12_15.1 (constants.%N.37f)]
// CHECK:STDOUT: %impl.elem0.loc14_28: %.b08 = impl_witness_access constants.%ImplicitAs.impl_witness.416, element0 [concrete = constants.%Int.as.ImplicitAs.impl.Convert.69e]
// CHECK:STDOUT: %bound_method.loc14_28.1: <bound method> = bound_method %N.ref, %impl.elem0.loc14_28 [symbolic = %Int.as.ImplicitAs.impl.Convert.bound (constants.%Int.as.ImplicitAs.impl.Convert.bound)]
// CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0.loc14_28, @Int.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Int.as.ImplicitAs.impl.Convert.specific_fn]
@@ -847,12 +847,12 @@ fn G(N:! i32) {
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @F(constants.%T.0e7) {
// CHECK:STDOUT: %T.patt.loc4_7.2 => constants.%T.patt.351
// CHECK:STDOUT: %T.loc4_7.1 => constants.%T.0e7
// CHECK:STDOUT: %T.patt.loc4_15.2 => constants.%T.patt.351
// CHECK:STDOUT: %T.loc4_15.1 => constants.%T.0e7
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @G(constants.%N.37f) {
// CHECK:STDOUT: %N.patt.loc12_7.2 => constants.%N.patt.38a
// CHECK:STDOUT: %N.loc12_7.1 => constants.%N.37f
// CHECK:STDOUT: %N.patt.loc12_15.2 => constants.%N.patt.38a
// CHECK:STDOUT: %N.loc12_15.1 => constants.%N.37f
// CHECK:STDOUT: }
// CHECK:STDOUT:
+3 -3
View File
@@ -71,7 +71,7 @@ library "[[@TEST_NAME]]";
eval fn F(x: i32) -> i32 { return x; }
fn G(N:! i32) {
fn G(generic N: i32) {
// CHECK:STDERR: fail_todo_dependent_call.carbon:[[@LINE+4]]:17: error: cannot evaluate type expression [TypeExprEvaluationFailure]
// CHECK:STDERR: var unused a: array(i32, F(N)) = (0, 1, 2);
// CHECK:STDERR: ^~~~~~~~~~~~~~~~
@@ -90,7 +90,7 @@ eval fn F(_: i32) -> type {
return C;
}
fn UseFGenerically(X:! i32) {
fn UseFGenerically(generic X: i32) {
// CHECK:STDERR: fail_todo_dependent_call_type.carbon:[[@LINE+12]]:3: error: member name of type `<dependent type>` in compound member access is not an instance member or an interface member [CompoundMemberAccessDoesNotUseBase]
// CHECK:STDERR: var unused v: F(X) = {};
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
@@ -164,7 +164,7 @@ class C {}
eval fn F() -> C { return {}; }
//@dump-sem-ir-end
fn G(_:! C) {}
fn G(generic _: C) {}
fn H() {
//@dump-sem-ir-begin
+12 -12
View File
@@ -13,12 +13,12 @@
// --- symbolic_call_to_eval_fn.carbon
library "[[@TEST_NAME]]";
eval fn F(B:! bool) -> type {
eval fn F(generic B: bool) -> type {
if (B) { return (); }
return ({}, {});
}
fn G(B:! bool) {
fn G(generic B: bool) {
let unused n: F(B) = ({}, {});
}
@@ -30,12 +30,12 @@ fn H() {
// --- fail_symbolic_call_to_eval_fn_invalid_instantiation.carbon
library "[[@TEST_NAME]]";
eval fn F(B:! bool) -> type {
eval fn F(generic B: bool) -> type {
if (B) { return (); }
return ({}, {});
}
fn G(B:! bool) {
fn G(generic B: bool) {
let unused n: F(B) = ({}, {});
}
@@ -55,7 +55,7 @@ library "[[@TEST_NAME]]";
interface Z {}
impl {} as Z {}
eval fn E(U:! Z) -> type {
eval fn E(generic U: Z) -> type {
// This makes a FacetAccessType(U) instruction, for which we have to get a
// constant value from the definition in the called function's specific. This
// requires that the called function has its specific definition resolved.
@@ -95,16 +95,16 @@ fn F() {
// CHECK:STDOUT: %Copy.impl_witness_table.4d6 = impl_witness_table (%Core.import_ref.9c3), @type.as.Copy.impl [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic fn @E(%U.loc6_12.2: %Z.type) {
// CHECK:STDOUT: generic fn @E(%U.loc6_20.2: %Z.type) {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %U.as_type.loc11_11.2: type = facet_access_type %U.loc6_12.1 [symbolic = %U.as_type.loc11_11.2 (constants.%U.as_type.ff1)]
// CHECK:STDOUT: %U.as_type.loc11_11.2: type = facet_access_type %U.loc6_20.1 [symbolic = %U.as_type.loc11_11.2 (constants.%U.as_type.ff1)]
// CHECK:STDOUT: %type.as.Copy.impl.Op.bound: <bound method> = bound_method %U.as_type.loc11_11.2, constants.%type.as.Copy.impl.Op [symbolic = %type.as.Copy.impl.Op.bound (constants.%type.as.Copy.impl.Op.bound.05d)]
// CHECK:STDOUT:
// CHECK:STDOUT: fn() -> out %return.param: type {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %U.ref: %Z.type = name_ref U, %U.loc6_12.2 [symbolic = %U.loc6_12.1 (constants.%U.013)]
// CHECK:STDOUT: %U.ref: %Z.type = name_ref U, %U.loc6_20.2 [symbolic = %U.loc6_20.1 (constants.%U.013)]
// CHECK:STDOUT: %U.as_type.loc11_11.1: type = facet_access_type %U.ref [symbolic = %U.as_type.loc11_11.2 (constants.%U.as_type.ff1)]
// CHECK:STDOUT: %.loc11: type = converted %U.ref, %U.as_type.loc11_11.1 [symbolic = %U.as_type.loc11_11.2 (constants.%U.as_type.ff1)]
// CHECK:STDOUT: %impl.elem0: %.524 = impl_witness_access constants.%Copy.impl_witness.638, element0 [concrete = constants.%type.as.Copy.impl.Op]
@@ -117,13 +117,13 @@ fn F() {
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @E(constants.%U.013) {
// CHECK:STDOUT: %U.patt.loc6_12.2 => constants.%U.patt.4a7
// CHECK:STDOUT: %U.loc6_12.1 => constants.%U.013
// CHECK:STDOUT: %U.patt.loc6_20.2 => constants.%U.patt.4a7
// CHECK:STDOUT: %U.loc6_20.1 => constants.%U.013
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @E(constants.%Z.facet) {
// CHECK:STDOUT: %U.patt.loc6_12.2 => constants.%U.patt.4a7
// CHECK:STDOUT: %U.loc6_12.1 => constants.%Z.facet
// CHECK:STDOUT: %U.patt.loc6_20.2 => constants.%U.patt.4a7
// CHECK:STDOUT: %U.loc6_20.1 => constants.%Z.facet
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %U.as_type.loc11_11.2 => constants.%empty_struct_type
+4 -4
View File
@@ -16,7 +16,7 @@ library "[[@TEST_NAME]]";
var x: type;
interface Z {
let T:! type;
let T: type;
}
class D {
@@ -33,7 +33,7 @@ library "[[@TEST_NAME]]";
var x: type;
interface Z {
let T:! type;
let T: type;
}
class D {
@@ -54,7 +54,7 @@ library "[[@TEST_NAME]]";
var x: type;
interface Z {
let T:! type;
let T: type;
}
class D {
@@ -70,7 +70,7 @@ library "[[@TEST_NAME]]";
var x: type;
interface Z(T:! type) {}
interface Z(T: type) {}
class D {
// CHECK:STDERR: fail_unexpected_runtime_impls_lhs.carbon:[[@LINE+4]]:29: error: cannot evaluate type expression [TypeExprEvaluationFailure]