Skip to content

Commit 36a4eb9

Browse files
committed
cleanup: refactor away ast::NodeIdAssigner
1 parent 4a13bcb commit 36a4eb9

File tree

5 files changed

+20
-57
lines changed

5 files changed

+20
-57
lines changed

src/librustc/hir/lowering.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use session::Session;
5050
use std::collections::BTreeMap;
5151
use std::iter;
5252
use syntax::ast::*;
53+
use syntax::errors;
5354
use syntax::ptr::P;
5455
use syntax::codemap::{respan, Spanned};
5556
use syntax::parse::token;
@@ -60,7 +61,7 @@ use syntax_pos::Span;
6061
pub struct LoweringContext<'a> {
6162
crate_root: Option<&'static str>,
6263
// Use to assign ids to hir nodes that do not directly correspond to an ast node
63-
id_assigner: &'a NodeIdAssigner,
64+
sess: Option<&'a Session>,
6465
// As we walk the AST we must keep track of the current 'parent' def id (in
6566
// the form of a DefIndex) so that if we create a new node which introduces
6667
// a definition, then we can properly create the def id.
@@ -99,7 +100,6 @@ impl Resolver for DummyResolver {
99100

100101
pub fn lower_crate(sess: &Session,
101102
krate: &Crate,
102-
id_assigner: &NodeIdAssigner,
103103
resolver: &mut Resolver)
104104
-> hir::Crate {
105105
// We're constructing the HIR here; we don't care what we will
@@ -115,17 +115,17 @@ pub fn lower_crate(sess: &Session,
115115
} else {
116116
Some("std")
117117
},
118-
id_assigner: id_assigner,
118+
sess: Some(sess),
119119
parent_def: None,
120120
resolver: resolver,
121121
}.lower_crate(krate)
122122
}
123123

