gccrs: Method resolution must support multiple candidates
authorPhilip Herron <philip.herron@embecosm.com>
Thu, 13 Oct 2022 09:10:37 +0000 (10:10 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 21 Feb 2023 11:36:33 +0000 (12:36 +0100)
commit59bee4d4663e7e285a41540e9378cb0f9f312ee1
tree2283b25ed9a4f4eee89372c65af858a16dcfd47a
parent2d7661e76e1276b3661502022ecee2861d040026
gccrs: Method resolution must support multiple candidates

This patch fixes bad method resolution in our operator_overload_9 case.
When we have a &mut reference to something and we deref we must resolve to
the mutable reference impl block. The interface we are using to resolve
methods is the can_eq interface which allows for permissive mutability
which means allowing for mutable reference being unified with an immutable
one. This meant we actual match against both the immutable and mutable
version leading to multiple candidate error.

The fix here adds a method resolution flag to the can_eq interface so that
we enforce mutability equality. The other hack is that we do not allow
can_eq of ParamTypes to generic Slices. I think there is some subtle thing
going on for that case. The Rustc method resolver actually filters the
impl blocks for reference types based looking up the relevant lang items
we need to do this as well but is a much larger refactor to our method
resolver which should be done seperately.

Fixes #1588

gcc/rust/ChangeLog:

* typecheck/rust-autoderef.cc: Add support for multiple resolution candidates.
* typecheck/rust-hir-dot-operator.cc (MethodResolver::MethodResolver): Edit
`try_result` field and change constructor.
(MethodResolver::Probe): Return set of candidates instead of singular candidate.
(MethodResolver::select): Add better implementation to account for multiple
candidates.
* typecheck/rust-hir-dot-operator.h (struct MethodCandidate): Overload comparison
operator in order to store them in `std::set`.
* typecheck/rust-hir-inherent-impl-overlap.h: Do not fail assertion on missing type.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Adapt code to use
multiple candidates.
* typecheck/rust-tyty.cc (set_cmp_autoderef_mode): Add code to handle automatic
derefs properly.
(reset_cmp_autoderef_mode): Add helper function to reset said mode.
* typecheck/rust-tyty.h (set_cmp_autoderef_mode): Declare function.
(reset_cmp_autoderef_mode): Likewise.
* typecheck/rust-tyty-cmp.h: Add handling of `autoderef_cmp_flag`

gcc/testsuite/ChangeLog:

* rust/compile/generics7.rs: Fix test with missing assertion.
* rust/execute/torture/operator_overload_9.rs: Fix test assertion.
gcc/rust/typecheck/rust-autoderef.cc
gcc/rust/typecheck/rust-hir-dot-operator.cc
gcc/rust/typecheck/rust-hir-dot-operator.h
gcc/rust/typecheck/rust-hir-inherent-impl-overlap.h
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/rust/typecheck/rust-tyty-cmp.h
gcc/rust/typecheck/rust-tyty.cc
gcc/rust/typecheck/rust-tyty.h
gcc/testsuite/rust/compile/generics7.rs
gcc/testsuite/rust/execute/torture/operator_overload_9.rs