about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDaniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>2022-01-22 21:07:00 +0100
committerDaniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>2022-02-14 17:27:37 +0100
commit6df63cc148d58fb87797f3dc1fc201b789e7fb0d (patch)
treed5f4cab58d764a0c95f276bbd0c0623d8e79f069
parent54e443dceb8d7eb11553fe19ea1fcaf5eedc9004 (diff)
downloadrust-6df63cc148d58fb87797f3dc1fc201b789e7fb0d.tar.gz
rust-6df63cc148d58fb87797f3dc1fc201b789e7fb0d.zip
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 <m-ou.se@m-ou.se>
-rw-r--r--library/core/src/pin.rs11
1 files changed, 6 insertions, 5 deletions
diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs
index 90f4a5390e7..84fed4ed243 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<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
@@ -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):
@@ -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 } }
 }