124124
impl<'a> LoweringContext<'a> {
125-
pub fn testing_context(id_assigner: &'a NodeIdAssigner, resolver: &'a mut Resolver) -> Self {
125+
pub fn testing_context(resolver: &'a mut Resolver) -> Self {
126126
LoweringContext {
127127
crate_root: None,
128-
id_assigner: id_assigner,
128+
sess: None,
129129
parent_def: None,
130130
resolver: resolver,
131131
}
@@ -161,7 +161,12 @@ impl<'a> LoweringContext<'a> {
161161
}
162162

163163
fn next_id(&self) -> NodeId {
164-
self.id_assigner.next_node_id()
164+
self.sess.map(Session::next_node_id).unwrap_or(0)
165+
}
166+
167+
fn diagnostic(&self) -> &errors::Handler {
168+
self.sess.map(Session::diagnostic)
169+
.unwrap_or_else(|| panic!("this lowerer cannot emit diagnostics"))
165170
}
166171

167172
fn str_to_ident(&self, s: &'static str) -> Name {
@@ -786,7 +791,7 @@ impl<'a> LoweringContext<'a> {
786791
if let Some(SelfKind::Explicit(..)) = sig.decl.get_self().map(|eself| eself.node) {
787792
match hir_sig.decl.get_self().map(|eself| eself.node) {
788793
Some(hir::SelfKind::Value(..)) | Some(hir::SelfKind::Region(..)) => {
789-
self.id_assigner.diagnostic().span_err(sig.decl.inputs[0].ty.span,
794+
self.diagnostic().span_err(sig.decl.inputs[0].ty.span,
790795
"the type placeholder `_` is not allowed within types on item signatures");
791796
}
792797
_ => {}
@@ -1212,7 +1217,7 @@ impl<'a> LoweringContext<'a> {
12121217
make_struct(self, e, &["RangeInclusive", "NonEmpty"],
12131218
&[("start", e1), ("end", e2)]),
12141219

1215-
_ => panic!(self.id_assigner.diagnostic()
1220+
_ => panic!(self.diagnostic()
12161221
.span_fatal(e.span, "inclusive range with no end")),
12171222
};
12181223
}

src/librustc/session/mod.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use ty::tls;
2020
use util::nodemap::{NodeMap, FnvHashMap};
2121
use mir::transform as mir_pass;
2222

23-
use syntax::ast::{NodeId, NodeIdAssigner, Name};
23+
use syntax::ast::{NodeId, Name};
2424
use errors::{self, DiagnosticBuilder};
2525
use errors::emitter::{Emitter, BasicEmitter, EmitterWriter};
2626
use syntax::json::JsonEmitter;
@@ -272,6 +272,9 @@ impl Session {
272272

273273
id
274274
}
275+
pub fn next_node_id(&self) -> NodeId {
276+
self.reserve_node_ids(1)
277+
}
275278
pub fn diagnostic<'a>(&'a self) -> &'a errors::Handler {
276279
&self.parse_sess.span_diagnostic
277280
}
@@ -345,20 +348,6 @@ impl Session {
345348
}
346349
}
347350

348-
impl NodeIdAssigner for Session {
349-
fn next_node_id(&self) -> NodeId {
350-
self.reserve_node_ids(1)
351-
}
352-
353-
fn peek_node_id(&self) -> NodeId {
354-
self.next_node_id.get().checked_add(1).unwrap()
355-
}
356-
357-
fn diagnostic(&self) -> &errors::Handler {
358-
self.diagnostic()
359-
}
360-
}
361-
362351
fn split_msg_into_multilines(msg: &str) -> Option<String> {
363352
// Conditions for enabling multi-line errors:
364353
if !msg.contains("mismatched types") &&

src/librustc_driver/driver.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,11 @@ use std::ffi::{OsString, OsStr};
4949
use std::fs;
5050
use std::io::{self, Write};
5151
use std::path::{Path, PathBuf};
52-
use syntax::ast::{self, NodeIdAssigner};
52+
use syntax::{ast, diagnostics, visit};
5353
use syntax::attr::{self, AttrMetaMethods};
54-
use syntax::diagnostics;
5554
use syntax::fold::Folder;
5655
use syntax::parse::{self, PResult, token};
5756
use syntax::util::node_count::NodeCounter;
58-
use syntax::visit;
5957
use syntax;
6058
use syntax_ext;
6159

@@ -823,7 +821,7 @@ pub fn lower_and_resolve<'a>(sess: &Session,
823821

824822
// Lower ast -> hir.
825823
let hir_forest = time(sess.time_passes(), "lowering ast -> hir", || {
826-
hir_map::Forest::new(lower_crate(sess, krate, sess, &mut resolver), dep_graph)
824+
hir_map::Forest::new(lower_crate(sess, krate, &mut resolver), dep_graph)
827825
});
828826

829827
(ty::CrateAnalysis {

src/librustc_metadata/astencode.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use rustc::ty::subst;
3838
use rustc::ty::{self, Ty, TyCtxt};
3939

4040
use syntax::ast;
41-
use syntax::ast::NodeIdAssigner;
4241
use syntax::ptr::P;
4342
use syntax_pos;
4443

@@ -56,7 +55,6 @@ use rustc_serialize::{Encodable, EncoderHelpers};
5655

5756
#[cfg(test)] use std::io::Cursor;
5857
#[cfg(test)] use syntax::parse;
59-
#[cfg(test)] use syntax::ast::NodeId;
6058
#[cfg(test)] use rustc::hir::print as pprust;
6159
#[cfg(test)] use rustc::hir::lowering::{LoweringContext, DummyResolver};
6260

@@ -1295,32 +1293,15 @@ impl FakeExtCtxt for parse::ParseSess {
12951293
fn parse_sess(&self) -> &parse::ParseSess { self }
12961294
}
12971295

1298-
#[cfg(test)]
1299-
struct FakeNodeIdAssigner;
1300-
1301-
#[cfg(test)]
1302-
// It should go without saying that this may give unexpected results. Avoid
1303-
// lowering anything which needs new nodes.
1304-
impl NodeIdAssigner for FakeNodeIdAssigner {
1305-
fn next_node_id(&self) -> NodeId {
1306-
0
1307-
}
1308-
1309-
fn peek_node_id(&self) -> NodeId {
1310-
0
1311-
}
1312-
}
1313-
13141296
#[cfg(test)]
13151297
fn mk_ctxt() -> parse::ParseSess {
13161298
parse::ParseSess::new()
13171299
}
13181300

13191301
#[cfg(test)]
13201302
fn with_testing_context<T, F: FnOnce(&mut LoweringContext) -> T>(f: F) -> T {
1321-
let assigner = FakeNodeIdAssigner;
13221303
let mut resolver = DummyResolver;
1323-
let mut lcx = LoweringContext::testing_context(&assigner, &mut resolver);
1304+
let mut lcx = LoweringContext::testing_context(&mut resolver);
13241305
f(&mut lcx)
13251306
}
13261307

src/libsyntax/ast.rs

-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub use util::ThinVec;
1919
use syntax_pos::{mk_sp, Span, DUMMY_SP, ExpnId};
2020
use codemap::{respan, Spanned};
2121
use abi::Abi;
22-
use errors;
2322
use parse::token::{self, keywords, InternedString};
2423
use print::pprust;
2524
use ptr::P;
@@ -362,15 +361,6 @@ pub const CRATE_NODE_ID: NodeId = 0;
362361
/// small, positive ids.
363362
pub const DUMMY_NODE_ID: NodeId = !0;
364363

365-
pub trait NodeIdAssigner {
366-
fn next_node_id(&self) -> NodeId;
367-
fn peek_node_id(&self) -> NodeId;
368-
369-
fn diagnostic(&self) -> &errors::Handler {
370-
panic!("this ID assigner cannot emit diagnostics")
371-
}
372-
}
373-
374364
/// The AST represents all type param bounds as types.
375365
/// typeck::collect::compute_bounds matches these against
376366
/// the "special" built-in traits (see middle::lang_items) and

0 commit comments

Comments
 (0)