Skip to content

Commit 3cf2bc0

Browse files
authored
Rollup merge of rust-lang#67574 - Centril:librustc_lowering, r=Mark-Simulacrum
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
2 parents 5095101 + 70eca99 commit 3cf2bc0

File tree

14 files changed

+126
-96
lines changed

14 files changed

+126
-96
lines changed

Cargo.lock

+18
Original file line numberDiff line numberDiff line change
@@ -3358,6 +3358,22 @@ dependencies = [
33583358
"core",
33593359
]
33603360

3361+
[[package]]
3362+
name = "rustc_ast_lowering"
3363+
version = "0.0.0"
3364+
dependencies = [
3365+
"log",
3366+
"rustc",
3367+
"rustc_data_structures",
3368+
"rustc_error_codes",
3369+
"rustc_errors",
3370+
"rustc_index",
3371+
"rustc_span",
3372+
"rustc_target",
3373+
"smallvec 1.0.0",
3374+
"syntax",
3375+
]
3376+
33613377
[[package]]
33623378
name = "rustc_builtin_macros"
33633379
version = "0.0.0"
@@ -3578,6 +3594,7 @@ dependencies = [
35783594
"once_cell",
35793595
"rustc",
35803596
"rustc-rayon",
3597+
"rustc_ast_lowering",
35813598
"rustc_builtin_macros",
35823599
"rustc_codegen_llvm",
35833600
"rustc_codegen_ssa",
@@ -3783,6 +3800,7 @@ dependencies = [
37833800
"bitflags",
37843801
"log",
37853802
"rustc",
3803+
"rustc_ast_lowering",
37863804
"rustc_data_structures",
37873805
"rustc_error_codes",
37883806
"rustc_errors",

src/librustc/hir/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ pub mod def;
3939
pub mod def_id;
4040
pub mod intravisit;
4141
pub mod itemlikevisit;
42-
pub mod lowering;
4342
pub mod map;
4443
pub mod pat_util;
4544
pub mod print;
@@ -599,7 +598,7 @@ pub enum SyntheticTyParamKind {
599598
pub struct WhereClause<'hir> {
600599
pub predicates: &'hir [WherePredicate<'hir>],
601600
// Only valid if predicates isn't empty.
602-
span: Span,
601+
pub span: Span,
603602
}
604603

605604
impl WhereClause<'_> {

src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
2929
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
3030
#![feature(arbitrary_self_types)]
31-
#![feature(array_value_iter)]
3231
#![feature(bool_to_option)]
3332
#![feature(box_patterns)]
3433
#![feature(box_syntax)]

src/librustc/lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ pub enum BuiltinLintDiagnostics {
523523
DeprecatedMacro(Option<Symbol>, Span),
524524
}
525525

526-
pub(crate) fn add_elided_lifetime_in_path_suggestion(
526+
pub fn add_elided_lifetime_in_path_suggestion(
527527
sess: &Session,
528528
db: &mut DiagnosticBuilder<'_>,
529529
n: usize,

src/librustc_ast_lowering/Cargo.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
authors = ["The Rust Project Developers"]
3+
name = "rustc_ast_lowering"
4+
version = "0.0.0"
5+
edition = "2018"
6+
7+
[lib]
8+
name = "rustc_ast_lowering"
9+
path = "lib.rs"
10+
doctest = false
11+
12+
[dependencies]
13+
log = { version = "0.4", features = ["release_max_level_info", "std"] }
14+
rustc = { path = "../librustc" }
15+
rustc_target = { path = "../librustc_target" }
16+
rustc_data_structures = { path = "../librustc_data_structures" }
17+
rustc_index = { path = "../librustc_index" }
18+
rustc_span = { path = "../librustc_span" }
19+
rustc_error_codes = { path = "../librustc_error_codes" }
20+
rustc_errors = { path = "../librustc_errors" }
21+
syntax = { path = "../libsyntax" }
22+
smallvec = { version = "1.0", features = ["union", "may_dangle"] }

src/librustc/hir/lowering/expr.rs src/librustc_ast_lowering/expr.rs

+14-28
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
2-
use crate::hir;
3-
use crate::hir::def::Res;
42

3+
use rustc::bug;
4+
use rustc::hir;
5+
use rustc::hir::def::Res;
56
use rustc_data_structures::thin_vec::ThinVec;
6-
7+
use rustc_error_codes::*;
8+
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
9+
use rustc_span::symbol::{sym, Symbol};
710
use syntax::ast::*;
811
use syntax::attr;
912
use syntax::ptr::P as AstP;
10-
use syntax::source_map::{respan, DesugaringKind, Span, Spanned};
11-
use syntax::symbol::{sym, Symbol};
12-
13-
use rustc_error_codes::*;
13+
use syntax::{span_err, struct_span_err};
1414

1515
impl<'hir> LoweringContext<'_, 'hir> {
1616
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
@@ -82,11 +82,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
8282
this.lower_expr_while_in_loop_scope(e.span, cond, body, opt_label)
8383
}),
8484
ExprKind::Loop(ref body, opt_label) => self.with_loop_scope(e.id, |this| {
85-
hir::ExprKind::Loop(
86-
this.lower_block(body, false),
87-
this.lower_label(opt_label),
88-
hir::LoopSource::Loop,
89-
)
85+
hir::ExprKind::Loop(this.lower_block(body, false), opt_label, hir::LoopSource::Loop)
9086
}),
9187
ExprKind::TryBlock(ref body) => self.lower_expr_try_block(body),
9288
ExprKind::Match(ref expr, ref arms) => hir::ExprKind::Match(
@@ -123,10 +119,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
123119
self.lower_expr_closure(capture_clause, movability, decl, body, fn_decl_span)
124120
}
125121
}
126-
ExprKind::Block(ref blk, opt_label) => hir::ExprKind::Block(
127-
self.lower_block(blk, opt_label.is_some()),
128-
self.lower_label(opt_label),
129-
),
122+
ExprKind::Block(ref blk, opt_label) => {
123+
hir::ExprKind::Block(self.lower_block(blk, opt_label.is_some()), opt_label)
124+
}
130125
ExprKind::Assign(ref el, ref er, span) => {
131126
hir::ExprKind::Assign(self.lower_expr(el), self.lower_expr(er), span)
132127
}
@@ -407,11 +402,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
407402
);
408403

