Skip to content

Commit

Permalink
Rollup merge of rust-lang#67574 - Centril:librustc_lowering, r=Mark-S…
Browse files Browse the repository at this point in the history
…imulacrum

Extract `rustc_ast_lowering` crate from `rustc`

Working towards rust-lang#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
  • Loading branch information
Centril committed Dec 31, 2019
2 parents 5095101 + 70eca99 commit 3cf2bc0
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 96 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -3578,6 +3594,7 @@ dependencies = [
"once_cell",
"rustc",
"rustc-rayon",
"rustc_ast_lowering",
"rustc_builtin_macros",
"rustc_codegen_llvm",
"rustc_codegen_ssa",
Expand Down Expand Up @@ -3783,6 +3800,7 @@ dependencies = [
"bitflags",
"log",
"rustc",
"rustc_ast_lowering",
"rustc_data_structures",
"rustc_error_codes",
"rustc_errors",
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<'_> {
Expand Down
1 change: 0 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions src/librustc_ast_lowering/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
Original file line number Diff line number Diff line change
@@ -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>] {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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>) }`,
Expand Down Expand Up @@ -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, _)) => {
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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>,
Expand Down Expand Up @@ -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),
Expand Down
72 changes: 37 additions & 35 deletions src/librustc/hir/lowering.rs → src/librustc_ast_lowering/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),*) => ({
Expand All @@ -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.
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,

Expand All @@ -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;
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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();
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc_interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
Loading

0 comments on commit 3cf2bc0

Please sign in to comment.