Skip to content

Commit 945a4b1

Browse files
committed
Fix closure migration suggestion when the body is a macro.
1 parent eb2226b commit 945a4b1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

compiler/rustc_typeck/src/check/upvar.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use rustc_middle::ty::{
4747
};
4848
use rustc_session::lint;
4949
use rustc_span::sym;
50-
use rustc_span::{MultiSpan, Span, Symbol};
50+
use rustc_span::{MultiSpan, Span, Symbol, DUMMY_SP};
5151
use rustc_trait_selection::infer::InferCtxtExt;
5252

5353
use rustc_data_structures::stable_map::FxHashMap;
@@ -644,7 +644,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
644644
}
645645
}
646646
diagnostics_builder.note("for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/disjoint-capture-in-closures.html>");
647-
let closure_body_span = self.tcx.hir().span(body_id.hir_id);
647+
648+
let mut closure_body_span = self.tcx.hir().span(body_id.hir_id);
649+
650+
// If the body was entirely expanded from a macro
651+
// invocation, as the body is not contained inside the
652+
// closure span. In that case, we walk up the expansion
653+
// until we find the span before the expansion.
654+
while !closure_body_span.is_dummy() && !closure_span.contains(closure_body_span) {
655+
closure_body_span = closure_body_span.parent().unwrap_or(DUMMY_SP);
656+
}
657+
648658
let (sugg, app) =
649659
match self.tcx.sess.source_map().span_to_snippet(closure_body_span) {
650660
Ok(s) => {

0 commit comments

Comments
 (0)