Skip to content

Commit 07c7aca

Browse files
committed
Auto merge of #142830 - cjgillot:lower-incr-2, r=<try>
Make lowering incremental, take 3/N
2 parents 68ffae4 + d7424ea commit 07c7aca

27 files changed

Lines changed: 447 additions & 469 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3526,7 +3526,6 @@ version = "0.0.0"
35263526
dependencies = [
35273527
"rustc_abi",
35283528
"rustc_ast",
3529-
"rustc_ast_pretty",
35303529
"rustc_attr_parsing",
35313530
"rustc_data_structures",
35323531
"rustc_errors",

compiler/rustc_ast/src/ast.rs

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

43094309
pub type ForeignItem = Item<ForeignItemKind>;
43104310

4311+
#[derive(Debug)]
4312+
pub enum AstOwner {
4313+
NonOwner,
4314+
Synthetic(rustc_span::def_id::LocalDefId),
4315+
Crate(Box<Crate>),
4316+
Item(Box<Item>),
4317+
TraitItem(Box<AssocItem>),
4318+
ImplItem(Box<AssocItem>),
4319+
ForeignItem(Box<ForeignItem>),
4320+
}
4321+
43114322
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
43124323
#[cfg(target_pointer_width = "64")]
43134324
mod size_asserts {

compiler/rustc_ast/src/mut_visit.rs

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

364-
fn walk_flat_map_stmt_kind<T: MutVisitor>(vis: &mut T, kind: StmtKind) -> SmallVec<[StmtKind; 1]> {
364+
pub fn walk_flat_map_stmt_kind<T: MutVisitor>(
365+
vis: &mut T,
366+
kind: StmtKind,
367+
) -> SmallVec<[StmtKind; 1]> {
365368
match kind {
366369
StmtKind::Let(mut local) => smallvec![StmtKind::Let({
367370
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/block.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,11 @@ impl<'hir> LoweringContext<'_, '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/expr.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::ops::ControlFlow;
33
use std::sync::Arc;
44

55
use rustc_ast::*;
6-
use rustc_ast_pretty::pprust::expr_to_string;
76
use rustc_data_structures::stack::ensure_sufficient_stack;
87
use rustc_errors::msg;
98
use rustc_hir as hir;
@@ -445,13 +444,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
445444
let mut invalid_expr_error = |tcx: TyCtxt<'_>, span| {
446445
// Avoid emitting the error multiple times.
447446
if error.is_none() {
447+
let sm = tcx.sess.source_map();
448448
let mut const_args = vec![];
449449
let mut other_args = vec![];
450450
for (idx, arg) in args.iter().enumerate() {
451-
if legacy_args_idx.contains(&idx) {
452-
const_args.push(format!("{{ {} }}", expr_to_string(arg)));
453-
} else {
454-
other_args.push(expr_to_string(arg));
451+
if let Ok(arg) = sm.span_to_snippet(arg.span) {
452+
if legacy_args_idx.contains(&idx) {
453+
const_args.push(format!("{{ {} }}", arg));
454+
} else {
455+
other_args.push(arg);
456+
}
455457
}
456458
}
457459
let suggestion = UseConstGenericArg {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 57 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,32 @@ use std::mem;
33
use rustc_abi::ExternAbi;
44
use rustc_ast::visit::AssocCtxt;
55
use rustc_ast::*;
6-
use rustc_data_structures::fx::FxIndexMap;
76
use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
87
use rustc_hir::attrs::{AttributeKind, EiiImplResolution};
98
use rustc_hir::def::{DefKind, PerNS, Res};
10-
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
9+
use rustc_hir::def_id::CRATE_DEF_ID;
1110
use rustc_hir::{
1211
self as hir, HirId, ImplItemImplKind, LifetimeSource, PredicateOrigin, Target, find_attr,
1312
};
14-
use rustc_index::{IndexSlice, IndexVec};
1513
use rustc_middle::span_bug;
1614
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1715
use rustc_span::def_id::DefId;
1816
use rustc_span::edit_distance::find_best_match_for_name;
1917
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
20-
use smallvec::{SmallVec, smallvec};
18+
use smallvec::SmallVec;
2119
use thin_vec::ThinVec;
2220
use tracing::instrument;
2321

2422
use super::errors::{InvalidAbi, InvalidAbiSuggestion, TupleStructWithDefault, UnionWithDefault};
2523
use super::stability::{enabled_names, gate_unstable_abi};
2624
use super::{
27-
AstOwner, FnDeclKind, GenericArgsMode, ImplTraitContext, ImplTraitPosition, LoweringContext,
28-
ParamMode, RelaxedBoundForbiddenReason, RelaxedBoundPolicy, ResolverAstLoweringExt,
25+
FnDeclKind, GenericArgsMode, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
26+
RelaxedBoundForbiddenReason, RelaxedBoundPolicy, ResolverAstLoweringExt,
2927
};
3028

31-
/// Wraps either IndexVec (during `hir_crate`), which acts like a primary
32-
/// storage for most of the MaybeOwners, or FxIndexMap during delayed AST -> HIR
33-
/// lowering of delegations (`lower_delayed_owner`),
34-
/// in this case we can not modify already created IndexVec, so we use other map.
35-
pub(super) enum Owners<'a, 'hir> {
36-
IndexVec(&'a mut IndexVec<LocalDefId, hir::MaybeOwner<'hir>>),
37-
Map(&'a mut FxIndexMap<LocalDefId, hir::MaybeOwner<'hir>>),
38-
}
39-
40-
impl<'hir> Owners<'_, 'hir> {
41-
fn get_or_insert_mut(&mut self, def_id: LocalDefId) -> &mut hir::MaybeOwner<'hir> {
42-
match self {
43-
Owners::IndexVec(index_vec) => {
44-
index_vec.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom)
45-
}
46-
Owners::Map(map) => map.entry(def_id).or_insert(hir::MaybeOwner::Phantom),
47-
}
48-
}
49-
}
50-
5129
pub(super) struct ItemLowerer<'a, 'hir> {
5230
pub(super) tcx: TyCtxt<'hir>,
5331
pub(super) resolver: &'a ResolverAstLowering<'hir>,
54-
pub(super) ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
55-
pub(super) owners: Owners<'a, 'hir>,
5632
}
5733

5834
/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
@@ -78,47 +54,43 @@ impl<'hir> ItemLowerer<'_, 'hir> {
7854
fn with_lctx(
7955
&mut self,
8056
owner: NodeId,
81-
f: impl for<'a> FnOnce(&mut LoweringContext<'a, 'hir>) -> hir::OwnerNode<'hir>,
82-
) {
83-
let mut lctx = LoweringContext::new(self.tcx, self.resolver);
84-
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
85-
86-
for (def_id, info) in lctx.children {
87-
let owner = self.owners.get_or_insert_mut(def_id);
88-
assert!(
89-
matches!(owner, hir::MaybeOwner::Phantom),
90-
"duplicate copy of {def_id:?} in lctx.children"
91-
);
92-
*owner = info;
93-
}
57+
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
58+
) -> hir::MaybeOwner<'hir> {
59+
let mut lctx = LoweringContext::new(self.tcx, self.resolver, owner);
60+
61+
let item = f(&mut lctx);
62+
debug_assert_eq!(lctx.current_hir_id_owner, item.def_id());
63+
64+
let info = lctx.make_owner_info(item);
65+
66+
hir::MaybeOwner::Owner(lctx.arena.alloc(info))
9467
}
9568

96-
pub(super) fn lower_node(&mut self, def_id: LocalDefId) {
97-
let owner = self.owners.get_or_insert_mut(def_id);
98-
if let hir::MaybeOwner::Phantom = owner {
99-
let node = self.ast_index[def_id];
100-
match node {
101-
AstOwner::NonOwner => {}
102-
AstOwner::Crate(c) => {
103-
assert_eq!(self.resolver.local_def_id(CRATE_NODE_ID), CRATE_DEF_ID);
104-
self.with_lctx(CRATE_NODE_ID, |lctx| {
105-
let module = lctx.lower_mod(&c.items, &c.spans);
106-
// FIXME(jdonszelman): is dummy span ever a problem here?
107-
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP, Target::Crate);
108-
hir::OwnerNode::Crate(module)
109-
})
110-
}
111-
AstOwner::Item(item) => {
112-
self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
113-
}
114-
AstOwner::AssocItem(item, ctxt) => {
115-
self.with_lctx(item.id, |lctx| lctx.lower_assoc_item(item, ctxt))
116-
}
117-
AstOwner::ForeignItem(item) => self.with_lctx(item.id, |lctx| {
118-
hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item))
119-
}),
120-
}
121-
}
69+
#[instrument(level = "debug", skip(self, c))]
70+
pub(super) fn lower_crate(&mut self, c: &Crate) -> hir::MaybeOwner<'hir> {
71+
debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
72+
self.with_lctx(CRATE_NODE_ID, |lctx| {
73+
let module = lctx.lower_mod(&c.items, &c.spans);
74+
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, c.spans.inner_span, Target::Crate);
75+
hir::OwnerNode::Crate(module)
76+
})
77+
}
78+
79+
#[instrument(level = "debug", skip(self))]
80+
pub(super) fn lower_item(&mut self, item: &Item) -> hir::MaybeOwner<'hir> {
81+
self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
82+
}
83+
84+
pub(super) fn lower_trait_item(&mut self, item: &AssocItem) -> hir::MaybeOwner<'hir> {
85+
self.with_lctx(item.id, |lctx| hir::OwnerNode::TraitItem(lctx.lower_trait_item(item)))
86+
}
87+
88+
pub(super) fn lower_impl_item(&mut self, item: &AssocItem) -> hir::MaybeOwner<'hir> {
89+
self.with_lctx(item.id, |lctx| hir::OwnerNode::ImplItem(lctx.lower_impl_item(item)))
90+
}
91+
92+
pub(super) fn lower_foreign_item(&mut self, item: &ForeignItem) -> hir::MaybeOwner<'hir> {
93+
self.with_lctx(item.id, |lctx| hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item)))
12294
}
12395
}
12496

