mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
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:
co-authored by
Jon Ross-Perkins
parent
b795cc6f51
commit
28946d4b87
@@ -105,6 +105,17 @@ class Pattern : public AstNode {
|
||||
auto VisitNestedPatterns(const Pattern& pattern,
|
||||
llvm::function_ref<bool(const Pattern&)> visitor)
|
||||
-> bool;
|
||||
inline auto VisitNestedPatterns(Pattern& pattern,
|
||||
llvm::function_ref<bool(Pattern&)> visitor)
|
||||
-> bool {
|
||||
// The non-const version is is implemented in terms of the const version. The
|
||||
// const_cast is safe because every pattern reachable through a non-const
|
||||
// pattern is also non-const.
|
||||
const Pattern& const_pattern = pattern;
|
||||
return VisitNestedPatterns(const_pattern, [&](const Pattern& inner) {
|
||||
return visitor(const_cast<Pattern&>(inner));
|
||||
});
|
||||
}
|
||||
|
||||
// A pattern consisting of the `auto` keyword.
|
||||
class AutoPattern : public Pattern {
|
||||
@@ -252,6 +263,16 @@ class GenericBinding : public Pattern {
|
||||
auto type() const -> const Expression& { return *type_; }
|
||||
auto type() -> Expression& { return *type_; }
|
||||
|
||||
// The index of this binding, which is the number of bindings that are in
|
||||
// scope at the point where this binding is declared.
|
||||
auto index() const -> int { return *index_; }
|
||||
|
||||
// Set the index of this binding. Should be called only during type-checking.
|
||||
void set_index(int index) {
|
||||
CARBON_CHECK(!index_) << "should only set depth and index once";
|
||||
index_ = index;
|
||||
}
|
||||
|
||||
auto value_category() const -> ValueCategory { return ValueCategory::Let; }
|
||||
|
||||
auto constant_value() const -> std::optional<Nonnull<const Value*>> {
|
||||
@@ -299,6 +320,7 @@ class GenericBinding : public Pattern {
|
||||
private:
|
||||
std::string name_;
|
||||
Nonnull<Expression*> type_;
|
||||
std::optional<int> index_;
|
||||
std::optional<Nonnull<const Value*>> symbolic_identity_;
|
||||
std::optional<Nonnull<const ImplBinding*>> impl_binding_;
|
||||
std::optional<Nonnull<const GenericBinding*>> original_;
|
||||
|
||||
Reference in New Issue
Block a user