mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 16:01:05 +01:00
Currently each interface has a `Self` facet internally that becomes a binding to every entity inside the interface: associated constants, functions, and require decls. Each of these has to be independently generic as a result. This makes is challenging in extended name lookup to move into an extended scope of an interface, as we have a specific for the interface, but the names within require a different specific that includes a `Self` facet value. We generalize this relationship by adding a second generic to Interface, called `generic_with_self`. When we want to work with entities inside the interface, we move from the interface-without-specific to the interface-with-self specific by adding a Self to the specific. This is done independently of any particular entity inside the Interface, as those entities are now all members of the interface-with-self generic. Associated constants no longer need a generic of their own, as they do not have separate generic bindings. Functions retain a generic, but if the function has no generic arguments, it will have no bindings of its own now. Require decls retain a generic so that their specific can be instantiated separately from the interface. Requiring the interface to be complete does not require the types in a require decl to be complete unless it is modified by `extend`. So we allow them to be completed later by keeping them in a separate generic. Named constraints look like interfaces and gain the additional inner generic-with-self, with the same relationship to require decls. This removes the need for name lookup to perform Substitution of a Self facet into the extended scope instruction. Instead, the `SpecificConstant` instruction inserted by a `require` decl is part of the interface-with-self generic. When looking through a FacetType for extended scopes, for each interface, we push the scope with the specific for the interface-with-self. Then the constant value of the `SpecificConstant` is correctly modified by the provided self automatically through applying that specific.
136 lines
7.4 KiB
Plaintext
136 lines
7.4 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
|
|
// 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/impl/impl_forall.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/impl_forall.carbon
|
|
|
|
interface Simple {
|
|
fn F();
|
|
}
|
|
|
|
impl forall [T:! type] T as Simple {
|
|
fn F() {}
|
|
}
|
|
|
|
// CHECK:STDOUT: --- impl_forall.carbon
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: constants {
|
|
// CHECK:STDOUT: %Simple.type: type = facet_type <@Simple> [concrete]
|
|
// CHECK:STDOUT: %Self: %Simple.type = symbolic_binding Self, 0 [symbolic]
|
|
// CHECK:STDOUT: %Simple.WithSelf.F.type.75b: type = fn_type @Simple.WithSelf.F, @Simple(%Self) [symbolic]
|
|
// CHECK:STDOUT: %Simple.WithSelf.F.58c: %Simple.WithSelf.F.type.75b = struct_value () [symbolic]
|
|
// CHECK:STDOUT: %Simple.assoc_type: type = assoc_entity_type @Simple [concrete]
|
|
// CHECK:STDOUT: %assoc0: %Simple.assoc_type = assoc_entity element0, @Simple.%Simple.WithSelf.F.decl [concrete]
|
|
// CHECK:STDOUT: %type: type = facet_type <type> [concrete]
|
|
// CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self]
|
|
// CHECK:STDOUT: %T: type = symbolic_binding T, 0 [symbolic]
|
|
// CHECK:STDOUT: %pattern_type: type = pattern_type type [concrete]
|
|
// CHECK:STDOUT: %Simple.impl_witness: <witness> = impl_witness @T.as.Simple.impl.%Simple.impl_witness_table, @T.as.Simple.impl(%T) [symbolic]
|
|
// CHECK:STDOUT: %T.as.Simple.impl.F.type: type = fn_type @T.as.Simple.impl.F, @T.as.Simple.impl(%T) [symbolic]
|
|
// CHECK:STDOUT: %T.as.Simple.impl.F: %T.as.Simple.impl.F.type = struct_value () [symbolic]
|
|
// CHECK:STDOUT: %Simple.facet: %Simple.type = facet_value %T, (%Simple.impl_witness) [symbolic]
|
|
// CHECK:STDOUT: %Simple.WithSelf.F.type.b56: type = fn_type @Simple.WithSelf.F, @Simple(%Simple.facet) [symbolic]
|
|
// CHECK:STDOUT: %Simple.WithSelf.F.2ed: %Simple.WithSelf.F.type.b56 = struct_value () [symbolic]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: file {
|
|
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
|
|
// CHECK:STDOUT: .Simple = %Simple.decl
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: %Simple.decl: type = interface_decl @Simple [concrete = constants.%Simple.type] {} {}
|
|
// CHECK:STDOUT: impl_decl @T.as.Simple.impl [concrete] {
|
|
// CHECK:STDOUT: %T.patt: %pattern_type = symbolic_binding_pattern T, 0 [concrete]
|
|
// CHECK:STDOUT: } {
|
|
// CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc19_14.2 [symbolic = %T.loc19_14.1 (constants.%T)]
|
|
// CHECK:STDOUT: %Simple.ref: type = name_ref Simple, file.%Simple.decl [concrete = constants.%Simple.type]
|
|
// CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self = constants.%.Self]
|
|
// CHECK:STDOUT: %T.loc19_14.2: type = symbolic_binding T, 0 [symbolic = %T.loc19_14.1 (constants.%T)]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: interface @Simple {
|
|
// CHECK:STDOUT: %Self.loc15: %Simple.type = symbolic_binding Self, 0 [symbolic = constants.%Self]
|
|
// CHECK:STDOUT: interface_with_self_decl @Simple [concrete]
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !with Self:
|
|
// CHECK:STDOUT: %Simple.WithSelf.F.decl: @Simple.%Simple.WithSelf.F.type (%Simple.WithSelf.F.type.75b) = fn_decl @Simple.WithSelf.F [symbolic = @Simple.%Simple.WithSelf.F (constants.%Simple.WithSelf.F.58c)] {} {}
|
|
// CHECK:STDOUT: %assoc0: %Simple.assoc_type = assoc_entity element0, %Simple.WithSelf.F.decl [concrete = constants.%assoc0]
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !members:
|
|
// CHECK:STDOUT: .Self = %Self.loc15
|
|
// CHECK:STDOUT: .F = %assoc0
|
|
// CHECK:STDOUT: witness = (%Simple.WithSelf.F.decl)
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !requires:
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: generic impl @T.as.Simple.impl(%T.loc19_14.2: type) {
|
|
// CHECK:STDOUT: %T.loc19_14.1: type = symbolic_binding T, 0 [symbolic = %T.loc19_14.1 (constants.%T)]
|
|
// CHECK:STDOUT: %Simple.impl_witness.loc19_36.2: <witness> = impl_witness %Simple.impl_witness_table, @T.as.Simple.impl(%T.loc19_14.1) [symbolic = %Simple.impl_witness.loc19_36.2 (constants.%Simple.impl_witness)]
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !definition:
|
|
// CHECK:STDOUT: %T.as.Simple.impl.F.type: type = fn_type @T.as.Simple.impl.F, @T.as.Simple.impl(%T.loc19_14.1) [symbolic = %T.as.Simple.impl.F.type (constants.%T.as.Simple.impl.F.type)]
|
|
// CHECK:STDOUT: %T.as.Simple.impl.F: @T.as.Simple.impl.%T.as.Simple.impl.F.type (%T.as.Simple.impl.F.type) = struct_value () [symbolic = %T.as.Simple.impl.F (constants.%T.as.Simple.impl.F)]
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: impl: %T.ref as %Simple.ref {
|
|
// CHECK:STDOUT: %T.as.Simple.impl.F.decl: @T.as.Simple.impl.%T.as.Simple.impl.F.type (%T.as.Simple.impl.F.type) = fn_decl @T.as.Simple.impl.F [symbolic = @T.as.Simple.impl.%T.as.Simple.impl.F (constants.%T.as.Simple.impl.F)] {} {}
|
|
// CHECK:STDOUT: %Simple.impl_witness_table = impl_witness_table (%T.as.Simple.impl.F.decl), @T.as.Simple.impl [concrete]
|
|
// CHECK:STDOUT: %Simple.impl_witness.loc19_36.1: <witness> = impl_witness %Simple.impl_witness_table, @T.as.Simple.impl(constants.%T) [symbolic = %Simple.impl_witness.loc19_36.2 (constants.%Simple.impl_witness)]
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !members:
|
|
// CHECK:STDOUT: .F = %T.as.Simple.impl.F.decl
|
|
// CHECK:STDOUT: witness = %Simple.impl_witness.loc19_36.1
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: generic fn @Simple.WithSelf.F(@Simple.%Self.loc15: %Simple.type) {
|
|
// CHECK:STDOUT: fn();
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: generic fn @T.as.Simple.impl.F(@T.as.Simple.impl.%T.loc19_14.2: type) {
|
|
// CHECK:STDOUT: !definition:
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn() {
|
|
// CHECK:STDOUT: !entry:
|
|
// CHECK:STDOUT: return
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: specific @Simple(constants.%Self) {
|
|
// CHECK:STDOUT: !definition:
|
|
// CHECK:STDOUT: %Self.loc16 => constants.%Self
|
|
// CHECK:STDOUT: %Simple.WithSelf.F.type => constants.%Simple.WithSelf.F.type.75b
|
|
// CHECK:STDOUT: %Simple.WithSelf.F => constants.%Simple.WithSelf.F.58c
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: specific @Simple.WithSelf.F(constants.%Self) {}
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: specific @T.as.Simple.impl(constants.%T) {
|
|
// CHECK:STDOUT: %T.loc19_14.1 => constants.%T
|
|
// CHECK:STDOUT: %Simple.impl_witness.loc19_36.2 => constants.%Simple.impl_witness
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !definition:
|
|
// CHECK:STDOUT: %T.as.Simple.impl.F.type => constants.%T.as.Simple.impl.F.type
|
|
// CHECK:STDOUT: %T.as.Simple.impl.F => constants.%T.as.Simple.impl.F
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: specific @T.as.Simple.impl.F(constants.%T) {}
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: specific @Simple(constants.%Simple.facet) {
|
|
// CHECK:STDOUT: !definition:
|
|
// CHECK:STDOUT: %Self.loc16 => constants.%Simple.facet
|
|
// CHECK:STDOUT: %Simple.WithSelf.F.type => constants.%Simple.WithSelf.F.type.b56
|
|
// CHECK:STDOUT: %Simple.WithSelf.F => constants.%Simple.WithSelf.F.2ed
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: specific @Simple.WithSelf.F(constants.%Simple.facet) {}
|
|
// CHECK:STDOUT:
|