diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2021-08-12 11:57:32 +0200 |
|---|---|---|
| committer | Mara Bos <m-ou.se@m-ou.se> | 2021-08-12 12:01:22 +0200 |
| commit | 945a4b18d949719a1ad737d6d39dda717d62c001 (patch) | |
| tree | 8b42fa23536356a3912098bc6be5103cb7b40699 /compiler | |
| parent | eb2226b1f174f3cc644275ef8663be6295a7f704 (diff) | |
| download | rust-945a4b18d949719a1ad737d6d39dda717d62c001.tar.gz rust-945a4b18d949719a1ad737d6d39dda717d62c001.zip | |
Fix closure migration suggestion when the body is a macro.
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_typeck/src/check/upvar.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/compiler/rustc_typeck/src/check/upvar.rs b/compiler/rustc_typeck/src/check/upvar.rs index 013cb2a49b2..862b650efd7 100644 --- a/compiler/rustc_typeck/src/check/upvar.rs +++ b/compiler/rustc_typeck/src/check/upvar.rs @@ -47,7 +47,7 @@ use rustc_middle::ty::{ }; use rustc_session::lint; use rustc_span::sym; -use rustc_span::{MultiSpan, Span, Symbol}; +use rustc_span::{MultiSpan, Span, Symbol, DUMMY_SP}; use rustc_trait_selection::infer::InferCtxtExt; use rustc_data_structures::stable_map::FxHashMap; @@ -644,7 +644,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } diagnostics_builder.note("for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/disjoint-capture-in-closures.html>"); - let closure_body_span = self.tcx.hir().span(body_id.hir_id); + + let mut closure_body_span = self.tcx.hir().span(body_id.hir_id); + + // If the body was entirely expanded from a macro + // invocation, as the body is not contained inside the + // closure span. In that case, we walk up the expansion + // until we find the span before the expansion. + while !closure_body_span.is_dummy() && !closure_span.contains(closure_body_span) { + closure_body_span = closure_body_span.parent().unwrap_or(DUMMY_SP); + } + let (sugg, app) = match self.tcx.sess.source_map().span_to_snippet(closure_body_span) { Ok(s) => { |
