Files
carbon-lang/toolchain/check/testdata/facet/self_in_interface_param.carbon
T
Dana Jansens 4261bb2dd2 Track and don't replace active .Self (#7443)
In #7436 we stopped substituting `.Self` when collecting witnesses out
of a facet type. While this was correct, it did not capture all the
cases that need to avoid substituting `.Self`. And it poisoned the
`IdentifiedFacetType` cache by not replacing `.Self` but storing the
result in the cache. This led to incoherent behaviour, where the result
of an impl lookup would change depending on which ones had been done
previously.

Now we use a flag to track for each `.Self` if we're currently
type-checking inside the scope where it was introduced in a facet type.
While inside that scope, identify should not replace the `.Self`. Any
use of it should remain as-is since we don't yet know what value will
replace it. We call this state "frozen" since it should not be modified
by identify. This requires a substitution step when we leave the scope
that introduced the `.Self`, to remove the flag. The flag is set in the
`EntityName` of the `SymbolicBinding`, and is part of the canonical
value, since `.Self` can become part of types, which are constants, and
the flag needs to follow it for correct behaviour.

We also have to ensure the flag is the same when doing comparison with
constants from inside a facet type and constants from outside. For
instance in `(Z where .Z1 = ()) where .Z2 = .Z1`, when we arrive at the
second `.Z1` its `.Self` will be frozen, while the `.Z1 = ()` contains a
non-frozen `.Self`. So we add the frozen flag to the first when storing
it in `where_stack` in order to compare the constant values of the two
`.Z1`.

The `WhereExpr` requirement inst kinds now have an `InstConstantKind` of
`AlwaysUnique` instead of `Never`. This allows us to add them to the
usual InstBlocks, and in an `eval fn` body they have a constant value,
so eval does not fail when trying to call that function. We have to be
careful to not consider `AlwaysUnique` as being actually concrete
though, since their constant value erases `.Self`-dependence. This
allows us to stop special casing them when thawing the requirements
block in a `WhereExpr`, and we can just thaw each `InstId` in the block
in a straightforward manner.

We add the new flag to the instruction's fingerprint and name in
formatted semir.
2026-07-08 18:04:56 +00:00

