Skip to content

Commit

Permalink
After expansion, fold and renumber AST nodes to ensure that each AST …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
nikomatsakis committed Sep 7, 2013
1 parent 510c4d8 commit 32c4d9b
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 142 deletions.
4 changes: 4 additions & 0 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ pub fn phase_2_configure_and_expand(sess: Session,
crate = time(time_passes, ~"std injection", ||
front::std_inject::maybe_inject_libstd_ref(sess, crate));

crate = time(time_passes, ~"renumbering AST", ||
front::renumber::renumber_crate(sess, crate));

return crate;
}

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

let time_passes = sess.time_passes();

let ast_map = time(time_passes, ~"ast indexing", ||
syntax::ast_map::map_crate(sess.diagnostic(), crate));

Expand Down
33 changes: 33 additions & 0 deletions src/librustc/front/renumber.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use driver::session::Session;

use syntax::ast;
use syntax::fold;

static STD_VERSION: &'static str = "0.8-pre";

pub fn renumber_crate(_sess: Session, crate: @ast::Crate) -> @ast::Crate {
let counter = @mut 0;

let precursor = @fold::AstFoldFns {
new_id: |_old_id| {
let new_id = *counter;
*counter += 1;
new_id
},
..*fold::default_ast_fold()
};

let fold = fold::make_fold(precursor);

@fold.fold_crate(crate)
}
10 changes: 9 additions & 1 deletion src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5401,7 +5401,15 @@ impl Resolver {

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

pub fn enforce_default_binding_mode(@mut self,
Expand Down
1 change: 1 addition & 0 deletions src/librustc/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub mod front {
pub mod config;
pub mod test;
pub mod std_inject;
pub mod renumber;
}

pub mod back {
Expand Down
Loading

0 comments on commit 32c4d9b

Please sign in to comment.