@@ -46,6 +46,12 @@ 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
+ /// This can generally be thought of as the macro call stack for a given location in code.
51
+ /// Although, when a macro emits code from input parameters, that emitted code retains its
52
+ /// call-site SyntaxContext.
53
+ ///
54
+ /// See <https://rustc-dev-guide.rust-lang.org/macro-expansion.html> for more explanation.
49
55
#[ derive( Clone , Copy , PartialEq , Eq , Hash ) ]
50
56
pub struct SyntaxContext ( u32 ) ;
51
57
@@ -61,7 +67,10 @@ pub type SyntaxContextKey = (SyntaxContext, ExpnId, Transparency);
61
67
62
68
#[ derive( Clone , Copy , Debug ) ]
63
69
struct SyntaxContextData {
70
+ /// The last macro expansion.
71
+ /// (Here we say the most deeply nested macro expansion is the "outermost" expansion.)
64
72
outer_expn : ExpnId ,
73
+ /// Transparency of the last macro expansion
65
74
outer_transparency : Transparency ,
66
75
parent : SyntaxContext ,
67
76
/// This context, but with all transparent and semi-opaque expansions filtered away.
@@ -450,11 +459,13 @@ impl HygieneData {
450
459
self . syntax_context_data [ ctxt. 0 as usize ] . opaque_and_semiopaque
451
460
}
452
461
462
+ /// See [`SyntaxContextData::outer_expn`]
453
463
#[ inline]
454
464
fn outer_expn ( & self , ctxt : SyntaxContext ) -> ExpnId {
455
465
self . syntax_context_data [ ctxt. 0 as usize ] . outer_expn
456
466
}
457
467
468
+ /// The last macro expansion and its Transparency
458
469
#[ inline]
459
470
fn outer_mark ( & self , ctxt : SyntaxContext ) -> ( ExpnId , Transparency ) {
460
471
let data = & self . syntax_context_data [ ctxt. 0 as usize ] ;
@@ -900,6 +911,7 @@ impl SyntaxContext {
900
911
HygieneData :: with ( |data| data. normalize_to_macro_rules ( self ) )
901
912
}
902
913
914
+ /// See [`SyntaxContextData::outer_expn`]
903
915
#[ inline]
904
916
pub fn outer_expn ( self ) -> ExpnId {
905
917
HygieneData :: with ( |data| data. outer_expn ( self ) )
@@ -912,6 +924,7 @@ impl SyntaxContext {
912
924
HygieneData :: with ( |data| data. expn_data ( data. outer_expn ( self ) ) . clone ( ) )
913
925
}
914
926
927
+ /// See [`HygieneData::outer_mark`]
915
928
#[ inline]
916
929
fn outer_mark ( self ) -> ( ExpnId , Transparency ) {
917
930
HygieneData :: with ( |data| data. outer_mark ( self ) )
@@ -986,15 +999,17 @@ pub struct ExpnData {
986
999
pub kind : ExpnKind ,
987
1000
/// The expansion that produced this expansion.
988
1001
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 {}`
1002
+ /// The span of the macro call which produced this expansion.
1003
+ ///
1004
+ /// This span will typically have a different `ExpnData` and `call_site`.
1005
+ /// This recursively traces back through any macro calls which expanded into further
1006
+ /// macro calls, until the "source call-site" is reached at the root SyntaxContext.
1007
+ /// For example, if `food!()` expands to `fruit!()` which then expands to `grape`,
1008
+ /// then the call-site of `grape` is `fruit!()` and the call-site of `fruit!()`
1009
+ /// is `food!()`.
991
1010
///
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.
1011
+ /// For a desugaring expansion, this is the span of the expression or node that was
1012
+ /// desugared.
998
1013
pub call_site : Span ,
999
1014
/// Used to force two `ExpnData`s to have different `Fingerprint`s.
1000
1015
/// Due to macro expansion, it's possible to end up with two `ExpnId`s
0 commit comments