163 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/none.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/facet/self_in_interface_param.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/self_in_interface_param.carbon
interface I(T:! type) {
let I1:! type;
}
//@dump-sem-ir-begin
fn F(T:! I(.Self) where .I1 = ()) -> T.I1 {
return ();
}
//@dump-sem-ir-end
fn G(_:! I(.Self) where .I1 = ()) {}
// CHECK:STDOUT: --- self_in_interface_param.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %.Self.frozen.197: %type = symbolic_binding .Self [symbolic_self]
// CHECK:STDOUT: %I.type.335: type = generic_interface_type @I [concrete]
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
// CHECK:STDOUT: %I.generic: %I.type.335 = struct_value () [concrete]
// CHECK:STDOUT: %.Self.frozen.as_type.bce: type = facet_access_type %.Self.frozen.197 [symbolic_self]
// CHECK:STDOUT: %I.type.0e8: type = facet_type <@I, @I(%.Self.frozen.as_type.bce)> [symbolic_self]
// CHECK:STDOUT: %.Self.frozen.edc: %I.type.0e8 = symbolic_binding .Self [symbolic_self]
// CHECK:STDOUT: %I.assoc_type.96e: type = assoc_entity_type @I, @I(%.Self.frozen.as_type.bce) [symbolic_self]
// CHECK:STDOUT: %assoc0.cab: %I.assoc_type.96e = assoc_entity element0, @I.WithSelf.%I1 [symbolic_self]
// CHECK:STDOUT: %.Self.frozen.as_type.7bb: type = facet_access_type %.Self.frozen.edc [symbolic_self]
// CHECK:STDOUT: %I.lookup_impl_witness.d6e: <witness> = lookup_impl_witness %.Self.frozen.edc, @I, @I(%.Self.frozen.as_type.bce) [symbolic_self]
// CHECK:STDOUT: %impl.elem0.14f: type = impl_witness_access %I.lookup_impl_witness.d6e, element0 [symbolic_self]
// CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [concrete]
// CHECK:STDOUT: %.Self.e0b: %type = symbolic_binding .Self [symbolic_self]
// CHECK:STDOUT: %.Self.as_type: type = facet_access_type %.Self.e0b [symbolic_self]
// CHECK:STDOUT: %I.type.a4e: type = facet_type <@I, @I(%.Self.as_type)> [symbolic_self]
// CHECK:STDOUT: %.Self.b30: %I.type.0e8 = symbolic_binding .Self [symbolic_self]
// CHECK:STDOUT: %I.lookup_impl_witness.eea: <witness> = lookup_impl_witness %.Self.b30, @I, @I(%.Self.as_type) [symbolic_self]
// CHECK:STDOUT: %impl.elem0.439: type = impl_witness_access %I.lookup_impl_witness.eea, element0 [symbolic_self]
// CHECK:STDOUT: %I_where.type.760: type = facet_type <@I, @I(%.Self.as_type) where %impl.elem0.439 = %empty_tuple.type> [symbolic_self]
// CHECK:STDOUT: %.Self.df8: %I.type.a4e = symbolic_binding .Self [symbolic_self]
// CHECK:STDOUT: %I.lookup_impl_witness.0d5: <witness> = lookup_impl_witness %.Self.df8, @I, @I(%.Self.as_type) [symbolic_self]
// CHECK:STDOUT: %impl.elem0.21c: type = impl_witness_access %I.lookup_impl_witness.0d5, element0 [symbolic_self]
// CHECK:STDOUT: %I_where.type.6d9: type = facet_type <@I, @I(%.Self.as_type) where %impl.elem0.21c = %empty_tuple.type> [symbolic_self]
// CHECK:STDOUT: %pattern_type.4af: type = pattern_type %I_where.type.6d9 [symbolic_self]
// CHECK:STDOUT: %T.patt.bfa: %pattern_type.4af = symbolic_binding_pattern T, 0 [symbolic]
// CHECK:STDOUT: %T.fb1: %I_where.type.6d9 = symbolic_binding T, 0 [symbolic]
// CHECK:STDOUT: %I.assoc_type.901: type = assoc_entity_type @I, @I(%.Self.as_type) [symbolic_self]
// CHECK:STDOUT: %assoc0.267: %I.assoc_type.901 = assoc_entity element0, @I.WithSelf.%I1 [symbolic_self]
// CHECK:STDOUT: %T.as_type: type = facet_access_type %T.fb1 [symbolic]
// CHECK:STDOUT: %facet_value: %type = facet_value %T.as_type, () [symbolic]
// CHECK:STDOUT: %I.type.1d2: type = facet_type <@I, @I(%T.as_type)> [symbolic]
// CHECK:STDOUT: %I_where.type.3f6: type = facet_type <@I, @I(%T.as_type) where %impl.elem0.21c = %empty_tuple.type> [symbolic]
// CHECK:STDOUT: %I.lookup_impl_witness.962: <witness> = lookup_impl_witness %T.fb1, @I, @I(%T.as_type) [symbolic]
// CHECK:STDOUT: %.262: Core.Form = init_form %empty_tuple.type [concrete]
// CHECK:STDOUT: %pattern_type.cb1: type = pattern_type %empty_tuple.type [concrete]
// CHECK:STDOUT: %return.param_patt: %pattern_type.cb1 = out_param_pattern [concrete]
// CHECK:STDOUT: %return.patt: %pattern_type.cb1 = return_slot_pattern %return.param_patt, %empty_tuple.type [concrete]
// CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
// CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
// CHECK:STDOUT: %T.patt.loc18_7.1: %pattern_type.4af = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc18_7.2 (constants.%T.patt.bfa)]
// CHECK:STDOUT: %return.param_patt: %pattern_type.cb1 = out_param_pattern [concrete = constants.%return.param_patt]
// CHECK:STDOUT: %return.patt: %pattern_type.cb1 = return_slot_pattern %return.param_patt, %impl.elem0.loc18_39 [concrete = constants.%return.patt]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %T.ref: %I_where.type.6d9 = name_ref T, %T.loc18_7.2 [symbolic = %T.loc18_7.1 (constants.%T.fb1)]
// CHECK:STDOUT: %T.as_type.loc18_39.2: type = facet_access_type %T.ref [symbolic = %T.as_type.loc18_39.1 (constants.%T.as_type)]
// CHECK:STDOUT: %.loc18_39.1: type = converted %T.ref, %T.as_type.loc18_39.2 [symbolic = %T.as_type.loc18_39.1 (constants.%T.as_type)]
// CHECK:STDOUT: %.loc18_39.2: %I.assoc_type.901 = specific_constant @I1.%assoc0, @I.WithSelf(constants.%.Self.as_type, constants.%T.fb1) [symbolic_self = constants.%assoc0.267]
// CHECK:STDOUT: %I1.ref.loc18_39: %I.assoc_type.901 = name_ref I1, %.loc18_39.2 [symbolic_self = constants.%assoc0.267]
// CHECK:STDOUT: %facet_value.loc18_39.2: %type = facet_value constants.%T.as_type, () [symbolic = %facet_value.loc18_39.1 (constants.%facet_value)]
// CHECK:STDOUT: %.loc18_39.3: %type = converted constants.%T.as_type, %facet_value.loc18_39.2 [symbolic = %facet_value.loc18_39.1 (constants.%facet_value)]
// CHECK:STDOUT: %I.type.loc18_39.2: type = facet_type <@I, @I(constants.%T.as_type)> [symbolic = %I.type.loc18_39.1 (constants.%I.type.1d2)]
// CHECK:STDOUT: %T.as_type.loc18_39.3: type = facet_access_type constants.%T.fb1 [symbolic = %T.as_type.loc18_39.1 (constants.%T.as_type)]
// CHECK:STDOUT: %facet_value.loc18_39.3: %type = facet_value %T.as_type.loc18_39.3, () [symbolic = %facet_value.loc18_39.1 (constants.%facet_value)]
// CHECK:STDOUT: %.loc18_39.4: %type = converted constants.%T.fb1, %facet_value.loc18_39.3 [symbolic = %facet_value.loc18_39.1 (constants.%facet_value)]
// CHECK:STDOUT: %I_where.type.loc18_39.2: type = facet_type <@I, @I(constants.%T.as_type) where constants.%impl.elem0.21c = constants.%empty_tuple.type> [symbolic = %I_where.type.loc18_39.1 (constants.%I_where.type.3f6)]
// CHECK:STDOUT: %T.as_type.loc18_39.4: type = facet_access_type constants.%T.fb1 [symbolic = %T.as_type.loc18_39.1 (constants.%T.as_type)]
// CHECK:STDOUT: %facet_value.loc18_39.4: %type = facet_value %T.as_type.loc18_39.4, () [symbolic = %facet_value.loc18_39.1 (constants.%facet_value)]
// CHECK:STDOUT: %.loc18_39.5: %type = converted constants.%T.fb1, %facet_value.loc18_39.4 [symbolic = %facet_value.loc18_39.1 (constants.%facet_value)]
// CHECK:STDOUT: %impl.elem0.loc18_39: type = impl_witness_access constants.%I.lookup_impl_witness.962, element0 [concrete = constants.%empty_tuple.type]
// CHECK:STDOUT: %.loc18_39.6: Core.Form = init_form %impl.elem0.loc18_39 [concrete = constants.%.262]
// CHECK:STDOUT: %.loc18_19.1: type = splice_block %.loc18_19.3 [symbolic_self = constants.%I_where.type.6d9] {
// CHECK:STDOUT: %.Self.frozen.loc18_7: %type = symbolic_binding .Self [symbolic_self = constants.%.Self.frozen.197]
// CHECK:STDOUT: %I.ref: %I.type.335 = name_ref I, file.%I.decl [concrete = constants.%I.generic]
// CHECK:STDOUT: %.Self.ref.loc18_12: %type = name_ref .Self, %.Self.frozen.loc18_7 [symbolic_self = constants.%.Self.frozen.197]
// CHECK:STDOUT: %.Self.as_type.loc18_17: type = facet_access_type %.Self.ref.loc18_12 [symbolic_self = constants.%.Self.frozen.as_type.bce]
// CHECK:STDOUT: %.loc18_17: type = converted %.Self.ref.loc18_12, %.Self.as_type.loc18_17 [symbolic_self = constants.%.Self.frozen.as_type.bce]
// CHECK:STDOUT: %I.type.loc18_17.1: type = facet_type <@I, @I(constants.%.Self.frozen.as_type.bce)> [symbolic_self = constants.%I.type.0e8]
// CHECK:STDOUT: %.Self.frozen.loc18_19: %I.type.0e8 = symbolic_binding .Self [symbolic_self = constants.%.Self.frozen.edc]
// CHECK:STDOUT: %base_facet_type.loc18_19.1 = requirement_base_facet_type %I.type.loc18_17.1 [concrete]
// CHECK:STDOUT: %.Self.ref.loc18_25: %I.type.0e8 = name_ref .Self, %.Self.frozen.loc18_19 [symbolic_self = constants.%.Self.frozen.edc]
// CHECK:STDOUT: %.Self.as_type.loc18_25: type = facet_access_type %.Self.ref.loc18_25 [symbolic_self = constants.%.Self.frozen.as_type.7bb]
// CHECK:STDOUT: %.loc18_25.1: type = converted %.Self.ref.loc18_25, %.Self.as_type.loc18_25 [symbolic_self = constants.%.Self.frozen.as_type.7bb]
// CHECK:STDOUT: %.loc18_25.2: %I.assoc_type.96e = specific_constant @I1.%assoc0, @I.WithSelf(constants.%.Self.frozen.as_type.bce, constants.%.Self.frozen.edc) [symbolic_self = constants.%assoc0.cab]
// CHECK:STDOUT: %I1.ref.loc18_25: %I.assoc_type.96e = name_ref I1, %.loc18_25.2 [symbolic_self = constants.%assoc0.cab]
// CHECK:STDOUT: %impl.elem0.loc18_25.1: type = impl_witness_access constants.%I.lookup_impl_witness.d6e, element0 [symbolic_self = constants.%impl.elem0.14f]
// CHECK:STDOUT: %.loc18_32.1: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
// CHECK:STDOUT: %.loc18_32.2: type = converted %.loc18_32.1, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type]
// CHECK:STDOUT: %rewrite.loc18_29.1 = requirement_rewrite %impl.elem0.loc18_25.1, %.loc18_32.2 [concrete]
// CHECK:STDOUT: %I.type.loc18_17.2: type = facet_type <@I, @I(constants.%.Self.as_type)> [symbolic_self = constants.%I.type.a4e]
// CHECK:STDOUT: %base_facet_type.loc18_19.2 = requirement_base_facet_type %I.type.loc18_17.2 [concrete]
// CHECK:STDOUT: %impl.elem0.loc18_25.2: type = impl_witness_access constants.%I.lookup_impl_witness.eea, element0 [symbolic_self = constants.%impl.elem0.439]
// CHECK:STDOUT: %rewrite.loc18_29.2 = requirement_rewrite %impl.elem0.loc18_25.2, %.loc18_32.2 [concrete]
// CHECK:STDOUT: %.loc18_19.2: type = where_expr [symbolic_self = constants.%I_where.type.760] {
// CHECK:STDOUT: %base_facet_type.loc18_19.2 = requirement_base_facet_type %I.type.loc18_17.2 [concrete]
// CHECK:STDOUT: %rewrite.loc18_29.2 = requirement_rewrite %impl.elem0.loc18_25.2, %.loc18_32.2 [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT: %impl.elem0.loc18_25.3: type = impl_witness_access constants.%I.lookup_impl_witness.0d5, element0 [symbolic_self = constants.%impl.elem0.21c]
// CHECK:STDOUT: %rewrite.loc18_29.3 = requirement_rewrite %impl.elem0.loc18_25.3, %.loc18_32.2 [concrete]
// CHECK:STDOUT: %.loc18_19.3: type = where_expr [symbolic_self = constants.%I_where.type.6d9] {
// CHECK:STDOUT: %base_facet_type.loc18_19.2 = requirement_base_facet_type %I.type.loc18_17.2 [concrete]
// CHECK:STDOUT: %rewrite.loc18_29.3 = requirement_rewrite %impl.elem0.loc18_25.3, %.loc18_32.2 [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT: %T.loc18_7.2: %I_where.type.6d9 = symbolic_binding T, 0 [symbolic = %T.loc18_7.1 (constants.%T.fb1)]
// CHECK:STDOUT: %return.param: ref %empty_tuple.type = out_param call_param0
// CHECK:STDOUT: %return: ref %empty_tuple.type = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic fn @F(%T.loc18_7.2: %I_where.type.6d9) {
// CHECK:STDOUT: %T.patt.loc18_7.2: %pattern_type.4af = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc18_7.2 (constants.%T.patt.bfa)]
// CHECK:STDOUT: %T.loc18_7.1: %I_where.type.6d9 = symbolic_binding T, 0 [symbolic = %T.loc18_7.1 (constants.%T.fb1)]
// CHECK:STDOUT: %T.as_type.loc18_39.1: type = facet_access_type %T.loc18_7.1 [symbolic = %T.as_type.loc18_39.1 (constants.%T.as_type)]
// CHECK:STDOUT: %facet_value.loc18_39.1: %type = facet_value %T.as_type.loc18_39.1, () [symbolic = %facet_value.loc18_39.1 (constants.%facet_value)]
// CHECK:STDOUT: %I.type.loc18_39.1: type = facet_type <@I, @I(%T.as_type.loc18_39.1)> [symbolic = %I.type.loc18_39.1 (constants.%I.type.1d2)]
// CHECK:STDOUT: %I_where.type.loc18_39.1: type = facet_type <@I, @I(%T.as_type.loc18_39.1) where constants.%impl.elem0.21c = constants.%empty_tuple.type> [symbolic = %I_where.type.loc18_39.1 (constants.%I_where.type.3f6)]
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT:
// CHECK:STDOUT: fn() -> out %return.param: %empty_tuple.type {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc19_11.1: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
// CHECK:STDOUT: %.loc19_11.2: init %empty_tuple.type = tuple_init () [concrete = constants.%empty_tuple]
// CHECK:STDOUT: %.loc19_12: init %empty_tuple.type = converted %.loc19_11.1, %.loc19_11.2 [concrete = constants.%empty_tuple]
// CHECK:STDOUT: return %.loc19_12
// CHECK:STDOUT:
// CHECK:STDOUT: !observes:
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @F(constants.%T.fb1) {
// CHECK:STDOUT: %T.patt.loc18_7.2 => constants.%T.patt.bfa
// CHECK:STDOUT: %T.loc18_7.1 => constants.%T.fb1
// CHECK:STDOUT: %T.as_type.loc18_39.1 => constants.%T.as_type
// CHECK:STDOUT: %facet_value.loc18_39.1 => constants.%facet_value
// CHECK:STDOUT: %I.type.loc18_39.1 => constants.%I.type.1d2
// CHECK:STDOUT: %I_where.type.loc18_39.1 => constants.%I_where.type.3f6
// CHECK:STDOUT: }
// CHECK:STDOUT: