Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2a06daa

Browse files
committed
Pull Span::find_ancestor_inside loop into its own function.
1 parent 76d18cf commit 2a06daa

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

compiler/rustc_span/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,14 @@ impl Span {
597597
if !expn_data.is_root() { Some(expn_data.call_site) } else { None }
598598
}
599599

600+
/// Walk down the expansion ancestors to find a span that's contained within `outer`.
601+
pub fn find_ancestor_inside(mut self, outer: Span) -> Option<Span> {
602+
while !outer.contains(self) {
603+
self = self.parent()?;
604+
}
605+
Some(self)
606+
}
607+
600608
/// Edition of the crate from which this span came.
601609
pub fn edition(self) -> edition::Edition {
602610
self.ctxt().edition()

compiler/rustc_typeck/src/check/upvar.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -680,15 +680,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
680680
migrated_variables_concat
681681
);
682682

683-
let mut closure_body_span = self.tcx.hir().span(body_id.hir_id);
684-
685683
// If the body was entirely expanded from a macro
686684
// invocation, i.e. the body is not contained inside the
687685
// closure span, then we walk up the expansion until we
688686
// find the span before the expansion.
689-
while !closure_body_span.is_dummy() && !closure_span.contains(closure_body_span) {
690-
closure_body_span = closure_body_span.parent().unwrap_or(DUMMY_SP);
691-
}
687+
let closure_body_span = self.tcx.hir().span(body_id.hir_id)
688+
.find_ancestor_inside(closure_span)
689+
.unwrap_or(DUMMY_SP);
692690

693691
if let Ok(s) = self.tcx.sess.source_map().span_to_snippet(closure_body_span) {
694692
let mut lines = s.lines();

0 commit comments

Comments
 (0)