Files
carbon-lang/toolchain/check/testdata/facet/early_rewrites.carbon
T
Chandler Carruth 8be274cf60 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
2026-07-11 01:22:44 +00:00

499 lines
18 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/convert.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/facet/early_rewrites.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/early_rewrites.carbon
// Tests where `ImplWitnessAccess` on the RHS of a rewrite constraint is used in
// other constraints within the same facet type in ways that require the RHS to
// be evaluated to a concrete value before the facet type is resolved (such as
// by passing it as a parameter to a generic class). This can be done either by
// eagerly reading rewrite constraints from the same facet type, or by relying
// on implied constraints which are checked later.
// --- fail_todo_implied_constraint_on_self.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
class C(T: Z where .Z1 = ()) {}
interface J {
// Implied: .Z1 == ()
// CHECK:STDERR: fail_todo_implied_constraint_on_self.carbon:[[@LINE+7]]:25: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
// CHECK:STDERR: let J1: Z where .Z2 = C(.Self);
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_implied_constraint_on_self.carbon:[[@LINE-7]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(T: Z where .Z1 = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let J1: Z where .Z2 = C(.Self);
}
fn F(unused generic T: J) {}
class D;
// Satisfies the implied constraint in J.J1.
// CHECK:STDERR: fail_todo_implied_constraint_on_self.carbon:[[@LINE+7]]:38: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
// CHECK:STDERR: impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_implied_constraint_on_self.carbon:[[@LINE-21]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(T: Z where .Z1 = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
fn G(generic T: J where .J1 = D) {
F(T);
}
// --- fail_todo_implied_constraint_on_associated_constant.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
interface Tuple {}
impl () as Tuple {}
class C(T: Tuple) {}
interface J {
// Implied: .Z1 impls Tuple
// CHECK:STDERR: fail_todo_implied_constraint_on_associated_constant.carbon:[[@LINE+7]]:25: error: cannot convert type `.(Z.Z1)` into type implementing `Tuple` [ConversionFailureTypeToFacet]
// CHECK:STDERR: let J1: Z where .Z2 = C(.Z1);
// CHECK:STDERR: ^~~~~~
// CHECK:STDERR: fail_todo_implied_constraint_on_associated_constant.carbon:[[@LINE-7]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(T: Tuple) {}
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR:
let J1: Z where .Z2 = C(.Z1);
}
fn F(unused generic T: J) {}
class D;
// Satisfies the implied constraint in J.J1.
impl D as Z where .Z1 = () and .Z2 = C(()) {}
fn G(generic T: J where .J1 = D) {
F(T);
}
// --- fail_todo_nested_constraint_visible_in_type_parameter.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
class C(T: Z where .Z1 = ()) {}
interface J {
// Implied: .Z1 == (), but this is also explicitly required for J1.
// CHECK:STDERR: fail_todo_nested_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:42: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
// CHECK:STDERR: let J1: (Z where .Z1 = ()) where .Z2 = C(.Self);
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_nested_constraint_visible_in_type_parameter.carbon:[[@LINE-7]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(T: Z where .Z1 = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let J1: (Z where .Z1 = ()) where .Z2 = C(.Self);
}
fn F(unused generic T: J) {}
class D;
// Satisfies the implied constraint in J.J1.
// CHECK:STDERR: fail_todo_nested_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:38: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
// CHECK:STDERR: impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_nested_constraint_visible_in_type_parameter.carbon:[[@LINE-21]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(T: Z where .Z1 = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
fn G(generic T: J where .J1 = D) {
F(T);
}
// --- fail_todo_outer_nested_constraint_visible_in_type_parameter.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
class C(T: Z where .Z1 = ()) {}
interface J {
// Implied: .Z1 == (), but this is also explicitly required for J1.
// CHECK:STDERR: fail_todo_outer_nested_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:26: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
// CHECK:STDERR: let J1: (Z where .Z2 = C(.Self)) where .Z1 = ();
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_outer_nested_constraint_visible_in_type_parameter.carbon:[[@LINE-7]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(T: Z where .Z1 = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let J1: (Z where .Z2 = C(.Self)) where .Z1 = ();
}
fn F(unused generic T: J) {}
class D;
// Satisfies the implied constraint in J.J1.
// CHECK:STDERR: fail_todo_outer_nested_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:38: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
// CHECK:STDERR: impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_outer_nested_constraint_visible_in_type_parameter.carbon:[[@LINE-21]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(T: Z where .Z1 = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
fn G(generic T: J where .J1 = D) {
F(T);
}
// --- fail_todo_earlier_constraint_visible_in_type_parameter.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
class C(T: Z where .Z1 = ()) {}
interface J {
// Implied: .Z1 == (), but this is also explicitly required for J1.
// CHECK:STDERR: fail_todo_earlier_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:38: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
// CHECK:STDERR: let J1: Z where .Z1 = () and .Z2 = C(.Self);
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_earlier_constraint_visible_in_type_parameter.carbon:[[@LINE-7]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(T: Z where .Z1 = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let J1: Z where .Z1 = () and .Z2 = C(.Self);
}
fn F(unused generic T: J) {}
class D;
// Satisfies the implied constraint in J.J1.
// CHECK:STDERR: fail_todo_earlier_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:38: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
// CHECK:STDERR: impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_earlier_constraint_visible_in_type_parameter.carbon:[[@LINE-21]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(T: Z where .Z1 = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
fn G(generic T: J where .J1 = D) {
F(T);
}
// --- fail_todo_later_constraint_visible_in_type_parameter.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
class C(T: Z where .Z1 = ()) {}
interface J {
// Implied: .Z1 == (), but this is also explicitly required for J1.
// CHECK:STDERR: fail_todo_later_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:25: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
// CHECK:STDERR: let J1: Z where .Z2 = C(.Self) and .Z1 = ();
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_later_constraint_visible_in_type_parameter.carbon:[[@LINE-7]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(T: Z where .Z1 = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let J1: Z where .Z2 = C(.Self) and .Z1 = ();
}
fn F(unused generic T: J) {}
class D;
// Satisfies the implied constraint in J.J1.
// CHECK:STDERR: fail_todo_later_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:38: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
// CHECK:STDERR: impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_later_constraint_visible_in_type_parameter.carbon:[[@LINE-21]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(T: Z where .Z1 = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
fn G(generic T: J where .J1 = D) {
F(T);
}
// --- early_rewrite_applied.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
interface Tuple {}
impl () as Tuple {}
class C(T: Tuple) {}
interface J {
// Implied: .Z1 == (), but this is also explicitly required for J1.
let J1: Z where .Z1 = () and .Z2 = C(.Z1);
}
fn F(unused generic T: J) {}
class D;
// Satisfies the implied constraint in J.J1.
impl D as Z where .Z1 = () and .Z2 = C(()) {}
fn G(generic T: J where .J1 = D) {
F(T);
}
// --- early_rewrite_applied_from_nested_self.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
interface Tuple {}
impl () as Tuple {}
class C(T: Tuple) {}
interface J {
// Implied: .Z1 == (), but this is also explicitly required for J1.
let J1: (Z where .Z1 = ()) where .Z2 = C(.Z1);
}
fn F(unused generic T: J) {}
class D;
// Satisfies the implied constraint in J.J1.
impl D as Z where .Z1 = () and .Z2 = C(()) {}
fn G(generic T: J where .J1 = D) {
F(T);
}
// --- early_rewrite_applied_extra_indirection.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
interface Y {
let Y1: Z;
}
interface Tuple {}
impl () as Tuple {}
class C(T: Tuple) {}
interface J {
// .Z2 depends on an two nested accesses of rewrite constraints.
let J1: Y & Z where .Y1 = .Self and .Z1 = () and .Z2 = C(.Y1.Z1);
}
fn F(unused generic T: J) {}
class D;
impl D as Z where .Z1 = () and .Z2 = C(()) {}
impl D as Y where .Y1 = D {}
fn G(generic T: J where .J1 = D) {
F(T);
}
// --- early_rewrite_of_compound_access.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
interface Y {}
class NeedY(T: Y) {}
impl () as Y {}
// These are written differently but should behave the same.
// Designator access of `.Z1`.
fn F(generic _: Z where .Z1 = () and .Z2 = NeedY(.Z1)) {}
// Compound member access of `.Z1`.
fn G(generic _: Z where .Z1 = () and .Z2 = NeedY(.Self.(Z.Z1))) {}
// --- fail_todo_resolved_constraint_visible_in_type_parameter.carbon
library "[[@TEST_NAME]]";
interface Y {
let Y1: type;
}
interface Z {
let Z1: Y;
let Z2: type;
let Z3: type;
}
interface Tuple {}
impl () as Tuple {}
class C(T: Tuple) {}
interface J {
// Implied: .Y1 impls Tuple, but this is also explicitly required for J1.
let J1: Z & Y where .Y1 = () and .Z1 = .Self and .Z2 = .Z1.Y1 and .Z3 = C(.Z2);
}
fn F(unused generic T: J) {}
class D;
// Satisfies the implied constraint in J.J1.
impl D as Y where .Y1 = () {}
// CHECK:STDERR: fail_todo_resolved_constraint_visible_in_type_parameter.carbon:[[@LINE+4]]:25: error: cannot convert type `.Self` that implements `Z` into type implementing `Y` [ConversionFailureFacetToFacet]
// CHECK:STDERR: impl D as Z where .Z1 = .Self and .Z2 = () and .Z3 = C(()) {}
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
impl D as Z where .Z1 = .Self and .Z2 = () and .Z3 = C(()) {}
// CHECK:STDERR: fail_todo_resolved_constraint_visible_in_type_parameter.carbon:[[@LINE+4]]:31: error: cannot convert type `D` into type implementing `Y & Z where .(Y.Y1) = () and .(Z.Z1) = .Self as Y and .(Z.Z2) = () and .(Z.Z3) = C(() as Tuple)` [ConversionFailureTypeToFacet]
// CHECK:STDERR: fn G(generic T: J where .J1 = D) {
// CHECK:STDERR: ^
// CHECK:STDERR:
fn G(generic T: J where .J1 = D) {
F(T);
}
// --- fail_todo_early_rewrite_from_previous_impls_constraint.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
interface Y {
let Y1: type;
}
interface Tuple {}
impl () as Tuple {}
class C(U: Tuple);
constraint NY(V: type) {
extend require impls Y where .Y1 = V;
}
// CHECK:STDERR: fail_todo_early_rewrite_from_previous_impls_constraint.carbon:[[@LINE+7]]:59: error: cannot convert type `.(Z.Z1).(Y.Y1)` into type implementing `Tuple` [ConversionFailureTypeToFacet]
// CHECK:STDERR: fn F(unused generic T: Z where .Z1 impls NY(()) and .Z2 = C(.Z1.(Y.Y1))) {}
// CHECK:STDERR: ^~~~~~~~~~~~~
// CHECK:STDERR: fail_todo_early_rewrite_from_previous_impls_constraint.carbon:[[@LINE-9]]:9: note: initializing generic parameter `U` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(U: Tuple);
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR:
fn F(unused generic T: Z where .Z1 impls NY(()) and .Z2 = C(.Z1.(Y.Y1))) {}
// --- fail_early_rewrite_correct_interface_wrong_self_access_rewrite_in_period_self.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
interface Y {
let Y1: type;
}
interface Tuple {}
impl () as Tuple {}
class C(U: Tuple);
constraint NY(V: type) {
extend require impls Y where .Y1 = V;
}
// This should fail: `T.(Y.Y1)` is rewritten to be `()` but `T.(Z.Z1).(Y.Y1)`
// is used in the argument to `C`.
//
// CHECK:STDERR: fail_early_rewrite_correct_interface_wrong_self_access_rewrite_in_period_self.carbon:[[@LINE+7]]:77: error: cannot convert type `.(Z.Z1).(Y.Y1)` into type implementing `Tuple` [ConversionFailureTypeToFacet]
// CHECK:STDERR: fn F(unused generic T: Z where .Self impls NY(()) and .Z1 impls Y and .Z2 = C(.Z1.(Y.Y1))) {}
// CHECK:STDERR: ^~~~~~~~~~~~~
// CHECK:STDERR: fail_early_rewrite_correct_interface_wrong_self_access_rewrite_in_period_self.carbon:[[@LINE-12]]:9: note: initializing generic parameter `U` declared here [InitializingGenericParam]
// CHECK:STDERR: class C(U: Tuple);
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR:
fn F(unused generic T: Z where .Self impls NY(()) and .Z1 impls Y and .Z2 = C(.Z1.(Y.Y1))) {}
// --- fail_early_rewrite_correct_interface_wrong_self_access_rewrite_in_associated_constant.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
let Z2: type;
}
interface Y {
let Y1: type;
}
interface Tuple {}
impl () as Tuple {}
class C(U: Tuple);
constraint NY(V: type) {
extend require impls Y where .Y1 = V;
}
// This should fail: `T.(Z.Z1).(Y.Y1)` is rewritten to be `()` but `T.(Y.Y1)` is used
// in the argument to `C`.
//
// CHECK:STDERR: fail_early_rewrite_correct_interface_wrong_self_access_rewrite_in_associated_constant.carbon:[[@LINE+4]]:79: error: name `Self` not found [NameNotFound]
// CHECK:STDERR: fn G(unused generic T: Z where .Z1 impls NY(()) and .Self impls Y and .Z2 = C(Self.(Y.Y1))) {}
// CHECK:STDERR: ^~~~
// CHECK:STDERR:
fn G(unused generic T: Z where .Z1 impls NY(()) and .Self impls Y and .Z2 = C(Self.(Y.Y1))) {}
// --- fail_nested_facet_type_in_rewrite_does_not_use_rewrite_from_outside.carbon
library "[[@TEST_NAME]]";
interface Z {
let X: type;
let Y: type;
}
constraint NZ(PeriodY: type) {
extend require impls Z where .X = PeriodY;
}
fn F(generic T: Z where .Y = {} and .X = NZ(.Y), generic U: T.X) -> U.(Z.X) {
// This should fail: `U: T.X` contains a rewrite `.X = .Y` in its type, but
// the value of `U.(Z.Y)` is not known. Only `T.(Z.Y)` is rewritten to `{}`.
//
// CHECK:STDERR: fail_nested_facet_type_in_rewrite_does_not_use_rewrite_from_outside.carbon:[[@LINE+7]]:3: error: cannot implicitly convert expression of type `{}` to `U.(Z.X)` [ConversionFailure]
// CHECK:STDERR: return {};
// CHECK:STDERR: ^~~~~~~~~~
// CHECK:STDERR: fail_nested_facet_type_in_rewrite_does_not_use_rewrite_from_outside.carbon:[[@LINE+4]]:3: note: type `{}` does not implement interface `Core.ImplicitAs(U.(Z.X))` [MissingImplInMemberAccessInContext]
// CHECK:STDERR: return {};
// CHECK:STDERR: ^~~~~~~~~~
// CHECK:STDERR:
return {};
}