Skip to content

Commit 00485e0

Browse files
committed
Keep a parent LocalDefId in SpanData.
1 parent 06f7ca3 commit 00485e0

File tree

22 files changed

+88
-40
lines changed

22 files changed

+88
-40
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ impl MetaItem {
367367
let is_first = i == 0;
368368
if !is_first {
369369
let mod_sep_span =
370-
Span::new(last_pos, segment.ident.span.lo(), segment.ident.span.ctxt());
370+
Span::new(last_pos, segment.ident.span.lo(), segment.ident.span.ctxt(), None);
371371
idents.push(TokenTree::token(token::ModSep, mod_sep_span).into());
372372
}
373373
idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident)).into());

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ impl server::Span for Rustc<'_> {
745745
self.sess.source_map().lookup_char_pos(span.lo()).file
746746
}
747747
fn parent(&mut self, span: Self::Span) -> Option<Self::Span> {
748-
span.parent()
748+
span.parent_callsite()
749749
}
750750
fn source(&mut self, span: Self::Span) -> Self::Span {
751751
span.source_callsite()

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Span {
536536
let hi =
537537
(hi + source_file.translated_source_file.start_pos) - source_file.original_start_pos;
538538

539-
Ok(Span::new(lo, hi, ctxt))
539+
// Do not try to decode parent for foreign spans.
540+
Ok(Span::new(lo, hi, ctxt, None))
540541
}
541542
}
542543

