diff options
| author | Jeffrey Seyfried <jeffrey.seyfried@gmail.com> | 2016-06-22 01:16:56 +0000 |
|---|---|---|
| committer | Jeffrey Seyfried <jeffrey.seyfried@gmail.com> | 2016-06-28 05:28:25 +0000 |
| commit | 36a4eb994099ad9763c32b229cac645cf76dff7a (patch) | |
| tree | c8ddb6de043a46dedc6ef7f4dada8393764c6af4 | |
| parent | 4a13bcb4fbd2a305ebc6906960a00f72342295a9 (diff) | |
| download | rust-36a4eb994099ad9763c32b229cac645cf76dff7a.tar.gz rust-36a4eb994099ad9763c32b229cac645cf76dff7a.zip | |
cleanup: refactor away `ast::NodeIdAssigner`
| -rw-r--r-- | src/librustc/hir/lowering.rs | 21 | ||||
| -rw-r--r-- | src/librustc/session/mod.rs | 19 | ||||
| -rw-r--r-- | src/librustc_driver/driver.rs | 6 | ||||
| -rw-r--r-- | src/librustc_metadata/astencode.rs | 21 | ||||
| -rw-r--r-- | src/libsyntax/ast.rs | 10 |
5 files changed, 20 insertions, 57 deletions
diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 5b655522f34..2cc39412182 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -50,6 +50,7 @@ use session::Session; use std::collections::BTreeMap; use std::iter; use syntax::ast::*; +use syntax::errors; use syntax::ptr::P; use syntax::codemap::{respan, Spanned}; use syntax::parse::token; @@ -60,7 +61,7 @@ use syntax_pos::Span; pub struct LoweringContext<'a> { crate_root: Option<&'static str>, // Use to assign ids to hir nodes that do not directly correspond to an ast node - id_assigner: &'a NodeIdAssigner, + sess: Option<&'a Session>, // As we walk the AST we must keep track of the current 'parent' def id (in // the form of a DefIndex) so that if we create a new node which introduces // a definition, then we can properly create the def id. @@ -99,7 +100,6 @@ impl Resolver for DummyResolver { pub fn lower_crate(sess: &Session, krate: &Crate, - id_assigner: &NodeIdAssigner, resolver: &mut Resolver) -> hir::Crate { // We're constructing the HIR here; we don't care what we will @@ -115,17 +115,17 @@ pub fn lower_crate(sess: &Session, } else { Some("std") }, - id_assigner: id_assigner, + sess: Some(sess), parent_def: None, resolver: resolver, }.lower_crate(krate) } impl<'a> LoweringContext<'a> { - pub fn testing_context(id_assigner: &'a NodeIdAssigner, resolver: &'a mut Resolver) -> Self { + pub fn testing_context(resolver: &'a mut Resolver) -> Self { LoweringContext { crate_root: None, - id_assigner: id_assigner, + sess: None, parent_def: None, resolver: resolver, } @@ -161,7 +161,12 @@ impl<'a> LoweringContext<'a> { } fn next_id(&self) -> NodeId { - self.id_assigner.next_node_id() + self.sess.map(Session::next_node_id).unwrap_or(0) + } + + fn diagnostic(&self) -> &errors::Handler { + self.sess.map(Session::diagnostic) + .unwrap_or_else(|| panic!("this lowerer cannot emit diagnostics")) } fn str_to_ident(&self, s: &'static str) -> Name { @@ -786,7 +791,7 @@ impl<'a> LoweringContext<'a> { if let Some(SelfKind::Explicit(..)) = sig.decl.get_self().map(|eself| eself.node) { match hir_sig.decl.get_self().map(|eself| eself.node) { Some(hir::SelfKind::Value(..)) | Some(hir::SelfKind::Region(..)) => { - self.id_assigner.diagnostic().span_err(sig.decl.inputs[0].ty.span, + self.diagnostic().span_err(sig.decl.inputs[0].ty.span, "the type placeholder `_` is not allowed within types on item signatures"); } _ => {} @@ -1212,7 +1217,7 @@ impl<'a> LoweringContext<'a> { make_struct(self, e, &["RangeInclusive", "NonEmpty"], &[("start", e1), ("end", e2)]), - _ => panic!(self.id_assigner.diagnostic() + _ => panic!(self.diagnostic() .span_fatal(e.span, "inclusive range with no end")), }; } diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 77259cea24d..fdaf182c605 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -20,7 +20,7 @@ use ty::tls; use util::nodemap::{NodeMap, FnvHashMap}; use mir::transform as mir_pass; -use syntax::ast::{NodeId, NodeIdAssigner, Name}; +use syntax::ast::{NodeId, Name}; use errors::{self, DiagnosticBuilder}; use errors::emitter::{Emitter, BasicEmitter, EmitterWriter}; use syntax::json::JsonEmitter; @@ -272,6 +272,9 @@ impl Session { id } + pub fn next_node_id(&self) -> NodeId { + self.reserve_node_ids(1) + } pub fn diagnostic<'a>(&'a self) -> &'a errors::Handler { &self.parse_sess.span_diagnostic } @@ -345,20 +348,6 @@ impl Session { } } -impl NodeIdAssigner for Session { - fn next_node_id(&self) -> NodeId { - self.reserve_node_ids(1) - } - - fn peek_node_id(&self) -> NodeId { - self.next_node_id.get().checked_add(1).unwrap() - } - - fn diagnostic(&self) -> &errors::Handler { - self.diagnostic() - } -} - fn split_msg_into_multilines(msg: &str) -> Option<String> { // Conditions for enabling multi-line errors: if !msg.contains("mismatched types") && diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index eb442c0a34e..980f0eeca18 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -49,13 +49,11 @@ use std::ffi::{OsString, OsStr}; use std::fs; use std::io::{self, Write}; use std::path::{Path, PathBuf}; -use syntax::ast::{self, NodeIdAssigner}; +use syntax::{ast, diagnostics, visit}; use syntax::attr::{self, AttrMetaMethods}; -use syntax::diagnostics; use syntax::fold::Folder; use syntax::parse::{self, PResult, token}; use syntax::util::node_count::NodeCounter; -use syntax::visit; use syntax; use syntax_ext; @@ -823,7 +821,7 @@ pub fn lower_and_resolve<'a>(sess: &Session, // Lower ast -> hir. let hir_forest = time(sess.time_passes(), "lowering ast -> hir", || { - hir_map::Forest::new(lower_crate(sess, krate, sess, &mut resolver), dep_graph) + hir_map::Forest::new(lower_crate(sess, krate, &mut resolver), dep_graph) }); (ty::CrateAnalysis { diff --git a/src/librustc_metadata/astencode.rs b/src/librustc_metadata/astencode.rs index dc37bdf6322..7ef00b971c5 100644 --- a/src/librustc_metadata/astencode.rs +++ b/src/librustc_metadata/astencode.rs @@ -38,7 +38,6 @@ use rustc::ty::subst; use rustc::ty::{self, Ty, TyCtxt}; use syntax::ast; -use syntax::ast::NodeIdAssigner; use syntax::ptr::P; use syntax_pos; @@ -56,7 +55,6 @@ use rustc_serialize::{Encodable, EncoderHelpers}; #[cfg(test)] use std::io::Cursor; #[cfg(test)] use syntax::parse; -#[cfg(test)] use syntax::ast::NodeId; #[cfg(test)] use rustc::hir::print as pprust; #[cfg(test)] use rustc::hir::lowering::{LoweringContext, DummyResolver}; @@ -1296,31 +1294,14 @@ impl FakeExtCtxt for parse::ParseSess { } #[cfg(test)] -struct FakeNodeIdAssigner; - -#[cfg(test)] -// It should go without saying that this may give unexpected results. Avoid -// lowering anything which needs new nodes. -impl NodeIdAssigner for FakeNodeIdAssigner { - fn next_node_id(&self) -> NodeId { - 0 - } - - fn peek_node_id(&self) -> NodeId { - 0 - } -} - -#[cfg(test)] fn mk_ctxt() -> parse::ParseSess { parse::ParseSess::new() } #[cfg(test)] fn with_testing_context<T, F: FnOnce(&mut LoweringContext) -> T>(f: F) -> T { - let assigner = FakeNodeIdAssigner; let mut resolver = DummyResolver; - let mut lcx = LoweringContext::testing_context(&assigner, &mut resolver); + let mut lcx = LoweringContext::testing_context(&mut resolver); f(&mut lcx) } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index a352715b20b..cc033cec8b8 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -19,7 +19,6 @@ pub use util::ThinVec; use syntax_pos::{mk_sp, Span, DUMMY_SP, ExpnId}; use codemap::{respan, Spanned}; use abi::Abi; -use errors; use parse::token::{self, keywords, InternedString}; use print::pprust; use ptr::P; @@ -362,15 +361,6 @@ pub const CRATE_NODE_ID: NodeId = 0; /// small, positive ids. pub const DUMMY_NODE_ID: NodeId = !0; -pub trait NodeIdAssigner { - fn next_node_id(&self) -> NodeId; - fn peek_node_id(&self) -> NodeId; - - fn diagnostic(&self) -> &errors::Handler { - panic!("this ID assigner cannot emit diagnostics") - } -} - /// The AST represents all type param bounds as types. /// typeck::collect::compute_bounds matches these against /// the "special" built-in traits (see middle::lang_items) and |
