Skip to content

Commit 1d8b291

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 ab6e478 commit 1d8b291

File tree

12 files changed

+62
-63
lines changed

12 files changed

+62
-63
lines changed

src/librustc/hir/lowering.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ pub trait Resolver {
187187
fn has_derives(&self, node_id: NodeId, derives: SpecialDerives) -> bool;
188188

189189
fn lint_buffer(&mut self) -> &mut lint::LintBuffer;
190+
191+
fn next_node_id(&mut self) -> NodeId;
190192
}
191193

192194
type NtToTokenstream = fn(&Nonterminal, &ParseSess, Span) -> TokenStream;
@@ -677,7 +679,8 @@ impl<'a> LoweringContext<'a> {
677679
}
678680

679681
fn next_id(&mut self) -> hir::HirId {
680-
self.lower_node_id(self.sess.next_node_id())
682+
let node_id = self.resolver.next_node_id();
683+
self.lower_node_id(node_id)
681684
}
682685

683686
fn lower_res(&mut self, res: Res<NodeId>) -> Res {
@@ -786,7 +789,7 @@ impl<'a> LoweringContext<'a> {
786789
hir_name: ParamName,
787790
parent_index: DefIndex,
788791
) -> hir::GenericParam {
789-
let node_id = self.sess.next_node_id();
792+
let node_id = self.resolver.next_node_id();
790793

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

1108-
let impl_trait_node_id = self.sess.next_node_id();
1111+
let impl_trait_node_id = self.resolver.next_node_id();
11091112
let parent_def_index = self.current_hir_id_owner.last().unwrap().0;
11101113
self.resolver.definitions().create_def_with_parent(
11111114
parent_def_index,
@@ -1116,9 +1119,10 @@ impl<'a> LoweringContext<'a> {
11161119
);
11171120

11181121
self.with_dyn_type_scope(false, |this| {
1122+
let node_id = this.resolver.next_node_id();
11191123
let ty = this.lower_ty(
11201124
&Ty {
1121-
id: this.sess.next_node_id(),
1125+
id: node_id,
11221126
kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
11231127
span: constraint.span,
11241128
},
@@ -1585,7 +1589,7 @@ impl<'a> LoweringContext<'a> {
15851589
name,
15861590
}));
15871591

1588-
let def_node_id = self.context.sess.next_node_id();
1592+
let def_node_id = self.context.resolver.next_node_id();
15891593
let hir_id =
15901594
self.context.lower_node_id_with_owner(def_node_id, self.opaque_ty_id);
15911595
self.context.resolver.definitions().create_def_with_parent(
@@ -3252,7 +3256,7 @@ impl<'a> LoweringContext<'a> {
32523256
Some(id) => (id, "`'_` cannot be used here", "`'_` is a reserved lifetime name"),
32533257

32543258
None => (
3255-
self.sess.next_node_id(),
3259+
self.resolver.next_node_id(),
32563260
"`&` without an explicit lifetime name cannot be used here",
32573261
"explicit lifetime name needed here",
32583262
),
@@ -3289,7 +3293,7 @@ impl<'a> LoweringContext<'a> {
32893293
span,
32903294
"expected 'implicit elided lifetime not allowed' error",
32913295
);
3292-
let id = self.sess.next_node_id();
3296+
let id = self.resolver.next_node_id();
32933297
self.new_named_lifetime(id, span, hir::LifetimeName::Error)
32943298
}
32953299
// `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
@@ -533,7 +533,7 @@ impl LoweringContext<'_> {
533533
let ident = *ident;
534534
let mut path = path.clone();
535535
for seg in &mut path.segments {
536-
seg.id = self.sess.next_node_id();
536+
seg.id = self.resolver.next_node_id();
537537
}
538538
let span = path.span;
539539

@@ -610,7 +610,7 @@ impl LoweringContext<'_> {
610610

611611
// Give the segments new node-ids since they are being cloned.
612612
for seg in &mut prefix.segments {
613-
seg.id = self.sess.next_node_id();
613+
seg.id = self.resolver.next_node_id();
614614
}
615615

616616
// 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

@@ -358,21 +355,6 @@ impl Session {
358355
self.diagnostic().span_note_without_error(sp, msg)
359356
}
360357

361-
pub fn reserve_node_ids(&self, count: usize) -> ast::NodeId {
362-
let id = self.next_node_id.get();
363-
364-
match id.as_usize().checked_add(count) {
365-
Some(next) => {
366-
self.next_node_id.set(ast::NodeId::from_usize(next));
367-
}
368-
None => bug!("input too large; ran out of node-IDs!"),
369-
}
370-
371-
id
372-
}
373-
pub fn next_node_id(&self) -> NodeId {
374-
self.reserve_node_ids(1)
375-
}
376358
pub fn diagnostic(&self) -> &errors::Handler {
377359
&self.parse_sess.span_diagnostic
378360
}
@@ -1190,7 +1172,6 @@ fn build_session_(
11901172
recursion_limit: Once::new(),
11911173
type_length_limit: Once::new(),
11921174
const_eval_stack_frame_limit: 100,
1193-
next_node_id: OneThread::new(Cell::new(NodeId::from_u32(1))),
11941175
allocator_kind: Once::new(),
11951176
injected_panic_runtime: Once::new(),
11961177
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
@@ -290,7 +290,6 @@ pub fn run_compiler(
290290

291291
if let Some((ppm, opt_uii)) = pretty_info {
292292
if ppm.needs_ast_map(&opt_uii) {
293-
pretty::visit_crate(sess, &mut compiler.parse()?.peek_mut(), ppm);
294293
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
295294
let expanded_crate = compiler.expansion()?.take().0;
296295
pretty::print_after_hir_lowering(
@@ -304,8 +303,7 @@ pub fn run_compiler(
304303
Ok(())
305304
})?;
306305
} else {
307-
let mut krate = compiler.parse()?.take();
308-
pretty::visit_crate(sess, &mut krate, ppm);
306+
let krate = compiler.parse()?.take();
309307
pretty::print_after_parsing(
310308
sess,
311309
&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
@@ -394,7 +394,7 @@ fn configure_and_expand_inner<'a>(
394394
// If we're actually rustdoc then there's no need to actually compile
395395
// anything, so switch everything to just looping
396396
if sess.opts.actually_rustdoc {
397-
util::ReplaceBodyWithLoop::new(sess).visit_crate(&mut krate);
397+
util::ReplaceBodyWithLoop::new(&mut resolver).visit_crate(&mut krate);
398398
}
399399

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

src/librustc_interface/util.rs

+17-17
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,7 +788,7 @@ impl<'a> ReplaceBodyWithLoop<'a> {
788788
}
789789
}
790790

791-
impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> {
791+
impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
792792
fn visit_item_kind(&mut self, i: &mut ast::ItemKind) {
793793
let is_const = match i {
794794
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => true,
@@ -826,40 +826,40 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> {
826826
fn visit_block(&mut self, b: &mut P<ast::Block>) {
827827
fn stmt_to_block(rules: ast::BlockCheckMode,
828828
s: Option<ast::Stmt>,
829-
sess: &Session) -> ast::Block {
829+
resolver: &mut Resolver<'_>) -> ast::Block {
830830
ast::Block {
831831
stmts: s.into_iter().collect(),
832832
rules,
833-
id: sess.next_node_id(),
833+
id: resolver.next_node_id(),
834834
span: syntax_pos::DUMMY_SP,
835835
}
836836
}
837837

838-
fn block_to_stmt(b: ast::Block, sess: &Session) -> ast::Stmt {
838+
fn block_to_stmt(b: ast::Block, resolver: &mut Resolver<'_>) -> ast::Stmt {
839839
let expr = P(ast::Expr {
840-
id: sess.next_node_id(),
840+
id: resolver.next_node_id(),
841841
kind: ast::ExprKind::Block(P(b), None),
842842
span: syntax_pos::DUMMY_SP,
843843
attrs: ThinVec::new(),
844844
});
845845

846846
ast::Stmt {
847-
id: sess.next_node_id(),
847+
id: resolver.next_node_id(),
848848
kind: ast::StmtKind::Expr(expr),
849849
span: syntax_pos::DUMMY_SP,
850850
}
851851
}
852852

853-
let empty_block = stmt_to_block(BlockCheckMode::Default, None, self.sess);
853+
let empty_block = stmt_to_block(BlockCheckMode::Default, None, self.resolver);
854854
let loop_expr = P(ast::Expr {
855855
kind: ast::ExprKind::Loop(P(empty_block), None),
856-
id: self.sess.next_node_id(),
856+
id: self.resolver.next_node_id(),
857857
span: syntax_pos::DUMMY_SP,
858858
attrs: ThinVec::new(),
859859
});
860860

861861
let loop_stmt = ast::Stmt {
862-
id: self.sess.next_node_id(),
862+
id: self.resolver.next_node_id(),
863863
span: syntax_pos::DUMMY_SP,
864864
kind: ast::StmtKind::Expr(loop_expr),
865865
};
@@ -877,7 +877,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> {
877877
// we put a Some in there earlier with that replace(), so this is valid
878878
let new_blocks = self.nested_blocks.take().unwrap();
879879
self.nested_blocks = old_blocks;
880-
stmts.extend(new_blocks.into_iter().map(|b| block_to_stmt(b, &self.sess)));
880+
stmts.extend(new_blocks.into_iter().map(|b| block_to_stmt(b, self.resolver)));
881881
}
882882

883883
let mut new_block = ast::Block {
@@ -891,7 +891,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> {
891891
old_blocks.push(new_block);
892892
}
893893

894-
stmt_to_block(b.rules, Some(loop_stmt), self.sess)
894+
stmt_to_block(b.rules, Some(loop_stmt), &mut self.resolver)
895895
} else {
896896
//push `loop {}` onto the end of our fresh block and yield that
897897
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
@@ -964,6 +964,8 @@ pub struct Resolver<'a> {
964964
variant_vis: DefIdMap<ty::Visibility>,
965965

966966
lint_buffer: lint::LintBuffer,
967+
968+
next_node_id: NodeId,
967969
}
968970

969971
/// Nothing really interesting here; it just provides memory for the rest of the crate.
@@ -1087,6 +1089,10 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
10871089
fn lint_buffer(&mut self) -> &mut lint::LintBuffer {
10881090
&mut self.lint_buffer
10891091
}
1092+
1093+
fn next_node_id(&mut self) -> NodeId {
1094+
self.next_node_id()
1095+
}
10901096
}
10911097

10921098
impl<'a> Resolver<'a> {
@@ -1235,7 +1241,24 @@ impl<'a> Resolver<'a> {
12351241
.collect(),
12361242
variant_vis: Default::default(),
12371243
lint_buffer: lint::LintBuffer::default(),
1244+
next_node_id: NodeId::from_u32(1),
1245+
}
1246+
}
1247+
1248+
pub fn reserve_node_ids(&mut self, count: usize) -> ast::NodeId {
1249+
let id = self.next_node_id;
1250+
1251+
match id.as_usize().checked_add(count) {
1252+
Some(next) => {
1253+
self.next_node_id = ast::NodeId::from_usize(next);
1254+
}
1255+
None => panic!("input too large; ran out of node-IDs!"),
12381256
}
1257+
1258+
id
1259+
}
1260+
pub fn next_node_id(&mut self) -> NodeId {
1261+
self.reserve_node_ids(1)
12391262
}
12401263

12411264
pub fn lint_buffer(&mut self) -> &mut lint::LintBuffer {
@@ -2840,9 +2863,9 @@ impl<'a> Resolver<'a> {
28402863
}
28412864
}
28422865

2843-
fn new_ast_path_segment(&self, ident: Ident) -> ast::PathSegment {
2866+
fn new_ast_path_segment(&mut self, ident: Ident) -> ast::PathSegment {
28442867
let mut seg = ast::PathSegment::from_ident(ident);
2845-
seg.id = self.session.next_node_id();
2868+
seg.id = self.next_node_id();
28462869
seg
28472870
}
28482871

0 commit comments

Comments
 (0)