1
Fork 0

Rollup merge of #138717 - jdonszelmann:pin-macro, r=WaffleLapkin

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

Fixes a regression, see issue below. This is a temporary fix, super let is the real solution.

Closes #138596
This commit is contained in:
Matthias Krüger 2025-03-21 15:48:57 +01:00 committed by GitHub
commit 7c2475e9aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 59 additions and 8 deletions

View file

@ -191,6 +191,7 @@ pub enum AttributeKind {
}, },
MacroTransparency(Transparency), MacroTransparency(Transparency),
Repr(ThinVec<(ReprAttr, Span)>), Repr(ThinVec<(ReprAttr, Span)>),
RustcMacroEdition2021,
Stability { Stability {
stability: Stability, stability: Stability,
/// Span of the `#[stable(...)]` or `#[unstable(...)]` attribute /// Span of the `#[stable(...)]` or `#[unstable(...)]` attribute

View file

@ -182,21 +182,18 @@ macro_rules! find_attr {
}}; }};
($attributes_list: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => {{ ($attributes_list: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => {{
fn check_attribute_iterator<'a>(_: &'_ impl IntoIterator<Item = &'a rustc_hir::Attribute>) {} 'done: {
check_attribute_iterator(&$attributes_list);
let find_attribute = |iter| {
for i in $attributes_list { for i in $attributes_list {
let i: &rustc_hir::Attribute = i;
match i { match i {
rustc_hir::Attribute::Parsed($pattern) $(if $guard)? => { rustc_hir::Attribute::Parsed($pattern) $(if $guard)? => {
return Some($e); break 'done Some($e);
} }
_ => {} _ => {}
} }
} }
None None
}; }
find_attribute($attributes_list)
}}; }};
} }

View file

@ -28,6 +28,7 @@ pub(crate) mod cfg;
pub(crate) mod confusables; pub(crate) mod confusables;
pub(crate) mod deprecation; pub(crate) mod deprecation;
pub(crate) mod repr; pub(crate) mod repr;
pub(crate) mod rustc;
pub(crate) mod stability; pub(crate) mod stability;
pub(crate) mod transparency; pub(crate) mod transparency;
pub(crate) mod util; pub(crate) mod util;

View file

@ -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)
}
}

View file

@ -15,6 +15,7 @@ use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInterna
use crate::attributes::confusables::ConfusablesParser; use crate::attributes::confusables::ConfusablesParser;
use crate::attributes::deprecation::DeprecationParser; use crate::attributes::deprecation::DeprecationParser;
use crate::attributes::repr::ReprParser; use crate::attributes::repr::ReprParser;
use crate::attributes::rustc::RustcMacroEdition2021Parser;
use crate::attributes::stability::{ use crate::attributes::stability::{
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser, BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
}; };
@ -76,6 +77,7 @@ attribute_groups!(
// tidy-alphabetical-start // tidy-alphabetical-start
Single<ConstStabilityIndirectParser>, Single<ConstStabilityIndirectParser>,
Single<DeprecationParser>, Single<DeprecationParser>,
Single<RustcMacroEdition2021Parser>,
Single<TransparencyParser>, Single<TransparencyParser>,
// tidy-alphabetical-end // tidy-alphabetical-end
]; ];

View file

@ -661,6 +661,14 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
"`rustc_never_type_options` is used to experiment with never type fallback and work on \ "`rustc_never_type_options` is used to experiment with never type fallback and work on \
never type stabilization, and will never be stable" 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: // Internal attributes: Runtime related:

View file

@ -8,7 +8,7 @@ use std::sync::Arc;
use rustc_ast::expand::StrippedCfgItem; use rustc_ast::expand::StrippedCfgItem;
use rustc_ast::{self as ast, Crate, NodeId, attr}; use rustc_ast::{self as ast, Crate, NodeId, attr};
use rustc_ast_pretty::pprust; 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_data_structures::intern::Interned;
use rustc_errors::{Applicability, StashKey}; use rustc_errors::{Applicability, StashKey};
use rustc_expand::base::{ use rustc_expand::base::{
@ -1125,6 +1125,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
edition, 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 { if let Some(builtin_name) = ext.builtin_name {
// The macro was marked with `#[rustc_builtin_macro]`. // The macro was marked with `#[rustc_builtin_macro]`.
if let Some(builtin_ext_kind) = self.builtin_macros.get(&builtin_name) { if let Some(builtin_ext_kind) = self.builtin_macros.get(&builtin_name) {

View file

@ -1795,6 +1795,7 @@ symbols! {
rustc_lint_opt_ty, rustc_lint_opt_ty,
rustc_lint_query_instability, rustc_lint_query_instability,
rustc_lint_untracked_query_information, rustc_lint_untracked_query_information,
rustc_macro_edition_2021,
rustc_macro_transparency, rustc_macro_transparency,
rustc_main, rustc_main,
rustc_mir, rustc_mir,

View file

@ -1943,6 +1943,7 @@ unsafe impl<T: ?Sized> PinCoerceUnsized for *mut T {}
#[stable(feature = "pin_macro", since = "1.68.0")] #[stable(feature = "pin_macro", since = "1.68.0")]
#[rustc_macro_transparency = "semitransparent"] #[rustc_macro_transparency = "semitransparent"]
#[allow_internal_unstable(unsafe_pin_internals)] #[allow_internal_unstable(unsafe_pin_internals)]
#[cfg_attr(not(bootstrap), rustc_macro_edition_2021)]
pub macro pin($value:expr $(,)?) { pub macro pin($value:expr $(,)?) {
// This is `Pin::new_unchecked(&mut { $value })`, so, for starters, let's // This is `Pin::new_unchecked(&mut { $value })`, so, for starters, let's
// review such a hypothetical macro (that any user-code could define): // review such a hypothetical macro (that any user-code could define):

View file

@ -34,6 +34,9 @@ fn pin_const() {
} }
pin_mut_const(); pin_mut_const();
// Check that we accept a Rust 2024 $expr.
std::pin::pin!(const { 1 });
} }
#[allow(unused)] #[allow(unused)]
@ -81,3 +84,14 @@ mod pin_coerce_unsized {
arg 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) {}
}