Skip to content

Commit 32c4d9b

Browse files
committed
After expansion, fold and renumber AST nodes to ensure that each AST node has a
unique id. Fixes numerous bugs in macro expansion and deriving. Add two representative tests. Fixes rust-lang#7971 Fixes rust-lang#6304 Fixes rust-lang#8367 Fixes rust-lang#8754 Fixes rust-lang#8852
1 parent 510c4d8 commit 32c4d9b

File tree

7 files changed

+307
-142
lines changed

7 files changed

+307
-142
lines changed

src/librustc/driver/driver.rs

+4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ pub fn phase_2_configure_and_expand(sess: Session,
190190
crate = time(time_passes, ~"std injection", ||
191191
front::std_inject::maybe_inject_libstd_ref(sess, crate));
192192

193+
crate = time(time_passes, ~"renumbering AST", ||
194+
front::renumber::renumber_crate(sess, crate));
195+
193196
return crate;
194197
}
195198

@@ -207,6 +210,7 @@ pub fn phase_3_run_analysis_passes(sess: Session,
207210
crate: @ast::Crate) -> CrateAnalysis {
208211

209212
let time_passes = sess.time_passes();
213+
210214
let ast_map = time(time_passes, ~"ast indexing", ||
211215
syntax::ast_map::map_crate(sess.diagnostic(), crate));
212216

src/librustc/front/renumber.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use driver::session::Session;
12+
13+
use syntax::ast;
14+
use syntax::fold;
15+
16+
static STD_VERSION: &'static str = "0.8-pre";
17+
18+
pub fn renumber_crate(_sess: Session, crate: @ast::Crate) -> @ast::Crate {
19+
let counter = @mut 0;
20+
21+
let precursor = @fold::AstFoldFns {
22+
new_id: |_old_id| {
23+
let new_id = *counter;
24+
*counter += 1;
25+
new_id
26+
},
27+
..*fold::default_ast_fold()
28+
};
29+
30+
let fold = fold::make_fold(precursor);
31+
32+
@fold.fold_crate(crate)
33+
}

src/librustc/middle/resolve.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -5401,7 +5401,15 @@ impl Resolver {
54015401

54025402
pub fn record_def(@mut self, node_id: NodeId, def: Def) {
54035403
debug!("(recording def) recording %? for %?", def, node_id);
5404-
self.def_map.insert(node_id, def);
5404+
do self.def_map.insert_or_update_with(node_id, def) |_, old_value| {
5405+
// Resolve appears to "resolve" the same ID multiple
5406+
// times, so here is a sanity check it at least comes to
5407+
// the same conclusion! - nmatsakis
5408+
if def != *old_value {
5409+
self.session.bug(fmt!("node_id %? resolved first to %? \
5410+
and then %?", node_id, *old_value, def));
5411+
}
5412+
};
54055413
}
54065414

54075415
pub fn enforce_default_binding_mode(@mut self,

src/librustc/rustc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub mod front {
8080
pub mod config;
8181
pub mod test;
8282
pub mod std_inject;
83+
pub mod renumber;
8384
}
8485

8586
pub mod back {

0 commit comments

Comments
 (0)