Files
carbon-lang/toolchain/check/testdata/facet/validate_impl_constraints.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

478 lines
13 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/validate_impl_constraints.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/validate_impl_constraints.carbon
// --- fail_todo_self_impls_modifies_assoc_constant.carbon
library "[[@TEST_NAME]]";
interface I { let X: type; }
fn F(unused generic T: I where .X = ()) {}
constraint N {
extend require impls I where .X = ();
}
fn G(generic T: I where .Self impls N) {
// CHECK:STDERR: fail_todo_self_impls_modifies_assoc_constant.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .Self impls N` into type implementing `I where .(I.X) = ()` [ConversionFailureFacetToFacet]
// CHECK:STDERR: F(T);
// CHECK:STDERR: ^~~~
// CHECK:STDERR: fail_todo_self_impls_modifies_assoc_constant.carbon:[[@LINE-10]]:21: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: fn F(unused generic T: I where .X = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
F(T);
}
// --- fail_self_impls_modifies_assoc_constant_type_differs.carbon
library "[[@TEST_NAME]]";
interface I { let X: type; }
fn F(unused generic T: I where .X = ()) {}
constraint N {
extend require impls I where .X = {};
}
fn G(generic T: I where .Self impls N) {
// CHECK:STDERR: fail_self_impls_modifies_assoc_constant_type_differs.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .Self impls N` into type implementing `I where .(I.X) = ()` [ConversionFailureFacetToFacet]
// CHECK:STDERR: F(T);
// CHECK:STDERR: ^~~~
// CHECK:STDERR: fail_self_impls_modifies_assoc_constant_type_differs.carbon:[[@LINE-10]]:21: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: fn F(unused generic T: I where .X = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
F(T);
}
// --- todo_fail_where_impls_tests_associated_constant_of_generic_type_non_final_impl.carbon
library "[[@TEST_NAME]]";
class C(U: type) {}
// C(U) impls M if U impls L.
interface L {}
interface M { let M0: type; }
impl forall [U: L] C(U) as M where .M0 = {} {}
constraint M0IsStruct {
extend require impls M where .M0 = {};
}
// U requires that C(.Self) impls M.
// - C(.Self) impls M can be rewritten as C(U) impls M.
// - C(U) impls M if U impls L => Requires U impls L.
fn F(unused generic U: type where C(.Self) impls M0IsStruct) {}
fn G(generic T: L) {
// We have no final impl for `C(T) as M`, so we don't know the value of
// `(C(T) as M).M0` concretely, so we don't know that it is `{}` and we can't
// convert assuming it is.
//
// TODO: This should fail. The rewrite constraint from `M0IsStruct` is not
// being checked.
F(T);
}
// --- where_impls_tests_associated_constant_of_generic_type.carbon
library "[[@TEST_NAME]]";
class C(U: type) {}
// C(U) impls M if U impls L.
interface L {}
interface M { let M0: type; }
final impl forall [U: L] C(U) as M where .M0 = {} {}
constraint M0IsStruct {
extend require impls M where .M0 = {};
}
// U requires that C(.Self) impls M.
// - C(.Self) impls M can be rewritten as C(U) impls M.
// - C(U) impls M if U impls L => Requires U impls L.
fn F(unused generic U: type where C(.Self) impls M0IsStruct) {}
fn G(generic T: L) {
F(T);
}
// --- todo_fail_where_impls_tests_associated_constant_of_generic_type_type_differs.carbon
library "[[@TEST_NAME]]";
class C(U: type) {}
// C(U) impls M if U impls L.
interface L {}
interface M { let M0: type; }
final impl forall [U: L] C(U) as M where .M0 = () {}
constraint M0IsStruct {
extend require impls M where .M0 = {};
}
// U requires that C(.Self) impls M.
// - C(.Self) impls M can be rewritten as C(U) impls M.
// - C(U) impls M if U impls L => Requires U impls L.
fn F(unused generic U: type where C(.Self) impls M0IsStruct) {}
fn G(generic T: L) {
// F requires .M0 = {}, but the final impl provides .M0 = ().
//
// TODO: This should fail.
F(T);
}
// --- todo_fail_where_impls_tests_associated_constant_of_generic_interface_non_final_impl.carbon
library "[[@TEST_NAME]]";
class C {}
// C impls M(U) if U impls L.
interface L {}
interface M(U: type) { let M0: type; }
impl forall [U: L] C as M(U) where .M0 = {} {}
constraint M0IsStruct(PeriodSelf: type) {
extend require impls M(PeriodSelf) where .M0 = {};
}
// U requires that C impls M(.Self).
// - C impls M(.Self) can be rewritten as C impls M(U).
// - C impls M(U) if U impls L => Requires U impls L.
fn F(unused generic U: type where C impls M0IsStruct(.Self)) {}
fn G(generic T: L) {
// We have no final impl for `C as M(T)`, so we don't know the value of
// `(C as M(T)).M0` concretely, so we don't know that it is `{}` and we can't
// convert assuming it is.
//
// TODO: This should fail.
F(T);
}
// --- where_impls_tests_associated_constant_of_generic_interface.carbon
library "[[@TEST_NAME]]";
class C {}
// C impls M(U) if U impls L.
interface L {}
interface M(U: type) { let M0: type; }
final impl forall [U: L] C as M(U) where .M0 = {} {}
constraint M0IsStruct(PeriodSelf: type) {
extend require impls M(PeriodSelf) where .M0 = {};
}
// U requires that C impls M(.Self).
// - C impls M(.Self) can be rewritten as C impls M(U).
// - C impls M(U) if U impls L => Requires U impls L.
fn F(unused generic U: type where C impls M0IsStruct(.Self)) {}
fn G(generic T: L) {
F(T);
}
// --- todo_fail_where_impls_tests_associated_constant_of_generic_interface_type_differs.carbon
library "[[@TEST_NAME]]";
class C {}
// C impls M(U) if U impls L.
interface L {}
interface M(U: type) { let M0: type; }
final impl forall [U: L] C as M(U) where .M0 = () {}
constraint M0IsStruct(PeriodSelf: type) {
extend require impls M(PeriodSelf) where .M0 = {};
}
// U requires that C impls M(.Self).
// - C impls M(.Self) can be rewritten as C impls M(U).
// - C impls M(U) if U impls L => Requires U impls L.
fn F(unused generic U: type where C impls M0IsStruct(.Self)) {}
fn G(generic T: L) {
// F requires that .M0 = {} but the final impl provides that .M0 = ().
//
// TODO: This should fail.
F(T);
}
// --- self_in_interface_generic_param_unconstrained.carbon
library "[[@TEST_NAME]]";
interface Z {}
interface I(T: type) {}
fn F(unused generic T: I(.Self) where .Self impls Z) {}
fn G(generic T: Z & I(.Self)) {
F(T);
}
// --- fail_todo_self_in_interface_generic_param_constrained.carbon
library "[[@TEST_NAME]]";
interface Z {}
interface I(T: Z) {}
// TODO: I(.Self) introduces an implied constraint `.Self impls Z`, which is
// satisfied and checked at the end of the fn signature.
//
// CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE+7]]:24: error: cannot convert type `.Self` that implements `type` into type implementing `Z` [ConversionFailureFacetToFacet]
// CHECK:STDERR: fn F(unused generic T: I(.Self) where .Self impls Z) {}
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE-8]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: interface I(T: Z) {}
// CHECK:STDERR: ^~~~
// CHECK:STDERR:
fn F(unused generic T: I(.Self) where .Self impls Z) {}
// CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE+7]]:21: error: cannot convert type `.Self` that implements `type` into type implementing `Z` [ConversionFailureFacetToFacet]
// CHECK:STDERR: fn G(generic T: Z & I(.Self)) {
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE-17]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: interface I(T: Z) {}
// CHECK:STDERR: ^~~~
// CHECK:STDERR:
fn G(generic T: Z & I(.Self)) {
F(T);
}
// --- where_period_self_rhs_sees_lhs.carbon
library "[[@TEST_NAME]]";
interface Z {}
interface Y {}
interface X(T: Z & Y) {}
constraint N {
require impls Y;
}
// The RHS of `where` can see the extend constraints on the LHS of `where`.
fn F(generic _: Z & N where .Self impls X(.Self)) {}
fn G() {
class C;
impl C as Z {}
impl C as Y {}
impl C as X(C) {}
F(C);
}
// --- where_type_rhs_sees_lhs.carbon
library "[[@TEST_NAME]]";
interface Z {}
interface Y {}
interface X(T: Z & Y) {}
constraint N {
require impls Y;
}
class R(T: Z & Y);
// The RHS of `where` can see the extend constraints on the LHS of `where`.
fn F(generic _: Z & N where R(.Self) impls X(.Self)) {}
fn G() {
class C;
impl C as Z {}
impl C as Y {}
impl R(C) as X(C) {}
F(C);
}
// --- associated_const_impls_interface_with_period_self.carbon
library "[[@TEST_NAME]]";
interface I {
let I1: type;
}
interface J(T: I) {}
// There is a .Self reference on the LHS and RHS of the `impls` constraint,
// which must be replaced, each with `C as I`
fn F(generic _: I where .I1 impls J(.Self)) {}
fn G() {
class C;
class D;
impl C as I where .I1 = D {}
impl D as J(C) {}
F(C);
}
// --- fail_concrete_witness_without_impl.carbon
library "[[@TEST_NAME]]";
interface I {
let I1: type;
}
interface J {}
fn F(generic _: I where .I1 impls J) {}
fn G() {
class C;
// This identifies `C as (I where .I1 impls J)`, which replaces `.Self.I1`
// with `C.(I.I1)`. This is a concrete lookup since C and I are both concrete,
// but it doesn't find anything as there is no impl.
//
// This tests that we correctly handle the case where the witness in an
// ImplWitnessAccess would become concrete without being able to provide any
// value, since it's still a `LookupImplWitness` type of witness.
// CHECK:STDERR: fail_concrete_witness_without_impl.carbon:[[@LINE+7]]:3: error: cannot convert type `C` into type implementing `I where .(I.I1) impls J` [ConversionFailureTypeToFacet]
// CHECK:STDERR: F(C);
// CHECK:STDERR: ^~~~
// CHECK:STDERR: fail_concrete_witness_without_impl.carbon:[[@LINE-16]]:14: note: initializing generic parameter `_` declared here [InitializingGenericParam]
// CHECK:STDERR: fn F(generic _: I where .I1 impls J) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
F(C);
}
// --- convert_to_period_self_preserves_self_facet_impls.carbon
library "[[@TEST_NAME]]";
interface K {}
interface J {}
interface I {
let I1: type;
}
fn F(unused generic U: I & J where .I1 impls K) {}
impl forall [T: type] T as J {}
fn G(generic T: I where .I1 impls K) {
// Replaces `.Self` with `T`, which converts `T` to `I & J`. Doing so has to
// invent a witness for `J`, and this tests we do so without losing the
// information that `.I1 impls K`.
F(T);
}
// --- find_witness_from_facet_in_lookup_target_facet_type.carbon
library "[[@TEST_NAME]]";
class D(T: type) {}
class E {}
interface X(T: type) {}
interface V(T: type) {}
constraint W(T: type) {
require Self impls X(T);
}
fn F(generic B: W(E), generic C: type where E impls V(.Self)) {
// Find a witness for `E as X(E)` from the facet `B`.
E as (V(C) where B impls X(.Self));
}
// --- find_witness_from_facet_in_specific_in_lookup_target_facet_type.carbon
library "[[@TEST_NAME]]";
class D(T: type) {}
class E {}
interface X(T: type) {}
interface V(T: type) {}
constraint W(T: type) {
require D(Self) impls X(T);
}
fn F(generic B: W(E), generic C: type where E impls V(.Self)) {
// Find a witness for `E as X(E)` from the facet `B`.
E as (V(C) where D(B) impls X(.Self));
}
// --- facet_has_witness_for_impl_witness_access.carbon
library "[[@TEST_NAME]]";
interface X {}
interface Y {}
interface Z {
// A type type, which has no witnesses.
let Z1: type;
// A facet type, but not one that provides a witness for `.Z2 as Y`.
let Z2: type where .Self impls X;
}
fn F(generic T: Z where .Z1 impls Y and .Z2 impls Y) {
// `T.Z1` is a `type` so this query self is ImplWitnessAccess(Z1).
T.Z1 as Y;
// `T.Z2` has a facet type so this query self is FacetAccessType(ImplWitnessAccess(Z2)).
T.Z2 as Y;
}
// --- impl_witness_access_has_access.carbon
library "[[@TEST_NAME]]";
class C(T: type) {}
interface Y {}
interface Z {
// Note that the facet type is not an exact type match for `Y` so that we
// cause an actual impl lookup to happen when we convert `.Z1` to `Y`.
let Z1: type where .Self impls Y and C(.Self) impls Y;
}
fn F(generic T: Z) {
T.Z1 as Y;
C(T.Z1) as Y;
}
// --- period_self_in_type_impls_named_constraint.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
}
interface Y {}
constraint GivesY {
require impls Y;
}
class C(T: type);
fn F(generic T: Z where C(.Self) impls GivesY) {
C(T) as Y;
}
// --- impl_witness_access_impls_named_constraint.carbon
library "[[@TEST_NAME]]";
interface Z {
let Z1: type;
}
interface Y {}
constraint GivesY {
require impls Y;
}
// This type places a `.Self` (in the .Z1) into the identified facet type as
// `.Self impls Y` through the named constraint. It ensures that the `.Self` is
// correctly replaced by `T` in order to compare equal with `T.Z1` in impl
// lookup.
fn F(generic T: Z where .Z1 impls GivesY) {
T.Z1 as Y;
}