compiler/rustc_middle/src/ty/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ fn foo(&self) -> Self::T { String::new() }
964964
{
965965
let (span, sugg) = if has_params {
966966
let pos = span.hi() - BytePos(1);
967-
let span = Span::new(pos, pos, span.ctxt());
967+
let span = Span::new(pos, pos, span.ctxt(), span.parent());
968968
(span, format!(", {} = {}", assoc.ident, ty))
969969
} else {
970970
let item_args = self.format_generic_args(assoc_substs);

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ impl CoverageSpan {
196196
/// body_span), returns the macro name symbol.
197197
pub fn visible_macro(&self, body_span: Span) -> Option<Symbol> {
198198
if let Some(current_macro) = self.current_macro() {
199-
if self.expn_span.parent().unwrap_or_else(|| bug!("macro must have a parent")).ctxt()
199+
if self
200+
.expn_span
201+
.parent_callsite()
202+
.unwrap_or_else(|| bug!("macro must have a parent"))
203+
.ctxt()
200204
== body_span.ctxt()
201205
{
202206
return Some(current_macro);

compiler/rustc_parse/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn maybe_source_file_to_parser(
131131
let mut parser = stream_to_parser(sess, stream, None);
132132
parser.unclosed_delims = unclosed_delims;
133133
if parser.token == token::Eof {
134-
parser.token.span = Span::new(end_pos, end_pos, parser.token.span.ctxt());
134+
parser.token.span = Span::new(end_pos, end_pos, parser.token.span.ctxt(), None);
135135
}
136136

137137
Ok(parser)

compiler/rustc_query_impl/src/on_disk_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
848848
let lo = file_lo.lines[line_lo - 1] + col_lo;
849849
let hi = lo + len;
850850

851-
Ok(Span::new(lo, hi, ctxt))
851+
Ok(Span::new(lo, hi, ctxt, None))
852852
}
853853
}
854854

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
768768
args[1].span.lo(),
769769
args.last().unwrap().span.hi(),
770770
call_span.ctxt(),
771+
None,
771772
))
772773
} else {
773774
None

compiler/rustc_span/src/lib.rs

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use hygiene::Transparency;
4141
pub use hygiene::{DesugaringKind, ExpnKind, ForLoopLoc, MacroKind};
4242
pub use hygiene::{ExpnData, ExpnHash, ExpnId, LocalExpnId, SyntaxContext};
4343
pub mod def_id;
44-
use def_id::{CrateNum, DefId, DefPathHash, LOCAL_CRATE};
44+
use def_id::{CrateNum, DefId, DefPathHash, LocalDefId, LOCAL_CRATE};
4545
pub mod lev_distance;
4646
mod span_encoding;
4747
pub use span_encoding::{Span, DUMMY_SP};
@@ -434,24 +434,38 @@ pub struct SpanData {
434434
/// Information about where the macro came from, if this piece of
435435
/// code was created by a macro expansion.
436436
pub ctxt: SyntaxContext,
437+
pub parent: Option<LocalDefId>,
437438
}
438439

439440
impl SpanData {
440441
#[inline]
441442
pub fn span(&self) -> Span {
442-
Span::new(self.lo, self.hi, self.ctxt)
443+
Span::new(self.lo, self.hi, self.ctxt, self.parent)
443444
}
444445
#[inline]
445446
pub fn with_lo(&self, lo: BytePos) -> Span {
446-
Span::new(lo, self.hi, self.ctxt)
447+
Span::new(lo, self.hi, self.ctxt, self.parent)
447448
}
448449
#[inline]
449450
pub fn with_hi(&self, hi: BytePos) -> Span {
450-
Span::new(self.lo, hi, self.ctxt)
451+
Span::new(self.lo, hi, self.ctxt, self.parent)
451452
}
452453
#[inline]
453454
pub fn with_ctxt(&self, ctxt: SyntaxContext) -> Span {
454-
Span::new(self.lo, self.hi, ctxt)
455+
Span::new(self.lo, self.hi, ctxt, self.parent)
456+
}
457+
#[inline]
458+
pub fn with_parent(&self, parent: Option<LocalDefId>) -> Span {
459+
Span::new(self.lo, self.hi, self.ctxt, parent)
460+
}
461+
/// Returns `true` if this is a dummy span with any hygienic context.
462+
#[inline]
463+
pub fn is_dummy(self) -> bool {
464+
self.lo.0 == 0 && self.hi.0 == 0
465+
}
466+
/// Returns `true` if `self` fully encloses `other`.
467+
pub fn contains(self, other: Self) -> bool {
468+
self.lo <= other.lo && other.hi <= self.hi
455469
}
456470
}
457471

@@ -513,12 +527,19 @@ impl Span {
513527
pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
514528
self.data().with_ctxt(ctxt)
515529
}
530+
#[inline]
531+
pub fn parent(self) -> Option<LocalDefId> {
532+
self.data().parent
533+
}
534+
#[inline]
535+
pub fn with_parent(self, ctxt: Option<LocalDefId>) -> Span {
536+
self.data().with_parent(ctxt)
537+
}
516538

517539
/// Returns `true` if this is a dummy span with any hygienic context.
518540
#[inline]
519541
pub fn is_dummy(self) -> bool {
520-
let span = self.data();
521-
span.lo.0 == 0 && span.hi.0 == 0
542+
self.data().is_dummy()
522543
}
523544

524545
/// Returns `true` if this span comes from a macro or desugaring.
@@ -534,7 +555,7 @@ impl Span {
534555

535556
#[inline]
536557
pub fn with_root_ctxt(lo: BytePos, hi: BytePos) -> Span {
537-
Span::new(lo, hi, SyntaxContext::root())
558+
Span::new(lo, hi, SyntaxContext::root(), None)
538559
}
539560

540561
/// Returns a new span representing an empty span at the beginning of this span.
@@ -566,7 +587,7 @@ impl Span {
566587
pub fn contains(self, other: Span) -> bool {
567588
let span = self.data();
568589
let other = other.data();
569-
span.lo <= other.lo && other.hi <= span.hi
590+
span.contains(other)
570591
}
571592

572593
/// Returns `true` if `self` touches `other`.
@@ -602,15 +623,15 @@ impl Span {
602623

603624
/// The `Span` for the tokens in the previous macro expansion from which `self` was generated,
604625
/// if any.
605-
pub fn parent(self) -> Option<Span> {
626+
pub fn parent_callsite(self) -> Option<Span> {
606627
let expn_data = self.ctxt().outer_expn_data();
607628
if !expn_data.is_root() { Some(expn_data.call_site) } else { None }
608629
}
609630

610631
/// Walk down the expansion ancestors to find a span that's contained within `outer`.
611632
pub fn find_ancestor_inside(mut self, outer: Span) -> Option<Span> {
612633
while !outer.contains(self) {
613-
self = self.parent()?;
634+
self = self.parent_callsite()?;
614635
}
615636
Some(self)
616637
}
@@ -731,6 +752,7 @@ impl Span {
731752
cmp::min(span_data.lo, end_data.lo),
732753
cmp::max(span_data.hi, end_data.hi),
733754
if span_data.ctxt == SyntaxContext::root() { end_data.ctxt } else { span_data.ctxt },
755+
if span_data.parent == end_data.parent { span_data.parent } else { None },
734756
)
735757
}
736758

@@ -748,6 +770,7 @@ impl Span {
748770
span.hi,
749771
end.lo,
750772
if end.ctxt == SyntaxContext::root() { end.ctxt } else { span.ctxt },
773+
if span.parent == end.parent { span.parent } else { None },
751774
)
752775
}
753776

@@ -765,6 +788,7 @@ impl Span {
765788
span.lo,
766789
end.lo,
767790
if end.ctxt == SyntaxContext::root() { end.ctxt } else { span.ctxt },
791+
if span.parent == end.parent { span.parent } else { None },
768792
)
769793
}
770794

@@ -774,6 +798,7 @@ impl Span {
774798
span.lo + BytePos::from_usize(inner.start),
775799
span.lo + BytePos::from_usize(inner.end),
776800
span.ctxt,
801+
span.parent,
777802
)
778803
}
779804

@@ -812,31 +837,31 @@ impl Span {
812837
pub fn remove_mark(&mut self) -> ExpnId {
813838
let mut span = self.data();
814839
let mark = span.ctxt.remove_mark();
815-
*self = Span::new(span.lo, span.hi, span.ctxt);
840+
*self = Span::new(span.lo, span.hi, span.ctxt, span.parent);
816841
mark
817842
}
818843

819844
#[inline]
820845
pub fn adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
821846
let mut span = self.data();
822847
let mark = span.ctxt.adjust(expn_id);
823-
*self = Span::new(span.lo, span.hi, span.ctxt);
848+
*self = Span::new(span.lo, span.hi, span.ctxt, span.parent);
824849
mark
825850
}
826851

827852
#[inline]
828853
pub fn normalize_to_macros_2_0_and_adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
829854
let mut span = self.data();
830855
let mark = span.ctxt.normalize_to_macros_2_0_and_adjust(expn_id);
831-
*self = Span::new(span.lo, span.hi, span.ctxt);
856+
*self = Span::new(span.lo, span.hi, span.ctxt, span.parent);
832857
mark
833858
}
834859

835860
#[inline]
836861
pub fn glob_adjust(&mut self, expn_id: ExpnId, glob_span: Span) -> Option<Option<ExpnId>> {
837862
let mut span = self.data();
838863
let mark = span.ctxt.glob_adjust(expn_id, glob_span);
839-
*self = Span::new(span.lo, span.hi, span.ctxt);
864+
*self = Span::new(span.lo, span.hi, span.ctxt, span.parent);
840865
mark
841866
}
842867

@@ -848,7 +873,7 @@ impl Span {
848873
) -> Option<Option<ExpnId>> {
849874
let mut span = self.data();
850875
let mark = span.ctxt.reverse_glob_adjust(expn_id, glob_span);
851-
*self = Span::new(span.lo, span.hi, span.ctxt);
876+
*self = Span::new(span.lo, span.hi, span.ctxt, span.parent);
852877
mark
853878
}
854879

@@ -900,7 +925,7 @@ impl<D: Decoder> Decodable<D> for Span {
900925
let lo = d.read_struct_field("lo", Decodable::decode)?;
901926
let hi = d.read_struct_field("hi", Decodable::decode)?;
902927

903-
Ok(Span::new(lo, hi, SyntaxContext::root()))
928+
Ok(Span::new(lo, hi, SyntaxContext::root(), None))
904929
})
905930
}
906931
}
@@ -961,7 +986,7 @@ impl fmt::Debug for Span {
961986

962987
impl fmt::Debug for SpanData {
963988
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
964-
(*SPAN_DEBUG)(Span::new(self.lo, self.hi, self.ctxt), f)
989+
(*SPAN_DEBUG)(Span::new(self.lo, self.hi, self.ctxt, self.parent), f)
965990
}
966991
}
967992

compiler/rustc_span/src/source_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ impl SourceMap {
794794
start_of_next_point.checked_add(width - 1).unwrap_or(start_of_next_point);
795795

796796
let end_of_next_point = BytePos(cmp::max(sp.lo().0 + 1, end_of_next_point));
797-
Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt())
797+
Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt(), None)
798798
}
799799

800800
/// Finds the width of the character, either before or after the end of provided span,

0 commit comments

Comments
 (0)