Skip to content

Commit b23ddc6

Browse files
committed
Implement get_enclosing_scope and use it in save-analysis
1 parent bbf0daa commit b23ddc6

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/librustc/ast_map/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,34 @@ impl<'ast> Map<'ast> {
321321
self.find_entry(id).and_then(|x| x.parent_node()).unwrap_or(id)
322322
}
323323

324+
/// Returns the nearest enclosing scope. A scope is an item or block.
325+
/// FIXME it is not clear to me that all items qualify as scopes - statics
326+
/// and associated types probably shouldn't, for example. Behaviour in this
327+
/// regard should be expected to be highly unstable.
328+
pub fn get_enclosing_scope(&self, id: NodeId) -> Option<NodeId> {
329+
let mut last_id = id;
330+
// Walk up the chain of parents until we find a 'scope'.
331+
loop {
332+
let cur_id = self.get_parent_node(last_id);
333+
if cur_id == last_id {
334+
return None;
335+
}
336+
337+
match self.get(cur_id) {
338+
NodeItem(_) |
339+
NodeForeignItem(_) |
340+
NodeTraitItem(_) |
341+
NodeImplItem(_) |
342+
NodeBlock(_) => {
343+
return Some(cur_id);
344+
}
345+
_ => {}
346+
}
347+
348+
last_id = cur_id;
349+
}
350+
}
351+
324352
pub fn get_parent_did(&self, id: NodeId) -> DefId {
325353
let parent = self.get_parent(id);
326354
match self.find_entry(parent) {

src/librustc_trans/save/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
172172
qualname: qualname,
173173
declaration: None,
174174
span: sub_span.unwrap(),
175-
scope: self.tcx.map.get_parent(item.id),
175+
scope: self.tcx.map.get_enclosing_scope(item.id).unwrap(),
176176
})
177177
}
178178
ast::ItemStatic(ref typ, mt, ref expr) => {
@@ -191,7 +191,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
191191
name: get_ident(item.ident).to_string(),
192192
qualname: qualname,
193193
span: sub_span.unwrap(),
194-
scope: self.tcx.map.get_parent(item.id),
194+
scope: self.tcx.map.get_enclosing_scope(item.id).unwrap(),
195195
value: value,
196196
type_value: ty_to_string(&typ),
197197
})
@@ -205,7 +205,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
205205
name: get_ident(item.ident).to_string(),
206206
qualname: qualname,
207207
span: sub_span.unwrap(),
208-
scope: self.tcx.map.get_parent(item.id),
208+
scope: self.tcx.map.get_enclosing_scope(item.id).unwrap(),
209209
value: self.span_utils.snippet(expr.span),
210210
type_value: ty_to_string(&typ),
211211
})
@@ -223,7 +223,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
223223
name: get_ident(item.ident).to_string(),
224224
qualname: qualname,
225225
span: sub_span.unwrap(),
226-
scope: self.tcx.map.get_parent(item.id),
226+
scope: self.tcx.map.get_enclosing_scope(item.id).unwrap(),
227227
filename: filename,
228228
})
229229
},
@@ -237,14 +237,14 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
237237
value: val,
238238
span: sub_span.unwrap(),
239239
qualname: enum_name,
240-
scope: self.tcx.map.get_parent(item.id),
240+
scope: self.tcx.map.get_enclosing_scope(item.id).unwrap(),
241241
})
242242
},
243243
ast::ItemImpl(_, _, _, ref trait_ref, ref typ, _) => {
244244
let mut type_data = None;
245245
let sub_span;
246246

247-
let parent = self.tcx.map.get_parent(item.id);
247+
let parent = self.tcx.map.get_enclosing_scope(item.id).unwrap();
248248

249249
match typ.node {
250250
// Common case impl for a struct or something basic.
@@ -337,7 +337,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
337337
return Some(Data::VariableRefData(VariableRefData {
338338
name: get_ident(ident.node).to_string(),
339339
span: sub_span.unwrap(),
340-
scope: self.tcx.map.get_parent(expr.id),
340+
scope: self.tcx.map.get_enclosing_scope(expr.id).unwrap(),
341341
ref_id: f.id,
342342
}));
343343
}
@@ -360,7 +360,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
360360
let sub_span = self.span_utils.span_for_last_ident(path.span);
361361
Some(Data::TypeRefData(TypeRefData {
362362
span: sub_span.unwrap(),
363-
scope: self.tcx.map.get_parent(expr.id),
363+
scope: self.tcx.map.get_enclosing_scope(expr.id).unwrap(),
364364
ref_id: def_id,
365365
}))
366366
}

0 commit comments

Comments
 (0)