@@ -1093,8 +1093,6 @@ impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Binder<T> {
1093
1093
}
1094
1094
1095
1095
pub mod tls {
1096
- use ast_map;
1097
- use middle:: def_id:: { DefId , DEF_ID_DEBUG , LOCAL_CRATE } ;
1098
1096
use middle:: ty;
1099
1097
use session:: Session ;
1100
1098
@@ -1108,28 +1106,6 @@ pub mod tls {
1108
1106
1109
1107
scoped_thread_local ! ( static TLS_TCX : ThreadLocalTyCx ) ;
1110
1108
1111
- fn def_id_debug ( def_id : DefId , f : & mut fmt:: Formatter ) -> fmt:: Result {
1112
- // Unfortunately, there seems to be no way to attempt to print
1113
- // a path for a def-id, so I'll just make a best effort for now
1114
- // and otherwise fallback to just printing the crate/node pair
1115
- with ( |tcx| {
1116
- if def_id. krate == LOCAL_CRATE {
1117
- match tcx. map . find ( def_id. node ) {
1118
- Some ( ast_map:: NodeItem ( ..) ) |
1119
- Some ( ast_map:: NodeForeignItem ( ..) ) |
1120
- Some ( ast_map:: NodeImplItem ( ..) ) |
1121
- Some ( ast_map:: NodeTraitItem ( ..) ) |
1122
- Some ( ast_map:: NodeVariant ( ..) ) |
1123
- Some ( ast_map:: NodeStructCtor ( ..) ) => {
1124
- return write ! ( f, "{}" , tcx. item_path_str( def_id) ) ;
1125
- }
1126
- _ => { }
1127
- }
1128
- }
1129
- Ok ( ( ) )
1130
- } )
1131
- }
1132
-
1133
1109
fn span_debug ( span : codemap:: Span , f : & mut fmt:: Formatter ) -> fmt:: Result {
1134
1110
with ( |tcx| {
1135
1111
write ! ( f, "{}" , tcx. sess. codemap( ) . span_to_string( span) )
@@ -1138,25 +1114,28 @@ pub mod tls {
1138
1114
1139
1115
pub fn enter < ' tcx , F : FnOnce ( & ty:: ctxt < ' tcx > ) -> R , R > ( tcx : ty:: ctxt < ' tcx > , f : F )
1140
1116
-> ( Session , R ) {
1141
- let result = DEF_ID_DEBUG . with ( |def_id_dbg| {
1142
- codemap:: SPAN_DEBUG . with ( |span_dbg| {
1143
- let original_def_id_debug = def_id_dbg. get ( ) ;
1144
- def_id_dbg. set ( def_id_debug) ;
1145
- let original_span_debug = span_dbg. get ( ) ;
1146
- span_dbg. set ( span_debug) ;
1147
- let tls_ptr = & tcx as * const _ as * const ThreadLocalTyCx ;
1148
- let result = TLS_TCX . set ( unsafe { & * tls_ptr } , || f ( & tcx) ) ;
1149
- def_id_dbg. set ( original_def_id_debug) ;
1150
- span_dbg. set ( original_span_debug) ;
1151
- result
1152
- } )
1117
+ let result = codemap:: SPAN_DEBUG . with ( |span_dbg| {
1118
+ let original_span_debug = span_dbg. get ( ) ;
1119
+ span_dbg. set ( span_debug) ;
1120
+ let tls_ptr = & tcx as * const _ as * const ThreadLocalTyCx ;
1121
+ let result = TLS_TCX . set ( unsafe { & * tls_ptr } , || f ( & tcx) ) ;
1122
+ span_dbg. set ( original_span_debug) ;
1123
+ result
1153
1124
} ) ;
1154
1125
( tcx. sess , result)
1155
1126
}
1156
1127
1157
1128
pub fn with < F : FnOnce ( & ty:: ctxt ) -> R , R > ( f : F ) -> R {
1158
1129
TLS_TCX . with ( |tcx| f ( unsafe { & * ( tcx as * const _ as * const ty:: ctxt ) } ) )
1159
1130
}
1131
+
1132
+ pub fn with_opt < F : FnOnce ( Option < & ty:: ctxt > ) -> R , R > ( f : F ) -> R {
1133
+ if TLS_TCX . is_set ( ) {
1134
+ with ( |v| f ( Some ( v) ) )
1135
+ } else {
1136
+ f ( None )
1137
+ }
1138
+ }
1160
1139
}
1161
1140
1162
1141
// Flags that we track on types. These flags are propagated upwards
0 commit comments