Skip to content

Commit c412771

Browse files
Auto merge of #142830 - cjgillot:lower-incr-2, r=<try>
Make lowering incremental, take 3/N
2 parents f977dfc + 081e243 commit c412771

File tree

29 files changed

+490
-390
lines changed

29 files changed

+490
-390
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3399,7 +3399,6 @@ version = "0.0.0"
33993399
dependencies = [
34003400
"rustc_abi",
34013401
"rustc_ast",
3402-
"rustc_ast_pretty",
34033402
"rustc_attr_parsing",
34043403
"rustc_data_structures",
34053404
"rustc_errors",

compiler/rustc_ast/src/ast.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4027,6 +4027,17 @@ impl TryFrom<ItemKind> for ForeignItemKind {
40274027

40284028
pub type ForeignItem = Item<ForeignItemKind>;
40294029

4030+
#[derive(Debug)]
4031+
pub enum AstOwner {
4032+
NonOwner,
4033+
Synthetic(rustc_span::def_id::LocalDefId),
4034+
Crate(Box<Crate>),
4035+
Item(Box<Item>),
4036+
TraitItem(Box<AssocItem>),
4037+
ImplItem(Box<AssocItem>),
4038+
ForeignItem(Box<ForeignItem>),
4039+
}
4040+
40304041
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
40314042
#[cfg(target_pointer_width = "64")]
40324043
mod size_asserts {

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,10 @@ pub fn walk_flat_map_stmt<T: MutVisitor>(
362362
stmts
363363
}
364364

365-
fn walk_flat_map_stmt_kind<T: MutVisitor>(vis: &mut T, kind: StmtKind) -> SmallVec<[StmtKind; 1]> {
365+
pub fn walk_flat_map_stmt_kind<T: MutVisitor>(
366+
vis: &mut T,
367+
kind: StmtKind,
368+
) -> SmallVec<[StmtKind; 1]> {
366369
match kind {
367370
StmtKind::Let(mut local) => smallvec![StmtKind::Let({
368371
vis.visit_local(&mut local);

compiler/rustc_ast_lowering/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ doctest = false
1010
# tidy-alphabetical-start
1111
rustc_abi = { path = "../rustc_abi" }
1212
rustc_ast = { path = "../rustc_ast" }
13-
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1413
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
1514
rustc_data_structures = { path = "../rustc_data_structures" }
1615
rustc_errors = { path = "../rustc_errors" }

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ use super::errors::{
2020
};
2121
use crate::{
2222
AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, ParamMode,
23-
ResolverAstLoweringExt, fluent_generated as fluent,
23+
fluent_generated as fluent,
2424
};
2525

26-
impl<'a, 'hir> LoweringContext<'a, 'hir> {
26+
impl<'hir> LoweringContext<'hir> {
2727
pub(crate) fn lower_inline_asm(
2828
&mut self,
2929
sp: Span,
@@ -201,7 +201,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
201201
},
202202
InlineAsmOperand::Sym { sym } => {
203203
let static_def_id = self
204-
.resolver
205204
.get_partial_res(sym.id)
206205
.and_then(|res| res.full_res())
207206
.and_then(|res| match res {

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use smallvec::SmallVec;
66

77
use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext};
88

9-
impl<'a, 'hir> LoweringContext<'a, 'hir> {
9+
impl<'hir> LoweringContext<'hir> {
1010
pub(super) fn lower_block(
1111
&mut self,
1212
b: &Block,
@@ -44,17 +44,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
4444
stmts.push(hir::Stmt { hir_id, kind, span });
4545
}
4646
StmtKind::Item(it) => {
47-
stmts.extend(self.lower_item_ref(it).into_iter().enumerate().map(
48-
|(i, item_id)| {
49-
let hir_id = match i {
50-
0 => self.lower_node_id(s.id),
51-
_ => self.next_id(),
52-
};
53-
let kind = hir::StmtKind::Item(item_id);
54-
let span = self.lower_span(s.span);
55-
hir::Stmt { hir_id, kind, span }
56-
},
57-
));
47+
let item_id = self.lower_item_ref(it);
48+
let hir_id = self.lower_node_id(s.id);
49+
let kind = hir::StmtKind::Item(item_id);
50+
let span = self.lower_span(s.span);
51+
stmts.push(hir::Stmt { hir_id, kind, span });
5852
}
5953
StmtKind::Expr(e) => {
6054
let e = self.lower_expr(e);

compiler/rustc_ast_lowering/src/contract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use thin_vec::thin_vec;
22

33
use crate::LoweringContext;
44

5-
impl<'a, 'hir> LoweringContext<'a, 'hir> {
5+
impl<'hir> LoweringContext<'hir> {
66
/// Lowered contracts are guarded with the `contract_checks` compiler flag,
77
/// i.e. the flag turns into a boolean guard in the lowered HIR. The reason
88
/// for not eliminating the contract code entirely when the `contract_checks`

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ use rustc_ast::*;
4646
use rustc_errors::ErrorGuaranteed;
4747
use rustc_hir::def_id::DefId;
4848
use rustc_middle::span_bug;
49-
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
49+
use rustc_middle::ty::Asyncness;
5050
use rustc_span::symbol::kw;
5151
use rustc_span::{Ident, Span, Symbol};
5252
use {rustc_ast as ast, rustc_hir as hir};
5353

54-
use super::{GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode};
55-
use crate::{AllowReturnTypeNotation, ImplTraitPosition, ResolverAstLoweringExt};
54+
use super::{
55+
AllowReturnTypeNotation, GenericArgsMode, ImplTraitContext, ImplTraitPosition, LoweringContext,
56+
ParamMode,
57+
};
5658

5759
pub(crate) struct DelegationResults<'hir> {
5860
pub body_id: hir::BodyId,
@@ -61,7 +63,7 @@ pub(crate) struct DelegationResults<'hir> {
6163
pub generics: &'hir hir::Generics<'hir>,
6264
}
6365

64-
impl<'hir> LoweringContext<'_, 'hir> {
66+
impl<'hir> LoweringContext<'hir> {
6567
fn is_method(&self, def_id: DefId, span: Span) -> bool {
6668
match self.tcx.def_kind(def_id) {
6769
DefKind::Fn => false,
@@ -112,8 +114,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
112114
}
113115

114116
fn get_resolution_id(&self, node_id: NodeId, span: Span) -> Result<DefId, ErrorGuaranteed> {
115-
let def_id =
116-
self.resolver.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id());
117+
let def_id = self.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id());
117118
def_id.ok_or_else(|| {
118119
self.tcx.dcx().span_delayed_bug(
119120
span,
@@ -292,7 +293,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
292293
&& idx == 0
293294
{
294295
let mut self_resolver = SelfResolver {
295-
resolver: this.resolver,
296+
ctxt: this,
296297
path_id: delegation.id,
297298
self_param_id: pat_node_id,
298299
};
@@ -438,25 +439,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
438439
}
439440
}
440441

441-
struct SelfResolver<'a> {
442-
resolver: &'a mut ResolverAstLowering,
442+
struct SelfResolver<'r, 'hir> {
443+
ctxt: &'r mut LoweringContext<'hir>,
443444
path_id: NodeId,
444445
self_param_id: NodeId,
445446
}
446447

447-
impl<'a> SelfResolver<'a> {
448+
impl SelfResolver<'_, '_> {
448449
fn try_replace_id(&mut self, id: NodeId) {
449-
if let Some(res) = self.resolver.partial_res_map.get(&id)
450+
if let Some(res) = self.ctxt.get_partial_res(id)
450451
&& let Some(Res::Local(sig_id)) = res.full_res()
451452
&& sig_id == self.path_id
452453
{
453454
let new_res = PartialRes::new(Res::Local(self.self_param_id));
454-
self.resolver.partial_res_map.insert(id, new_res);
455+
self.ctxt.partial_res_overrides.insert(id, new_res);
455456
}
456457
}
457458
}
458459

459-
impl<'ast, 'a> Visitor<'ast> for SelfResolver<'a> {
460+
impl<'ast> Visitor<'ast> for SelfResolver<'_, '_> {
460461
fn visit_id(&mut self, id: NodeId) {
461462
self.try_replace_id(id);
462463
}

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::ops::ControlFlow;
22
use std::sync::Arc;
33

44
use rustc_ast::*;
5-
use rustc_ast_pretty::pprust::expr_to_string;
65
use rustc_data_structures::stack::ensure_sufficient_stack;
76
use rustc_hir as hir;
87
use rustc_hir::attrs::AttributeKind;
@@ -52,7 +51,7 @@ impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
5251
}
5352
}
5453

55-
impl<'hir> LoweringContext<'_, 'hir> {
54+
impl<'hir> LoweringContext<'hir> {
5655
fn lower_exprs(&mut self, exprs: &[Box<Expr>]) -> &'hir [hir::Expr<'hir>] {
5756
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
5857
}
@@ -436,13 +435,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
436435
let mut invalid_expr_error = |tcx: TyCtxt<'_>, span| {
437436
// Avoid emitting the error multiple times.
438437
if error.is_none() {
438+
let sm = tcx.sess.source_map();
439439
let mut const_args = vec![];
440440
let mut other_args = vec![];
441441
for (idx, arg) in args.iter().enumerate() {
442-
if legacy_args_idx.contains(&idx) {
443-
const_args.push(format!("{{ {} }}", expr_to_string(arg)));
444-
} else {
445-
other_args.push(expr_to_string(arg));
442+
if let Ok(arg) = sm.span_to_snippet(arg.span) {
443+
if legacy_args_idx.contains(&idx) {
444+
const_args.push(format!("{{ {} }}", arg));
445+
} else {
446+
other_args.push(arg);
447+
}
446448
}
447449
}
448450
let suggestion = UseConstGenericArg {
@@ -1205,7 +1207,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12051207
whole_span: Span,
12061208
) -> hir::ExprKind<'hir> {
12071209
// Return early in case of an ordinary assignment.
1208-
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
1210+
fn is_ordinary(lower_ctx: &mut LoweringContext<'_>, lhs: &Expr) -> bool {
12091211
match &lhs.kind {
12101212
ExprKind::Array(..)
12111213
| ExprKind::Struct(..)
@@ -1265,7 +1267,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12651267
) -> Option<(&'a Option<Box<QSelf>>, &'a Path)> {
12661268
if let ExprKind::Path(qself, path) = &expr.kind {
12671269
// Does the path resolve to something disallowed in a tuple struct/variant pattern?
1268-
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1270+
if let Some(partial_res) = self.get_partial_res(expr.id) {
12691271
if let Some(res) = partial_res.full_res()
12701272
&& !res.expected_in_tuple_struct_pat()
12711273
{
@@ -1287,7 +1289,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12871289
) -> Option<(&'a Option<Box<QSelf>>, &'a Path)> {
12881290
if let ExprKind::Path(qself, path) = &expr.kind {
12891291
// Does the path resolve to something disallowed in a unit struct/variant pattern?
1290-
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1292+
if let Some(partial_res) = self.get_partial_res(expr.id) {
12911293
if let Some(res) = partial_res.full_res()
12921294
&& !res.expected_in_unit_struct_pat()
12931295
{

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_span::{DesugaringKind, Ident, Span, Symbol, sym};
88

99
use super::LoweringContext;
1010

11-
impl<'hir> LoweringContext<'_, 'hir> {
11+
impl<'hir> LoweringContext<'hir> {
1212
pub(crate) fn lower_format_args(&mut self, sp: Span, fmt: &FormatArgs) -> hir::ExprKind<'hir> {
1313
// Never call the const constructor of `fmt::Arguments` if the
1414
// format_args!() had any arguments _before_ flattening/inlining.
@@ -236,7 +236,7 @@ enum ArgumentType {
236236
/// <core::fmt::Argument>::new_…(arg)
237237
/// ```
238238
fn make_argument<'hir>(
239-
ctx: &mut LoweringContext<'_, 'hir>,
239+
ctx: &mut LoweringContext<'hir>,
240240
sp: Span,
241241
arg: &'hir hir::Expr<'hir>,
242242
ty: ArgumentType,
@@ -285,7 +285,7 @@ fn make_argument<'hir>(
285285
/// <core::fmt::rt::Count>::Implied
286286
/// ```
287287
fn make_count<'hir>(
288-
ctx: &mut LoweringContext<'_, 'hir>,
288+
ctx: &mut LoweringContext<'hir>,
289289
sp: Span,
290290
count: &Option<FormatCount>,
291291
argmap: &mut FxIndexMap<(usize, ArgumentType), Option<Span>>,
@@ -336,7 +336,7 @@ fn make_count<'hir>(
336336
/// }
337337
/// ```
338338
fn make_format_spec<'hir>(
339-
ctx: &mut LoweringContext<'_, 'hir>,
339+
ctx: &mut LoweringContext<'hir>,
340340
sp: Span,
341341
placeholder: &FormatPlaceholder,
342342
argmap: &mut FxIndexMap<(usize, ArgumentType), Option<Span>>,
@@ -397,7 +397,7 @@ fn make_format_spec<'hir>(
397397
}
398398

399399
fn expand_format_args<'hir>(
400-
ctx: &mut LoweringContext<'_, 'hir>,
400+
ctx: &mut LoweringContext<'hir>,
401401
macsp: Span,
402402
fmt: &FormatArgs,
403403
allow_const: bool,

0 commit comments

Comments
 (0)