Files
carbon-lang/toolchain/check/testdata/facet/validate_impl_constraints.carbon
T
Dana Jansens d9841992cb Replace .Self in facet types (#7097)
This allows `T impls X` constraints to function, since they must contain
some reference to `.Self` in order to be valid. This should be
sufficient to support the interfaces we need for for loops over C++
range-for-compatible types.

We replace `.Self` in the following places:
- In a require decl, as we have a specific self facet to replace it with
from the declaration, either a user-specified facet or the symbolic
`Self`.
- When identifying a facet type, as we have a specific self that we are
identifying the facet type with. That self gets used for all `.Self`
references.
- Implicit `.Self` references on the RHS of an `impls` constraint when
building a facet type. The `.Self` references there no longer refer to
the top level self facet, so replace them with the facet that we now
know they refer to, which is found on the LHS of the `where` before the
`impls`.
- Rewrite constraints in impl lookup when validating them and comparing
them with constants from witnesses, which come from identifying a facet
type.
- Rewrite constraints in ImplWitnessAccess eval when comparing them with
constants from witnesses, which come from identifying a facet type.

Substitution is done through `SubstPeriodSelf`. It handles replacing
`.Self` and `.Self as type`, for a replacement facet that is either of
type FacetType or TypeType.

Eval currently diagnoses some ambiguous `.Self` references when doing
substitution of `.Self` but this is the incorrect place to do it, so
there are TODOs about moving this to name lookup. To support these
diagnostics there's some additional complexity in `SubstPeriodSelf` that
can go away once the TODOs are addressed, such as asking the caller if
they want to replace each `.Self`, in order for it to report a
diagnostic.

There are a number of follow-up work items here:
- Some TODO tests.
- Remove `SymbolicBindingType` since its intention was to support
`.Self` but we don't need it with this approach.
- Replace `.Self` in rewrite constraints of require decls.
- Replace `.Self` in rewrite constraints of impl as when constructing
the witness table.
- Reject explicit `.Self` in name lookup when it would be ambiguous.
- Officially disallow `.Self.A = B` in rewrite constraints in the design
docs, so that we don't have the case where `.A` is allowed but `.Self.A`
is not due to ambiguity.
2026-04-24 19:08:11 +00:00

398 lines
14 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
// --- self_impls_modifies_assoc_constant.carbon
library "[[@TEST_NAME]]";
interface I { let X:! type; }
fn F(unused T:! I where .X = ()) {}
fn G(T:! I where .Self impls (I where .X = ())) {
F(T);
}
// --- fail_self_impls_modifies_assoc_constant_type_differs.carbon
library "[[@TEST_NAME]]";
interface I { let X:! type; }
fn F(unused T:! I where .X = ()) {}
fn G(T:! I where .Self impls (I where .X = {})) {
// CHECK:STDERR: fail_self_impls_modifies_assoc_constant_type_differs.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.X) = {}` 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-6]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: fn F(unused T:! I where .X = ()) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
F(T);
}
// --- 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 = {} {}
// 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 U:! type where C(.Self) impls (M where .M0 = {})) {}
fn G(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.
//
// CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_type_non_final_impl.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `L` into type implementing `type where C(.Self) impls M and C(.Self).(M.M0) = {}` [ConversionFailureFacetToFacet]
// CHECK:STDERR: F(T);
// CHECK:STDERR: ^~~~
// CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_type_non_final_impl.carbon:[[@LINE-10]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
// CHECK:STDERR: fn F(unused U:! type where C(.Self) impls (M where .M0 = {})) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
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 = {} {}
// 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 U:! type where C(.Self) impls (M where .M0 = {})) {}
fn G(T:! L) {
F(T);
}
// --- 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 = () {}
// 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 U:! type where C(.Self) impls (M where .M0 = {})) {}
fn G(T:! L) {
// CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_type_type_differs.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `L` into type implementing `type where C(.Self) impls M and C(.Self).(M.M0) = {}` [ConversionFailureFacetToFacet]
// CHECK:STDERR: F(T);
// CHECK:STDERR: ^~~~
// CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_type_type_differs.carbon:[[@LINE-6]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
// CHECK:STDERR: fn F(unused U:! type where C(.Self) impls (M where .M0 = {})) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
F(T);
}
// --- 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 = {} {}
// 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 U:! type where C impls (M(.Self) where .M0 = {})) {}
fn G(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.
//
// CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_interface_non_final_impl.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `L` into type implementing `type where C impls M(.Self) and C.(M(.Self).M0) = {}` [ConversionFailureFacetToFacet]
// CHECK:STDERR: F(T);
// CHECK:STDERR: ^~~~
// CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_interface_non_final_impl.carbon:[[@LINE-10]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
// CHECK:STDERR: fn F(unused U:! type where C impls (M(.Self) where .M0 = {})) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
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 = {} {}
// 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 U:! type where C impls (M(.Self) where .M0 = {})) {}
fn G(T:! L) {
F(T);
}
// --- 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 = () {}
// 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 U:! type where C impls (M(.Self) where .M0 = {})) {}
fn G(T:! L) {
// CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_interface_type_differs.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `L` into type implementing `type where C impls M(.Self) and C.(M(.Self).M0) = {}` [ConversionFailureFacetToFacet]
// CHECK:STDERR: F(T);
// CHECK:STDERR: ^~~~
// CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_interface_type_differs.carbon:[[@LINE-6]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
// CHECK:STDERR: fn F(unused U:! type where C impls (M(.Self) where .M0 = {})) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
F(T);
}
// --- self_in_interface_generic_param_unconstrained.carbon
library "[[@TEST_NAME]]";
interface Z {}
interface I(T:! type) {}
fn F(unused T:! I(.Self) where .Self impls Z) {}
fn G(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]]:17: error: cannot convert type `.Self` that implements `type` into type implementing `Z` [ConversionFailureFacetToFacet]
// CHECK:STDERR: fn F(unused 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 T:! I(.Self) where .Self impls Z) {}
// CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE+7]]:14: error: cannot convert type `.Self` that implements `type` into type implementing `Z` [ConversionFailureFacetToFacet]
// CHECK:STDERR: fn G(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(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(_:! 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(_:! 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);
}
// --- fail_todo_where_period_self_rhs_impls_nested_period_self.carbon
library "[[@TEST_NAME]]";
interface Z {}
interface Y {}
interface X(T:! Z & Y) {}
constraint N {
require impls Y;
}
// TODO: The inner nested facet type (`N where...`) does not know about
// constraints on the outer facet type (`Z where...`). But it should produce an
// implied constraint that `.Self impls Z & Y` that is checked once the full
// facet type is known.
//
// CHECK:STDERR: fail_todo_where_period_self_rhs_impls_nested_period_self.carbon:[[@LINE+7]]:51: error: cannot convert type `.Self` that implements `N` into type implementing `Z & Y` [ConversionFailureFacetToFacet]
// CHECK:STDERR: fn F(_:! Z where .Self impls (N where .Self impls X(.Self))) {}
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_todo_where_period_self_rhs_impls_nested_period_self.carbon:[[@LINE-14]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
// CHECK:STDERR: interface X(T:! Z & Y) {}
// CHECK:STDERR: ^~~~~~~~~
// CHECK:STDERR:
fn F(_:! Z where .Self impls (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);
}
// --- 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(_:! 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(_:! 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]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
// CHECK:STDERR: fn F(_:! I where .I1 impls J) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
F(C);
}
// --- fail_todo_convert_to_period_self_preserves_self_facet_impls.carbon
library "[[@TEST_NAME]]";
interface K {}
interface J {}
interface I {
let I1:! type;
}
fn F(unused U:! I & J where .I1 impls K) {}
impl forall [T:! type] T as J {}
fn G(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`.
//
// TODO: When converting T to the type of U, the identified facet type
// includes `.Self.I1 impls K`, which is replaced with `T.I1 impls K`. The
// identified facet type of `T` also contains `T.I1 impls K` so we should find
// a witness for that.
//
// CHECK:STDERR: fail_todo_convert_to_period_self_preserves_self_facet_impls.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.I1) impls K` into type implementing `J & I where .(I.I1) impls K` [ConversionFailureFacetToFacet]
// CHECK:STDERR: F(T);
// CHECK:STDERR: ^~~~
// CHECK:STDERR: fail_todo_convert_to_period_self_preserves_self_facet_impls.carbon:[[@LINE-17]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
// CHECK:STDERR: fn F(unused U:! I & J where .I1 impls K) {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
F(T);
}