Files
carbon-lang/toolchain/check/testdata/class/method/generic_method.carbon
T
Dana Jansens 24766e213e Defer evaluation of witnesses substituted while identifying a facet type (#7930)
When identifying a facet type, defer evaluation of LookupImplWitnesses
by redirecting them to evaluate into IdentifiedWitness. Then the caller
of identify will need to substitute the IdentifiedWitnesses, either with
witnesses of their own, or with LookupImplWitness to cause a lookup
operation.

This allows:
- An `impl as` on a named constraint can subst the witnesses in
designators from the named constraint with a witness to the impl itself.
This prevents them from looking for and finding some other overlapping
impl. (This PR)
- Impl lookup to collect rewrite/equality requirements from all named
constraints and then use the impls it has already found to evaluate any
designators in those requirements instead of performing impl lookup
again (creating a cycle). (Done in
https://github.com/carbon-language/carbon-lang/pull/7944)
- Similarly, an `impl as` can collect impls and equality requirements
from all named constraints and verify them correctly by replacing
witnesses that should refer to the impl with the correct witness instead
of performing a lookup and finding an overlapping impl. (Done in
https://github.com/carbon-language/carbon-lang/pull/7937)
- ImplWitnessAccess to be able to find rewrites from a named constraint,
not just from the initial DeclaredFacetType. (TBD)

To do this we need to introduce the concept of "identified" specifics.
In order to identify a facet type, we must construct a specific for each
require decl in a named constraint. These specifics will have
IdentifiedWitnesses inside them, but their args would otherwise match a
specific for the require decl constructed in another context. To avoid
polluting the canonical specific namespace, we introduce a second
namespace of specifics that are "identified" since they are created by
identifying.

As we have introduced a new global state that modifies eval behaviour,
we must disable that state when resolving a specific (as per
https://github.com/carbon-language/carbon-lang/pull/7923). But inside an
"identified" specific, we preserve the behaviour so that the witnesses
modified by identify (through SubstPeriodSelf or through evaluating a
require specific) have their evaluation deferred.
2026-10-14 14:12:56 +00:00

183 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
// TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
// EXTRA-ARGS: --dump-sem-ir-ranges=if-present
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/method/generic_method.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/method/generic_method.carbon
class Class(T: type) {
var a: T;
fn F(self, n: T);
}
fn Class(T: type).F(unused self, unused n: T) {}
// CHECK:STDOUT: --- generic_method.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %.Self.frozen: type = symbolic_binding .Self [symbolic_self]
// CHECK:STDOUT: %pattern_type.9a5: type = pattern_type type [concrete]
// CHECK:STDOUT: %T.patt: %pattern_type.9a5 = symbolic_binding_pattern T, 0 [symbolic]
// CHECK:STDOUT: %T: type = symbolic_binding T, 0 [symbolic]
// CHECK:STDOUT: %Class.type: type = parameterized_class_name_type @Class [concrete]
// CHECK:STDOUT: %Class.generic: %Class.type = struct_value () [concrete]
// CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T) [symbolic]
// CHECK:STDOUT: %require_complete.944: <witness> = require_complete_type %T [symbolic]
// CHECK:STDOUT: %pattern_type.51d: type = pattern_type %T [symbolic]
// CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %T [symbolic]
// CHECK:STDOUT: %pattern_type.28f: type = pattern_type %Class [symbolic]
// CHECK:STDOUT: %self.param_patt: %pattern_type.28f = value_param_pattern [symbolic]
// CHECK:STDOUT: %self.patt: %pattern_type.28f = wrapper_binding_pattern self, %self.param_patt [symbolic]
// CHECK:STDOUT: %n.param_patt: %pattern_type.51d = value_param_pattern [symbolic]
// CHECK:STDOUT: %n.patt: %pattern_type.51d = wrapper_binding_pattern n, %n.param_patt [symbolic]
// CHECK:STDOUT: %Class.F.type: type = fn_type @Class.F, @Class(%T) [symbolic]
// CHECK:STDOUT: %Class.F: %Class.F.type = struct_value () [symbolic]
// CHECK:STDOUT: %struct_type.a: type = struct_type {.a: %T} [symbolic]
// CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.a [symbolic]
// CHECK:STDOUT: %require_complete.ac0: <witness> = require_complete_type %Class [symbolic]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .Class = %Class.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [concrete = constants.%Class.generic] {
// CHECK:STDOUT: %T.patt.loc15_14.1: %pattern_type.9a5 = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc15_14.2 (constants.%T.patt)]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %.loc15_16.1: type = splice_block %.loc15_16.2 [concrete = type] {
// CHECK:STDOUT: %.Self.frozen: type = symbolic_binding .Self [symbolic_self = constants.%.Self.frozen]
// CHECK:STDOUT: %.loc15_16.2: type = type_literal type [concrete = type]
// CHECK:STDOUT: }
// CHECK:STDOUT: %T.loc15_14.2: type = symbolic_binding T, 0 [symbolic = %T.loc15_14.1 (constants.%T)]
// CHECK:STDOUT: }
// CHECK:STDOUT: %Class.F.decl: %Class.F.type = fn_decl @Class.F [symbolic = constants.%Class.F] {
// CHECK:STDOUT: %self.param_patt.loc20: @Class.F.%pattern_type.loc17_8 (%pattern_type.28f) = value_param_pattern [symbolic = %self.param_patt.loc17 (constants.%self.param_patt)]
// CHECK:STDOUT: %self.patt.loc20: @Class.F.%pattern_type.loc17_8 (%pattern_type.28f) = wrapper_binding_pattern self, %self.param_patt.loc20 [symbolic = %self.patt.loc17 (constants.%self.patt)]
// CHECK:STDOUT: %n.param_patt.loc20: @Class.F.%pattern_type.loc17_15 (%pattern_type.51d) = value_param_pattern [symbolic = %n.param_patt.loc17 (constants.%n.param_patt)]
// CHECK:STDOUT: %n.patt.loc20: @Class.F.%pattern_type.loc17_15 (%pattern_type.51d) = wrapper_binding_pattern n, %n.param_patt.loc20 [symbolic = %n.patt.loc17 (constants.%n.patt)]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %.loc20_13.1: type = splice_block %.loc20_13.2 [concrete = type] {
// CHECK:STDOUT: %.Self.frozen: type = symbolic_binding .Self [symbolic_self = constants.%.Self.frozen]
// CHECK:STDOUT: %.loc20_13.2: type = type_literal type [concrete = type]
// CHECK:STDOUT: }
// CHECK:STDOUT: %T.loc20: type = symbolic_binding T, 0 [symbolic = @Class.%T.loc15_14.1 (constants.%T)]
// CHECK:STDOUT: %self.param.loc20: @Class.F.%Class (%Class) = value_param call_param0
// CHECK:STDOUT: %.loc20_28.1: type = splice_block %Self.ref.loc20 [symbolic = %Class (constants.%Class)] {
// CHECK:STDOUT: %.loc20_28.2: type = specific_constant constants.%Class, @Class(constants.%T) [symbolic = %Class (constants.%Class)]
// CHECK:STDOUT: %Self.ref.loc20: type = name_ref Self, %.loc20_28.2 [symbolic = %Class (constants.%Class)]
// CHECK:STDOUT: }
// CHECK:STDOUT: %self.loc20: @Class.F.%Class (%Class) = wrapper_binding self, %self.param.loc20
// CHECK:STDOUT: %n.param.loc20: @Class.F.%T.loc17 (%T) = value_param call_param1
// CHECK:STDOUT: %T.ref.loc20: type = name_ref T, %T.loc20 [symbolic = %T.loc17 (constants.%T)]
// CHECK:STDOUT: %n.loc20: @Class.F.%T.loc17 (%T) = wrapper_binding n, %n.param.loc20
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic class @Class(%T.loc15_14.2: type) {
// CHECK:STDOUT: %T.patt.loc15_14.2: %pattern_type.9a5 = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc15_14.2 (constants.%T.patt)]
// CHECK:STDOUT: %T.loc15_14.1: type = symbolic_binding T, 0 [symbolic = %T.loc15_14.1 (constants.%T)]
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %require_complete: <witness> = require_complete_type %T.loc15_14.1 [symbolic = %require_complete (constants.%require_complete.944)]
// CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T.loc15_14.1) [symbolic = %Class (constants.%Class)]
// CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %T.loc15_14.1 [symbolic = %Class.elem (constants.%Class.elem)]
// CHECK:STDOUT: %Class.F.type: type = fn_type @Class.F, @Class(%T.loc15_14.1) [symbolic = %Class.F.type (constants.%Class.F.type)]
// CHECK:STDOUT: %Class.F: @Class.%Class.F.type (%Class.F.type) = struct_value () [symbolic = %Class.F (constants.%Class.F)]
// CHECK:STDOUT: %struct_type.a: type = struct_type {.a: @Class.%T.loc15_14.1 (%T)} [symbolic = %struct_type.a (constants.%struct_type.a)]
// CHECK:STDOUT: %complete_type.loc18_1.2: <witness> = complete_type_witness %struct_type.a [symbolic = %complete_type.loc18_1.2 (constants.%complete_type)]
// CHECK:STDOUT:
// CHECK:STDOUT: class {
// CHECK:STDOUT: %field_decl: @Class.%Class.elem (%Class.elem) = field_decl a, element0, %T.ref in [concrete] {
// CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc15_14.2 [symbolic = %T.loc15_14.1 (constants.%T)]
// CHECK:STDOUT: }
// CHECK:STDOUT: %Class.F.decl: @Class.%Class.F.type (%Class.F.type) = fn_decl @Class.F [symbolic = @Class.%Class.F (constants.%Class.F)] {
// CHECK:STDOUT: %self.param_patt.loc20: @Class.F.%pattern_type.loc17_8 (%pattern_type.28f) = value_param_pattern [symbolic = %self.param_patt.loc17 (constants.%self.param_patt)]
// CHECK:STDOUT: %self.patt.loc20: @Class.F.%pattern_type.loc17_8 (%pattern_type.28f) = wrapper_binding_pattern self, %self.param_patt.loc20 [symbolic = %self.patt.loc17 (constants.%self.patt)]
// CHECK:STDOUT: %n.param_patt.loc20: @Class.F.%pattern_type.loc17_15 (%pattern_type.51d) = value_param_pattern [symbolic = %n.param_patt.loc17 (constants.%n.param_patt)]
// CHECK:STDOUT: %n.patt.loc20: @Class.F.%pattern_type.loc17_15 (%pattern_type.51d) = wrapper_binding_pattern n, %n.param_patt.loc20 [symbolic = %n.patt.loc17 (constants.%n.patt)]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %self.param.loc17: @Class.F.%Class (%Class) = value_param call_param0
// CHECK:STDOUT: %.loc17_8.1: type = splice_block %Self.ref.loc17 [symbolic = %Class (constants.%Class)] {
// CHECK:STDOUT: %.loc17_8.2: type = specific_constant constants.%Class, @Class(constants.%T) [symbolic = %Class (constants.%Class)]
// CHECK:STDOUT: %Self.ref.loc17: type = name_ref Self, %.loc17_8.2 [symbolic = %Class (constants.%Class)]
// CHECK:STDOUT: }
// CHECK:STDOUT: %self.loc17: @Class.F.%Class (%Class) = wrapper_binding self, %self.param.loc17
// CHECK:STDOUT: %n.param.loc17: @Class.F.%T.loc17 (%T) = value_param call_param1
// CHECK:STDOUT: %T.ref.loc17: type = name_ref T, @Class.%T.loc15_14.2 [symbolic = %T.loc17 (constants.%T)]
// CHECK:STDOUT: %n.loc17: @Class.F.%T.loc17 (%T) = wrapper_binding n, %n.param.loc17
// CHECK:STDOUT: }
// CHECK:STDOUT: %complete_type.loc18_1.1: <witness> = complete_type_witness constants.%struct_type.a [symbolic = %complete_type.loc18_1.2 (constants.%complete_type)]
// CHECK:STDOUT: complete_type_witness = %complete_type.loc18_1.1
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%Class
// CHECK:STDOUT: .T = <poisoned>
// CHECK:STDOUT: .a = %field_decl
// CHECK:STDOUT: .F = %Class.F.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic fn @Class.F(@Class.%T.loc15_14.2: type) {
// CHECK:STDOUT: %T.loc17: type = symbolic_binding T, 0 [symbolic = %T.loc17 (constants.%T)]
// CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T.loc17) [symbolic = %Class (constants.%Class)]
// CHECK:STDOUT: %pattern_type.loc17_8: type = pattern_type %Class [symbolic = %pattern_type.loc17_8 (constants.%pattern_type.28f)]
// CHECK:STDOUT: %self.param_patt.loc17: @Class.F.%pattern_type.loc17_8 (%pattern_type.28f) = value_param_pattern [symbolic = %self.param_patt.loc17 (constants.%self.param_patt)]
// CHECK:STDOUT: %self.patt.loc17: @Class.F.%pattern_type.loc17_8 (%pattern_type.28f) = wrapper_binding_pattern self, %self.param_patt.loc17 [symbolic = %self.patt.loc17 (constants.%self.patt)]
// CHECK:STDOUT: %pattern_type.loc17_15: type = pattern_type %T.loc17 [symbolic = %pattern_type.loc17_15 (constants.%pattern_type.51d)]
// CHECK:STDOUT: %n.param_patt.loc17: @Class.F.%pattern_type.loc17_15 (%pattern_type.51d) = value_param_pattern [symbolic = %n.param_patt.loc17 (constants.%n.param_patt)]
// CHECK:STDOUT: %n.patt.loc17: @Class.F.%pattern_type.loc17_15 (%pattern_type.51d) = wrapper_binding_pattern n, %n.param_patt.loc17 [symbolic = %n.patt.loc17 (constants.%n.patt)]
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %require_complete.loc20_28: <witness> = require_complete_type %Class [symbolic = %require_complete.loc20_28 (constants.%require_complete.ac0)]
// CHECK:STDOUT: %require_complete.loc20_42: <witness> = require_complete_type %T.loc17 [symbolic = %require_complete.loc20_42 (constants.%require_complete.944)]
// CHECK:STDOUT:
// CHECK:STDOUT: fn(%self.param.loc20: @Class.F.%Class (%Class), %n.param.loc20: @Class.F.%T.loc17 (%T)) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @Class(constants.%T) {
// CHECK:STDOUT: %T.patt.loc15_14.2 => constants.%T.patt
// CHECK:STDOUT: %T.loc15_14.1 => constants.%T
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %require_complete => constants.%require_complete.944
// CHECK:STDOUT: %Class => constants.%Class
// CHECK:STDOUT: %Class.elem => constants.%Class.elem
// CHECK:STDOUT: %Class.F.type => constants.%Class.F.type
// CHECK:STDOUT: %Class.F => constants.%Class.F
// CHECK:STDOUT: %struct_type.a => constants.%struct_type.a
// CHECK:STDOUT: %complete_type.loc18_1.2 => constants.%complete_type
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @Class.F(constants.%T) {
// CHECK:STDOUT: %T.loc17 => constants.%T
// CHECK:STDOUT: %Class => constants.%Class
// CHECK:STDOUT: %pattern_type.loc17_8 => constants.%pattern_type.28f
// CHECK:STDOUT: %self.param_patt.loc17 => constants.%self.param_patt
// CHECK:STDOUT: %self.patt.loc17 => constants.%self.patt
// CHECK:STDOUT: %pattern_type.loc17_15 => constants.%pattern_type.51d
// CHECK:STDOUT: %n.param_patt.loc17 => constants.%n.param_patt
// CHECK:STDOUT: %n.patt.loc17 => constants.%n.patt
// CHECK:STDOUT: }
// CHECK:STDOUT: