Files
carbon-lang/toolchain/check/testdata/impl/lookup/access.carbon
T
Dana JansensandRichard Smith 6326bbdbe1 Resolve cycles in .Self replacement in nested designators (#7183)
A nested designator like `.(X.X1).(Y.Y1)` results in nested
ImplWitnessAccess instructions, which can produce cycles in the
toolchain easily when replacing `.Self`.

First, when constructing a facet type like `V:! Z where .Z1 impls (Y
where .Y1 = U)` we substitute replace `.Self` in the nested facet type,
and in this case we replace `.Self` with `.Z1` which contains a `.Self`
of its own. This was coming from us being lazy about replacing `.Self`
in an `impl as` declaration, such as `impl C as Z where .Z1 = .Self`.
The self type is known there, so we can more eagerly replace `.Self` as
we do in a `require impls` declaration. Then the replacement for `.Self`
never comes with a `.Self` that needs to also be replaced. Any resulting
`.Self` would always be the top-level one.

Second, when evaluating ImplWitnessAccess, we were replacing .Self in
the LHS of rewrite constraints, but the `.Self` may itself have a type
that contains rewrite constraints. If one of those rewrite constraints
has nested ImplWitnessAccess instructions, we evaluate the new
ImplWitnessAccess, which again finds rewrite constraints to replace
`.Self` in, and we repeat forever. For this one we just stop replacing
.Self in the LHS of rewrite constraints. Since they are always against
.Self, we can always look in the access facet's type for a value.

While fixing ImplWitness access, also correct the lookup to search
through the types of nested ImplWitnessAccess instructions to find a
rewrite value, since it may find it at any level up to the eventual
`.Self`.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2026-05-13 16:11:20 +00:00

56 lines
2.0 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/none.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/lookup/access.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/access.carbon
// --- impl_witness_access_in_impl_type_structure.carbon
library "[[@TEST_NAME]]";
interface X(T:! type) {}
interface Y {
let Y1:! type;
}
interface Z {
let Z1:! type;
}
// There's 2 ImplWitnessAccess instructions in the type, but they together
// resolve to a symbolic type value, so the type structure is: `? as X(?)`
impl forall [T:! Z where .Z1 impls Y] T as X(T.Z1.(Y.Y1)) {}
class C {
impl as Y where .Y1 = {} {}
}
fn F(V:! Z where .Z1 = C) {
// The type stucture is `? as X({})` which will match the impl's less specific
// `? as X(?)`, then the impl will deduce the parameter of `X` to be `{}` from
// the type of `V`. This would fail if ImplWitnessAccess instructions were
// treated as Concrete in the type structure, since the impl would have a
// different concrete value (an ImplWitnessAccess) than the query (a
// StructValue).
V as X({});
}
fn G(V:! Z where .Z1 impls Y) {
// The type structure is `? as X(?)`, also built from ImplWitnessAccess insts,
// which will match the impl's `? as X(?)`.
V as X(V.(Z.Z1).(Y.Y1));
}
fn H(U:! type, V:! Z where .Z1 impls (Y where .Y1 = U)) {
// The type structure is `? as X(?)`, without using an ImplWitnessAccess,
// which will match the impl's `? as X(?)`. This would fail if
// ImplWitnessAccess instructions were treated as Concrete in the type
// structure, since the impl's type structure would be more specific than the
// query's.
V as X(U);
}