mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 18:31:08 +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.
214 lines
6.0 KiB
Plaintext
214 lines
6.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/convert.carbon
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/impl_as_named_constraint.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/impl_as_named_constraint.carbon
|
|
|
|
// --- fail_empty_constraint.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
constraint A {}
|
|
|
|
// CHECK:STDERR: fail_empty_constraint.carbon:[[@LINE+4]]:1: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
|
|
// CHECK:STDERR: impl () as A {}
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
impl () as A {}
|
|
|
|
// --- fail_too_many_interfaces_in_constraint.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface A1 {}
|
|
interface A2 {}
|
|
constraint B {
|
|
extend require impls A1;
|
|
extend require impls A2;
|
|
}
|
|
|
|
// CHECK:STDERR: fail_too_many_interfaces_in_constraint.carbon:[[@LINE+4]]:1: error: impl as 2 interfaces, expected 1 [ImplOfNotOneInterface]
|
|
// CHECK:STDERR: impl () as B {}
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
impl () as B {}
|
|
|
|
// --- one_extend_impls_interface_in_constraint.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface A {}
|
|
constraint B {
|
|
extend require impls A;
|
|
}
|
|
|
|
impl () as B {}
|
|
|
|
// --- fail_one_impls_interface_in_constraint.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface A;
|
|
constraint B {
|
|
require impls A;
|
|
}
|
|
|
|
// CHECK:STDERR: fail_one_impls_interface_in_constraint.carbon:[[@LINE+4]]:1: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
|
|
// CHECK:STDERR: impl () as B {}
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
impl () as B {}
|
|
|
|
// --- nested_constraints.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface A {}
|
|
constraint B {
|
|
extend require impls A;
|
|
}
|
|
constraint C {
|
|
extend require impls B;
|
|
}
|
|
|
|
impl () as C {}
|
|
|
|
// --- fail_nested_constraints_not_extend_outer.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface A {}
|
|
|
|
constraint B {
|
|
extend require impls A;
|
|
}
|
|
constraint C {
|
|
require impls B;
|
|
}
|
|
|
|
// CHECK:STDERR: fail_nested_constraints_not_extend_outer.carbon:[[@LINE+4]]:1: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
|
|
// CHECK:STDERR: impl () as C {}
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
impl () as C {}
|
|
|
|
// --- fail_nested_constraints_not_extend_inner.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface A {}
|
|
|
|
constraint B {
|
|
require impls A;
|
|
}
|
|
constraint C {
|
|
extend require impls B;
|
|
}
|
|
|
|
// CHECK:STDERR: fail_nested_constraints_not_extend_inner.carbon:[[@LINE+4]]:1: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
|
|
// CHECK:STDERR: impl () as C {}
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
impl () as C {}
|
|
|
|
// --- fail_nested_constraints_not_extend_both.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface A {}
|
|
|
|
constraint B {
|
|
require impls A;
|
|
}
|
|
constraint C {
|
|
require impls B;
|
|
}
|
|
|
|
// CHECK:STDERR: fail_nested_constraints_not_extend_both.carbon:[[@LINE+4]]:1: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
|
|
// CHECK:STDERR: impl () as C {}
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
impl () as C {}
|
|
|
|
// --- fail_duplicate_through_generic_constraint.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface A(T:! type) {}
|
|
|
|
constraint B(X:! type, Y:! type) {
|
|
extend require impls A(Y);
|
|
}
|
|
|
|
// This should impl () as A({}).
|
|
impl () as B((), {}) {}
|
|
|
|
// CHECK:STDERR: fail_duplicate_through_generic_constraint.carbon:[[@LINE+7]]:1: error: found non-final `impl` with the same type structure as another non-final `impl` [ImplNonFinalSameTypeStructure]
|
|
// CHECK:STDERR: impl () as A({}) {}
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
|
|
// CHECK:STDERR: fail_duplicate_through_generic_constraint.carbon:[[@LINE-5]]:1: note: other `impl` here [ImplNonFinalSameTypeStructureNote]
|
|
// CHECK:STDERR: impl () as B((), {}) {}
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
impl () as A({}) {}
|
|
|
|
// --- fail_error_self_in_require.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface A(T:! type) {}
|
|
|
|
// This makes the type of `Self` into an ErrorInst. No `require` is added to
|
|
// the constraint.
|
|
//@dump-sem-ir-begin
|
|
//
|
|
// CHECK:STDERR: fail_error_self_in_require.carbon:[[@LINE+4]]:18: error: name `Undefined` not found [NameNotFound]
|
|
// CHECK:STDERR: constraint B(X:! Undefined) {
|
|
// CHECK:STDERR: ^~~~~~~~~
|
|
// CHECK:STDERR:
|
|
constraint B(X:! Undefined) {
|
|
extend require impls A(X);
|
|
}
|
|
//@dump-sem-ir-end
|
|
|
|
impl () as B(1) {}
|
|
|
|
// CHECK:STDOUT: --- fail_error_self_in_require.carbon
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: constants {
|
|
// CHECK:STDOUT: %B.type: type = generic_named_constaint_type @B [concrete]
|
|
// CHECK:STDOUT: %empty_struct: %B.type = struct_value () [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: imports {
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: file {
|
|
// CHECK:STDOUT: %B.decl: %B.type = constraint_decl @B [concrete = constants.%empty_struct] {
|
|
// CHECK:STDOUT: %X.patt: <error> = symbolic_binding_pattern X, 0 [concrete]
|
|
// CHECK:STDOUT: } {
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT: %X: <error> = symbolic_binding X, 0 [concrete = <error>]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: generic constraint @B(%X: <error>) {
|
|
// CHECK:STDOUT: !definition:
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: constraint {
|
|
// CHECK:STDOUT: %Self: <error> = symbolic_binding Self, 1 [concrete = <error>]
|
|
// CHECK:STDOUT: constraint_with_self_decl @B [concrete]
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !members:
|
|
// CHECK:STDOUT: .Self = %Self
|
|
// CHECK:STDOUT: .A = <poisoned>
|
|
// CHECK:STDOUT: .X = <poisoned>
|
|
// CHECK:STDOUT: .A = <poisoned>
|
|
// CHECK:STDOUT: .X = <poisoned>
|
|
// CHECK:STDOUT: has_error
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !requires:
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: specific @B(<error>) {}
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: specific @B(<error>, <error>) {}
|
|
// CHECK:STDOUT:
|