From: Arthur Cohen <[email protected]>

This error is triggering on valid code from `core 1.49`, so turn it into a 
warning for now
and add extra info for when the fix happens.

gcc/rust/ChangeLog:

        * typecheck/rust-hir-trait-reference.cc 
(TraitReference::is_object_safe): Turn the error
        into a warning.

gcc/testsuite/ChangeLog:

        * rust/compile/traits10.rs: Change error into a warning.
        * rust/compile/traits11.rs: Likewise.
        * rust/compile/invalid-non-object-safe-warning.rs: New test.
        * rust/core/core-1.49.expected: Regenerate
---
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/b635f58370c2e130ad52f1b724ab38cdfc95825d

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/5106

 .../typecheck/rust-hir-trait-reference.cc     | 15 ++++++-
 .../invalid-non-object-safe-warning.rs        | 39 +++++++++++++++++++
 gcc/testsuite/rust/compile/traits10.rs        |  4 +-
 gcc/testsuite/rust/compile/traits11.rs        |  4 +-
 gcc/testsuite/rust/core/core-1.49.expected    |  1 -
 5 files changed, 57 insertions(+), 6 deletions(-)
 create mode 100644 
gcc/testsuite/rust/compile/invalid-non-object-safe-warning.rs

diff --git a/gcc/rust/typecheck/rust-hir-trait-reference.cc 
b/gcc/rust/typecheck/rust-hir-trait-reference.cc
index b59c63554..5d5b2a690 100644
--- a/gcc/rust/typecheck/rust-hir-trait-reference.cc
+++ b/gcc/rust/typecheck/rust-hir-trait-reference.cc
@@ -418,7 +418,20 @@ TraitReference::is_object_safe (bool emit_error, 
location_t locus) const
       for (auto &item : non_object_safe_items)
        r.add_range (item->get_locus ());
 
-      rust_error_at (r, "trait bound is not object safe");
+      // FIXME: This should be an error, but this triggers on valid code like
+      // the implementation of `Hash` for `Pin<P>`. For now, the rich_location
+      // is unused since `rust_warning_at` cannot handle rich locations - when
+      // you turn this back into an error, switch from
+      // `rust_warning_at(r.get_loc(), ...)` to `rust_error_at(r, ...)`
+      //
+      // ```rust
+      // impl<P: Deref<Target: Hash>> Hash for Pin<P> {
+      //     fn hash<H: Hasher>(&self, state: &mut H) {
+      //         P::Target::hash(self, state);
+      //     }
+      // }
+      // ```
+      rust_warning_at (r.get_loc (), 0, "trait bound is not object safe");
     }
 
   return is_safe;
diff --git a/gcc/testsuite/rust/compile/invalid-non-object-safe-warning.rs 
b/gcc/testsuite/rust/compile/invalid-non-object-safe-warning.rs
new file mode 100644
index 000000000..53cc351af
--- /dev/null
+++ b/gcc/testsuite/rust/compile/invalid-non-object-safe-warning.rs
@@ -0,0 +1,39 @@
+#![feature(no_core, lang_items)]
+#![no_core]
+
+#[lang = "sized"]
+trait Sized {}
+
+#[lang = "deref"]
+pub trait Deref {
+    type Target: ?Sized;
+
+    fn deref(&self) -> &Self::Target;
+}
+
+pub trait Hash {
+    fn hash<H: Hasher>(&self, state: &mut H);
+
+    // FWIW, this is the function that creates the invalid object-safe error 
on `Hash for Pin<P>`
+    fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)
+    where
+        Self: Sized,
+    {}
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait Hasher {}
+
+#[stable(feature = "pin", since = "1.33.0")]
+#[lang = "pin"]
+#[fundamental]
+#[repr(transparent)]
+pub struct Pin<P> {
+    _pointer: P,
+}
+
+impl<P: Deref<Target: Hash>> Hash for Pin<P> { // { dg-warning "not object 
safe" }
+    fn hash<H: Hasher>(&self, state: &mut H) {
+        P::Target::hash(self, state);
+    }
+}
diff --git a/gcc/testsuite/rust/compile/traits10.rs 
b/gcc/testsuite/rust/compile/traits10.rs
index 5531dcd86..7853ad330 100644
--- a/gcc/testsuite/rust/compile/traits10.rs
+++ b/gcc/testsuite/rust/compile/traits10.rs
@@ -17,6 +17,6 @@ pub fn main() {
     let a;
     a = Foo(123);
 
-    let b: &dyn Bar = &a;
-    // { dg-error "trait bound is not object safe" "" { target *-*-* } .-1 }
+    let _: &dyn Bar = &a;
+    // { dg-warning "trait bound is not object safe" "" { target *-*-* } .-1 }
 }
diff --git a/gcc/testsuite/rust/compile/traits11.rs 
b/gcc/testsuite/rust/compile/traits11.rs
index c8cfc73bc..f248fae88 100644
--- a/gcc/testsuite/rust/compile/traits11.rs
+++ b/gcc/testsuite/rust/compile/traits11.rs
@@ -21,6 +21,6 @@ pub fn main() {
     let a;
     a = Foo(123);
 
-    let b: &dyn B = &a;
-    // { dg-error "trait bound is not object safe" "" { target *-*-* } .-1 }
+    let _: &dyn B = &a;
+    // { dg-warning "trait bound is not object safe" "" { target *-*-* } .-1 }
 }
diff --git a/gcc/testsuite/rust/core/core-1.49.expected 
b/gcc/testsuite/rust/core/core-1.49.expected
index 1f70b67ec..3ae4d6a89 100644
--- a/gcc/testsuite/rust/core/core-1.49.expected
+++ b/gcc/testsuite/rust/core/core-1.49.expected
@@ -6,5 +6,4 @@ core/src/num/wrapping.rs:145:55: error: cannot find value 
'self::shift_max::i32'
 core/src/num/wrapping.rs:145:55: error: cannot find value 
'self::shift_max::i64' in this scope [E0425]
 core/src/num/wrapping.rs:145:55: error: cannot find value 
'self::shift_max::i128' in this scope [E0425]
 core/src/num/wrapping.rs:983:32: error: cannot find value 'super::i64' in this 
scope [E0425]
-core/src/pin.rs:462:23: error: trait bound is not object safe
 crab1: internal compiler error: in add_constrints_from_param, at 
rust/typecheck/rust-tyty-variance-analysis.cc:497

base-commit: 7ca6e9af90ed332b9cc446c0e868f6875470a0eb
-- 
2.56.1

Reply via email to