mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-27 16:30:15 +01:00
As described in [the generics design](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/details.md#type-structure-of-an-impl-declaration), `impl` declarations are prioritized by type structure. Given two `impl` declarations that match a `type as interface` query, the one that describes the longest prefix of the query without using placeholders is preferred. We implement this by putting all impls in a total order, first by type structure equivalence classes and then by lexical order. When matching an impl, we walk this total order, and stop once we find a match and reach the end of its equivalence class. Equivalence classes are determined by finding the locations of the "holes" (the positions where deduced parameters appear) within the type structure, viewed as a tree. Two impls are in the same equivalence class if their holes are in the same place, and equivalence classes are ordered based on a reverse lexicographical ordering of their holes. Explorer doesn't keep the `Bindings` list for a parameterized type in any particular order, but the type structure rule requires that we consider them in lexical order. In order to support this, we now track an index on the declared parameters of each generic. This is a simple numbering of enclosing generic parameters, both on that generic and on all lexically enclosing generics. Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
66 lines
1.6 KiB
Plaintext
66 lines
1.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
|
|
//
|
|
// AUTOUPDATE
|
|
// RUN: %{explorer-run}
|
|
// RUN: %{explorer-run-trace}
|
|
// CHECK:STDOUT: (C(i32) as A).FA()
|
|
// CHECK:STDOUT: 6
|
|
// CHECK:STDOUT: (C(i32) as A).FA()
|
|
// CHECK:STDOUT: 7
|
|
// CHECK:STDOUT: (C(T) as B).FB()
|
|
// CHECK:STDOUT: (C(T) as A).FA()
|
|
// CHECK:STDOUT: 3
|
|
// CHECK:STDOUT: result: 0
|
|
|
|
package ExplorerTest api;
|
|
|
|
interface A {
|
|
let TA:! type;
|
|
fn FA() -> TA;
|
|
}
|
|
interface B {
|
|
let TB:! type;
|
|
fn FB() -> TB;
|
|
}
|
|
|
|
class C(T:! type) {
|
|
impl as A & B where .TA = i32 and .TB = i32 {
|
|
fn FA() -> i32 {
|
|
Print("(C(T) as A).FA()");
|
|
// OK, know that TA is i32 here.
|
|
let v: Self.(A.TA) = 1;
|
|
let w: i32 = v;
|
|
return w;
|
|
}
|
|
fn FB() -> i32 {
|
|
Print("(C(T) as B).FB()");
|
|
// OK, know that TB is i32 here.
|
|
let v: Self.(B.TB) = 2;
|
|
// Don't know that TA is i32; it could be specialized.
|
|
// TODO: We should not accept this.
|
|
let w: Self.(A.TA) = Self.(A.FA)();
|
|
return v + w;
|
|
}
|
|
}
|
|
}
|
|
|
|
external impl C(i32) as A where .TA = (i32, i32) {
|
|
fn FA() -> (i32, i32) {
|
|
Print("(C(i32) as A).FA()");
|
|
return (6, 7);
|
|
}
|
|
}
|
|
|
|
fn Main() -> i32 {
|
|
Print("{0}", C(i32).(A.FA)()[0]);
|
|
Print("{0}", C(i32).(A.FA)()[1]);
|
|
// TODO: The implementation of C(T) as B eagerly picked the (non-final)
|
|
// implementation of C(T) as A, so this ends up using a different
|
|
// implementation of C(i32) as A than the one we just used, violating
|
|
// coherence.
|
|
Print("{0}", C(i32).(B.FB)());
|
|
return 0;
|
|
}
|