@@ -133,28 +105,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
133105
inner_span: self.lower_span(spans.inner_span),
134106
inject_use_span: self.lower_span(spans.inject_use_span),
135107
},
136-
item_ids: self.arena.alloc_from_iter(items.iter().flat_map(|x| self.lower_item_ref(x))),
108+
item_ids: self.arena.alloc_from_iter(items.iter().map(|x| self.lower_item_ref(x))),
137109
})
138110
}
139111

140-
pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
141-
let mut node_ids = smallvec![hir::ItemId { owner_id: self.owner_id(i.id) }];
142-
if let ItemKind::Use(use_tree) = &i.kind {
143-
self.lower_item_id_use_tree(use_tree, &mut node_ids);
144-
}
145-
node_ids
146-
}
147-
148-
fn lower_item_id_use_tree(&mut self, tree: &UseTree, vec: &mut SmallVec<[hir::ItemId; 1]>) {
149-
match &tree.kind {
150-
UseTreeKind::Nested { items, .. } => {
151-
for &(ref nested, id) in items {
152-
vec.push(hir::ItemId { owner_id: self.owner_id(id) });
153-
self.lower_item_id_use_tree(nested, vec);
154-
}
155-
}
156-
UseTreeKind::Simple(..) | UseTreeKind::Glob(_) => {}
157-
}
112+
pub(super) fn lower_item_ref(&mut self, i: &Item) -> hir::ItemId {
113+
hir::ItemId { owner_id: self.owner_id(i.id) }
158114
}
159115

160116
fn lower_eii_decl(
@@ -252,8 +208,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
252208
}
253209

254210
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
211+
let owner_id = self.current_hir_id_owner;
212+
let hir_id: HirId = owner_id.into();
255213
let vis_span = self.lower_span(i.vis.span);
256-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
257214

258215
let extra_hir_attributes = self.generate_extra_attrs_for_item_kind(i.id, &i.kind);
259216
let attrs = self.lower_attrs_with_extra(
@@ -266,7 +223,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
266223

267224
let kind = self.lower_item_kind(i.span, i.id, hir_id, attrs, vis_span, &i.kind);
268225
let item = hir::Item {
269-
owner_id: hir_id.expect_owner(),
226+
owner_id,
270227
kind,
271228
vis_span,
272229
span: self.lower_span(i.span),
@@ -779,21 +736,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
779736
}
780737
}
781738

782-
fn lower_assoc_item(&mut self, item: &AssocItem, ctxt: AssocCtxt) -> hir::OwnerNode<'hir> {
783-
// Evaluate with the lifetimes in `params` in-scope.
784-
// This is used to track which lifetimes have already been defined,
785-
// and which need to be replicated when lowering an async fn.
786-
match ctxt {
787-
AssocCtxt::Trait => hir::OwnerNode::TraitItem(self.lower_trait_item(item)),
788-
AssocCtxt::Impl { of_trait } => {
789-
hir::OwnerNode::ImplItem(self.lower_impl_item(item, of_trait))
790-
}
791-
}
792-
}
793-
794739
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
795-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
796-
let owner_id = hir_id.expect_owner();
740+
let owner_id = self.current_hir_id_owner;
741+
let hir_id: HirId = owner_id.into();
797742
let attrs =
798743
self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_foreign_item_kind(&i.kind));
799744
let (ident, kind) = match &i.kind {
@@ -973,14 +918,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
973918
}
974919

