Move next_node_id to Resolver

This doesn't migrate the pretty-printing everybody loops, which will be
done in the next few commits.
This commit is contained in:
Mark Rousskov 2019-11-03 17:38:02 -05:00
parent 5a5027519a
commit 516a817dbd
12 changed files with 64 additions and 64 deletions

View File

@ -183,6 +183,8 @@ fn resolve_str_path(
) -> (ast::Path, Res<NodeId>);
fn lint_buffer(&mut self) -> &mut lint::LintBuffer;
fn next_node_id(&mut self) -> NodeId;
}
type NtToTokenstream = fn(&Nonterminal, &ParseSess, Span) -> TokenStream;
@ -672,7 +674,8 @@ fn lower_node_id_with_owner(&mut self, ast_node_id: NodeId, owner: NodeId) -> hi
}
fn next_id(&mut self) -> hir::HirId {
self.lower_node_id(self.sess.next_node_id())
let node_id = self.resolver.next_node_id();
self.lower_node_id(node_id)
}
fn lower_res(&mut self, res: Res<NodeId>) -> Res {
@ -781,7 +784,7 @@ fn lifetime_to_generic_param(
hir_name: ParamName,
parent_index: DefIndex,
) -> hir::GenericParam {
let node_id = self.sess.next_node_id();
let node_id = self.resolver.next_node_id();
// Get the name we'll use to make the def-path. Note
// that collisions are ok here and this shouldn't
@ -1106,7 +1109,7 @@ fn lower_assoc_ty_constraint(
// Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by
// constructing the HIR for `impl bounds...` and then lowering that.
let impl_trait_node_id = self.sess.next_node_id();
let impl_trait_node_id = self.resolver.next_node_id();
let parent_def_index = self.current_hir_id_owner.last().unwrap().0;
self.resolver.definitions().create_def_with_parent(
parent_def_index,
@ -1117,9 +1120,10 @@ fn lower_assoc_ty_constraint(
);
self.with_dyn_type_scope(false, |this| {
let node_id = this.resolver.next_node_id();
let ty = this.lower_ty(
&Ty {
id: this.sess.next_node_id(),
id: node_id,
kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
span: constraint.span,
},
@ -1586,7 +1590,7 @@ fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
name,
}));
let def_node_id = self.context.sess.next_node_id();
let def_node_id = self.context.resolver.next_node_id();
let hir_id =
self.context.lower_node_id_with_owner(def_node_id, self.opaque_ty_id);
self.context.resolver.definitions().create_def_with_parent(
@ -3234,7 +3238,7 @@ fn new_error_lifetime(&mut self, id: Option<NodeId>, span: Span) -> hir::Lifetim
Some(id) => (id, "`'_` cannot be used here", "`'_` is a reserved lifetime name"),
None => (
self.sess.next_node_id(),
self.resolver.next_node_id(),
"`&` without an explicit lifetime name cannot be used here",
"explicit lifetime name needed here",
),
@ -3271,7 +3275,7 @@ fn elided_path_lifetime(&mut self, span: Span) -> hir::Lifetime {
span,
"expected 'implicit elided lifetime not allowed' error",
);
let id = self.sess.next_node_id();
let id = self.resolver.next_node_id();
self.new_named_lifetime(id, span, hir::LifetimeName::Error)
}
// `PassThrough` is the normal case.

View File

@ -595,7 +595,7 @@ fn lower_expr_await(&mut self, await_span: Span, expr: &Expr) -> hir::ExprKind {
};
// `::std::task::Poll::Ready(result) => break result`
let loop_node_id = self.sess.next_node_id();
let loop_node_id = self.resolver.next_node_id();
let loop_hir_id = self.lower_node_id(loop_node_id);
let ready_arm = {
let x_ident = Ident::with_dummy_span(sym::result);

View File

@ -522,7 +522,7 @@ fn lower_use_tree(
let ident = *ident;
let mut path = path.clone();
for seg in &mut path.segments {
seg.id = self.sess.next_node_id();
seg.id = self.resolver.next_node_id();
}
let span = path.span;
@ -599,7 +599,7 @@ fn lower_use_tree(
// Give the segments new node-ids since they are being cloned.
for seg in &mut prefix.segments {
seg.id = self.sess.next_node_id();
seg.id = self.resolver.next_node_id();
}
// Each `use` import is an item and thus are owners of the

View File

@ -21,7 +21,6 @@
use errors::emitter::{Emitter, EmitterWriter};
use errors::emitter::HumanReadableErrorType;
use errors::annotate_snippet_emitter_writer::{AnnotateSnippetEmitterWriter};
use syntax::ast::{self, NodeId};
use syntax::edition::Edition;
use syntax::expand::allocator::AllocatorKind;
use syntax::feature_gate::{self, AttributeType};
@ -38,7 +37,7 @@
use ::jobserver::Client;
use std;
use std::cell::{self, Cell, RefCell};
use std::cell::{self, RefCell};
use std::env;
use std::fmt;
use std::io::Write;
@ -127,8 +126,6 @@ pub struct Session {
/// Data about code being compiled, gathered during compilation.
pub code_stats: Lock<CodeStats>,
next_node_id: OneThread<Cell<ast::NodeId>>,
/// If `-zfuel=crate=n` is specified, `Some(crate)`.
optimization_fuel_crate: Option<String>,
@ -355,21 +352,6 @@ pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
self.diagnostic().span_note_without_error(sp, msg)
}
pub fn reserve_node_ids(&self, count: usize) -> ast::NodeId {
let id = self.next_node_id.get();
match id.as_usize().checked_add(count) {
Some(next) => {
self.next_node_id.set(ast::NodeId::from_usize(next));
}
None => bug!("input too large; ran out of node-IDs!"),
}
id
}
pub fn next_node_id(&self) -> NodeId {
self.reserve_node_ids(1)
}
pub fn diagnostic(&self) -> &errors::Handler {
&self.parse_sess.span_diagnostic
}
@ -1187,7 +1169,6 @@ fn build_session_(
recursion_limit: Once::new(),
type_length_limit: Once::new(),
const_eval_stack_frame_limit: 100,
next_node_id: OneThread::new(Cell::new(NodeId::from_u32(1))),
allocator_kind: Once::new(),
injected_panic_runtime: Once::new(),
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),

View File

@ -27,5 +27,6 @@ rustc_save_analysis = { path = "../librustc_save_analysis" }
rustc_codegen_utils = { path = "../librustc_codegen_utils" }
rustc_interface = { path = "../librustc_interface" }
rustc_serialize = { path = "../libserialize", package = "serialize" }
rustc_resolve = { path = "../librustc_resolve" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }

View File

@ -291,7 +291,6 @@ pub fn run_compiler(
if let Some((ppm, opt_uii)) = pretty_info {
if ppm.needs_ast_map(&opt_uii) {
pretty::visit_crate(sess, &mut compiler.parse()?.peek_mut(), ppm);
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
let expanded_crate = compiler.expansion()?.take().0;
pretty::print_after_hir_lowering(
@ -305,8 +304,7 @@ pub fn run_compiler(
Ok(())
})?;
} else {
let mut krate = compiler.parse()?.take();
pretty::visit_crate(sess, &mut krate, ppm);
let krate = compiler.parse()?.take();
pretty::print_after_parsing(
sess,
&compiler.input(),

View File

@ -8,11 +8,9 @@
use rustc::session::config::Input;
use rustc::ty::{self, TyCtxt};
use rustc::util::common::ErrorReported;
use rustc_interface::util::ReplaceBodyWithLoop;
use rustc_mir::util::{write_mir_pretty, write_mir_graphviz};
use syntax::ast;
use syntax::mut_visit::MutVisitor;
use syntax::print::{pprust};
use syntax_pos::FileName;
@ -572,12 +570,6 @@ fn to_one_node_id(self,
}
}
pub fn visit_crate(sess: &Session, krate: &mut ast::Crate, ppm: PpMode) {
if let PpmSource(PpmEveryBodyLoops) = ppm {
ReplaceBodyWithLoop::new(sess).visit_crate(krate);
}
}
fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
let src_name = source_name(input);
let src = String::clone(&sess.source_map()

View File

@ -395,7 +395,7 @@ fn configure_and_expand_inner<'a>(
// If we're actually rustdoc then there's no need to actually compile
// anything, so switch everything to just looping
if sess.opts.actually_rustdoc {
util::ReplaceBodyWithLoop::new(sess).visit_crate(&mut krate);
util::ReplaceBodyWithLoop::new(&mut resolver).visit_crate(&mut krate);
}
let has_proc_macro_decls = time(sess, "AST validation", || {

View File

@ -18,7 +18,7 @@
use rustc_passes;
use rustc_plugin;
use rustc_privacy;
use rustc_resolve;
use rustc_resolve::{self, Resolver};
use rustc_typeck;
use std::env;
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
@ -715,18 +715,18 @@ pub fn build_output_filenames(
// ambitious form of the closed RFC #1637. See also [#34511].
//
// [#34511]: https://github.com/rust-lang/rust/issues/34511#issuecomment-322340401
pub struct ReplaceBodyWithLoop<'a> {
pub struct ReplaceBodyWithLoop<'a, 'b> {
within_static_or_const: bool,
nested_blocks: Option<Vec<ast::Block>>,
sess: &'a Session,
resolver: &'a mut Resolver<'b>,
}
impl<'a> ReplaceBodyWithLoop<'a> {
pub fn new(sess: &'a Session) -> ReplaceBodyWithLoop<'a> {
impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> {
pub fn new(resolver: &'a mut Resolver<'b>) -> ReplaceBodyWithLoop<'a, 'b> {
ReplaceBodyWithLoop {
within_static_or_const: false,
nested_blocks: None,
sess
resolver,
}
}
@ -788,11 +788,12 @@ fn any_involves_impl_trait<'a, I: Iterator<Item = &'a P<ast::Ty>>>(mut it: I) ->
}
fn is_sig_const(sig: &ast::FnSig) -> bool {
sig.header.constness.node == ast::Constness::Const || Self::should_ignore_fn(&sig.decl)
sig.header.constness.node == ast::Constness::Const ||
ReplaceBodyWithLoop::should_ignore_fn(&sig.decl)
}
}
impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> {
impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
fn visit_item_kind(&mut self, i: &mut ast::ItemKind) {
let is_const = match i {
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => true,
@ -827,40 +828,40 @@ fn visit_anon_const(&mut self, c: &mut ast::AnonConst) {
fn visit_block(&mut self, b: &mut P<ast::Block>) {
fn stmt_to_block(rules: ast::BlockCheckMode,
s: Option<ast::Stmt>,
sess: &Session) -> ast::Block {
resolver: &mut Resolver<'_>) -> ast::Block {
ast::Block {
stmts: s.into_iter().collect(),
rules,
id: sess.next_node_id(),
id: resolver.next_node_id(),
span: syntax_pos::DUMMY_SP,
}
}
fn block_to_stmt(b: ast::Block, sess: &Session) -> ast::Stmt {
fn block_to_stmt(b: ast::Block, resolver: &mut Resolver<'_>) -> ast::Stmt {
let expr = P(ast::Expr {
id: sess.next_node_id(),
id: resolver.next_node_id(),
kind: ast::ExprKind::Block(P(b), None),
span: syntax_pos::DUMMY_SP,
attrs: ThinVec::new(),
});
ast::Stmt {
id: sess.next_node_id(),
id: resolver.next_node_id(),
kind: ast::StmtKind::Expr(expr),
span: syntax_pos::DUMMY_SP,
}
}
let empty_block = stmt_to_block(BlockCheckMode::Default, None, self.sess);
let empty_block = stmt_to_block(BlockCheckMode::Default, None, self.resolver);
let loop_expr = P(ast::Expr {
kind: ast::ExprKind::Loop(P(empty_block), None),
id: self.sess.next_node_id(),
id: self.resolver.next_node_id(),
span: syntax_pos::DUMMY_SP,
attrs: ThinVec::new(),
});
let loop_stmt = ast::Stmt {
id: self.sess.next_node_id(),
id: self.resolver.next_node_id(),
span: syntax_pos::DUMMY_SP,
kind: ast::StmtKind::Expr(loop_expr),
};
@ -878,7 +879,7 @@ fn block_to_stmt(b: ast::Block, sess: &Session) -> ast::Stmt {
// we put a Some in there earlier with that replace(), so this is valid
let new_blocks = self.nested_blocks.take().unwrap();
self.nested_blocks = old_blocks;
stmts.extend(new_blocks.into_iter().map(|b| block_to_stmt(b, &self.sess)));
stmts.extend(new_blocks.into_iter().map(|b| block_to_stmt(b, self.resolver)));
}
let mut new_block = ast::Block {
@ -892,7 +893,7 @@ fn block_to_stmt(b: ast::Block, sess: &Session) -> ast::Stmt {
old_blocks.push(new_block);
}
stmt_to_block(b.rules, Some(loop_stmt), self.sess)
stmt_to_block(b.rules, Some(loop_stmt), &mut self.resolver)
} else {
//push `loop {}` onto the end of our fresh block and yield that
new_block.stmts.push(loop_stmt);

View File

@ -449,7 +449,7 @@ fn build_reduced_graph_for_use_tree(
name: kw::PathRoot,
span: source.ident.span,
},
id: Some(self.r.session.next_node_id()),
id: Some(self.r.next_node_id()),
});
source.ident.name = crate_name;
}

View File

@ -961,6 +961,8 @@ pub struct Resolver<'a> {
variant_vis: DefIdMap<ty::Visibility>,
lint_buffer: lint::LintBuffer,
next_node_id: NodeId,
}
/// Nothing really interesting here; it just provides memory for the rest of the crate.
@ -1078,6 +1080,10 @@ fn definitions(&mut self) -> &mut Definitions {
fn lint_buffer(&mut self) -> &mut lint::LintBuffer {
&mut self.lint_buffer
}
fn next_node_id(&mut self) -> NodeId {
self.next_node_id()
}
}
impl<'a> Resolver<'a> {
@ -1226,9 +1232,26 @@ pub fn new(session: &'a Session,
.collect(),
variant_vis: Default::default(),
lint_buffer: lint::LintBuffer::default(),
next_node_id: NodeId::from_u32(1),
}
}
pub fn reserve_node_ids(&mut self, count: usize) -> ast::NodeId {
let id = self.next_node_id;
match id.as_usize().checked_add(count) {
Some(next) => {
self.next_node_id = ast::NodeId::from_usize(next);
}
None => panic!("input too large; ran out of node-IDs!"),
}
id
}
pub fn next_node_id(&mut self) -> NodeId {
self.reserve_node_ids(1)
}
pub fn lint_buffer(&mut self) -> &mut lint::LintBuffer {
&mut self.lint_buffer
}
@ -2827,9 +2850,9 @@ fn resolve_ast_path(
}
}
fn new_ast_path_segment(&self, ident: Ident) -> ast::PathSegment {
fn new_ast_path_segment(&mut self, ident: Ident) -> ast::PathSegment {
let mut seg = ast::PathSegment::from_ident(ident);
seg.id = self.session.next_node_id();
seg.id = self.next_node_id();
seg
}

View File

@ -95,7 +95,7 @@ fn fast_print_path(path: &ast::Path) -> Symbol {
impl<'a> base::Resolver for Resolver<'a> {
fn next_node_id(&mut self) -> NodeId {
self.session.next_node_id()
self.next_node_id()
}
fn resolve_dollar_crates(&mut self) {