]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix bad recursive operator overload call
authorPhilip Herron <herron.philip@googlemail.com>
Fri, 11 Oct 2024 16:53:50 +0000 (17:53 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 21 Mar 2025 11:32:53 +0000 (12:32 +0100)
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>
gcc/rust/typecheck/rust-hir-trait-reference.h
gcc/rust/typecheck/rust-hir-trait-resolve.cc
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/testsuite/rust/compile/issue-3032-1.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/issue-3032-2.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/nr2/exclude

index bdfd9879111d9b1d7f33b2c9ff3c792fc48f686d..419703353752940364163a2e46e0851a392a4e61 100644 (file)
@@ -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;
 
index ec331cf6e95c70d461778f51629c772f2a05d99d..91842df85b22ca8bddfa47c2bcda80f19d7ffd35 100644 (file)
@@ -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
 {
index ba22eaff44105e0baa09e473407ffa75ec7a3c80..dfdf85a141dbdc10645bf75991226839da9c4662 100644 (file)
@@ -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
diff --git a/gcc/testsuite/rust/compile/issue-3032-1.rs b/gcc/testsuite/rust/compile/issue-3032-1.rs
new file mode 100644 (file)
index 0000000..e9eb027
--- /dev/null
@@ -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
+    }
+}
diff --git a/gcc/testsuite/rust/compile/issue-3032-2.rs b/gcc/testsuite/rust/compile/issue-3032-2.rs
new file mode 100644 (file)
index 0000000..9e09d41
--- /dev/null
@@ -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
+    }
+}
index ecef6d2bb25946542cf1b421a54b685a68bff193..383950ca86357cce184eab7a0fa623e24a65cee8 100644 (file)
@@ -234,4 +234,6 @@ issue-3139-3.rs
 issue-3036.rs
 issue-2951.rs
 issue-2203.rs
-issue-2499.rs
\ No newline at end of file
+issue-2499.rs
+issue-3032-1.rs
+issue-3032-2.rs
\ No newline at end of file