From 6df63cc148d58fb87797f3dc1fc201b789e7fb0d Mon Sep 17 00:00:00 2001 From: Daniel Henry-Mantilla Date: Sat, 22 Jan 2022 21:07:00 +0100 Subject: [PATCH] Replace `def_site`-&-privacy implementation with a stability-based one. Since `decl_macro`s and/or `Span::def_site()` is deemed quite unstable, no public-facing macro that relies on it can hope to be, itself, stabilized. We circumvent the issue by no longer relying on field privacy for safety and, instead, relying on an unstable feature-gate to act as the gate keeper for non users of the macro (thanks to `allow_internal_unstable`). This is technically not correct (since a `nightly` user could technically enable the feature and cause unsoundness with it); or, in other words, this makes the feature-gate used to gate the access to the field be (technically unsound, and in practice) `unsafe`. Hence it having `unsafe` in its name. Back to the macro, we go back to `macro_rules!` / `mixed_site()`-span rules thanks to declaring the `decl_macro` as `semitransparent`, which is a hack to basically have `pub macro_rules!` Co-Authored-By: Mara Bos --- library/core/src/pin.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 90f4a5390e76..84fed4ed2436 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -406,7 +406,9 @@ use crate::ops::{CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Receiver}; #[repr(transparent)] #[derive(Copy, Clone)] pub struct Pin

{ - pointer: P, + #[unstable(feature = "unsafe_pin_internals", issue = "none")] + #[doc(hidden)] + pub pointer: P, } // The following implementations aren't derived in order to avoid soundness @@ -1074,6 +1076,8 @@ impl DispatchFromDyn> for Pin

where P: DispatchFromDyn {} /// /// [`Box::pin`]: ../../std/boxed/struct.Box.html#method.pin #[unstable(feature = "pin_macro", issue = "93178")] +#[rustc_macro_transparency = "semitransparent"] +#[allow_internal_unstable(unsafe_pin_internals)] pub macro pin($value:expr $(,)?) { // This is `Pin::new_unchecked(&mut { $value })`, so, for starters, let's // review such a hypothetical macro (that any user-code could define): @@ -1145,8 +1149,5 @@ pub macro pin($value:expr $(,)?) { // // See https://doc.rust-lang.org/1.58.1/reference/destructors.html#temporary-lifetime-extension // for more info. - // - // Finally, we don't hit problems _w.r.t._ the privacy of the `pointer` field, or the - // unqualified `Pin` name, thanks to `decl_macro`s being _fully_ hygienic (`def_site` hygiene). - Pin::<&mut _> { pointer: &mut { $value } } + $crate::pin::Pin::<&mut _> { pointer: &mut { $value } } }