mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-17 23:00:29 +00:00
When doing name lookup into an extended scope of an interface or named
constraint, the containing scope has an inner `Self` facet which can
appear in the specific of the extended scope. For instance a constraint
`N` which requires an interface `Z(Self)`:
```js
constraint N {
extend require impls Z(Self);
}
```
When doing member lookup into a facet constrained by `N`, we need to
find the specific interface `Z(...)` where the `Self` is replaced by the
self-type the member lookup is happening on in order for impl lookup to
find a witness later.
Inside that specific interface we repeat the name lookup to find an
associated entity. Then to produce a witness we perform impl lookup
against the specific interface that name lookup returned with the
self-type of the member access. So if we do member access into `A:! N`
for a member `F`, like `A.F`, we would be doing impl lookup with a query
self of `A` and looking for the interface `Z(...)` returned from name
lookup.
When impl lookup has a facet as the query self, which we do here as `A`,
it takes its type (a facet type) and identifies it to find all the
required interfaces, and it substitutes the query self into those
specific interfaces for `Self`. If the `Z(...)` we acquired from name
lookup is `Z(Self)` it will fail the lookup for `A as Z(Self)`, since in
the facet type of `A` it finds a witness for `Z(A)` instead.
Thus, we replace the inner `Self` in extended scopes, such as `N`, with
the self-type of the member access, which produces the extended scope
`Z(A)` for this example. This allows the impl lookup for `A as Z(A)` to
find a witness from the facet type of `A`.
In order to do this, we include an instruction for the inner self when
registering the extended scope. Then, when we find the extended scope in
name lookup, we can use its CompileTimeBindIndex to replace any instance
of that `Self` facet with a new facet. If the self-type of member access
is a type, we construct a FacetValue with an empty facet type that
refers to the type.
185 lines
6.6 KiB
Plaintext
185 lines
6.6 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/facet/runtime_value.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/runtime_value.carbon
|
|
|
|
// --- facet_value_copy_from_reference.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface I {}
|
|
|
|
class C {
|
|
var i: I;
|
|
}
|
|
|
|
fn F(c: C) {
|
|
// Member access produces a reference, which is copied into the value
|
|
// binding.
|
|
//@dump-sem-ir-begin
|
|
let a: I = c.i;
|
|
//@dump-sem-ir-end
|
|
}
|
|
|
|
// --- fail_todo_facet_copy_narrowing_from_reference.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface I {}
|
|
interface J {}
|
|
|
|
class C {
|
|
var ij: I & J;
|
|
}
|
|
|
|
fn F(c: C) {
|
|
// Member access produces a reference, which is copied into the value
|
|
// binding.
|
|
//
|
|
// TODO: This copy should work, but the narrowing prevents it currently.
|
|
//
|
|
//@dump-sem-ir-begin
|
|
// CHECK:STDERR: fail_todo_facet_copy_narrowing_from_reference.carbon:[[@LINE+4]]:14: error: semantics TODO: `conversion of runtime facet value` [SemanticsTodo]
|
|
// CHECK:STDERR: let a: I = c.ij;
|
|
// CHECK:STDERR: ^~~~
|
|
// CHECK:STDERR:
|
|
let a: I = c.ij;
|
|
//@dump-sem-ir-end
|
|
}
|
|
|
|
// --- fail_compound_member_lookup_in_runtime_facet_without_value.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z {
|
|
let X:! type;
|
|
}
|
|
|
|
// CHECK:STDERR: fail_compound_member_lookup_in_runtime_facet_without_value.carbon:[[@LINE+4]]:15: error: semantics TODO: `associated value lookup on runtime facet value` [SemanticsTodo]
|
|
// CHECK:STDERR: fn F(T: Z, v: T.(Z.X));
|
|
// CHECK:STDERR: ^~~~~~~
|
|
// CHECK:STDERR:
|
|
fn F(T: Z, v: T.(Z.X));
|
|
|
|
// --- fail_member_lookup_in_runtime_facet_without_value.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z {
|
|
let X:! type;
|
|
}
|
|
|
|
// TODO: Improve this diagnostic. The problem is that T is a runtime facet
|
|
// _and_ its facet type doesn't specific a value for `.X`. Or make this work
|
|
// by producing an ImplWitnessAccess that functions against a runtime facet.
|
|
//
|
|
// CHECK:STDERR: fail_member_lookup_in_runtime_facet_without_value.carbon:[[@LINE+4]]:15: error: cannot access member of interface `Z` in type `Z` that does not implement that interface [MissingImplInMemberAccess]
|
|
// CHECK:STDERR: fn F(T: Z, v: T.X);
|
|
// CHECK:STDERR: ^~~
|
|
// CHECK:STDERR:
|
|
fn F(T: Z, v: T.X);
|
|
|
|
// --- fail_todo_compound_member_lookup_in_runtime_facet_with_value.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z {
|
|
let X:! type;
|
|
}
|
|
|
|
// TODO: We should be able to get the value of `.X` from the `FacetType` of
|
|
// `T`.
|
|
//
|
|
// CHECK:STDERR: fail_todo_compound_member_lookup_in_runtime_facet_with_value.carbon:[[@LINE+4]]:29: error: semantics TODO: `conversion of runtime facet value` [SemanticsTodo]
|
|
// CHECK:STDERR: fn F(T: Z where .X = (), v: T.(Z.X));
|
|
// CHECK:STDERR: ^~~~~~~
|
|
// CHECK:STDERR:
|
|
fn F(T: Z where .X = (), v: T.(Z.X));
|
|
|
|
// --- fail_todo_member_lookup_in_runtime_facet_with_value.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z {
|
|
let X:! type;
|
|
}
|
|
|
|
// TODO: We should be able to get the value of `.X` from the `FacetType` of
|
|
// `T`.
|
|
//
|
|
// CHECK:STDERR: fail_todo_member_lookup_in_runtime_facet_with_value.carbon:[[@LINE+4]]:29: error: cannot access member of interface `Z` in type `Z where .(Z.X) = ()` that does not implement that interface [MissingImplInMemberAccess]
|
|
// CHECK:STDERR: fn F(T: Z where .X = (), v: T.X);
|
|
// CHECK:STDERR: ^~~
|
|
// CHECK:STDERR:
|
|
fn F(T: Z where .X = (), v: T.X);
|
|
|
|
// --- fail_todo_member_lookup_in_type_of_runtime_facet_with_value.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z {
|
|
fn G();
|
|
}
|
|
|
|
fn F(T: Z) {
|
|
// CHECK:STDERR: fail_todo_member_lookup_in_type_of_runtime_facet_with_value.carbon:[[@LINE+4]]:3: error: cannot access member of interface `Z` in type `Z` that does not implement that interface [MissingImplInMemberAccess]
|
|
// CHECK:STDERR: T.G();
|
|
// CHECK:STDERR: ^~~
|
|
// CHECK:STDERR:
|
|
T.G();
|
|
}
|
|
|
|
// CHECK:STDOUT: --- facet_value_copy_from_reference.carbon
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: constants {
|
|
// CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
|
|
// CHECK:STDOUT: %C: type = class_type @C [concrete]
|
|
// CHECK:STDOUT: %C.elem: type = unbound_element_type %C, %I.type [concrete]
|
|
// CHECK:STDOUT: %pattern_type.9d9: type = pattern_type %I.type [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: imports {
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @F(%c.param: %C) {
|
|
// CHECK:STDOUT: !entry:
|
|
// CHECK:STDOUT: name_binding_decl {
|
|
// CHECK:STDOUT: %a.patt: %pattern_type.9d9 = value_binding_pattern a [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: %c.ref: %C = name_ref c, %c
|
|
// CHECK:STDOUT: %i.ref: %C.elem = name_ref i, @C.%.loc6 [concrete = @C.%.loc6]
|
|
// CHECK:STDOUT: %.loc13_15.1: ref %I.type = class_element_access %c.ref, element0
|
|
// CHECK:STDOUT: %.loc13_15.2: %I.type = acquire_value %.loc13_15.1
|
|
// CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
|
|
// CHECK:STDOUT: %a: %I.type = value_binding a, %.loc13_15.2
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: --- fail_todo_facet_copy_narrowing_from_reference.carbon
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: constants {
|
|
// CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
|
|
// CHECK:STDOUT: %C: type = class_type @C [concrete]
|
|
// CHECK:STDOUT: %facet_type: type = facet_type <@I & @J> [concrete]
|
|
// CHECK:STDOUT: %C.elem: type = unbound_element_type %C, %facet_type [concrete]
|
|
// CHECK:STDOUT: %pattern_type.9d9: type = pattern_type %I.type [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: imports {
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @F(%c.param: %C) {
|
|
// CHECK:STDOUT: !entry:
|
|
// CHECK:STDOUT: name_binding_decl {
|
|
// CHECK:STDOUT: %a.patt: %pattern_type.9d9 = value_binding_pattern a [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: %c.ref: %C = name_ref c, %c
|
|
// CHECK:STDOUT: %ij.ref: %C.elem = name_ref ij, @C.%.loc7_9 [concrete = @C.%.loc7_9]
|
|
// CHECK:STDOUT: %.loc21_15.1: ref %facet_type = class_element_access %c.ref, element0
|
|
// CHECK:STDOUT: %.loc21_15.2: %facet_type = acquire_value %.loc21_15.1
|
|
// CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
|
|
// CHECK:STDOUT: %a: %I.type = value_binding a, <error> [concrete = <error>]
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|