975920
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
976-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
921+
let trait_item_def_id = self.current_hir_id_owner;
922+
let hir_id: HirId = trait_item_def_id.into();
977923
let attrs = self.lower_attrs(
978924
hir_id,
979925
&i.attrs,
980926
i.span,
981927
Target::from_assoc_item_kind(&i.kind, AssocCtxt::Trait),
982928
);
983-
let trait_item_def_id = hir_id.expect_owner();
984929

985930
let (ident, generics, kind, has_value) = match &i.kind {
986931
AssocItemKind::Const(box ConstItem {
@@ -1202,16 +1147,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
12021147
})
12031148
}
12041149

1205-
fn lower_impl_item(
1206-
&mut self,
1207-
i: &AssocItem,
1208-
is_in_trait_impl: bool,
1209-
) -> &'hir hir::ImplItem<'hir> {
1150+
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
1151+
let owner_id = self.current_hir_id_owner;
1152+
let hir_id: HirId = owner_id.into();
1153+
let parent_id = self.tcx.local_parent(owner_id.def_id);
1154+
let is_in_trait_impl =
1155+
matches!(self.tcx.def_kind(parent_id), DefKind::Impl { of_trait: true });
1156+
12101157
// Since `default impl` is not yet implemented, this is always true in impls.
12111158
let has_value = true;
12121159
let (defaultness, _) =
12131160
self.lower_defaultness(i.kind.defaultness(), has_value, || hir::Defaultness::Final);
1214-
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
12151161
let attrs = self.lower_attrs(
12161162
hir_id,
12171163
&i.attrs,
@@ -1329,7 +1275,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13291275

13301276
let span = self.lower_span(i.span);
13311277
let item = hir::ImplItem {
1332-
owner_id: hir_id.expect_owner(),
1278+
owner_id,
13331279
ident: self.lower_ident(ident),
13341280
generics,
13351281
impl_kind: if is_in_trait_impl {

0 commit comments

Comments
 (0)