Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an attribute that makes the spans from a macro edition 2021, and fix pin on edition 2024 with it #138717

Merged
merged 4 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_attr_data_structures/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ pub enum AttributeKind {
},
MacroTransparency(Transparency),
Repr(ThinVec<(ReprAttr, Span)>),
RustcMacroEdition2021,
Stability {
stability: Stability,
/// Span of the `#[stable(...)]` or `#[unstable(...)]` attribute
Expand Down
11 changes: 4 additions & 7 deletions compiler/rustc_attr_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,18 @@ macro_rules! find_attr {
}};

($attributes_list: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => {{
fn check_attribute_iterator<'a>(_: &'_ impl IntoIterator<Item = &'a rustc_hir::Attribute>) {}
check_attribute_iterator(&$attributes_list);

let find_attribute = |iter| {
'done: {
for i in $attributes_list {
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed($pattern) $(if $guard)? => {
return Some($e);
break 'done Some($e);
}
_ => {}
}
}

None
};
find_attribute($attributes_list)
}
}};
}
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) mod cfg;
pub(crate) mod confusables;
pub(crate) mod deprecation;
pub(crate) mod repr;
pub(crate) mod rustc;
pub(crate) mod stability;
pub(crate) mod transparency;
pub(crate) mod util;
Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/rustc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use rustc_attr_data_structures::AttributeKind;
use rustc_span::sym;

use super::{AcceptContext, SingleAttributeParser};
use crate::parser::ArgParser;

pub(crate) struct RustcMacroEdition2021Parser;

// FIXME(jdonszelmann): make these proper diagnostics
impl SingleAttributeParser for RustcMacroEdition2021Parser {
const PATH: &'static [rustc_span::Symbol] = &[sym::rustc_macro_edition_2021];

fn on_duplicate(_cx: &crate::context::AcceptContext<'_>, _first_span: rustc_span::Span) {}

fn convert(_cx: &AcceptContext<'_>, args: &ArgParser<'_>) -> Option<AttributeKind> {
assert!(args.no_args());
Some(AttributeKind::RustcMacroEdition2021)
}
}
2 changes: 2 additions & 0 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInterna
use crate::attributes::confusables::ConfusablesParser;
use crate::attributes::deprecation::DeprecationParser;
use crate::attributes::repr::ReprParser;
use crate::attributes::rustc::RustcMacroEdition2021Parser;
use crate::attributes::stability::{
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
};
Expand Down Expand Up @@ -76,6 +77,7 @@ attribute_groups!(
// tidy-alphabetical-start
Single<ConstStabilityIndirectParser>,
Single<DeprecationParser>,
Single<RustcMacroEdition2021Parser>,
Single<TransparencyParser>,
// tidy-alphabetical-end
];
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,14 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
"`rustc_never_type_options` is used to experiment with never type fallback and work on \
never type stabilization, and will never be stable"
),
rustc_attr!(
rustc_macro_edition_2021,
Normal,
template!(Word),
ErrorFollowing,
EncodeCrossCrate::No,
"makes spans in this macro edition 2021"
),

// ==========================================================================
// Internal attributes: Runtime related:
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::Arc;
use rustc_ast::expand::StrippedCfgItem;
use rustc_ast::{self as ast, Crate, NodeId, attr};
use rustc_ast_pretty::pprust;
use rustc_attr_parsing::StabilityLevel;
use rustc_attr_parsing::{AttributeKind, StabilityLevel, find_attr};
use rustc_data_structures::intern::Interned;
use rustc_errors::{Applicability, StashKey};
use rustc_expand::base::{
Expand Down Expand Up @@ -1125,6 +1125,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
edition,
);

// The #[rustc_macro_edition_2021] attribute is used by the pin!() macro
// as a temporary workaround for a regression in expressiveness in Rust 2024.
// See https://github.com/rust-lang/rust/issues/138718.
if find_attr!(attrs.iter(), AttributeKind::RustcMacroEdition2021) {
ext.edition = Edition::Edition2021;
}

if let Some(builtin_name) = ext.builtin_name {
// The macro was marked with `#[rustc_builtin_macro]`.
if let Some(builtin_macro) = self.builtin_macros.get_mut(&builtin_name) {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,7 @@ symbols! {
rustc_lint_opt_ty,
rustc_lint_query_instability,
rustc_lint_untracked_query_information,
rustc_macro_edition_2021,
rustc_macro_transparency,
rustc_main,
rustc_mir,
Expand Down
1 change: 1 addition & 0 deletions library/core/src/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,7 @@ unsafe impl<T: ?Sized> PinCoerceUnsized for *mut T {}
#[stable(feature = "pin_macro", since = "1.68.0")]
#[rustc_macro_transparency = "semitransparent"]
#[allow_internal_unstable(unsafe_pin_internals)]
#[cfg_attr(not(bootstrap), rustc_macro_edition_2021)]
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
14 changes: 14 additions & 0 deletions library/coretests/tests/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ fn pin_const() {
}

pin_mut_const();

// Check that we accept a Rust 2024 $expr.
std::pin::pin!(const { 1 });
}

#[allow(unused)]
Expand Down Expand Up @@ -81,3 +84,14 @@ mod pin_coerce_unsized {
arg
}
}

#[test]
#[cfg(not(bootstrap))]
fn temp_lifetime() {
// Check that temporary lifetimes work as in Rust 2021.
// Regression test for https://github.com/rust-lang/rust/issues/138596
match std::pin::pin!(foo(&mut 0)) {
_ => {}
}
async fn foo(_: &mut usize) {}
}
Comment on lines +88 to +97
Copy link
Contributor

@danielhenrymantilla danielhenrymantilla Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried to work on this with my failed attempt, I thought of extra tests which seem important for pin, so you may want to add them if you like them 🙂:

fn transitive_extension {
    async fn temporary() {}

    // `pin!` witnessed in the wild being used like this, even if it yields
    // a `Pin<&mut &mut impl Unpin>`; it does work because `pin!`
    // happens to transitively extend the lifespan of `temporary()`.
    let p = pin!(&mut temporary());
    let _use = p;
}

as well as the following things which have to fail to compile to guarantee the soundness of pin!: pin! needs to consume ownership of the argument given to it, by having moved that value to its own super let local, or by having funneled it through a value expression such as { $value }.

  • struct NotCopy<T>(T);
    #[allow(unused_mut)]
    let mut pointee = NotCopy(core::marker::PhantomPinned);
    pin!(pointee);
    let _moved = pointee; // Error, use of moved value.
  • struct NotCopy<T>(T);
    let mut pointee = NotCopy(core::marker::PhantomPinned);
    pin!(*&mut pointee); // Error, cannot move out of `&mut`, `NotCopy` is not `Copy`.
    let _moved = pointee;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Would you mind putting that in a PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've put them in #139097

Loading