Skip to content

Commit 195a27f

Browse files
committed
Move node id assigning into resolve
1 parent 3265bd5 commit 195a27f

File tree

3 files changed

+73
-57
lines changed

3 files changed

+73
-57
lines changed

src/librustc_driver/driver.rs

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ use std::io::{self, Write};
5050
use std::path::{Path, PathBuf};
5151
use syntax::{ast, diagnostics, visit};
5252
use syntax::attr::{self, AttrMetaMethods};
53-
use syntax::fold::Folder;
5453
use syntax::parse::{self, PResult, token};
5554
use syntax::util::node_count::NodeCounter;
5655
use syntax;
@@ -695,6 +694,19 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
695694
sess.diagnostic())
696695
});
697696

697+
let resolver_arenas = Resolver::arenas();
698+
let mut resolver = Resolver::new(sess, make_glob_map, &resolver_arenas);
699+
700+
let krate = time(sess.time_passes(), "assigning node ids", || resolver.assign_node_ids(krate));
701+
702+
if sess.opts.debugging_opts.input_stats {
703+
println!("Post-expansion node count: {}", count_nodes(&krate));
704+
}
705+
706+
if sess.opts.debugging_opts.ast_json {
707+
println!("{}", json::as_json(&krate));
708+
}
709+
698710
time(time_passes,
699711
"checking for inline asm in case the target doesn't support it",
700712
|| no_asm::check_crate(sess, &krate));
@@ -710,15 +722,6 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
710722
})
711723
})?;
712724

713-
if sess.opts.debugging_opts.input_stats {
714-
println!("Post-expansion node count: {}", count_nodes(&krate));
715-
}
716-
717-
krate = assign_node_ids(sess, krate);
718-
719-
let resolver_arenas = Resolver::arenas();
720-
let mut resolver = Resolver::new(sess, make_glob_map, &resolver_arenas);
721-
722725
// Collect defintions for def ids.
723726
time(sess.time_passes(), "collecting defs", || resolver.definitions.collect(&krate));
724727

@@ -783,53 +786,6 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
783786
})
784787
}
785788

786-
pub fn assign_node_ids(sess: &Session, krate: ast::Crate) -> ast::Crate {
787-
use syntax::ptr::P;
788-
use syntax::util::move_map::MoveMap;
789-
790-
struct NodeIdAssigner<'a> {
791-
sess: &'a Session,
792-
}
793-
794-
impl<'a> Folder for NodeIdAssigner<'a> {
795-
fn new_id(&mut self, old_id: ast::NodeId) -> ast::NodeId {
796-
assert_eq!(old_id, ast::DUMMY_NODE_ID);
797-
self.sess.next_node_id()
798-
}
799-
800-
fn fold_block(&mut self, block: P<ast::Block>) -> P<ast::Block> {
801-
block.map(|mut block| {
802-
block.id = self.new_id(block.id);
803-
804-
let stmt = block.stmts.pop();
805-
block.stmts = block.stmts.move_flat_map(|s| self.fold_stmt(s).into_iter());
806-
if let Some(ast::Stmt { node: ast::StmtKind::Expr(expr), span, .. }) = stmt {
807-
let expr = self.fold_expr(expr);
808-
block.stmts.push(ast::Stmt {
809-
id: expr.id,
810-
node: ast::StmtKind::Expr(expr),
811-
span: span,
812-
});
813-
} else if let Some(stmt) = stmt {
814-
block.stmts.extend(self.fold_stmt(stmt));
815-
}
816-
817-
block
818-
})
819-
}
820-
}
821-
822-
let krate = time(sess.time_passes(),
823-
"assigning node ids",
824-
|| NodeIdAssigner { sess: sess }.fold_crate(krate));
825-
826-
if sess.opts.debugging_opts.ast_json {
827-
println!("{}", json::as_json(&krate));
828-
}
829-
830-
krate
831-
}
832-
833789
/// Run the resolution, typechecking, region checking and other
834790
/// miscellaneous analysis passes on the crate. Return various
835791
/// structures carrying the results of the analysis.

src/librustc_resolve/assign_ids.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2016 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 Resolver;
12+
use rustc::session::Session;
13+
use syntax::ast;
14+
use syntax::fold::Folder;
15+
use syntax::ptr::P;
16+
use syntax::util::move_map::MoveMap;
17+
18+
impl<'a> Resolver<'a> {
19+
pub fn assign_node_ids(&mut self, krate: ast::Crate) -> ast::Crate {
20+
NodeIdAssigner {
21+
sess: self.session,
22+
}.fold_crate(krate)
23+
}
24+
}
25+
26+
struct NodeIdAssigner<'a> {
27+
sess: &'a Session,
28+
}
29+
30+
impl<'a> Folder for NodeIdAssigner<'a> {
31+
fn new_id(&mut self, old_id: ast::NodeId) -> ast::NodeId {
32+
assert_eq!(old_id, ast::DUMMY_NODE_ID);
33+
self.sess.next_node_id()
34+
}
35+
36+
fn fold_block(&mut self, block: P<ast::Block>) -> P<ast::Block> {
37+
block.map(|mut block| {
38+
block.id = self.new_id(block.id);
39+
40+
let stmt = block.stmts.pop();
41+
block.stmts = block.stmts.move_flat_map(|s| self.fold_stmt(s).into_iter());
42+
if let Some(ast::Stmt { node: ast::StmtKind::Expr(expr), span, .. }) = stmt {
43+
// Avoid wasting a node id on a trailing expression statement,
44+
// which shares a HIR node with the expression itself.
45+
let expr = self.fold_expr(expr);
46+
block.stmts.push(ast::Stmt {
47+
id: expr.id,
48+
node: ast::StmtKind::Expr(expr),
49+
span: span,
50+
});
51+
} else if let Some(stmt) = stmt {
52+
block.stmts.extend(self.fold_stmt(stmt));
53+
}
54+
55+
block
56+
})
57+
}
58+
}
59+

src/librustc_resolve/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ mod diagnostics;
8383
mod check_unused;
8484
mod build_reduced_graph;
8585
mod resolve_imports;
86+
mod assign_ids;
8687

8788
enum SuggestionType {
8889
Macro(String),

0 commit comments

Comments
 (0)