@@ -41,7 +41,7 @@ use hygiene::Transparency;
4141pub use hygiene:: { DesugaringKind , ExpnKind , ForLoopLoc , MacroKind } ;
4242pub use hygiene:: { ExpnData , ExpnHash , ExpnId , LocalExpnId , SyntaxContext } ;
4343pub mod def_id;
44- use def_id:: { CrateNum , DefId , DefPathHash , LOCAL_CRATE } ;
44+ use def_id:: { CrateNum , DefId , DefPathHash , LocalDefId , LOCAL_CRATE } ;
4545pub mod lev_distance;
4646mod span_encoding;
4747pub 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
439440impl 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
962987impl 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
0 commit comments