From 4499f8b3e767e2ebfcc7a9f1141ed7705b92cab7 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 22 Nov 2025 00:53:53 +0000 Subject: [PATCH] Suppress "consider borrowing here" diagnostic on Rust 1.85+ This change applies `#[diagnostic::do_not_recommend]` to the `Immutable` implementations for `&T` and `&mut T`. This suppresses the compiler's unhelpful "consider borrowing here" recommendation when a type does not implement `Immutable`. The attribute is gated on Rust 1.85.0+, which stabilized this diagnostic attribute. Closes #1296. --- Cargo.toml | 3 +++ src/impls.rs | 10 ++++++++-- .../diagnostic-not-implemented-immutable.stderr | 4 ++-- .../diagnostic-not-implemented-issue-1296.rs | 8 ++++---- tests/ui-nightly/transmute-ref-dst-not-nocell.stderr | 4 ++-- tests/ui-nightly/transmute-ref-src-not-nocell.stderr | 8 ++++---- ...transmute_ref-dst-not-immutable-tryfrombytes.stderr | 4 ++-- ...ry_transmute_ref-src-not-immutable-intobytes.stderr | 4 ++-- .../diagnostic-not-implemented-immutable.stderr | 4 ++-- tests/ui-stable/transmute-ref-dst-not-nocell.stderr | 4 ++-- tests/ui-stable/transmute-ref-src-not-nocell.stderr | 8 ++++---- ...transmute_ref-dst-not-immutable-tryfrombytes.stderr | 4 ++-- ...ry_transmute_ref-src-not-immutable-intobytes.stderr | 4 ++-- zerocopy-derive/tests/ui-nightly/enum.stderr | 8 ++++---- zerocopy-derive/tests/ui-nightly/struct.stderr | 8 ++++---- zerocopy-derive/tests/ui-nightly/union.stderr | 4 ++-- zerocopy-derive/tests/ui-stable/enum.stderr | 8 ++++---- zerocopy-derive/tests/ui-stable/struct.stderr | 8 ++++---- zerocopy-derive/tests/ui-stable/union.stderr | 4 ++-- 19 files changed, 59 insertions(+), 50 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f72003f4bc..74510b98b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,9 @@ exclude = [".*"] # `stdarch_x86_avx512` feature). zerocopy-simd-x86-avx12-1-89-0 = "1.89.0" +# From 1.85.0, Rust supports the `#[diagnostic::do_not_recommend]` attribute. +zerocopy-diagnostic-do-not-recommend-1-85-0 = "1.85.0" + # From 1.81.0, Rust supports the `core::error::Error` trait. zerocopy-core-error-1-81-0 = "1.81.0" diff --git a/src/impls.rs b/src/impls.rs index 322b385672..6aa1f1653d 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -947,8 +947,14 @@ const _: () = unsafe { unsafe_impl!(T: ?Sized => Immutable for NonNull) }; // SAFETY: Reference types do not contain any `UnsafeCell`s. #[allow(clippy::multiple_unsafe_ops_per_block)] const _: () = unsafe { - unsafe_impl!(T: ?Sized => Immutable for &'_ T); - unsafe_impl!(T: ?Sized => Immutable for &'_ mut T); + unsafe_impl!( + #[cfg_attr(zerocopy_diagnostic_do_not_recommend_1_85_0, diagnostic::do_not_recommend)] + T: ?Sized => Immutable for &'_ T + ); + unsafe_impl!( + #[cfg_attr(zerocopy_diagnostic_do_not_recommend_1_85_0, diagnostic::do_not_recommend)] + T: ?Sized => Immutable for &'_ mut T + ); }; // SAFETY: `Option` is not `#[non_exhaustive]` [1], which means that the types diff --git a/tests/ui-nightly/diagnostic-not-implemented-immutable.stderr b/tests/ui-nightly/diagnostic-not-implemented-immutable.stderr index 6a7dfb9b82..80db7c070b 100644 --- a/tests/ui-nightly/diagnostic-not-implemented-immutable.stderr +++ b/tests/ui-nightly/diagnostic-not-implemented-immutable.stderr @@ -11,14 +11,14 @@ help: the trait `zerocopy::Immutable` is not implemented for `NotZerocopy` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: Consider adding `#[derive(Immutable)]` to `NotZerocopy` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 Box F32 + F64 + I128 and $N others note: required by a bound in `takes_immutable` --> tests/ui-nightly/diagnostic-not-implemented-immutable.rs:21:23 diff --git a/tests/ui-nightly/diagnostic-not-implemented-issue-1296.rs b/tests/ui-nightly/diagnostic-not-implemented-issue-1296.rs index e12737a6a3..b001c64dd5 100644 --- a/tests/ui-nightly/diagnostic-not-implemented-issue-1296.rs +++ b/tests/ui-nightly/diagnostic-not-implemented-issue-1296.rs @@ -45,10 +45,10 @@ fn main() { // Taking the compiler's suggestion results in a different error with a // recommendation to remove the reference (back to the original code). // - // As of this writing, the described problem is still happening thanks to - // https://github.com/rust-lang/rust/issues/130563. We include this test so - // that we can capture the current behavior, but we will update it once that - // Rust issue is fixed. + // This problem was mitigated in Rust 1.85.0 with the stabilization of + // `#[diagnostic::do_not_recommend]`. We apply this attribute to the + // `Immutable` impls for `&T` and `&mut T` on supported versions, which + // suppresses the "consider borrowing here" recommendation. Foo.write_obj(NotZerocopy(())); } diff --git a/tests/ui-nightly/transmute-ref-dst-not-nocell.stderr b/tests/ui-nightly/transmute-ref-dst-not-nocell.stderr index 62bec102fb..5c448f5466 100644 --- a/tests/ui-nightly/transmute-ref-dst-not-nocell.stderr +++ b/tests/ui-nightly/transmute-ref-dst-not-nocell.stderr @@ -14,14 +14,14 @@ help: the trait `zerocopy::Immutable` is not implemented for `Dst` | ^^^^^^^^^^ = note: Consider adding `#[derive(Immutable)]` to `Dst` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 Box F32 + F64 + I128 and $N others note: required by a bound in `AssertDstIsImmutable` --> tests/ui-nightly/transmute-ref-dst-not-nocell.rs:23:33 diff --git a/tests/ui-nightly/transmute-ref-src-not-nocell.stderr b/tests/ui-nightly/transmute-ref-src-not-nocell.stderr index 33240d5ae5..e820f95df2 100644 --- a/tests/ui-nightly/transmute-ref-src-not-nocell.stderr +++ b/tests/ui-nightly/transmute-ref-src-not-nocell.stderr @@ -14,14 +14,14 @@ help: the trait `zerocopy::Immutable` is not implemented for `Src` | ^^^^^^^^^^ = note: Consider adding `#[derive(Immutable)]` to `Src` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 Box F32 + F64 + I128 and $N others note: required by a bound in `AssertSrcIsImmutable` --> tests/ui-nightly/transmute-ref-src-not-nocell.rs:23:34 @@ -43,14 +43,14 @@ help: the trait `zerocopy::Immutable` is not implemented for `Src` | ^^^^^^^^^^ = note: Consider adding `#[derive(Immutable)]` to `Src` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 Box F32 + F64 + I128 and $N others note: required by a bound in `AssertSrcIsImmutable` --> tests/ui-nightly/transmute-ref-src-not-nocell.rs:23:34 diff --git a/tests/ui-nightly/try_transmute_ref-dst-not-immutable-tryfrombytes.stderr b/tests/ui-nightly/try_transmute_ref-dst-not-immutable-tryfrombytes.stderr index 9454a1c852..90721f3b96 100644 --- a/tests/ui-nightly/try_transmute_ref-dst-not-immutable-tryfrombytes.stderr +++ b/tests/ui-nightly/try_transmute_ref-dst-not-immutable-tryfrombytes.stderr @@ -71,14 +71,14 @@ help: the trait `zerocopy::Immutable` is not implemented for `NotZerocopy` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: Consider adding `#[derive(Immutable)]` to `NotZerocopy` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 Box F32 + F64 + I128 and $N others note: required by a bound in `try_transmute_ref` --> src/util/macro_util.rs diff --git a/tests/ui-nightly/try_transmute_ref-src-not-immutable-intobytes.stderr b/tests/ui-nightly/try_transmute_ref-src-not-immutable-intobytes.stderr index 3c74c3817b..1ef8acd479 100644 --- a/tests/ui-nightly/try_transmute_ref-src-not-immutable-intobytes.stderr +++ b/tests/ui-nightly/try_transmute_ref-src-not-immutable-intobytes.stderr @@ -43,14 +43,14 @@ help: the trait `zerocopy::Immutable` is not implemented for `NotZerocopy` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: Consider adding `#[derive(Immutable)]` to `NotZerocopy` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 Box F32 + F64 + I128 and $N others note: required by a bound in `try_transmute_ref` --> src/util/macro_util.rs diff --git a/tests/ui-stable/diagnostic-not-implemented-immutable.stderr b/tests/ui-stable/diagnostic-not-implemented-immutable.stderr index 47c4e83982..2ac3520c2f 100644 --- a/tests/ui-stable/diagnostic-not-implemented-immutable.stderr +++ b/tests/ui-stable/diagnostic-not-implemented-immutable.stderr @@ -11,14 +11,14 @@ help: the trait `zerocopy::Immutable` is not implemented for `NotZerocopy` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: Consider adding `#[derive(Immutable)]` to `NotZerocopy` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 Box F32 + F64 + I128 and $N others note: required by a bound in `takes_immutable` --> tests/ui-stable/diagnostic-not-implemented-immutable.rs:21:23 diff --git a/tests/ui-stable/transmute-ref-dst-not-nocell.stderr b/tests/ui-stable/transmute-ref-dst-not-nocell.stderr index 3f7edc29cd..5a1d67d933 100644 --- a/tests/ui-stable/transmute-ref-dst-not-nocell.stderr +++ b/tests/ui-stable/transmute-ref-dst-not-nocell.stderr @@ -14,14 +14,14 @@ help: the trait `zerocopy::Immutable` is not implemented for `Dst` | ^^^^^^^^^^ = note: Consider adding `#[derive(Immutable)]` to `Dst` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 Box F32 + F64 + I128 and $N others note: required by a bound in `AssertDstIsImmutable` --> tests/ui-stable/transmute-ref-dst-not-nocell.rs:23:33 diff --git a/tests/ui-stable/transmute-ref-src-not-nocell.stderr b/tests/ui-stable/transmute-ref-src-not-nocell.stderr index 46bf46ae07..8b1f3dbf0c 100644 --- a/tests/ui-stable/transmute-ref-src-not-nocell.stderr +++ b/tests/ui-stable/transmute-ref-src-not-nocell.stderr @@ -14,14 +14,14 @@ help: the trait `zerocopy::Immutable` is not implemented for `Src` | ^^^^^^^^^^ = note: Consider adding `#[derive(Immutable)]` to `Src` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 Box F32 + F64 + I128 and $N others note: required by a bound in `AssertSrcIsImmutable` --> tests/ui-stable/transmute-ref-src-not-nocell.rs:23:34 @@ -43,14 +43,14 @@ help: the trait `zerocopy::Immutable` is not implemented for `Src` | ^^^^^^^^^^ = note: Consider adding `#[derive(Immutable)]` to `Src` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 Box F32 + F64 + I128 and $N others note: required by a bound in `AssertSrcIsImmutable` --> tests/ui-stable/transmute-ref-src-not-nocell.rs:23:34 diff --git a/tests/ui-stable/try_transmute_ref-dst-not-immutable-tryfrombytes.stderr b/tests/ui-stable/try_transmute_ref-dst-not-immutable-tryfrombytes.stderr index 0a004a6081..c87ee37ac7 100644 --- a/tests/ui-stable/try_transmute_ref-dst-not-immutable-tryfrombytes.stderr +++ b/tests/ui-stable/try_transmute_ref-dst-not-immutable-tryfrombytes.stderr @@ -71,14 +71,14 @@ help: the trait `zerocopy::Immutable` is not implemented for `NotZerocopy` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: Consider adding `#[derive(Immutable)]` to `NotZerocopy` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 Box F32 + F64 + I128 and $N others note: required by a bound in `try_transmute_ref` --> src/util/macro_util.rs diff --git a/tests/ui-stable/try_transmute_ref-src-not-immutable-intobytes.stderr b/tests/ui-stable/try_transmute_ref-src-not-immutable-intobytes.stderr index fcd3836c40..7d2eb4a3f2 100644 --- a/tests/ui-stable/try_transmute_ref-src-not-immutable-intobytes.stderr +++ b/tests/ui-stable/try_transmute_ref-src-not-immutable-intobytes.stderr @@ -43,14 +43,14 @@ help: the trait `zerocopy::Immutable` is not implemented for `NotZerocopy` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: Consider adding `#[derive(Immutable)]` to `NotZerocopy` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 Box F32 + F64 + I128 and $N others note: required by a bound in `try_transmute_ref` --> src/util/macro_util.rs diff --git a/zerocopy-derive/tests/ui-nightly/enum.stderr b/zerocopy-derive/tests/ui-nightly/enum.stderr index b8ec9a1695..e661fc5b57 100644 --- a/zerocopy-derive/tests/ui-nightly/enum.stderr +++ b/zerocopy-derive/tests/ui-nightly/enum.stderr @@ -307,14 +307,14 @@ error[E0277]: the trait bound `UnsafeCell<()>: Immutable` is not satisfied | = note: Consider adding `#[derive(Immutable)]` to `UnsafeCell<()>` = help: the following other types implement trait `Immutable`: - &T - &mut T () *const T *mut T F32 F64 I128 + I16 + I32 and $N others = help: see issue #48214 = note: this error originates in the derive macro `Immutable` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -331,14 +331,14 @@ error[E0277]: the trait bound `UnsafeCell: Immutable` is not satisfied | = note: Consider adding `#[derive(Immutable)]` to `UnsafeCell` = help: the following other types implement trait `Immutable`: - &T - &mut T () *const T *mut T F32 F64 I128 + I16 + I32 and $N others = help: see issue #48214 = note: this error originates in the derive macro `Immutable` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/zerocopy-derive/tests/ui-nightly/struct.stderr b/zerocopy-derive/tests/ui-nightly/struct.stderr index e1eb1fd12f..82fe80100c 100644 --- a/zerocopy-derive/tests/ui-nightly/struct.stderr +++ b/zerocopy-derive/tests/ui-nightly/struct.stderr @@ -193,14 +193,14 @@ error[E0277]: the trait bound `UnsafeCell<()>: zerocopy::Immutable` is not satis | = note: Consider adding `#[derive(Immutable)]` to `UnsafeCell<()>` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 F32 F64 + I128 + I16 and $N others = help: see issue #48214 = note: this error originates in the derive macro `Immutable` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -217,14 +217,14 @@ error[E0277]: the trait bound `UnsafeCell: zerocopy::Immutable` is not satis | = note: Consider adding `#[derive(Immutable)]` to `UnsafeCell` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 F32 F64 + I128 + I16 and $N others = note: required for `[UnsafeCell; 0]` to implement `zerocopy::Immutable` = help: see issue #48214 diff --git a/zerocopy-derive/tests/ui-nightly/union.stderr b/zerocopy-derive/tests/ui-nightly/union.stderr index 61dde476c4..b5a6901c79 100644 --- a/zerocopy-derive/tests/ui-nightly/union.stderr +++ b/zerocopy-derive/tests/ui-nightly/union.stderr @@ -84,14 +84,14 @@ error[E0277]: the trait bound `UnsafeCell<()>: zerocopy::Immutable` is not satis | = note: Consider adding `#[derive(Immutable)]` to `UnsafeCell<()>` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 F32 F64 + I128 + I16 and $N others = note: required for `ManuallyDrop>` to implement `zerocopy::Immutable` = help: see issue #48214 diff --git a/zerocopy-derive/tests/ui-stable/enum.stderr b/zerocopy-derive/tests/ui-stable/enum.stderr index b1cd667675..65fe981896 100644 --- a/zerocopy-derive/tests/ui-stable/enum.stderr +++ b/zerocopy-derive/tests/ui-stable/enum.stderr @@ -306,14 +306,14 @@ error[E0277]: the trait bound `UnsafeCell<()>: Immutable` is not satisfied | = note: Consider adding `#[derive(Immutable)]` to `UnsafeCell<()>` = help: the following other types implement trait `Immutable`: - &T - &mut T () *const T *mut T F32 F64 I128 + I16 + I32 and $N others = help: see issue #48214 = note: this error originates in the derive macro `Immutable` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -326,14 +326,14 @@ error[E0277]: the trait bound `UnsafeCell: Immutable` is not satisfied | = note: Consider adding `#[derive(Immutable)]` to `UnsafeCell` = help: the following other types implement trait `Immutable`: - &T - &mut T () *const T *mut T F32 F64 I128 + I16 + I32 and $N others = help: see issue #48214 = note: this error originates in the derive macro `Immutable` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/zerocopy-derive/tests/ui-stable/struct.stderr b/zerocopy-derive/tests/ui-stable/struct.stderr index e2354dd4c3..0bff4d2de2 100644 --- a/zerocopy-derive/tests/ui-stable/struct.stderr +++ b/zerocopy-derive/tests/ui-stable/struct.stderr @@ -174,14 +174,14 @@ error[E0277]: the trait bound `UnsafeCell<()>: zerocopy::Immutable` is not satis | = note: Consider adding `#[derive(Immutable)]` to `UnsafeCell<()>` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 F32 F64 + I128 + I16 and $N others = help: see issue #48214 = note: this error originates in the derive macro `Immutable` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -194,14 +194,14 @@ error[E0277]: the trait bound `UnsafeCell: zerocopy::Immutable` is not satis | = note: Consider adding `#[derive(Immutable)]` to `UnsafeCell` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 F32 F64 + I128 + I16 and $N others = note: required for `[UnsafeCell; 0]` to implement `zerocopy::Immutable` = help: see issue #48214 diff --git a/zerocopy-derive/tests/ui-stable/union.stderr b/zerocopy-derive/tests/ui-stable/union.stderr index c17306eadc..b53c2b207a 100644 --- a/zerocopy-derive/tests/ui-stable/union.stderr +++ b/zerocopy-derive/tests/ui-stable/union.stderr @@ -84,14 +84,14 @@ error[E0277]: the trait bound `UnsafeCell<()>: zerocopy::Immutable` is not satis | = note: Consider adding `#[derive(Immutable)]` to `UnsafeCell<()>` = help: the following other types implement trait `zerocopy::Immutable`: - &T - &mut T () *const T *mut T AU16 F32 F64 + I128 + I16 and $N others = note: required for `ManuallyDrop>` to implement `zerocopy::Immutable` = help: see issue #48214