Skip to content

Commit 0ed2bae

Browse files
committed
---
yaml --- r: 227988 b: refs/heads/try c: b23ddc6 h: refs/heads/master v: v3
1 parent 0825185 commit 0ed2bae

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: aca2057ed5fb7af3f8905b2bc01f72fa001c35c8
33
refs/heads/snap-stage3: 1af31d4974e33027a68126fa5a5a3c2c6491824f
4-
refs/heads/try: bbf0daa19276354d5759d0b0bd7d31bcd3cc301c
4+
refs/heads/try: b23ddc60e9fcb188421060a83f1af2b815fc60e2
55
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
66
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
77
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try/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) {

branches/try/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)