diff options
| author | Gary Guo <gary@garyguo.net> | 2026-04-28 14:10:59 +0100 |
|---|---|---|
| committer | Gary Guo <gary@garyguo.net> | 2026-05-10 22:58:34 +0100 |
| commit | e5cece935531997cd9903c129cc9a2471ed05a4b (patch) | |
| tree | 9627dca00742f9bffc1b95b166c7115b35568f3b /rust | |
| parent | 3dc01266cdf02b7abf733b022f6b936be567fc17 (diff) | |
| download | linux-next-history-e5cece935531997cd9903c129cc9a2471ed05a4b.tar.gz | |
rust: pin-init: internal: turn `PhantomPinned` error into warnings
The `PhantomPinned` detection is just a lint, and is emitted as an error
because there is no `compile_warning!()` macro, and
`proc-macro-diagnostics` is not stable.
Use of `#[deprecated = ""]` attribute to approximate custom proc-macro
warnings. A new line is added before message for visual clarity.
An example warning with this trick looks like this:
warning: use of deprecated function `_::warn`:
The field `pin` of type `PhantomPinned` only has an effect if it has the `#[pin]` attribute
--> test.rs:9:5
|
9 | pin: marker::PhantomPinned,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Suggested-by: Benno Lossin <lossin@kernel.org>
Link: https://github.com/Rust-for-Linux/pin-init/issues/51
Link: https://patch.msgid.link/20260428-pin-init-sync-v1-10-07f9bd3859fb@garyguo.net
Signed-off-by: Gary Guo <gary@garyguo.net>
Diffstat (limited to 'rust')
| -rw-r--r-- | rust/pin-init/internal/src/diagnostics.rs | 14 | ||||
| -rw-r--r-- | rust/pin-init/internal/src/pin_data.rs | 2 |
2 files changed, 15 insertions, 1 deletions
diff --git a/rust/pin-init/internal/src/diagnostics.rs b/rust/pin-init/internal/src/diagnostics.rs index 3bdb477c2f2b8..c7d9b3e624fc8 100644 --- a/rust/pin-init/internal/src/diagnostics.rs +++ b/rust/pin-init/internal/src/diagnostics.rs @@ -3,6 +3,7 @@ use std::fmt::Display; use proc_macro2::TokenStream; +use quote::quote_spanned; use syn::{spanned::Spanned, Error}; pub(crate) struct DiagCtxt(TokenStream); @@ -15,6 +16,19 @@ impl DiagCtxt { ErrorGuaranteed(()) } + pub(crate) fn warn(&mut self, span: impl Spanned, msg: impl Display) { + // Have the message start on a new line for visual clarity. + let msg = format!("\n{}", msg); + self.0.extend(quote_spanned!(span.span() => + // Approximate using deprecated warning while `proc_macro_diagnostic` is unstable. + const _: () = { + #[deprecated = #msg] + const fn warn() {} + warn(); + }; + )); + } + pub(crate) fn with( fun: impl FnOnce(&mut DiagCtxt) -> Result<TokenStream, ErrorGuaranteed>, ) -> TokenStream { diff --git a/rust/pin-init/internal/src/pin_data.rs b/rust/pin-init/internal/src/pin_data.rs index 76cd11bf28ebb..163a31ed1556e 100644 --- a/rust/pin-init/internal/src/pin_data.rs +++ b/rust/pin-init/internal/src/pin_data.rs @@ -85,7 +85,7 @@ pub(crate) fn pin_data( for (pinned, field) in &fields { if !pinned && is_phantom_pinned(&field.ty) { - dcx.error( + dcx.warn( field, format!( "The field `{}` of type `PhantomPinned` only has an effect \ |