409404
// `[opt_ident]: loop { ... }`
410-
hir::ExprKind::Loop(
411-
self.block_expr(self.arena.alloc(match_expr)),
412-
self.lower_label(opt_label),
413-
source,
414-
)
405+
hir::ExprKind::Loop(self.block_expr(self.arena.alloc(match_expr)), opt_label, source)
415406
}
416407

417408
/// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_ok(<expr>) }`,
@@ -836,10 +827,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
836827
}
837828
}
838829

839-
fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
840-
label.map(|label| hir::Label { ident: label.ident })
841-
}
842-
843830
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
844831
let target_id = match destination {
845832
Some((id, _)) => {
@@ -857,7 +844,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
857844
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
858845
.into(),
859846
};
860-
hir::Destination { label: self.lower_label(destination.map(|(_, label)| label)), target_id }
847+
hir::Destination { label: destination.map(|(_, label)| label), target_id }
861848
}
862849

863850
fn lower_jump_destination(&mut self, id: NodeId, opt_label: Option<Label>) -> hir::Destination {
@@ -1100,8 +1087,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11001087
);
11011088

11021089
// `[opt_ident]: loop { ... }`
1103-
let kind =
1104-
hir::ExprKind::Loop(loop_block, self.lower_label(opt_label), hir::LoopSource::ForLoop);
1090+
let kind = hir::ExprKind::Loop(loop_block, opt_label, hir::LoopSource::ForLoop);
11051091
let loop_expr = self.arena.alloc(hir::Expr {
11061092
hir_id: self.lower_node_id(e.id),
11071093
kind,

src/librustc/hir/lowering/item.rs src/librustc_ast_lowering/item.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
use super::AnonymousLifetimeMode;
2-
use super::ImplTraitContext;
3-
use super::ImplTraitPosition;
4-
use super::ImplTraitTypeIdVisitor;
5-
use super::LoweringContext;
6-
use super::ParamMode;
7-
8-
use crate::arena::Arena;
9-
use crate::hir;
10-
use crate::hir::def::{DefKind, Res};
11-
use crate::hir::def_id::DefId;
12-
use crate::util::nodemap::NodeMap;
13-
1+
use super::{AnonymousLifetimeMode, LoweringContext, ParamMode};
2+
use super::{ImplTraitContext, ImplTraitPosition, ImplTraitTypeIdVisitor};
3+
4+
use rustc::arena::Arena;
5+
use rustc::bug;
6+
use rustc::hir;
7+
use rustc::hir::def::{DefKind, Res};
8+
use rustc::hir::def_id::DefId;
9+
use rustc::util::nodemap::NodeMap;
10+
use rustc_error_codes::*;
11+
use rustc_span::source_map::{respan, DesugaringKind};
12+
use rustc_span::symbol::{kw, sym};
13+
use rustc_span::Span;
1414
use rustc_target::spec::abi;
15-
16-
use smallvec::SmallVec;
17-
use std::collections::BTreeSet;
1815
use syntax::ast::*;
1916
use syntax::attr;
20-
use syntax::source_map::{respan, DesugaringKind};
21-
use syntax::symbol::{kw, sym};
17+
use syntax::struct_span_err;
2218
use syntax::visit::{self, Visitor};
23-
use syntax_pos::Span;
2419

25-
use rustc_error_codes::*;
20+
use log::debug;
21+
use smallvec::{smallvec, SmallVec};
22+
use std::collections::BTreeSet;
2623

2724
pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
2825
pub(super) lctx: &'a mut LoweringContext<'lowering, 'hir>,
@@ -1429,7 +1426,7 @@ pub(super) struct GenericsCtor<'hir> {
14291426
span: Span,
14301427
}
14311428

1432-
impl GenericsCtor<'hir> {
1429+
impl<'hir> GenericsCtor<'hir> {
14331430
pub(super) fn into_generics(self, arena: &'hir Arena<'hir>) -> hir::Generics<'hir> {
14341431
hir::Generics {
14351432
params: arena.alloc_from_iter(self.params),

src/librustc/hir/lowering.rs src/librustc_ast_lowering/lib.rs

+37-35
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,47 @@
3232
//! get confused if the spans from leaf AST nodes occur in multiple places
3333
//! in the HIR, especially for multiple identifiers.
3434
35-
use crate::arena::Arena;
36-
use crate::dep_graph::DepGraph;
37-
use crate::hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
38-
use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
39-
use crate::hir::map::{DefKey, DefPathData, Definitions};
40-
use crate::hir::{self, ParamName};
41-
use crate::hir::{ConstArg, GenericArg};
42-
use crate::lint;
43-
use crate::lint::builtin::{self, ELIDED_LIFETIMES_IN_PATHS};
44-
use crate::middle::cstore::CrateStore;
45-
use crate::session::config::nightly_options;
46-
use crate::session::Session;
47-
use crate::util::captures::Captures;
48-
use crate::util::common::FN_OUTPUT_NAME;
49-
use crate::util::nodemap::{DefIdMap, NodeMap};
50-
use errors::Applicability;
35+
#![feature(array_value_iter)]
36+
37+
use rustc::arena::Arena;
38+
use rustc::dep_graph::DepGraph;
39+
use rustc::hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
40+
use rustc::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
41+
use rustc::hir::map::{DefKey, DefPathData, Definitions};
42+
use rustc::hir::{self, ConstArg, GenericArg, ParamName};
43+
use rustc::lint;
44+
use rustc::lint::builtin::{self, ELIDED_LIFETIMES_IN_PATHS};
45+
use rustc::middle::cstore::CrateStore;
46+
use rustc::session::config::nightly_options;
47+
use rustc::session::Session;
48+
use rustc::util::captures::Captures;
49+
use rustc::util::common::FN_OUTPUT_NAME;
50+
use rustc::util::nodemap::{DefIdMap, NodeMap};
51+
use rustc::{bug, span_bug};
5152
use rustc_data_structures::fx::FxHashSet;
5253
use rustc_data_structures::sync::Lrc;
54+
use rustc_error_codes::*;
55+
use rustc_errors::Applicability;
5356
use rustc_index::vec::IndexVec;
54-
55-
use smallvec::SmallVec;
56-
use std::collections::BTreeMap;
57-
use std::mem;
57+
use rustc_span::hygiene::ExpnId;
58+
use rustc_span::source_map::{respan, DesugaringKind, ExpnData, ExpnKind, Spanned};
59+
use rustc_span::symbol::{kw, sym, Symbol};
60+
use rustc_span::Span;
5861
use syntax::ast;
5962
use syntax::ast::*;
6063
use syntax::attr;
61-
use syntax::errors;
6264
use syntax::print::pprust;
6365
use syntax::ptr::P as AstP;
6466
use syntax::sess::ParseSess;
65-
use syntax::source_map::{respan, DesugaringKind, ExpnData, ExpnKind, Spanned};
66-
use syntax::symbol::{kw, sym, Symbol};
6767
use syntax::token::{self, Nonterminal, Token};
6868
use syntax::tokenstream::{TokenStream, TokenTree};
6969
use syntax::visit::{self, Visitor};
70-
use syntax_pos::hygiene::ExpnId;
71-
use syntax_pos::Span;
70+
use syntax::{help, struct_span_err, walk_list};
7271

73-
use rustc_error_codes::*;
72+
use log::{debug, trace};
73+
use smallvec::{smallvec, SmallVec};
74+
use std::collections::BTreeMap;
75+
use std::mem;
7476

7577
macro_rules! arena_vec {
7678
($this:expr; $($x:expr),*) => ({
@@ -84,7 +86,7 @@ mod item;
8486

8587
const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;
8688

87-
pub struct LoweringContext<'a, 'hir: 'a> {
89+
struct LoweringContext<'a, 'hir: 'a> {
8890
crate_root: Option<Symbol>,
8991

9092
/// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.
@@ -235,13 +237,13 @@ enum ImplTraitPosition {
235237
Other,
236238
}
237239

238-
impl<'b, 'a> ImplTraitContext<'b, 'a> {
240+
impl<'a> ImplTraitContext<'_, 'a> {
239241
#[inline]
240242
fn disallowed() -> Self {
241243
ImplTraitContext::Disallowed(ImplTraitPosition::Other)
242244
}
243245

244-
fn reborrow(&'c mut self) -> ImplTraitContext<'c, 'a> {
246+
fn reborrow<'this>(&'this mut self) -> ImplTraitContext<'this, 'a> {
245247
use self::ImplTraitContext::*;
246248
match self {
247249
Universal(params) => Universal(params),
@@ -372,8 +374,8 @@ struct ImplTraitTypeIdVisitor<'a> {
372374
ids: &'a mut SmallVec<[NodeId; 1]>,
373375
}
374376

375-
impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
376-
fn visit_ty(&mut self, ty: &'a Ty) {
377+
impl Visitor<'_> for ImplTraitTypeIdVisitor<'_> {
378+
fn visit_ty(&mut self, ty: &Ty) {
377379
match ty.kind {
378380
TyKind::Typeof(_) | TyKind::BareFn(_) => return,
379381

@@ -383,7 +385,7 @@ impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
383385
visit::walk_ty(self, ty);
384386
}
385387

386-
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
388+
fn visit_path_segment(&mut self, path_span: Span, path_segment: &PathSegment) {
387389
if let Some(ref p) = path_segment.args {
388390
if let GenericArgs::Parenthesized(_) = **p {
389391
return;
@@ -687,7 +689,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
687689
self.resolver.get_import_res(id).present_items()
688690
}
689691

690-
fn diagnostic(&self) -> &errors::Handler {
692+
fn diagnostic(&self) -> &rustc_errors::Handler {
691693
self.sess.diagnostic()
692694
}
693695

@@ -3288,7 +3290,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
32883290
}
32893291
}
32903292

3291-
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body<'hir>>) -> Vec<hir::BodyId> {
3293+
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body<'_>>) -> Vec<hir::BodyId> {
32923294
// Sorting by span ensures that we get things in order within a
32933295
// file, and also puts the files in a sensible order.
32943296
let mut body_ids: Vec<_> = bodies.keys().cloned().collect();
@@ -3303,7 +3305,7 @@ struct GenericArgsCtor<'hir> {
33033305
parenthesized: bool,
33043306
}
33053307

3306-
impl GenericArgsCtor<'hir> {
3308+
impl<'hir> GenericArgsCtor<'hir> {
33073309
fn is_empty(&self) -> bool {
33083310
self.args.is_empty() && self.bindings.is_empty() && !self.parenthesized
33093311
}

src/librustc_interface/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rustc_parse = { path = "../librustc_parse" }
2020
syntax_pos = { path = "../librustc_span", package = "rustc_span" }
2121
rustc_serialize = { path = "../libserialize", package = "serialize" }
2222
rustc = { path = "../librustc" }
23+
rustc_ast_lowering = { path = "../librustc_ast_lowering" }
2324
rustc_incremental = { path = "../librustc_incremental" }
2425
rustc_traits = { path = "../librustc_traits" }
2526
rustc_data_structures = { path = "../librustc_data_structures" }

0 commit comments

Comments
 (0)