diff options
author | Philip Herron <herron.philip@googlemail.com> | 2024-10-11 17:53:50 +0100 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2025-03-21 12:32:53 +0100 |
commit | 0c0f33bc439a98f9923a48be592bb9a2be540ac3 (patch) | |
tree | 643383c52089cd44082083d7c8069ef1cdee5a4c /gcc/rust | |
parent | 28caa267dc757d80ffe37d638d981d3572c164ae (diff) | |
download | gcc-0c0f33bc439a98f9923a48be592bb9a2be540ac3.zip gcc-0c0f33bc439a98f9923a48be592bb9a2be540ac3.tar.gz gcc-0c0f33bc439a98f9923a48be592bb9a2be540ac3.tar.bz2 |
gccrs: Fix bad recursive operator overload call
When we are typechecking the impl block for DerefMut for &mut T
the implementation follows the usual operator overload check
but this ended up just resolving directly to the Trait definition
which ends up being recursive which we usually handle. The issue
we had is that a dereference call can be for either the DEREF
or DEREF_MUT lang item here it was looking for a recurisve call
to the DEREF lang item but we were in the DEREF_MUT lang item
so this case was not accounted for.
Fixes #3032
gcc/rust/ChangeLog:
* typecheck/rust-hir-trait-reference.h: new get locus helper
* typecheck/rust-hir-trait-resolve.cc (AssociatedImplTrait::get_locus): implemention
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::resolve_operator_overload):
fix overload
gcc/testsuite/ChangeLog:
* rust/compile/nr2/exclude: nr2 cant handle this
* rust/compile/issue-3032-1.rs: New test.
* rust/compile/issue-3032-2.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r-- | gcc/rust/typecheck/rust-hir-trait-reference.h | 2 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-trait-resolve.cc | 6 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-expr.cc | 21 |
3 files changed, 24 insertions, 5 deletions
diff --git a/gcc/rust/typecheck/rust-hir-trait-reference.h b/gcc/rust/typecheck/rust-hir-trait-reference.h index bdfd987..4197033 100644 --- a/gcc/rust/typecheck/rust-hir-trait-reference.h +++ b/gcc/rust/typecheck/rust-hir-trait-reference.h @@ -246,6 +246,8 @@ public: HIR::ImplBlock *get_impl_block (); + location_t get_locus () const; + TyTy::BaseType *get_self (); const TyTy::BaseType *get_self () const; diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc index ec331cf..91842df 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc @@ -669,6 +669,12 @@ AssociatedImplTrait::reset_associated_types () trait->clear_associated_types (); } +location_t +AssociatedImplTrait::get_locus () const +{ + return impl->get_locus (); +} + Analysis::NodeMapping TraitItemReference::get_parent_trait_mappings () const { diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index ba22eaf..dfdf85a 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -1822,10 +1822,20 @@ TypeCheckExpr::resolve_operator_overload (LangItem::Kind lang_item_type, HIR::ImplBlock *parent = impl_item.first; HIR::Function *fn = impl_item.second; - if (parent->has_trait_ref () - && fn->get_function_name ().as_string ().compare ( - associated_item_name) - == 0) + bool is_deref = lang_item_type == LangItem::Kind::DEREF + || lang_item_type == LangItem::Kind::DEREF_MUT; + bool is_deref_match = fn->get_function_name ().as_string ().compare ( + LangItem::ToString (LangItem::Kind::DEREF)) + == 0 + || fn->get_function_name ().as_string ().compare ( + LangItem::ToString (LangItem::Kind::DEREF_MUT)) + == 0; + + bool is_recursive_op + = fn->get_function_name ().as_string ().compare (associated_item_name) + == 0 + || (is_deref && is_deref_match); + if (parent->has_trait_ref () && is_recursive_op) { TraitReference *trait_reference = TraitResolver::Lookup (*parent->get_trait_ref ().get ()); @@ -1842,7 +1852,8 @@ TypeCheckExpr::resolve_operator_overload (LangItem::Kind lang_item_type, bool is_lang_item_impl = trait_reference->get_mappings ().get_defid () - == respective_lang_item_id; + == respective_lang_item_id + || (is_deref && is_deref_match); bool self_is_lang_item_self = fntype->get_self_type ()->is_equal (*adjusted_self); bool recursive_operator_overload |