Rollup merge of #67574 - Centril:librustc_lowering, r=Mark-Simulacrum

Extract `rustc_ast_lowering` crate from `rustc`

Working towards https://github.com/rust-lang/rust/issues/65031.

This PR moves `src/librustc/hir/lowering{/, .rs}` to its own crate (`librustc_ast_lowering`) which is very self-contained (only `fn lower_crate` and `trait Resolver` are exposed).

r? @Mark-Simulacrum
This commit is contained in:
Mazdak Farrokhzad 2019-12-31 19:19:31 +01:00 committed by GitHub
commit 3cf2bc0e51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 125 additions and 95 deletions

View File

@ -3358,6 +3358,22 @@ dependencies = [
"core",
]
[[package]]
name = "rustc_ast_lowering"
version = "0.0.0"
dependencies = [
"log",
"rustc",
"rustc_data_structures",
"rustc_error_codes",
"rustc_errors",
"rustc_index",
"rustc_span",
"rustc_target",
"smallvec 1.0.0",
"syntax",
]
[[package]]
name = "rustc_builtin_macros"
version = "0.0.0"
@ -3578,6 +3594,7 @@ dependencies = [
"once_cell",
"rustc",
"rustc-rayon",
"rustc_ast_lowering",
"rustc_builtin_macros",
"rustc_codegen_llvm",
"rustc_codegen_ssa",
@ -3783,6 +3800,7 @@ dependencies = [
"bitflags",
"log",
"rustc",
"rustc_ast_lowering",
"rustc_data_structures",
"rustc_error_codes",
"rustc_errors",

View File

@ -39,7 +39,6 @@ pub mod def;
pub mod def_id;
pub mod intravisit;
pub mod itemlikevisit;
pub mod lowering;
pub mod map;
pub mod pat_util;
pub mod print;
@ -599,7 +598,7 @@ pub enum SyntheticTyParamKind {
pub struct WhereClause<'hir> {
pub predicates: &'hir [WherePredicate<'hir>],
// Only valid if predicates isn't empty.
span: Span,
pub span: Span,
}
impl WhereClause<'_> {

View File

@ -28,7 +28,6 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![feature(arbitrary_self_types)]
#![feature(array_value_iter)]
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(box_syntax)]

View File

