@@ -87,42 +87,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
87
87
prefix : & str ,
88
88
region : ty:: Region < ' tcx > ,
89
89
suffix : & str ) {
90
- fn item_scope_tag ( item : & hir:: Item ) -> & ' static str {
91
- match item. node {
92
- hir:: ItemImpl ( ..) => "impl" ,
93
- hir:: ItemStruct ( ..) => "struct" ,
94
- hir:: ItemUnion ( ..) => "union" ,
95
- hir:: ItemEnum ( ..) => "enum" ,
96
- hir:: ItemTrait ( ..) => "trait" ,
97
- hir:: ItemFn ( ..) => "function body" ,
98
- _ => "item"
99
- }
100
- }
101
-
102
- fn trait_item_scope_tag ( item : & hir:: TraitItem ) -> & ' static str {
103
- match item. node {
104
- hir:: TraitItemKind :: Method ( ..) => "method body" ,
105
- hir:: TraitItemKind :: Const ( ..) |
106
- hir:: TraitItemKind :: Type ( ..) => "associated item"
107
- }
108
- }
109
-
110
- fn impl_item_scope_tag ( item : & hir:: ImplItem ) -> & ' static str {
111
- match item. node {
112
- hir:: ImplItemKind :: Method ( ..) => "method body" ,
113
- hir:: ImplItemKind :: Const ( ..) |
114
- hir:: ImplItemKind :: Type ( _) => "associated item"
115
- }
116
- }
117
-
118
- fn explain_span < ' a , ' gcx , ' tcx > ( tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
119
- heading : & str , span : Span )
120
- -> ( String , Option < Span > ) {
121
- let lo = tcx. sess . codemap ( ) . lookup_char_pos_adj ( span. lo ( ) ) ;
122
- ( format ! ( "the {} at {}:{}" , heading, lo. line, lo. col. to_usize( ) + 1 ) ,
123
- Some ( span) )
124
- }
125
-
126
90
let ( description, span) = match * region {
127
91
ty:: ReScope ( scope) => {
128
92
let new_string;
@@ -143,9 +107,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
143
107
_ => "expression" ,
144
108
} ,
145
109
Some ( hir_map:: NodeStmt ( _) ) => "statement" ,
146
- Some ( hir_map:: NodeItem ( it) ) => item_scope_tag ( & it) ,
147
- Some ( hir_map:: NodeTraitItem ( it) ) => trait_item_scope_tag ( & it) ,
148
- Some ( hir_map:: NodeImplItem ( it) ) => impl_item_scope_tag ( & it) ,
110
+ Some ( hir_map:: NodeItem ( it) ) => Self :: item_scope_tag ( & it) ,
111
+ Some ( hir_map:: NodeTraitItem ( it) ) => Self :: trait_item_scope_tag ( & it) ,
112
+ Some ( hir_map:: NodeImplItem ( it) ) => Self :: impl_item_scope_tag ( & it) ,
149
113
Some ( _) | None => {
150
114
err. span_note ( span, & unknown_scope ( ) ) ;
151
115
return ;
@@ -169,57 +133,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
169
133
& new_string[ ..]
170
134
}
171
135
} ;
172
- explain_span ( self , scope_decorated_tag, span)
136
+ self . explain_span ( scope_decorated_tag, span)
173
137
}
174
138
175
139
ty:: ReEarlyBound ( _) |
176
140
ty:: ReFree ( _) => {
177
- let scope = region. free_region_binding_scope ( self ) ;
178
- let node = self . hir . as_local_node_id ( scope)
179
- . unwrap_or ( DUMMY_NODE_ID ) ;
180
- let unknown;
181
- let tag = match self . hir . find ( node) {
182
- Some ( hir_map:: NodeBlock ( _) ) |
183
- Some ( hir_map:: NodeExpr ( _) ) => "body" ,
184
- Some ( hir_map:: NodeItem ( it) ) => item_scope_tag ( & it) ,
185
- Some ( hir_map:: NodeTraitItem ( it) ) => trait_item_scope_tag ( & it) ,
186
- Some ( hir_map:: NodeImplItem ( it) ) => impl_item_scope_tag ( & it) ,
187
-
188
- // this really should not happen, but it does:
189
- // FIXME(#27942)
190
- Some ( _) => {
191
- unknown = format ! ( "unexpected node ({}) for scope {:?}. \
192
- Please report a bug.",
193
- self . hir. node_to_string( node) , scope) ;
194
- & unknown
195
- }
196
- None => {
197
- unknown = format ! ( "unknown node for scope {:?}. \
198
- Please report a bug.", scope) ;
199
- & unknown
200
- }
201
- } ;
202
- let ( prefix, span) = match * region {
203
- ty:: ReEarlyBound ( ref br) => {
204
- ( format ! ( "the lifetime {} as defined on" , br. name) ,
205
- self . sess . codemap ( ) . def_span ( self . hir . span ( node) ) )
206
- }
207
- ty:: ReFree ( ref fr) => {
208
- match fr. bound_region {
209
- ty:: BrAnon ( idx) => {
210
- ( format ! ( "the anonymous lifetime #{} defined on" , idx + 1 ) ,
211
- self . hir . span ( node) )
212
- }
213
- ty:: BrFresh ( _) => ( "an anonymous lifetime defined on" . to_owned ( ) ,
214
- self . hir . span ( node) ) ,
215
- _ => ( format ! ( "the lifetime {} as defined on" , fr. bound_region) ,
216
- self . sess . codemap ( ) . def_span ( self . hir . span ( node) ) ) ,
217
- }
218
- }
219
- _ => bug ! ( )
220
- } ;
221
- let ( msg, opt_span) = explain_span ( self , tag, span) ;
222
- ( format ! ( "{} {}" , prefix, msg) , opt_span)
141
+ self . msg_span_from_free_region ( region)
223
142
}
224
143
225
144
ty:: ReStatic => ( "the static lifetime" . to_owned ( ) , None ) ,
@@ -253,6 +172,93 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
253
172
err. note ( & message) ;
254
173
}
255
174
}
175
+
176
+ fn msg_span_from_free_region ( self ,
177
+ region : ty:: Region < ' tcx > )
178
+ -> ( String , Option < Span > ) {
179
+ let scope = region. free_region_binding_scope ( self ) ;
180
+ let node = self . hir . as_local_node_id ( scope)
181
+ . unwrap_or ( DUMMY_NODE_ID ) ;
182
+ let unknown;
183
+ let tag = match self . hir . find ( node) {
184
+ Some ( hir_map:: NodeBlock ( _) ) |
185
+ Some ( hir_map:: NodeExpr ( _) ) => "body" ,
186
+ Some ( hir_map:: NodeItem ( it) ) => Self :: item_scope_tag ( & it) ,
187
+ Some ( hir_map:: NodeTraitItem ( it) ) => Self :: trait_item_scope_tag ( & it) ,
188
+ Some ( hir_map:: NodeImplItem ( it) ) => Self :: impl_item_scope_tag ( & it) ,
189
+
190
+ // this really should not happen, but it does:
191
+ // FIXME(#27942)
192
+ Some ( _) => {
193
+ unknown = format ! ( "unexpected node ({}) for scope {:?}. \
194
+ Please report a bug.",
195
+ self . hir. node_to_string( node) , scope) ;
196
+ & unknown
197
+ }
198
+ None => {
199
+ unknown = format ! ( "unknown node for scope {:?}. \
200
+ Please report a bug.", scope) ;
201
+ & unknown
202
+ }
203
+ } ;
204
+ let ( prefix, span) = match * region {
205
+ ty:: ReEarlyBound ( ref br) => {
206
+ ( format ! ( "the lifetime {} as defined on" , br. name) ,
207
+ self . sess . codemap ( ) . def_span ( self . hir . span ( node) ) )
208
+ }
209
+ ty:: ReFree ( ref fr) => {
210
+ match fr. bound_region {
211
+ ty:: BrAnon ( idx) => {
212
+ ( format ! ( "the anonymous lifetime #{} defined on" , idx + 1 ) ,
213
+ self . hir . span ( node) )
214
+ }
215
+ ty:: BrFresh ( _) => ( "an anonymous lifetime defined on" . to_owned ( ) ,
216
+ self . hir . span ( node) ) ,
217
+ _ => ( format ! ( "the lifetime {} as defined on" , fr. bound_region) ,
218
+ self . sess . codemap ( ) . def_span ( self . hir . span ( node) ) ) ,
219
+ }
220
+ }
221
+ _ => bug ! ( )
222
+ } ;
223
+ let ( msg, opt_span) = self . explain_span ( tag, span) ;
224
+ ( format ! ( "{} {}" , prefix, msg) , opt_span)
225
+ }
226
+
227
+ fn item_scope_tag ( item : & hir:: Item ) -> & ' static str {
228
+ match item. node {
229
+ hir:: ItemImpl ( ..) => "impl" ,
230
+ hir:: ItemStruct ( ..) => "struct" ,
231
+ hir:: ItemUnion ( ..) => "union" ,
232
+ hir:: ItemEnum ( ..) => "enum" ,
233
+ hir:: ItemTrait ( ..) => "trait" ,
234
+ hir:: ItemFn ( ..) => "function body" ,
235
+ _ => "item"
236
+ }
237
+ }
238
+
239
+ fn trait_item_scope_tag ( item : & hir:: TraitItem ) -> & ' static str {
240
+ match item. node {
241
+ hir:: TraitItemKind :: Method ( ..) => "method body" ,
242
+ hir:: TraitItemKind :: Const ( ..) |
243
+ hir:: TraitItemKind :: Type ( ..) => "associated item"
244
+ }
245
+ }
246
+
247
+ fn impl_item_scope_tag ( item : & hir:: ImplItem ) -> & ' static str {
248
+ match item. node {
249
+ hir:: ImplItemKind :: Method ( ..) => "method body" ,
250
+ hir:: ImplItemKind :: Const ( ..) |
251
+ hir:: ImplItemKind :: Type ( _) => "associated item"
252
+ }
253
+ }
254
+
255
+ fn explain_span ( self ,
256
+ heading : & str , span : Span )
257
+ -> ( String , Option < Span > ) {
258
+ let lo = self . sess . codemap ( ) . lookup_char_pos_adj ( span. lo ( ) ) ;
259
+ ( format ! ( "the {} at {}:{}" , heading, lo. line, lo. col. to_usize( ) + 1 ) ,
260
+ Some ( span) )
261
+ }
256
262
}
257
263
258
264
impl < ' a , ' gcx , ' tcx > InferCtxt < ' a , ' gcx , ' tcx > {
0 commit comments