@@ -41,7 +41,7 @@ use hygiene::Transparency;
41
41
pub use hygiene:: { DesugaringKind , ExpnKind , ForLoopLoc , MacroKind } ;
42
42
pub use hygiene:: { ExpnData , ExpnHash , ExpnId , LocalExpnId , SyntaxContext } ;
43
43
pub mod def_id;
44
- use def_id:: { CrateNum , DefId , DefPathHash , LOCAL_CRATE } ;
44
+ use def_id:: { CrateNum , DefId , DefPathHash , LocalDefId , LOCAL_CRATE } ;
45
45
pub mod lev_distance;
46
46
mod span_encoding;
47
47
pub use span_encoding:: { Span , DUMMY_SP } ;
@@ -434,24 +434,38 @@ pub struct SpanData {
434
434
/// Information about where the macro came from, if this piece of
435
435
/// code was created by a macro expansion.
436
436
pub ctxt : SyntaxContext ,
437
+ pub parent : Option < LocalDefId > ,
437
438
}
438
439
439
440
impl SpanData {
440
441
#[ inline]
441
442
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 )
443
444
}
444
445
#[ inline]
445
446
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 )
447
448
}
448
449
#[ inline]
449
450
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 )
451
452
}
452
453
#[ inline]
453
454
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
455
469
}
456
470
}
457
471
@@ -513,12 +527,19 @@ impl Span {
513
527
pub fn with_ctxt ( self , ctxt : SyntaxContext ) -> Span {
514
528
self . data ( ) . with_ctxt ( ctxt)
515
529
}
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
+ }
516
538
517
539
/// Returns `true` if this is a dummy span with any hygienic context.
518
540
#[ inline]
519
541
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 ( )
522
543
}
523
544
524
545
/// Returns `true` if this span comes from a macro or desugaring.
@@ -534,7 +555,7 @@ impl Span {
534
555
535
556
#[ inline]
536
557
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 )
538
559
}
539
560
540
561
/// Returns a new span representing an empty span at the beginning of this span.
@@ -566,7 +587,7 @@ impl Span {
566
587
pub fn contains ( self , other : Span ) -> bool {
567
588
let span = self . data ( ) ;
568
589
let other = other. data ( ) ;
569
- span. lo <= other. lo && other . hi <= span . hi
590
+ span. contains ( other)
570
591
}
571
592
572
593
/// Returns `true` if `self` touches `other`.
@@ -602,15 +623,15 @@ impl Span {
602
623
603
624
/// The `Span` for the tokens in the previous macro expansion from which `self` was generated,
604
625
/// if any.
605
- pub fn parent ( self ) -> Option < Span > {
626
+ pub fn parent_callsite ( self ) -> Option < Span > {
606
627
let expn_data = self . ctxt ( ) . outer_expn_data ( ) ;
607
628
if !expn_data. is_root ( ) { Some ( expn_data. call_site ) } else { None }
608
629
}
609
630
610
631
/// Walk down the expansion ancestors to find a span that's contained within `outer`.
611
632
pub fn find_ancestor_inside ( mut self , outer : Span ) -> Option < Span > {
612
633
while !outer. contains ( self ) {
613
- self = self . parent ( ) ?;
634
+ self = self . parent_callsite ( ) ?;
614
635
}
615
636
Some ( self )
616
637
}
@@ -731,6 +752,7 @@ impl Span {
731
752
cmp:: min ( span_data. lo , end_data. lo ) ,
732
753
cmp:: max ( span_data. hi , end_data. hi ) ,
733
754
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 } ,
734
756
)
735
757
}
736
758
@@ -748,6 +770,7 @@ impl Span {
748
770
span. hi ,
749
771
end. lo ,
750
772
if end. ctxt == SyntaxContext :: root ( ) { end. ctxt } else { span. ctxt } ,
773
+ if span. parent == end. parent { span. parent } else { None } ,
751
774
)
752
775
}
753
776
@@ -765,6 +788,7 @@ impl Span {
765
788
span. lo ,
766
789
end. lo ,
767
790
if end. ctxt == SyntaxContext :: root ( ) { end. ctxt } else { span. ctxt } ,
791
+ if span. parent == end. parent { span. parent } else { None } ,
768
792
)
769
793
}
770
794
@@ -774,6 +798,7 @@ impl Span {
774
798
span. lo + BytePos :: from_usize ( inner. start ) ,
775
799
span. lo + BytePos :: from_usize ( inner. end ) ,
776
800
span. ctxt ,
801
+ span. parent ,
777
802
)
778
803
}
779
804
@@ -812,31 +837,31 @@ impl Span {
812
837
pub fn remove_mark ( & mut self ) -> ExpnId {
813
838
let mut span = self . data ( ) ;
814
839
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 ) ;
816
841
mark
817
842
}
818
843
819
844
#[ inline]
820
845
pub fn adjust ( & mut self , expn_id : ExpnId ) -> Option < ExpnId > {
821
846
let mut span = self . data ( ) ;
822
847
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 ) ;
824
849
mark
825
850
}
826
851
827
852
#[ inline]
828
853
pub fn normalize_to_macros_2_0_and_adjust ( & mut self , expn_id : ExpnId ) -> Option < ExpnId > {
829
854
let mut span = self . data ( ) ;
830
855
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 ) ;
832
857
mark
833
858
}
834
859
835
860
#[ inline]
836
861
pub fn glob_adjust ( & mut self , expn_id : ExpnId , glob_span : Span ) -> Option < Option < ExpnId > > {
837
862
let mut span = self . data ( ) ;
838
863
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 ) ;
840
865
mark
841
866
}
842
867
@@ -848,7 +873,7 @@ impl Span {
848
873
) -> Option < Option < ExpnId > > {
849
874
let mut span = self . data ( ) ;
850
875
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 ) ;
852
877
mark
853
878
}
854
879
@@ -900,7 +925,7 @@ impl<D: Decoder> Decodable<D> for Span {
900
925
let lo = d. read_struct_field ( "lo" , Decodable :: decode) ?;
901
926
let hi = d. read_struct_field ( "hi" , Decodable :: decode) ?;
902
927
903
- Ok ( Span :: new ( lo, hi, SyntaxContext :: root ( ) ) )
928
+ Ok ( Span :: new ( lo, hi, SyntaxContext :: root ( ) , None ) )
904
929
} )
905
930
}
906
931
}
@@ -961,7 +986,7 @@ impl fmt::Debug for Span {
961
986
962
987
impl fmt:: Debug for SpanData {
963
988
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)
965
990
}
966
991
}
967
992
0 commit comments