Order impl matching by type structure (#2691)

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>
This commit is contained in:
Richard Smith
2023-03-17 16:50:06 -07:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent b795cc6f51
commit 28946d4b87
22 changed files with 733 additions and 220 deletions
@@ -1,28 +0,0 @@
// 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: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
class A(T:! type) {}
interface B(T:! type) {}
external impl forall [T:! type] A(T) as B(i32) {}
external impl forall [T:! type] A(i32) as B(T) {}
fn F[T:! B(i32)](x: T) {}
fn G[T:! B(bool)](x: T) {}
fn Main() -> i32 {
let a: A(bool) = {};
let b: A(i32) = {};
F(a);
G(b);
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/impl/fail_ambiguous_impl_generic.carbon:[[@LINE+1]]: ambiguous implementations of interface B(T = i32) for class A(T = i32)
F(b);
return 0;
}
@@ -1,36 +0,0 @@
// 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: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
class A(T:! type) {}
interface B(T:! type) {}
// This match_first doesn't affect the ambiguity between
// these impls and the one below.
// TODO: Once we order by type structure, the second impl
// in this block should win.
__match_first {
external impl A(i32) as B(i32) {}
external impl forall [T:! type] A(i32) as B(T) {}
}
external impl forall [T:! type] A(T) as B(i32) {}
fn F[T:! B(i32)](x: T) {}
fn G[T:! B(bool)](x: T) {}
fn Main() -> i32 {
let a: A(bool) = {};
let b: A(i32) = {};
F(a);
G(b);
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/impl/fail_ambiguous_impl_match_first.carbon:[[@LINE+1]]: ambiguous implementations of interface B(T = i32) for class A(T = i32)
F(b);
return 0;
}
@@ -35,7 +35,7 @@ external impl Point as Vector {
fn Scale[self: Point](v: i32) -> Point {
return {.x = self.x * v, .y = self.y * v};
}
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/impl/fail_ambiguous_impl.carbon:[[@LINE+1]]: ambiguous implementations of interface Vector for class Point
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/impl/fail_impl_redefinition.carbon:[[@LINE+1]]: ambiguous implementations of interface Vector for class Point
}
fn AddAndScaleGeneric[T:! Vector](a: T, b: T, s: i32) -> T {
@@ -1,50 +0,0 @@
// 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: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
interface Vector(T:! type) {
fn Add[self: Self](b: Self) -> Self;
fn Scale[self: Self](v: T) -> Self;
}
class Point(T:! type) {
var x: T;
var y: T;
}
external impl Point(i32) as Vector(i32) {
fn Add[self: Self](b: Self) -> Self {
return {.x = self.x + b.x, .y = self.y + b.y};
}
fn Scale[self: Self](v: i32) -> Self {
return {.x = self.x * v, .y = self.y * v};
}
}
external impl forall [T:! type] Point(T) as Vector(T) {
fn Add[self: Self](b: Self) -> Self {
return self;
}
fn Scale[self: Self](v: T) -> Self {
return self;
}
}
fn AddAndScaleGeneric[T:! Vector(i32)](a: T, b: T, s: i32) -> T {
return a.Add(b).Scale(s);
}
fn Main() -> i32 {
var a: Point(i32) = {.x = 1, .y = 1};
var b: Point(i32) = {.x = 2, .y = 3};
// TODO: This shouldn't be considered ambiguous.
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/impl/fail_unambiguous_impl_generic.carbon:[[@LINE+1]]: ambiguous implementations of interface Vector(T = i32) for class Point(T = i32)
var p: Point(i32) = AddAndScaleGeneric(a, b, 5);
return p.x - 15;
}
-70
View File
@@ -1,70 +0,0 @@
// 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: XYZ: 1
// CHECK:STDOUT: XY: 1
// CHECK:STDOUT: XZ: 1
// CHECK:STDOUT: YZ: 2
// CHECK:STDOUT: JustX: 1
// CHECK:STDOUT: JustY: 2
// CHECK:STDOUT: JustZ: 3
// CHECK:STDOUT: None: 4
// CHECK:STDOUT: result: 0
package ExplorerTest api;
interface A {
fn Which() -> i32;
}
interface X {}
interface Y {}
interface Z {}
__match_first {
external impl forall [T:! X] T as A {
fn Which() -> i32 { return 1; }
}
external impl forall [T:! Y] T as A {
fn Which() -> i32 { return 2; }
}
external impl forall [T:! Z] T as A {
fn Which() -> i32 { return 3; }
}
external impl forall [T:! type] T as A {
fn Which() -> i32 { return 4; }
}
}
class XYZ {}
class XY {}
class XZ {}
class YZ {}
class JustX {}
class JustY {}
class JustZ {}
class None {}
impl XYZ as X & Y & Z {}
impl XY as X & Y {}
impl XZ as X & Z {}
impl YZ as Y & Z {}
impl JustX as X {}
impl JustY as Y {}
impl JustZ as Z {}
fn Main() -> i32 {
Print("XYZ: {0}", XYZ.(A.Which)());
Print("XY: {0}", XY.(A.Which)());
Print("XZ: {0}", XZ.(A.Which)());
Print("YZ: {0}", YZ.(A.Which)());
Print("JustX: {0}", JustX.(A.Which)());
Print("JustY: {0}", JustY.(A.Which)());
Print("JustZ: {0}", JustZ.(A.Which)());
Print("None: {0}", None.(A.Which)());
return 0;
}