Skip to content

Commit fc8d2b3

Browse files
Rollup merge of rust-lang#133080 - ehuss:edition-desugar-span, r=compiler-errors
Fix span edition for 2024 RPIT coming from an external macro This fixes a problem where code generated by an external macro with an RPIT would end up using the call-site edition instead of the macro's edition for the RPIT. When used from a 2024 crate, this caused the code to change behavior to the 2024 capturing rules, which we don't want. This was caused by the impl-trait lowering code would replace the span with one marked with `DesugaringKind::OpaqueTy` desugaring. However, it was also overriding the edition of the span with the edition of the local crate. Instead it should be using the edition of the span itself. Fixes rust-lang#132917
2 parents c3a632c + 03e2828 commit fc8d2b3

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
738738
allow_internal_unstable: Option<Lrc<[Symbol]>>,
739739
) -> Span {
740740
self.tcx.with_stable_hashing_context(|hcx| {
741-
span.mark_with_reason(allow_internal_unstable, reason, self.tcx.sess.edition(), hcx)
741+
span.mark_with_reason(allow_internal_unstable, reason, span.edition(), hcx)
742742
})
743743
}
744744

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// A macro_rules macro in 2015 that has an RPIT without `use<>` that would
2+
// cause a problem with 2024 capturing rules.
3+
4+
#[macro_export]
5+
macro_rules! macro_rpit {
6+
() => {
7+
fn test_mbe(x: &Vec<i32>) -> impl std::fmt::Display {
8+
x[0]
9+
}
10+
11+
pub fn from_mbe() {
12+
let mut x = vec![];
13+
x.push(1);
14+
15+
let element = test_mbe(&x);
16+
x.push(2);
17+
println!("{element}");
18+
}
19+
};
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// A proc-macro in 2015 that has an RPIT without `use<>` that would cause a
2+
// problem with 2024 capturing rules.
3+
4+
//@ force-host
5+
//@ no-prefer-dynamic
6+
7+
#![crate_type = "proc-macro"]
8+
9+
extern crate proc_macro;
10+
use proc_macro::TokenStream;
11+
12+
#[proc_macro]
13+
pub fn pm_rpit(input: TokenStream) -> TokenStream {
14+
"fn test_pm(x: &Vec<i32>) -> impl std::fmt::Display {
15+
x[0]
16+
}
17+
18+
pub fn from_pm() {
19+
let mut x = vec![];
20+
x.push(1);
21+
22+
let element = test_pm(&x);
23+
x.push(2);
24+
println!(\"{element}\");
25+
}
26+
"
27+
.parse()
28+
.unwrap()
29+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Tests that code generated from an external macro (MBE and proc-macro) that
2+
// has an RPIT will not fail when the call-site is 2024.
3+
// https://github.com/rust-lang/rust/issues/132917
4+
5+
//@ aux-crate: no_use_pm=no-use-pm.rs
6+
//@ aux-crate: no_use_macro=no-use-macro.rs
7+
//@ edition: 2024
8+
//@ compile-flags:-Z unstable-options
9+
//@ check-pass
10+
11+
no_use_pm::pm_rpit!{}
12+
13+
no_use_macro::macro_rpit!{}
14+
15+
fn main() {
16+
let mut x = vec![];
17+
x.push(1);
18+
19+
let element = test_pm(&x);
20+
x.push(2);
21+
println!("{element}");
22+
23+
let element = test_mbe(&x);
24+
x.push(2);
25+
println!("{element}");
26+
}

0 commit comments

Comments
 (0)