Skip to content

Commit

Permalink
Replace def_site-&-privacy implementation with a stability-based one.
Browse files Browse the repository at this point in the history
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 <m-ou.se@m-ou.se>
  • Loading branch information
danielhenrymantilla and m-ou-se committed Feb 14, 2022
1 parent 54e443d commit 6df63cc
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions library/core/src/pin.rs
Expand Up @@ -406,7 +406,9 @@ use crate::ops::{CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Receiver};
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct Pin<P> {
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
Expand Down Expand Up @@ -1074,6 +1076,8 @@ impl<P, U> DispatchFromDyn<Pin<U>> for Pin<P> where P: DispatchFromDyn<U> {}
///
/// [`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):
Expand Down Expand Up @@ -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 } }
}

0 comments on commit 6df63cc

Please sign in to comment.