@@ -87,6 +87,14 @@ pub struct SessionGlobals {
87
87
symbol_interner : symbol:: Interner ,
88
88
span_interner : Lock < span_encoding:: SpanInterner > ,
89
89
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`.
90
98
source_map : Lock < Option < Lrc < SourceMap > > > ,
91
99
}
92
100
@@ -1013,16 +1021,9 @@ impl<D: Decoder> Decodable<D> for Span {
1013
1021
}
1014
1022
}
1015
1023
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 {
1026
1027
with_session_globals ( |session_globals| {
1027
1028
* session_globals. source_map . borrow_mut ( ) = Some ( source_map) ;
1028
1029
} ) ;
@@ -1041,6 +1042,8 @@ pub fn with_source_map<T, F: FnOnce() -> T>(source_map: Lrc<SourceMap>, f: F) ->
1041
1042
1042
1043
impl fmt:: Debug for Span {
1043
1044
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.
1044
1047
with_session_globals ( |session_globals| {
1045
1048
if let Some ( source_map) = & * session_globals. source_map . borrow ( ) {
1046
1049
write ! ( f, "{} ({:?})" , source_map. span_to_diagnostic_string( * self ) , self . ctxt( ) )
0 commit comments