@@ -46,6 +46,8 @@ use crate::symbol::{Symbol, kw, sym};
46
46
use crate :: { DUMMY_SP , HashStableContext , Span , SpanDecoder , SpanEncoder , with_session_globals} ;
47
47
48
48
/// A `SyntaxContext` represents a chain of pairs `(ExpnId, Transparency)` named "marks".
49
+ ///
50
+ /// See <https://rustc-dev-guide.rust-lang.org/macro-expansion.html> for more explanation.
49
51
#[ derive( Clone , Copy , PartialEq , Eq , Hash ) ]
50
52
pub struct SyntaxContext ( u32 ) ;
51
53
@@ -61,7 +63,10 @@ pub type SyntaxContextKey = (SyntaxContext, ExpnId, Transparency);
61
63
62
64
#[ derive( Clone , Copy , Debug ) ]
63
65
struct SyntaxContextData {
66
+ /// The last macro expansion in the chain.
67
+ /// (Here we say the most deeply nested macro expansion is the "outermost" expansion.)
64
68
outer_expn : ExpnId ,
69
+ /// Transparency of the last macro expansion
65
70
outer_transparency : Transparency ,
66
71
parent : SyntaxContext ,
67
72
/// This context, but with all transparent and semi-opaque expansions filtered away.
@@ -450,11 +455,13 @@ impl HygieneData {
450
455
self . syntax_context_data [ ctxt. 0 as usize ] . opaque_and_semiopaque
451
456
}
452
457
458
+ /// See [`SyntaxContextData::outer_expn`]
453
459
#[ inline]
454
460
fn outer_expn ( & self , ctxt : SyntaxContext ) -> ExpnId {
455
461
self . syntax_context_data [ ctxt. 0 as usize ] . outer_expn
456
462
}
457
463
464
+ /// The last macro expansion and its Transparency
458
465
#[ inline]
459
466
fn outer_mark ( & self , ctxt : SyntaxContext ) -> ( ExpnId , Transparency ) {
460
467
let data = & self . syntax_context_data [ ctxt. 0 as usize ] ;
@@ -900,6 +907,7 @@ impl SyntaxContext {
900
907
HygieneData :: with ( |data| data. normalize_to_macro_rules ( self ) )
901
908
}
902
909
910
+ /// See [`SyntaxContextData::outer_expn`]
903
911
#[ inline]
904
912
pub fn outer_expn ( self ) -> ExpnId {
905
913
HygieneData :: with ( |data| data. outer_expn ( self ) )
@@ -912,6 +920,7 @@ impl SyntaxContext {
912
920
HygieneData :: with ( |data| data. expn_data ( data. outer_expn ( self ) ) . clone ( ) )
913
921
}
914
922
923
+ /// See [`HygieneData::outer_mark`]
915
924
#[ inline]
916
925
fn outer_mark ( self ) -> ( ExpnId , Transparency ) {
917
926
HygieneData :: with ( |data| data. outer_mark ( self ) )
@@ -982,19 +991,20 @@ impl Span {
982
991
#[ derive( Clone , Debug , Encodable , Decodable , HashStable_Generic ) ]
983
992
pub struct ExpnData {
984
993
// --- The part unique to each expansion.
985
- /// The kind of this expansion - macro or compiler desugaring.
986
994
pub kind : ExpnKind ,
987
- /// The expansion that produced this expansion.
995
+ /// The expansion that contains the definition of the macro for this expansion.
988
996
pub parent : ExpnId ,
989
- /// The location of the actual macro invocation or syntax sugar , e.g.
990
- /// `let x = foo!();` or `if let Some(y) = x {}`
997
+ /// The span of the macro call which produced this expansion.
998
+ ///
999
+ /// This span will typically have a different `ExpnData` and `call_site`.
1000
+ /// This recursively traces back through any macro calls which expanded into further
1001
+ /// macro calls, until the "source call-site" is reached at the root SyntaxContext.
1002
+ /// For example, if `food!()` expands to `fruit!()` which then expands to `grape`,
1003
+ /// then the call-site of `grape` is `fruit!()` and the call-site of `fruit!()`
1004
+ /// is `food!()`.
991
1005
///
992
- /// This may recursively refer to other macro invocations, e.g., if
993
- /// `foo!()` invoked `bar!()` internally, and there was an
994
- /// expression inside `bar!`; the call_site of the expression in
995
- /// the expansion would point to the `bar!` invocation; that
996
- /// call_site span would have its own ExpnData, with the call_site
997
- /// pointing to the `foo!` invocation.
1006
+ /// For a desugaring expansion, this is the span of the expression or node that was
1007
+ /// desugared.
998
1008
pub call_site : Span ,
999
1009
/// Used to force two `ExpnData`s to have different `Fingerprint`s.
1000
1010
/// Due to macro expansion, it's possible to end up with two `ExpnId`s
0 commit comments