Skip to content

Commit 516a817

Browse files
Move next_node_id to Resolver
This doesn't migrate the pretty-printing everybody loops, which will be done in the next few commits.
1 parent 5a50275 commit 516a817

File tree

12 files changed

+64
-64
lines changed

12 files changed

+64
-64
lines changed

src/librustc/hir/lowering.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ pub trait Resolver {
183183
) -> (ast::Path, Res<NodeId>);
184184

185185
fn lint_buffer(&mut self) -> &mut lint::LintBuffer;
186+
187+
fn next_node_id(&mut self) -> NodeId;
186188
}
187189

188190
type NtToTokenstream = fn(&Nonterminal, &ParseSess, Span) -> TokenStream;
@@ -672,7 +674,8 @@ impl<'a> LoweringContext<'a> {
672674
}
673675

674676
fn next_id(&mut self) -> hir::HirId {
675-
self.lower_node_id(self.sess.next_node_id())
677+
let node_id = self.resolver.next_node_id();
678+
self.lower_node_id(node_id)
676679
}
677680

678681
fn lower_res(&mut self, res: Res<NodeId>) -> Res {
@@ -781,7 +784,7 @@ impl<'a> LoweringContext<'a> {
781784
hir_name: ParamName,
782785
parent_index: DefIndex,
783786
) -> hir::GenericParam {
784-
let node_id = self.sess.next_node_id();
787+
let node_id = self.resolver.next_node_id();
785788

786789
// Get the name we'll use to make the def-path. Note
787790
// that collisions are ok here and this shouldn't
@@ -1106,7 +1109,7 @@ impl<'a> LoweringContext<'a> {
11061109
// Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by
11071110
// constructing the HIR for `impl bounds...` and then lowering that.
11081111

1109-
let impl_trait_node_id = self.sess.next_node_id();
1112+
let impl_trait_node_id = self.resolver.next_node_id();
11101113
let parent_def_index = self.current_hir_id_owner.last().unwrap().0;
11111114
self.resolver.definitions().create_def_with_parent(
11121115
parent_def_index,
@@ -1117,9 +1120,10 @@ impl<'a> LoweringContext<'a> {
11171120
);
11181121

11191122
self.with_dyn_type_scope(false, |this| {
1123+
let node_id = this.resolver.next_node_id();
11201124
let ty = this.lower_ty(
11211125
&Ty {
1122-
id: this.sess.next_node_id(),
1126+
id: node_id,
11231127
kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
11241128
span: constraint.span,
11251129
},
@@ -1586,7 +1590,7 @@ impl<'a> LoweringContext<'a> {
15861590
name,
15871591
}));
15881592

1589-
let def_node_id = self.context.sess.next_node_id();
1593+
let def_node_id = self.context.resolver.next_node_id();
15901594
let hir_id =
15911595
self.context.lower_node_id_with_owner(def_node_id, self.opaque_ty_id);
15921596
self.context.resolver.definitions().create_def_with_parent(
@@ -3234,7 +3238,7 @@ impl<'a> LoweringContext<'a> {
32343238
Some(id) => (id, "`'_` cannot be used here", "`'_` is a reserved lifetime name"),
32353239

32363240
None => (
3237-
self.sess.next_node_id(),
3241+
self.resolver.next_node_id(),
32383242
"`&` without an explicit lifetime name cannot be used here",
32393243
"explicit lifetime name needed here",
32403244
),
@@ -3271,7 +3275,7 @@ impl<'a> LoweringContext<'a> {
32713275
span,
32723276
"expected 'implicit elided lifetime not allowed' error",
32733277
);
3274-
let id = self.sess.next_node_id();
3278+
let id = self.resolver.next_node_id();
32753279
self.new_named_lifetime(id, span, hir::LifetimeName::Error)
32763280
}
32773281
// `PassThrough` is the normal case.

src/librustc/hir/lowering/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ impl LoweringContext<'_> {
595595
};
596596

597597
// `::std::task::Poll::Ready(result) => break result`
598-
let loop_node_id = self.sess.next_node_id();
598+
let loop_node_id = self.resolver.next_node_id();
599599
let loop_hir_id = self.lower_node_id(loop_node_id);
600600
let ready_arm = {
601601
let x_ident = Ident::with_dummy_span(sym::result);

src/librustc/hir/lowering/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ impl LoweringContext<'_> {
522522
let ident = *ident;
523523
let mut path = path.clone();
524524
for seg in &mut path.segments {
525-
seg.id = self.sess.next_node_id();
525+
seg.id = self.resolver.next_node_id();
526526
}
527527
let span = path.span;
528528

@@ -599,7 +599,7 @@ impl LoweringContext<'_> {
599599

600600
// Give the segments new node-ids since they are being cloned.
601601
for seg in &mut prefix.segments {
602-
seg.id = self.sess.next_node_id();
602+
seg.id = self.resolver.next_node_id();
603603
}
604604

605605
// Each `use` import is an item and thus are owners of the

src/librustc/session/mod.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use errors::{DiagnosticBuilder, DiagnosticId, Applicability};
2121
use errors::emitter::{Emitter, EmitterWriter};
2222
use errors::emitter::HumanReadableErrorType;
2323
use errors::annotate_snippet_emitter_writer::{AnnotateSnippetEmitterWriter};
24-
use syntax::ast::{self, NodeId};
2524
use syntax::edition::Edition;
2625
use syntax::expand::allocator::AllocatorKind;
2726
use syntax::feature_gate::{self, AttributeType};
@@ -38,7 +37,7 @@ use rustc_data_structures::jobserver;
3837
use ::jobserver::Client;
3938

4039
use std;
41-
use std::cell::{self, Cell, RefCell};
40+
use std::cell::{self, RefCell};
4241
use std::env;
4342
use std::fmt;
4443
use std::io::Write;
@@ -127,8 +126,6 @@ pub struct Session {
127126
/// Data about code being compiled, gathered during compilation.
128127
pub code_stats: Lock<CodeStats>,
129128

130-
next_node_id: OneThread<Cell<ast::NodeId>>,
131-
132129
/// If `-zfuel=crate=n` is specified, `Some(crate)`.
133130
optimization_fuel_crate: Option<String>,
134131

@@ -355,21 +352,6 @@ impl Session {
355352
self.diagnostic().span_note_without_error(sp, msg)
356353
}
357354

358-
pub fn reserve_node_ids(&self, count: usize) -> ast::NodeId {
359-
let id = self.next_node_id.get();
360-
361-
match id.as_usize().checked_add(count) {
362-
Some(next) => {
363-
self.next_node_id.set(ast::NodeId::from_usize(next));
364-
}
365-
None => bug!("input too large; ran out of node-IDs!"),
366-
}
367-
368-
id
369-
}
370-
pub fn next_node_id(&self) -> NodeId {
371-
self.reserve_node_ids(1)
372-
}
373355
pub fn diagnostic(&self) -> &errors::Handler {
374356
&self.parse_sess.span_diagnostic
375357
}
@@ -1187,7 +1169,6 @@ fn build_session_(
11871169
recursion_limit: Once::new(),
11881170
type_length_limit: Once::new(),
11891171
const_eval_stack_frame_limit: 100,
1190-
next_node_id: OneThread::new(Cell::new(NodeId::from_u32(1))),
11911172
allocator_kind: Once::new(),
11921173
injected_panic_runtime: Once::new(),
11931174
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),

src/librustc_driver/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ rustc_save_analysis = { path = "../librustc_save_analysis" }
2727
rustc_codegen_utils = { path = "../librustc_codegen_utils" }
2828
rustc_interface = { path = "../librustc_interface" }
2929
rustc_serialize = { path = "../libserialize", package = "serialize" }
30+
rustc_resolve = { path = "../librustc_resolve" }
3031
syntax = { path = "../libsyntax" }
3132
syntax_pos = { path = "../libsyntax_pos" }

src/librustc_driver/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ pub fn run_compiler(
291291

292292
if let Some((ppm, opt_uii)) = pretty_info {
293293
if ppm.needs_ast_map(&opt_uii) {
294-
pretty::visit_crate(sess, &mut compiler.parse()?.peek_mut(), ppm);
295294
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
296295
let expanded_crate = compiler.expansion()?.take().0;
297296
pretty::print_after_hir_lowering(
@@ -305,8 +304,7 @@ pub fn run_compiler(
305304
Ok(())
306305
})?;
307306
} else {
308-
let mut krate = compiler.parse()?.take();
309-
pretty::visit_crate(sess, &mut krate, ppm);
307+
let krate = compiler.parse()?.take();
310308
pretty::print_after_parsing(
311309
sess,
312310
&compiler.input(),

src/librustc_driver/pretty.rs

-8
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ use rustc::session::Session;
88
use rustc::session::config::Input;
99
use rustc::ty::{self, TyCtxt};
1010
use rustc::util::common::ErrorReported;
11-
use rustc_interface::util::ReplaceBodyWithLoop;
1211
use rustc_mir::util::{write_mir_pretty, write_mir_graphviz};
1312

1413
use syntax::ast;
15-
use syntax::mut_visit::MutVisitor;
1614
use syntax::print::{pprust};
1715
use syntax_pos::FileName;
1816

@@ -572,12 +570,6 @@ impl UserIdentifiedItem {
572570
}
573571
}
574572

575-
pub fn visit_crate(sess: &Session, krate: &mut ast::Crate, ppm: PpMode) {
576-
if let PpmSource(PpmEveryBodyLoops) = ppm {
577-
ReplaceBodyWithLoop::new(sess).visit_crate(krate);
578-
}
579-
}
580-
581573
fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
582574
let src_name = source_name(input);
583575
let src = String::clone(&sess.source_map()

src/librustc_interface/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ fn configure_and_expand_inner<'a>(
395395
// If we're actually rustdoc then there's no need to actually compile
396396
// anything, so switch everything to just looping
397397
if sess.opts.actually_rustdoc {
398-
util::ReplaceBodyWithLoop::new(sess).visit_crate(&mut krate);
398+
util::ReplaceBodyWithLoop::new(&mut resolver).visit_crate(&mut krate);
399399
}
400400

401401
let has_proc_macro_decls = time(sess, "AST validation", || {

src/librustc_interface/util.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_mir;
1818
use rustc_passes;
1919
use rustc_plugin;
2020
use rustc_privacy;
21-
use rustc_resolve;
21+
use rustc_resolve::{self, Resolver};
2222
use rustc_typeck;
2323
use std::env;
2424
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
@@ -715,18 +715,18 @@ pub fn build_output_filenames(
715715
// ambitious form of the closed RFC #1637. See also [#34511].
716716
//
717717
// [#34511]: https://github.com/rust-lang/rust/issues/34511#issuecomment-322340401
718-
pub struct ReplaceBodyWithLoop<'a> {
718+
pub struct ReplaceBodyWithLoop<'a, 'b> {
719719
within_static_or_const: bool,
720720
nested_blocks: Option<Vec<ast::Block>>,
721-
sess: &'a Session,
721+
resolver: &'a mut Resolver<'b>,
722722
}
723723

724-
impl<'a> ReplaceBodyWithLoop<'a> {
725-
pub fn new(sess: &'a Session) -> ReplaceBodyWithLoop<'a> {
724+
impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> {
725+
pub fn new(resolver: &'a mut Resolver<'b>) -> ReplaceBodyWithLoop<'a, 'b> {
726726
ReplaceBodyWithLoop {
727727
within_static_or_const: false,
728728
nested_blocks: None,
729-
sess
729+
resolver,
730730
}
731731
}
732732

@@ -788,11 +788,12 @@ impl<'a> ReplaceBodyWithLoop<'a> {
788788
}
789789

790790
fn is_sig_const(sig: &ast::FnSig) -> bool {
791-
sig.header.constness.node == ast::Constness::Const || Self::should_ignore_fn(&sig.decl)
791+
sig.header.constness.node == ast::Constness::Const ||
792+
ReplaceBodyWithLoop::should_ignore_fn(&sig.decl)
792793
}
793794
}
794795

795-
impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> {
796+
impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
796797
fn visit_item_kind(&mut self, i: &mut ast::ItemKind) {
797798
let is_const = match i {
798799
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => true,
@@ -827,40 +828,40 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> {
827828
fn visit_block(&mut self, b: &mut P<ast::Block>) {
828829
fn stmt_to_block(rules: ast::BlockCheckMode,
829830
s: Option<ast::Stmt>,
830-
sess: &Session) -> ast::Block {
831+
resolver: &mut Resolver<'_>) -> ast::Block {
831832
ast::Block {
832833
stmts: s.into_iter().collect(),
833834
rules,
834-
id: sess.next_node_id(),
835+
id: resolver.next_node_id(),
835836
span: syntax_pos::DUMMY_SP,
836837
}
837838
}
838839

839-
fn block_to_stmt(b: ast::Block, sess: &Session) -> ast::Stmt {
840+
fn block_to_stmt(b: ast::Block, resolver: &mut Resolver<'_>) -> ast::Stmt {
840841
let expr = P(ast::Expr {
841-
id: sess.next_node_id(),
842+
id: resolver.next_node_id(),
842843
kind: ast::ExprKind::Block(P(b), None),
843844
span: syntax_pos::DUMMY_SP,
844845
attrs: ThinVec::new(),
845846
});
846847

847848
ast::Stmt {
848-
id: sess.next_node_id(),
849+
id: resolver.next_node_id(),
849850
kind: ast::StmtKind::Expr(expr),
850851
span: syntax_pos::DUMMY_SP,
851852
}
852853
}
853854

854-
let empty_block = stmt_to_block(BlockCheckMode::Default, None, self.sess);
855+
let empty_block = stmt_to_block(BlockCheckMode::Default, None, self.resolver);
855856
let loop_expr = P(ast::Expr {
856857
kind: ast::ExprKind::Loop(P(empty_block), None),
857-
id: self.sess.next_node_id(),
858+
id: self.resolver.next_node_id(),
858859
span: syntax_pos::DUMMY_SP,
859860
attrs: ThinVec::new(),
860861
});
861862

862863
let loop_stmt = ast::Stmt {
863-
id: self.sess.next_node_id(),
864+
id: self.resolver.next_node_id(),
864865
span: syntax_pos::DUMMY_SP,
865866
kind: ast::StmtKind::Expr(loop_expr),
866867
};
@@ -878,7 +879,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> {
878879
// we put a Some in there earlier with that replace(), so this is valid
879880
let new_blocks = self.nested_blocks.take().unwrap();
880881
self.nested_blocks = old_blocks;
881-
stmts.extend(new_blocks.into_iter().map(|b| block_to_stmt(b, &self.sess)));
882+
stmts.extend(new_blocks.into_iter().map(|b| block_to_stmt(b, self.resolver)));
882883
}
883884

884885
let mut new_block = ast::Block {
@@ -892,7 +893,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> {
892893
old_blocks.push(new_block);
893894
}
894895

895-
stmt_to_block(b.rules, Some(loop_stmt), self.sess)
896+
stmt_to_block(b.rules, Some(loop_stmt), &mut self.resolver)
896897
} else {
897898
//push `loop {}` onto the end of our fresh block and yield that
898899
new_block.stmts.push(loop_stmt);

src/librustc_resolve/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
449449
name: kw::PathRoot,
450450
span: source.ident.span,
451451
},
452-
id: Some(self.r.session.next_node_id()),
452+
id: Some(self.r.next_node_id()),
453453
});
454454
source.ident.name = crate_name;
455455
}

src/librustc_resolve/lib.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,8 @@ pub struct Resolver<'a> {
961961
variant_vis: DefIdMap<ty::Visibility>,
962962

963963
lint_buffer: lint::LintBuffer,
964+
965+
next_node_id: NodeId,
964966
}
965967

966968
/// Nothing really interesting here; it just provides memory for the rest of the crate.
@@ -1078,6 +1080,10 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
10781080
fn lint_buffer(&mut self) -> &mut lint::LintBuffer {
10791081
&mut self.lint_buffer
10801082
}
1083+
1084+
fn next_node_id(&mut self) -> NodeId {
1085+
self.next_node_id()
1086+
}
10811087
}
10821088

10831089
impl<'a> Resolver<'a> {
@@ -1226,7 +1232,24 @@ impl<'a> Resolver<'a> {
12261232
.collect(),
12271233
variant_vis: Default::default(),
12281234
lint_buffer: lint::LintBuffer::default(),
1235+
next_node_id: NodeId::from_u32(1),
1236+
}
1237+
}
1238+
1239+
pub fn reserve_node_ids(&mut self, count: usize) -> ast::NodeId {
1240+
let id = self.next_node_id;
1241+
1242+
match id.as_usize().checked_add(count) {
1243+
Some(next) => {
1244+
self.next_node_id = ast::NodeId::from_usize(next);
1245+
}
1246+
None => panic!("input too large; ran out of node-IDs!"),
12291247
}
1248+
1249+
id
1250+
}
1251+
pub fn next_node_id(&mut self) -> NodeId {
1252+
self.reserve_node_ids(1)
12301253
}
12311254

12321255
pub fn lint_buffer(&mut self) -> &mut lint::LintBuffer {
@@ -2827,9 +2850,9 @@ impl<'a> Resolver<'a> {
28272850
}
28282851
}
28292852

2830-
fn new_ast_path_segment(&self, ident: Ident) -> ast::PathSegment {
2853+
fn new_ast_path_segment(&mut self, ident: Ident) -> ast::PathSegment {
28312854
let mut seg = ast::PathSegment::from_ident(ident);
2832-
seg.id = self.session.next_node_id();
2855+
seg.id = self.next_node_id();
28332856
seg
28342857
}
28352858

0 commit comments

Comments
 (0)