From: Wesley Lewis <[email protected]>
A trait can declare and associated type and a method with the same
identifier, since they live in different namespaces. Several lookups
matched trait items by name alone and picked whichever came first, which
caused an ICE in resolve_method_address.
Filter impl probe candidates to functions before falling back to the
trait's default methods, and makeup lookup_hir_trait_item and the
TraitItemReference overload of lookup_associated_item match on the item
itself instead of it's name.
Fixes Rust-GCC/gccrs#4986
gcc/rust/ChangeLog:
* backend/rust-compile-base.cc
(HirCompileBase::resolve_method_address): Filter probe candidate
first before checking for an empty set.
* typecheck/rust-hir-trait-reference.cc
(TraitReference::lookup_hir_trait_item): Match on HirId instead
of identifier.
* typecheck/rust-tyty-bounds.cc
(TypeBoundPredicate::lookup_associated_item): Match by identity.
gcc/testsuite/ChangeLog:
* rust/compile/method-assoc-type-same-name.rs: New test.
* rust/execute/torture/issue-4986.rs: New test.
Signed-off-by: Wesley Lewis <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/4c39865583026c74b70e8f6bfe9644a739386b11
The commit has been mentioned in the following issue(s):
- Rust-GCC/gccrs#4986: https://github.com/Rust-GCC/gccrs/issues/4986
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/5127
gcc/rust/backend/rust-compile-base.cc | 29 +++++++++-------
.../typecheck/rust-hir-trait-reference.cc | 11 ++++++-
gcc/rust/typecheck/rust-tyty-bounds.cc | 16 ++++++++-
.../compile/method-assoc-type-same-name.rs | 19 +++++++++++
.../rust/execute/torture/issue-4986.rs | 33 +++++++++++++++++++
5 files changed, 94 insertions(+), 14 deletions(-)
create mode 100644 gcc/testsuite/rust/compile/method-assoc-type-same-name.rs
create mode 100644 gcc/testsuite/rust/execute/torture/issue-4986.rs
diff --git a/gcc/rust/backend/rust-compile-base.cc
b/gcc/rust/backend/rust-compile-base.cc
index 65ec002fc..02c606a33 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -1096,7 +1096,23 @@ HIRCompileBase::resolve_method_address (TyTy::FnType
*fntype,
auto root = receiver->get_root ();
auto candidates
= Resolver::PathProbeImplTrait::Probe (root, segment, trait_ref);
- if (candidates.size () == 0)
+
+ // The probe matches on the identifier only, so it can also return from the
+ // type namespace, such as an associated type with the same name as the
+ // method. Only function items are relevant here.
+
+ // filter for the possible case of non fn type items
+ std::set<Resolver::PathProbeCandidate> filteredFunctionCandidates;
+ for (auto &candidate : candidates)
+ {
+ rust_debug ("Candidate: %s", candidate.ty->as_string ().c_str ());
+ bool is_fntype = candidate.ty->get_kind () == TyTy::TypeKind::FNDEF;
+ if (!is_fntype)
+ continue;
+
+ filteredFunctionCandidates.insert (candidate);
+ }
+ if (filteredFunctionCandidates.empty ())
{
// this means we are defaulting back to the trait_item if
// possible
@@ -1118,17 +1134,6 @@ HIRCompileBase::resolve_method_address (TyTy::FnType
*fntype,
rust_debug_loc (expr_locus, "resolved to %lu candidates",
(unsigned long) candidates.size ());
- // filter for the possible case of non fn type items
- std::set<Resolver::PathProbeCandidate> filteredFunctionCandidates;
- for (auto &candidate : candidates)
- {
- bool is_fntype = candidate.ty->get_kind () == TyTy::TypeKind::FNDEF;
- if (!is_fntype)
- continue;
-
- filteredFunctionCandidates.insert (candidate);
- }
-
// look for the exact fntype
for (auto &candidate : filteredFunctionCandidates)
{
diff --git a/gcc/rust/typecheck/rust-hir-trait-reference.cc
b/gcc/rust/typecheck/rust-hir-trait-reference.cc
index 9305defc0..b59c63554 100644
--- a/gcc/rust/typecheck/rust-hir-trait-reference.cc
+++ b/gcc/rust/typecheck/rust-hir-trait-reference.cc
@@ -204,7 +204,16 @@ bool
TraitReference::lookup_hir_trait_item (const HIR::TraitItem &item,
TraitItemReference **ref)
{
- return lookup_trait_item (item.trait_identifier (), ref);
+ HirId id = item.get_mappings ().get_hirid ();
+ for (auto &item_ref : item_refs)
+ {
+ if (item_ref.get_hir_trait_item ()->get_mappings ().get_hirid () == id)
+ {
+ *ref = &item_ref;
+ return true;
+ }
+ }
+ return false;
}
bool
diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc
b/gcc/rust/typecheck/rust-tyty-bounds.cc
index 14f74111b..a3061bb0c 100644
--- a/gcc/rust/typecheck/rust-tyty-bounds.cc
+++ b/gcc/rust/typecheck/rust-tyty-bounds.cc
@@ -665,7 +665,21 @@ tl::optional<TypeBoundPredicateItem>
TypeBoundPredicate::lookup_associated_item (
const Resolver::TraitItemReference *ref) const
{
- return lookup_associated_item (ref->get_identifier ());
+ // Match on identity, not name: a trait may have an associated type
+ // and a method with the same identifier.
+ for (const auto &item : get ()->get_trait_items ())
+ {
+ if (&item == ref)
+ return TypeBoundPredicateItem (this, ref);
+ }
+ // This item may belong to a super trait.
+ for (auto &super_trait : super_traits)
+ {
+ auto lookup = super_trait.lookup_associated_item (ref);
+ if (lookup.has_value ())
+ return lookup;
+ }
+ return tl::nullopt;
}
BaseType *
diff --git a/gcc/testsuite/rust/compile/method-assoc-type-same-name.rs
b/gcc/testsuite/rust/compile/method-assoc-type-same-name.rs
new file mode 100644
index 000000000..8eaf0c1a6
--- /dev/null
+++ b/gcc/testsuite/rust/compile/method-assoc-type-same-name.rs
@@ -0,0 +1,19 @@
+#![feature(no_core)]
+#![no_core]
+#![feature(lang_items)]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+pub trait Foo {
+ type bar;
+ fn bar(&self) {}
+}
+
+impl Foo for char {
+ type bar = u8;
+}
+
+fn main() {
+ 'a'.bar()
+}
\ No newline at end of file
diff --git a/gcc/testsuite/rust/execute/torture/issue-4986.rs
b/gcc/testsuite/rust/execute/torture/issue-4986.rs
new file mode 100644
index 000000000..91d71070a
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/issue-4986.rs
@@ -0,0 +1,33 @@
+#![feature(no_core)]
+#![no_core]
+#![feature(lang_items)]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+pub trait Foo {
+ type bar;
+ fn bar(&self) -> i32 {
+ 1
+ }
+}
+
+// No override: must use the trait's default `bar`
+impl Foo for char {
+ type bar = u8;
+}
+
+// Override: must select the impl's `fn bar`, not `type bar`
+impl Foo for bool {
+ type bar = u8;
+ fn bar(&self) -> i32 {
+ 2
+ }
+}
+
+fn main() -> i32 {
+ let a = 'a'.bar();
+ let b = true.bar();
+
+ a + b - 3
+}
base-commit: b853d101be5eef25180aeb17281d08c1a03af294
--
2.56.1