Skip to content

Commit

Permalink
Fix span edition for 2024 RPIT coming from an external macro
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ehuss committed Nov 15, 2024
1 parent d163541 commit 03e2828
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 94 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
allow_internal_unstable: Option<Lrc<[Symbol]>>,
) -> Span {
self.tcx.with_stable_hashing_context(|hcx| {
span.mark_with_reason(allow_internal_unstable, reason, self.tcx.sess.edition(), hcx)
span.mark_with_reason(allow_internal_unstable, reason, span.edition(), hcx)
})
}

Expand Down
5 changes: 1 addition & 4 deletions tests/ui/impl-trait/precise-capturing/external-macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@
//@ aux-crate: no_use_macro=no-use-macro.rs
//@ edition: 2024
//@ compile-flags:-Z unstable-options
//@ check-pass

no_use_pm::pm_rpit!{}
//~^ ERROR: cannot borrow `x` as mutable

no_use_macro::macro_rpit!{}
//~^ ERROR: cannot borrow `x` as mutable

fn main() {
let mut x = vec![];
x.push(1);

let element = test_pm(&x);
x.push(2);
//~^ ERROR: cannot borrow `x` as mutable
println!("{element}");

let element = test_mbe(&x);
x.push(2);
//~^ ERROR: cannot borrow `x` as mutable
println!("{element}");
}
89 changes: 0 additions & 89 deletions tests/ui/impl-trait/precise-capturing/external-macro.stderr

This file was deleted.

0 comments on commit 03e2828

Please sign in to comment.