Skip to content

Commit 11aee2e

Browse files
authoredMar 21, 2025
Rollup merge of rust-lang#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 rust-lang#138596
2 parents 2a8b44c + 0577300 commit 11aee2e

File tree

10 files changed

+59
-8
lines changed

10 files changed

+59
-8
lines changed
 

‎compiler/rustc_attr_data_structures/src/attributes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ pub enum AttributeKind {
191191
},
192192
MacroTransparency(Transparency),
193193
Repr(ThinVec<(ReprAttr, Span)>),
194+
RustcMacroEdition2021,
194195
Stability {
195196
stability: Stability,
196197
/// Span of the `#[stable(...)]` or `#[unstable(...)]` attribute

‎compiler/rustc_attr_data_structures/src/lib.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -182,21 +182,18 @@ macro_rules! find_attr {
182182
}};
183183

184184
($attributes_list: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => {{
185-
fn check_attribute_iterator<'a>(_: &'_ impl IntoIterator<Item = &'a rustc_hir::Attribute>) {}
186-
check_attribute_iterator(&$attributes_list);
187-
188-
let find_attribute = |iter| {
185+
'done: {
189186
for i in $attributes_list {
187+
let i: &rustc_hir::Attribute = i;
190188
match i {
191189
rustc_hir::Attribute::Parsed($pattern) $(if $guard)? => {
192-
return Some($e);
190+
break 'done Some($e);
193191
}
194192
_ => {}
195193
}
196194
}
197195

198196
None
199-
};
200-
find_attribute($attributes_list)
197+
}
201198
}};
202199
}

‎compiler/rustc_attr_parsing/src/attributes/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub(crate) mod cfg;
2828
pub(crate) mod confusables;
2929
pub(crate) mod deprecation;
3030
pub(crate) mod repr;
31+
pub(crate) mod rustc;
3132
pub(crate) mod stability;
3233
pub(crate) mod transparency;
3334
pub(crate) mod util;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_span::sym;
3+
4+
use super::{AcceptContext, SingleAttributeParser};
5+
use crate::parser::ArgParser;
6+
7+
pub(crate) struct RustcMacroEdition2021Parser;
8+
9+
// FIXME(jdonszelmann): make these proper diagnostics
10+
impl SingleAttributeParser for RustcMacroEdition2021Parser {
11+
const PATH: &'static [rustc_span::Symbol] = &[sym::rustc_macro_edition_2021];
12+
13+
fn on_duplicate(_cx: &crate::context::AcceptContext<'_>, _first_span: rustc_span::Span) {}
14+
15+
fn convert(_cx: &AcceptContext<'_>, args: &ArgParser<'_>) -> Option<AttributeKind> {
16+
assert!(args.no_args());
17+
Some(AttributeKind::RustcMacroEdition2021)
18+
}
19+
}

‎compiler/rustc_attr_parsing/src/context.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInterna
1515
use crate::attributes::confusables::ConfusablesParser;
1616
use crate::attributes::deprecation::DeprecationParser;
1717
use crate::attributes::repr::ReprParser;
18+
use crate::attributes::rustc::RustcMacroEdition2021Parser;
1819
use crate::attributes::stability::{
1920
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
2021
};
@@ -76,6 +77,7 @@ attribute_groups!(
7677
// tidy-alphabetical-start
7778
Single<ConstStabilityIndirectParser>,
7879
Single<DeprecationParser>,
80+
Single<RustcMacroEdition2021Parser>,
7981
Single<TransparencyParser>,
8082
// tidy-alphabetical-end
8183
];

‎compiler/rustc_feature/src/builtin_attrs.rs

+8
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,14 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
661661
"`rustc_never_type_options` is used to experiment with never type fallback and work on \
662662
never type stabilization, and will never be stable"
663663
),
664+
rustc_attr!(
665+
rustc_macro_edition_2021,
666+
Normal,
667+
template!(Word),
668+
ErrorFollowing,
669+
EncodeCrossCrate::No,
670+
"makes spans in this macro edition 2021"
671+
),
664672

665673
// ==========================================================================
666674
// Internal attributes: Runtime related:

‎compiler/rustc_resolve/src/macros.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::sync::Arc;
88
use rustc_ast::expand::StrippedCfgItem;
99
use rustc_ast::{self as ast, Crate, NodeId, attr};
1010
use rustc_ast_pretty::pprust;
11-
use rustc_attr_parsing::StabilityLevel;
11+
use rustc_attr_parsing::{AttributeKind, StabilityLevel, find_attr};
1212
use rustc_data_structures::intern::Interned;
1313
use rustc_errors::{Applicability, StashKey};
1414
use rustc_expand::base::{
@@ -1125,6 +1125,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11251125
edition,
11261126
);
11271127

1128+
// The #[rustc_macro_edition_2021] attribute is used by the pin!() macro
1129+
// as a temporary workaround for a regression in expressiveness in Rust 2024.
1130+
// See https://github.com/rust-lang/rust/issues/138718.
1131+
if find_attr!(attrs.iter(), AttributeKind::RustcMacroEdition2021) {
1132+
ext.edition = Edition::Edition2021;
1133+
}
1134+
11281135
if let Some(builtin_name) = ext.builtin_name {
11291136
// The macro was marked with `#[rustc_builtin_macro]`.
11301137
if let Some(builtin_ext_kind) = self.builtin_macros.get(&builtin_name) {

‎compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,7 @@ symbols! {
17951795
rustc_lint_opt_ty,
17961796
rustc_lint_query_instability,
17971797
rustc_lint_untracked_query_information,
1798+
rustc_macro_edition_2021,
17981799
rustc_macro_transparency,
17991800
rustc_main,
18001801
rustc_mir,

‎library/core/src/pin.rs

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

‎library/coretests/tests/pin.rs

+14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ fn pin_const() {
3434
}
3535

3636
pin_mut_const();
37+
38+
// Check that we accept a Rust 2024 $expr.
39+
std::pin::pin!(const { 1 });
3740
}
3841

3942
#[allow(unused)]
@@ -81,3 +84,14 @@ mod pin_coerce_unsized {
8184
arg
8285
}
8386
}
87+
88+
#[test]
89+
#[cfg(not(bootstrap))]
90+
fn temp_lifetime() {
91+
// Check that temporary lifetimes work as in Rust 2021.
92+
// Regression test for https://github.com/rust-lang/rust/issues/138596
93+
match std::pin::pin!(foo(&mut 0)) {
94+
_ => {}
95+
}
96+
async fn foo(_: &mut usize) {}
97+
}

0 commit comments

Comments
 (0)