Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gccrs: Fix bad recursive operator overload call #3214

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gcc/rust/typecheck/rust-hir-trait-reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ class AssociatedImplTrait

HIR::ImplBlock *get_impl_block ();

location_t get_locus () const;

TyTy::BaseType *get_self ();
const TyTy::BaseType *get_self () const;

Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/typecheck/rust-hir-trait-resolve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
21 changes: 16 additions & 5 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
Expand All @@ -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
Expand Down
58 changes: 58 additions & 0 deletions gcc/testsuite/rust/compile/issue-3032-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#![feature(negative_impls)]

#[lang = "sized"]
trait Sized {}

#[lang = "deref"]
pub trait Deref {
/// The resulting type after dereferencing.
#[stable(feature = "rust1", since = "1.0.0")]
// #[rustc_diagnostic_item = "deref_target"]
type Target: ?Sized;

/// Dereferences the value.
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
// #[rustc_diagnostic_item = "deref_method"]
fn deref(&self) -> &Self::Target;
}

impl<T: ?Sized> Deref for &T {
type Target = T;

fn deref(&self) -> &T {
*self
}
}

// this is added because of #3030
extern "C" {
fn never() -> !;
}

impl<T: ?Sized> !DerefMut for &T {
fn deref_mut(&mut self) -> &mut T {
unsafe { never() }
}
}

impl<T: ?Sized> Deref for &mut T {
type Target = T;

fn deref(&self) -> &T {
*self
}
}

#[lang = "deref_mut"]
pub trait DerefMut: Deref {
/// Mutably dereferences the value.
#[stable(feature = "rust1", since = "1.0.0")]
fn deref_mut(&mut self) -> &mut Self::Target;
}

impl<T: ?Sized> DerefMut for &mut T {
fn deref_mut(&mut self) -> &mut T {
*self
}
}
49 changes: 49 additions & 0 deletions gcc/testsuite/rust/compile/issue-3032-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![feature(negative_impls)]

#[lang = "sized"]
trait Sized {}

#[lang = "deref"]
pub trait Deref {
/// The resulting type after dereferencing.
#[stable(feature = "rust1", since = "1.0.0")]
// #[rustc_diagnostic_item = "deref_target"]
type Target: ?Sized;

/// Dereferences the value.
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
// #[rustc_diagnostic_item = "deref_method"]
fn deref(&self) -> &Self::Target;
}

impl<T: ?Sized> Deref for &T {
type Target = T;

fn deref(&self) -> &T {
*self
}
}

impl<T: ?Sized> !DerefMut for &T {}

impl<T: ?Sized> Deref for &mut T {
type Target = T;

fn deref(&self) -> &T {
*self
}
}

#[lang = "deref_mut"]
pub trait DerefMut: Deref {
/// Mutably dereferences the value.
#[stable(feature = "rust1", since = "1.0.0")]
fn deref_mut(&mut self) -> &mut Self::Target;
}

impl<T: ?Sized> DerefMut for &mut T {
fn deref_mut(&mut self) -> &mut T {
*self
}
}
4 changes: 3 additions & 1 deletion gcc/testsuite/rust/compile/nr2/exclude
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,6 @@ issue-3139-3.rs
issue-3036.rs
issue-2951.rs
issue-2203.rs
issue-2499.rs
issue-2499.rs
issue-3032-1.rs
issue-3032-2.rs
Loading