@ -523,7 +523,7 @@ pub enum BuiltinLintDiagnostics {
DeprecatedMacro(Option<Symbol>, Span),
}
pub(crate) fn add_elided_lifetime_in_path_suggestion(
pub fn add_elided_lifetime_in_path_suggestion(
sess: &Session,
db: &mut DiagnosticBuilder<'_>,
n: usize,

View File

@ -0,0 +1,22 @@
[package]
authors = ["The Rust Project Developers"]
name = "rustc_ast_lowering"
version = "0.0.0"
edition = "2018"
[lib]
name = "rustc_ast_lowering"
path = "lib.rs"
doctest = false
[dependencies]
log = { version = "0.4", features = ["release_max_level_info", "std"] }
rustc = { path = "../librustc" }
rustc_target = { path = "../librustc_target" }
rustc_data_structures = { path = "../librustc_data_structures" }
rustc_index = { path = "../librustc_index" }
rustc_span = { path = "../librustc_span" }
rustc_error_codes = { path = "../librustc_error_codes" }
rustc_errors = { path = "../librustc_errors" }
syntax = { path = "../libsyntax" }
smallvec = { version = "1.0", features = ["union", "may_dangle"] }

View File

@ -1,16 +1,16 @@
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
use crate::hir;
use crate::hir::def::Res;
use rustc::bug;
use rustc::hir;
use rustc::hir::def::Res;
use rustc_data_structures::thin_vec::ThinVec;
use rustc_error_codes::*;
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
use rustc_span::symbol::{sym, Symbol};
use syntax::ast::*;
use syntax::attr;
use syntax::ptr::P as AstP;
use syntax::source_map::{respan, DesugaringKind, Span, Spanned};
use syntax::symbol::{sym, Symbol};
use rustc_error_codes::*;
use syntax::{span_err, struct_span_err};
impl<'hir> LoweringContext<'_, 'hir> {
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
@ -82,11 +82,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
this.lower_expr_while_in_loop_scope(e.span, cond, body, opt_label)
}),
ExprKind::Loop(ref body, opt_label) => self.with_loop_scope(e.id, |this| {
hir::ExprKind::Loop(
this.lower_block(body, false),
this.lower_label(opt_label),
hir::LoopSource::Loop,
)
hir::ExprKind::Loop(this.lower_block(body, false), opt_label, hir::LoopSource::Loop)
}),
ExprKind::TryBlock(ref body) => self.lower_expr_try_block(body),
ExprKind::Match(ref expr, ref arms) => hir::ExprKind::Match(
@ -123,10 +119,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.lower_expr_closure(capture_clause, movability, decl, body, fn_decl_span)
}
}
ExprKind::Block(ref blk, opt_label) => hir::ExprKind::Block(
self.lower_block(blk, opt_label.is_some()),
self.lower_label(opt_label),
),
ExprKind::Block(ref blk, opt_label) => {
hir::ExprKind::Block(self.lower_block(blk, opt_label.is_some()), opt_label)
}
ExprKind::Assign(ref el, ref er, span) => {
hir::ExprKind::Assign(self.lower_expr(el), self.lower_expr(er), span)
}
@ -407,11 +402,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
);
// `[opt_ident]: loop { ... }`
hir::ExprKind::Loop(
self.block_expr(self.arena.alloc(match_expr)),
self.lower_label(opt_label),
source,
)
hir::ExprKind::Loop(self.block_expr(self.arena.alloc(match_expr)), opt_label, source)
}
/// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_ok(<expr>) }`,
@ -836,10 +827,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}
fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
label.map(|label| hir::Label { ident: label.ident })
}
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
let target_id = match destination {
Some((id, _)) => {
@ -857,7 +844,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
.into(),
};
hir::Destination { label: self.lower_label(destination.map(|(_, label)| label)), target_id }
hir::Destination { label: destination.map(|(_, label)| label), target_id }
}
fn lower_jump_destination(&mut self, id: NodeId, opt_label: Option<Label>) -> hir::Destination {
@ -1100,8 +1087,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
);
// `[opt_ident]: loop { ... }`
let kind =
hir::ExprKind::Loop(loop_block, self.lower_label(opt_label), hir::LoopSource::ForLoop);
let kind = hir::ExprKind::Loop(loop_block, opt_label, hir::LoopSource::ForLoop);
let loop_expr = self.arena.alloc(hir::Expr {
hir_id: self.lower_node_id(e.id),
kind,

View File

@ -1,28 +1,25 @@
use super::AnonymousLifetimeMode;
use super::ImplTraitContext;
use super::ImplTraitPosition;
use super::ImplTraitTypeIdVisitor;
use super::LoweringContext;
use super::ParamMode;
use crate::arena::Arena;
use crate::hir;
use crate::hir::def::{DefKind, Res};
use crate::hir::def_id::DefId;
use crate::util::nodemap::NodeMap;
use super::{AnonymousLifetimeMode, LoweringContext, ParamMode};
use super::{ImplTraitContext, ImplTraitPosition, ImplTraitTypeIdVisitor};
use rustc::arena::Arena;
use rustc::bug;
use rustc::hir;
use rustc::hir::def::{DefKind, Res};
use rustc::hir::def_id::DefId;
use rustc::util::nodemap::NodeMap;
use rustc_error_codes::*;
use rustc_span::source_map::{respan, DesugaringKind};
use rustc_span::symbol::{kw, sym};
use rustc_span::Span;
use rustc_target::spec::abi;
use smallvec::SmallVec;
use std::collections::BTreeSet;
use syntax::ast::*;
use syntax::attr;
use syntax::source_map::{respan, DesugaringKind};
use syntax::symbol::{kw, sym};
use syntax::struct_span_err;
use syntax::visit::{self, Visitor};
use syntax_pos::Span;
use rustc_error_codes::*;
use log::debug;
use smallvec::{smallvec, SmallVec};
use std::collections::BTreeSet;
pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
pub(super) lctx: &'a mut LoweringContext<'lowering, 'hir>,
@ -1429,7 +1426,7 @@ pub(super) struct GenericsCtor<'hir> {
span: Span,
}
impl GenericsCtor<'hir> {
impl<'hir> GenericsCtor<'hir> {
pub(super) fn into_generics(self, arena: &'hir Arena<'hir>) -> hir::Generics<'hir> {
hir::Generics {
params: arena.alloc_from_iter(self.params),

View File

@ -32,45 +32,47 @@
//! get confused if the spans from leaf AST nodes occur in multiple places
//! in the HIR, especially for multiple identifiers.
use crate::arena::Arena;
use crate::dep_graph::DepGraph;
use crate::hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
use crate::hir::map::{DefKey, DefPathData, Definitions};
use crate::hir::{self, ParamName};
use crate::hir::{ConstArg, GenericArg};
use crate::lint;
use crate::lint::builtin::{self, ELIDED_LIFETIMES_IN_PATHS};
use crate::middle::cstore::CrateStore;
use crate::session::config::nightly_options;
use crate::session::Session;
use crate::util::captures::Captures;
use crate::util::common::FN_OUTPUT_NAME;
use crate::util::nodemap::{DefIdMap, NodeMap};
use errors::Applicability;
#![feature(array_value_iter)]
use rustc::arena::Arena;
use rustc::dep_graph::DepGraph;
use rustc::hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
use rustc::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
use rustc::hir::map::{DefKey, DefPathData, Definitions};
use rustc::hir::{self, ConstArg, GenericArg, ParamName};
use rustc::lint;
use rustc::lint::builtin::{self, ELIDED_LIFETIMES_IN_PATHS};
use rustc::middle::cstore::CrateStore;
use rustc::session::config::nightly_options;
use rustc::session::Session;
use rustc::util::captures::Captures;
use rustc::util::common::FN_OUTPUT_NAME;
use rustc::util::nodemap::{DefIdMap, NodeMap};
use rustc::{bug, span_bug};
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc;
use rustc_error_codes::*;
use rustc_errors::Applicability;
use rustc_index::vec::IndexVec;
use smallvec::SmallVec;
use std::collections::BTreeMap;
use std::mem;
use rustc_span::hygiene::ExpnId;
use rustc_span::source_map::{respan, DesugaringKind, ExpnData, ExpnKind, Spanned};
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;
use syntax::ast;
use syntax::ast::*;
use syntax::attr;
use syntax::errors;
use syntax::print::pprust;
use syntax::ptr::P as AstP;
use syntax::sess::ParseSess;
use syntax::source_map::{respan, DesugaringKind, ExpnData, ExpnKind, Spanned};
use syntax::symbol::{kw, sym, Symbol};
use syntax::token::{self, Nonterminal, Token};
use syntax::tokenstream::{TokenStream, TokenTree};
use syntax::visit::{self, Visitor};
use syntax_pos::hygiene::ExpnId;
use syntax_pos::Span;
use syntax::{help, struct_span_err, walk_list};
use rustc_error_codes::*;
use log::{debug, trace};
use smallvec::{smallvec, SmallVec};
use std::collections::BTreeMap;
use std::mem;
macro_rules! arena_vec {
($this:expr; $($x:expr),*) => ({
@ -84,7 +86,7 @@ mod item;
const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;
pub struct LoweringContext<'a, 'hir: 'a> {
struct LoweringContext<'a, 'hir: 'a> {
crate_root: Option<Symbol>,
/// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.
@ -235,13 +237,13 @@ enum ImplTraitPosition {
Other,
}
impl<'b, 'a> ImplTraitContext<'b, 'a> {
impl<'a> ImplTraitContext<'_, 'a> {
#[inline]
fn disallowed() -> Self {
ImplTraitContext::Disallowed(ImplTraitPosition::Other)
}
fn reborrow(&'c mut self) -> ImplTraitContext<'c, 'a> {
fn reborrow<'this>(&'this mut self) -> ImplTraitContext<'this, 'a> {
use self::ImplTraitContext::*;
match self {
Universal(params) => Universal(params),
@ -372,8 +374,8 @@ struct ImplTraitTypeIdVisitor<'a> {
ids: &'a mut SmallVec<[NodeId; 1]>,
}
impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
fn visit_ty(&mut self, ty: &'a Ty) {
impl Visitor<'_> for ImplTraitTypeIdVisitor<'_> {
fn visit_ty(&mut self, ty: &Ty) {
match ty.kind {
TyKind::Typeof(_) | TyKind::BareFn(_) => return,
@ -383,7 +385,7 @@ impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
visit::walk_ty(self, ty);
}
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
fn visit_path_segment(&mut self, path_span: Span, path_segment: &PathSegment) {
if let Some(ref p) = path_segment.args {
if let GenericArgs::Parenthesized(_) = **p {
return;
@ -687,7 +689,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.resolver.get_import_res(id).present_items()
}
fn diagnostic(&self) -> &errors::Handler {
fn diagnostic(&self) -> &rustc_errors::Handler {
self.sess.diagnostic()
}
@ -3288,7 +3290,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
}
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body<'hir>>) -> Vec<hir::BodyId> {
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body<'_>>) -> Vec<hir::BodyId> {
// Sorting by span ensures that we get things in order within a
// file, and also puts the files in a sensible order.
let mut body_ids: Vec<_> = bodies.keys().cloned().collect();
@ -3303,7 +3305,7 @@ struct GenericArgsCtor<'hir> {
parenthesized: bool,
}
impl GenericArgsCtor<'hir> {
impl<'hir> GenericArgsCtor<'hir> {
fn is_empty(&self) -> bool {
self.args.is_empty() && self.bindings.is_empty() && !self.parenthesized
}

View File

@ -20,6 +20,7 @@ rustc_parse = { path = "../librustc_parse" }
syntax_pos = { path = "../librustc_span", package = "rustc_span" }
rustc_serialize = { path = "../libserialize", package = "serialize" }
rustc = { path = "../librustc" }
rustc_ast_lowering = { path = "../librustc_ast_lowering" }
rustc_incremental = { path = "../librustc_incremental" }
rustc_traits = { path = "../librustc_traits" }
rustc_data_structures = { path = "../librustc_data_structures" }

View File

@ -7,7 +7,6 @@ use rustc::arena::Arena;
use rustc::dep_graph::DepGraph;
use rustc::hir;
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
use rustc::hir::lowering::lower_crate;
use rustc::lint;
use rustc::middle::cstore::{CrateStore, MetadataLoader, MetadataLoaderDyn};
use rustc::middle::{self, resolve_lifetime, stability};
@ -442,8 +441,14 @@ pub fn lower_to_hir<'res, 'tcx>(
) -> Result<hir::map::Forest<'tcx>> {
// Lower AST to HIR.
let hir_forest = time(sess, "lowering AST -> HIR", || {
let nt_to_tokenstream = rustc_parse::nt_to_tokenstream;
let hir_crate = lower_crate(sess, &dep_graph, &krate, resolver, nt_to_tokenstream, arena);
let hir_crate = rustc_ast_lowering::lower_crate(
sess,
&dep_graph,
&krate,
resolver,
rustc_parse::nt_to_tokenstream,
arena,
);
if sess.opts.debugging_opts.hir_stats {
hir_stats::print_hir_stats(&hir_crate);

View File

@ -16,8 +16,8 @@ use std::{f32, f64, i16, i32, i64, i8, u16, u32, u64, u8};
use rustc_target::spec::abi::Abi;
use syntax::errors::Applicability;
use syntax::symbol::sym;
use syntax::{ast, attr, source_map};
use syntax_pos::symbol::sym;
use syntax_pos::Span;
use rustc::hir;

View File

@ -15,10 +15,11 @@ bitflags = "1.2.1"
log = "0.4"
syntax = { path = "../libsyntax" }
rustc_expand = { path = "../librustc_expand" }
rustc = { path = "../librustc" }
arena = { path = "../libarena" }
errors = { path = "../librustc_errors", package = "rustc_errors" }
syntax_pos = { path = "../librustc_span", package = "rustc_span" }
rustc = { path = "../librustc" }
rustc_ast_lowering = { path = "../librustc_ast_lowering" }
rustc_data_structures = { path = "../librustc_data_structures" }
rustc_feature = { path = "../librustc_feature" }
rustc_metadata = { path = "../librustc_metadata" }

View File

@ -24,7 +24,7 @@ use rustc::hir::def::Namespace::*;
use rustc::hir::def::{self, CtorKind, CtorOf, DefKind, ExportMap, NonMacroAttrKind, PartialRes};
use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc::hir::map::Definitions;
use rustc::hir::{self, Bool, Char, Float, Int, PrimTy, Str, Uint};
use rustc::hir::{Bool, Char, Float, Int, PrimTy, Str, Uint};
use rustc::hir::{GlobMap, TraitMap};
use rustc::lint;
use rustc::middle::cstore::{CrateStore, MetadataLoaderDyn};
@ -1026,7 +1026,7 @@ impl<'a, 'b> DefIdTree for &'a Resolver<'b> {
/// This interface is used through the AST→HIR step, to embed full paths into the HIR. After that
/// the resolver is no longer needed as all the relevant information is inline.
impl<'a> hir::lowering::Resolver for Resolver<'a> {
impl rustc_ast_lowering::Resolver for Resolver<'_> {
fn cstore(&self) -> &dyn CrateStore {
self.cstore()
}

View File

@ -6,8 +6,8 @@ use errors::{Applicability, DiagnosticBuilder};
use rustc::hir::{self, is_range_literal, print, Node};
use rustc::ty::adjustment::AllowTwoPhase;
use rustc::ty::{self, AssocItem, Ty};
use syntax::symbol::sym;
use syntax::util::parser::PREC_POSTFIX;
use syntax_pos::symbol::sym;
use syntax_pos::Span;
use super::method::probe;