Skip to content

Commit b25fe99

Browse files
committed
Extended syntax::{fold, ast_map} to include lifetimes.
Part of this required added an override of `fold_type_method` in the Folder for Ctx impl; it follows the same pattern as `fold_method`. Also, as a drive-by fix, I moved all of the calls to `folder.new_id` in syntax::fold's no-op default traversal to really be the first statement in each function. * This is to uphold the invariant that `folder.new_id` is always called first (an unfortunate requirement of the current `ast_map` code), an invariant that we seemingly were breaking in e.g. the previous `noop_fold_block`. * Now it should be easier to see when adding new code that this invariant must be upheld. * (note that the breakage in `noop_fold_block` may not have mattered so much previously, since the only thing that blocks can bind are lifetimes, which I am only adding support for now.)
1 parent 0e30f07 commit b25fe99

File tree

3 files changed

+86
-33
lines changed

3 files changed

+86
-33
lines changed

src/librustc/middle/trans/monomorphize.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ pub fn monomorphic_fn(ccx: &CrateContext,
272272
}
273273

274274
// Ugh -- but this ensures any new variants won't be forgotten
275+
ast_map::NodeLifetime(..) |
275276
ast_map::NodeExpr(..) |
276277
ast_map::NodeStmt(..) |
277278
ast_map::NodeArg(..) |

src/libsyntax/ast_map.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ pub enum Node {
107107

108108
/// NodeStructCtor represents a tuple struct.
109109
NodeStructCtor(@StructDef),
110+
111+
NodeLifetime(@Lifetime),
110112
}
111113

112114
// The odd layout is to bring down the total size.
@@ -127,6 +129,7 @@ enum MapEntry {
127129
EntryLocal(NodeId, @Pat),
128130
EntryBlock(NodeId, P<Block>),
129131
EntryStructCtor(NodeId, @StructDef),
132+
EntryLifetime(NodeId, @Lifetime),
130133

131134
// Roots for node trees.
132135
RootCrate,
@@ -153,6 +156,7 @@ impl MapEntry {
153156
EntryLocal(id, _) => id,
154157
EntryBlock(id, _) => id,
155158
EntryStructCtor(id, _) => id,
159+
EntryLifetime(id, _) => id,
156160
_ => return None
157161
})
158162
}
@@ -170,6 +174,7 @@ impl MapEntry {
170174
EntryLocal(_, p) => NodeLocal(p),
171175
EntryBlock(_, p) => NodeBlock(p),
172176
EntryStructCtor(_, p) => NodeStructCtor(p),
177+
EntryLifetime(_, p) => NodeLifetime(p),
173178
_ => return None
174179
})
175180
}
@@ -213,6 +218,8 @@ impl Map {
213218
self.find_entry(id).and_then(|x| x.to_node())
214219
}
215220

221+
/// Retrieve the parent NodeId for `id`, or `id` itself if no
222+
/// parent is registered in this map.
216223
pub fn get_parent(&self, id: NodeId) -> NodeId {
217224
self.find_entry(id).and_then(|x| x.parent()).unwrap_or(id)
218225
}
@@ -500,6 +507,15 @@ impl<'a, F: FoldOps> Folder for Ctx<'a, F> {
500507
SmallVector::one(stmt)
501508
}
502509

510+
fn fold_type_method(&mut self, m: &TypeMethod) -> TypeMethod {
511+
let parent = self.parent;
512+
self.parent = DUMMY_NODE_ID;
513+
let m = fold::noop_fold_type_method(m, self);
514+
assert_eq!(self.parent, m.id);
515+
self.parent = parent;
516+
m
517+
}
518+
503519
fn fold_method(&mut self, m: @Method) -> @Method {
504520
let parent = self.parent;
505521
self.parent = DUMMY_NODE_ID;
@@ -522,6 +538,12 @@ impl<'a, F: FoldOps> Folder for Ctx<'a, F> {
522538
self.insert(block.id, EntryBlock(self.parent, block));
523539
block
524540
}
541+
542+
fn fold_lifetime(&mut self, lifetime: &Lifetime) -> Lifetime {
543+
let lifetime = fold::noop_fold_lifetime(lifetime, self);
544+
self.insert(lifetime.id, EntryLifetime(self.parent, @lifetime));
545+
lifetime
546+
}
525547
}
526548

527549
pub fn map_crate<F: FoldOps>(krate: Crate, fold_ops: F) -> (Crate, Map) {
@@ -658,6 +680,9 @@ fn node_id_to_str(map: &Map, id: NodeId) -> ~str {
658680
Some(NodeStructCtor(_)) => {
659681
format!("struct_ctor {} (id={})", map.path_to_str(id), id)
660682
}
683+
Some(NodeLifetime(ref l)) => {
684+
format!("lifetime {} (id={})", pprust::lifetime_to_str(*l), id)
685+
}
661686
None => {
662687
format!("unknown node (id={})", id)
663688
}

0 commit comments

Comments
 (0)