@@ -87,6 +87,14 @@ pub struct SessionGlobals {
8787 symbol_interner : symbol:: Interner ,
8888 span_interner : Lock < span_encoding:: SpanInterner > ,
8989 hygiene_data : Lock < hygiene:: HygieneData > ,
90+
91+ /// A reference to the source map in the `Session`. It's an `Option`
92+ /// because it can't be initialized until `Session` is created, which
93+ /// happens after `SessionGlobals`. `set_source_map` does the
94+ /// initialization.
95+ ///
96+ /// This field should only be used in places where the `Session` is truly
97+ /// not available, such as `<Span as Debug>::fmt`.
9098 source_map : Lock < Option < Lrc < SourceMap > > > ,
9199}
92100
@@ -1013,16 +1021,9 @@ impl<D: Decoder> Decodable<D> for Span {
10131021 }
10141022}
10151023
1016- /// Calls the provided closure, using the provided `SourceMap` to format
1017- /// any spans that are debug-printed during the closure's execution.
1018- ///
1019- /// Normally, the global `TyCtxt` is used to retrieve the `SourceMap`
1020- /// (see `rustc_interface::callbacks::span_debug1`). However, some parts
1021- /// of the compiler (e.g. `rustc_parse`) may debug-print `Span`s before
1022- /// a `TyCtxt` is available. In this case, we fall back to
1023- /// the `SourceMap` provided to this function. If that is not available,
1024- /// we fall back to printing the raw `Span` field values.
1025- pub fn with_source_map < T , F : FnOnce ( ) -> T > ( source_map : Lrc < SourceMap > , f : F ) -> T {
1024+ /// Insert `source_map` into the session globals for the duration of the
1025+ /// closure's execution.
1026+ pub fn set_source_map < T , F : FnOnce ( ) -> T > ( source_map : Lrc < SourceMap > , f : F ) -> T {
10261027 with_session_globals ( |session_globals| {
10271028 * session_globals. source_map . borrow_mut ( ) = Some ( source_map) ;
10281029 } ) ;
@@ -1041,6 +1042,8 @@ pub fn with_source_map<T, F: FnOnce() -> T>(source_map: Lrc<SourceMap>, f: F) ->
10411042
10421043impl fmt:: Debug for Span {
10431044 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1045+ // Use the global `SourceMap` to print the span. If that's not
1046+ // available, fall back to printing the raw values.
10441047 with_session_globals ( |session_globals| {
10451048 if let Some ( source_map) = & * session_globals. source_map . borrow ( ) {
10461049 write ! ( f, "{} ({:?})" , source_map. span_to_diagnostic_string( * self ) , self . ctxt( ) )
0 commit comments