Skip to content

Commit 89a9993

Browse files
committed
Auto merge of #142830 - cjgillot:lower-incr-2, r=petrochenkov
Make lowering incremental, take 3/N Rebase of #88186 that was rebased by #127262 No real change in design, so I'm not expecting a perf miracle. r? @ghost
2 parents b998449 + 2a30bc0 commit 89a9993

29 files changed

Lines changed: 552 additions & 582 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3640,7 +3640,6 @@ version = "0.0.0"
36403640
dependencies = [
36413641
"rustc_abi",
36423642
"rustc_ast",
3643-
"rustc_ast_pretty",
36443643
"rustc_attr_parsing",
36453644
"rustc_data_structures",
36463645
"rustc_errors",

compiler/rustc_ast/src/ast.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
3030
use rustc_data_structures::tagged_ptr::Tag;
3131
use rustc_macros::{Decodable, Encodable, StableHash, Walkable};
3232
pub use rustc_span::AttrId;
33+
use rustc_span::def_id::LocalDefId;
3334
use rustc_span::{
3435
ByteSymbol, DUMMY_SP, ErrorGuaranteed, Ident, LocalExpnId, Span, Spanned, Symbol, kw, respan,
3536
sym,
@@ -4362,6 +4363,23 @@ impl TryFrom<ItemKind> for ForeignItemKind {
43624363

43634364
pub type ForeignItem = Item<ForeignItemKind>;
43644365

4366+
/// Fragment of the AST according to "HIR owner" semantics.
4367+
///
4368+
/// This is used to map each `LocalDefId` to its content's AST.
4369+
#[derive(Debug)]
4370+
pub enum AstOwner {
4371+
/// This definition does not correspond to a HIR owner.
4372+
NonOwner,
4373+
/// This definition corresponds to a nested `use` tree.
4374+
/// The `LocalDefId` points to its HIR owner.
4375+
NestedUseTree(LocalDefId),
4376+
Crate(Box<Crate>),
4377+
Item(Box<Item>),
4378+
TraitItem(Box<AssocItem>),
4379+
ImplItem(Box<AssocItem>),
4380+
ForeignItem(Box<ForeignItem>),
4381+
}
4382+
43654383
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
43664384
#[cfg(target_pointer_width = "64")]
43674385
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/delegation.rs

Lines changed: 40 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ use rustc_abi::ExternAbi;
4646
use rustc_ast as ast;
4747
use rustc_ast::node_id::NodeMap;
4848
use rustc_ast::*;
49-
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
49+
use rustc_data_structures::fx::FxHashSet;
5050
use rustc_hir::attrs::{AttributeKind, InlineAttr};
51-
use rustc_hir::def_id::{DefId, LocalDefId};
5251
use rustc_hir::{self as hir, FnDeclFlags};
5352
use rustc_middle::span_bug;
54-
use rustc_middle::ty::{Asyncness, PerOwnerResolverData, TyCtxt};
53+
use rustc_middle::ty::{Asyncness, PerOwnerResolverData};
54+
use rustc_span::def_id::{DefId, LocalDefId};
5555
use rustc_span::symbol::kw;
5656
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};
5757

@@ -62,7 +62,6 @@ use crate::diagnostics::{
6262
};
6363
use crate::{
6464
AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
65-
index_crate,
6665
};
6766

6867
mod generics;
@@ -121,64 +120,6 @@ static ATTRS_ADDITIONS: &[AttrAdditionInfo] = &[
121120
},
122121
];
123122

124-
pub(crate) fn delegations_resolutions(
125-
tcx: TyCtxt<'_>,
126-
_: (),
127-
) -> FxIndexMap<LocalDefId, Result<DefId, ErrorGuaranteed>> {
128-
let krate = tcx.hir_crate(());
129-
130-
let (resolver, ast_crate) = &*krate.delayed_resolver.borrow();
131-
132-
// FIXME!!!(fn_delegation): make ast index lifetime same as resolver,
133-
// as it is too bad to reindex whole crate on each delegation lowering.
134-
let ast_index = index_crate(resolver, ast_crate);
135-
136-
let mut result = FxIndexMap::<LocalDefId, Result<DefId, ErrorGuaranteed>>::default();
137-
138-
for &def_id in &krate.delayed_ids {
139-
let delegation = ast_index[def_id].delegation().expect("processing delegations");
140-
let span = delegation.last_segment_span();
141-
142-
if let Some(info) = tcx.resolutions(()).delegation_infos.get(&def_id) {
143-
let res = info.resolution_id.map(|id| check_for_cycles(tcx, id, span).map(|_| id));
144-
result.insert(def_id, res.flatten());
145-
} else {
146-
tcx.dcx().span_delayed_bug(
147-
span,
148-
format!("delegation resolution record was not found for {def_id:?}"),
149-
);
150-
}
151-
}
152-
153-
result
154-
}
155-
156-
fn check_for_cycles(tcx: TyCtxt<'_>, mut def_id: DefId, span: Span) -> Result<(), ErrorGuaranteed> {
157-
let mut visited: FxHashSet<DefId> = Default::default();
158-
159-
loop {
160-
visited.insert(def_id);
161-
162-
// If def_id is in local crate and it corresponds to another delegation
163-
// it means that we refer to another delegation as a callee, so in order to obtain
164-
// a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
165-
if let Some(local_id) = def_id.as_local()
166-
&& let Some(info) = tcx.resolutions(()).delegation_infos.get(&local_id)
167-
&& let Ok(id) = info.resolution_id
168-
{
169-
def_id = id;
170-
if visited.contains(&def_id) {
171-
return Err(match visited.len() {
172-
1 => tcx.dcx().emit_err(UnresolvedDelegationCallee { span }),
173-
_ => tcx.dcx().emit_err(CycleInDelegationSignatureResolution { span }),
174-
});
175-
}
176-
} else {
177-
return Ok(());
178-
}
179-
}
180-
}
181-
182123
impl<'hir> LoweringContext<'_, 'hir> {
183124
fn is_method(&self, def_id: DefId, span: Span) -> bool {
184125
match self.tcx.def_kind(def_id) {
@@ -188,18 +129,53 @@ impl<'hir> LoweringContext<'_, 'hir> {
188129
}
189130
}
190131

132+
fn check_for_cycles(&self, mut def_id: DefId, span: Span) -> Result<(), ErrorGuaranteed> {
133+
let mut visited: FxHashSet<DefId> = Default::default();
134+
135+
loop {
136+
visited.insert(def_id);
137+
138+
// If def_id is in local crate and it corresponds to another delegation
139+
// it means that we refer to another delegation as a callee, so in order to obtain
140+
// a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
141+
if let Some(local_id) = def_id.as_local()
142+
&& let Some(info) = self.tcx.resolutions(()).delegation_infos.get(&local_id)
143+
&& let Ok(id) = info.resolution_id
144+
{
145+
def_id = id;
146+
if visited.contains(&def_id) {
147+
return Err(match visited.len() {
148+
1 => self.dcx().emit_err(UnresolvedDelegationCallee { span }),
149+
_ => self.dcx().emit_err(CycleInDelegationSignatureResolution { span }),
150+
});
151+
}
152+
} else {
153+
return Ok(());
154+
}
155+
}
156+
}
157+
191158
pub(crate) fn lower_delegation(
192159
&mut self,
193160
delegation: &Delegation,
194161
item_id: NodeId,
195162
) -> DelegationResults<'hir> {
196163
let span = self.lower_span(delegation.last_segment_span());
197164

198-
let sig_id = self.tcx.delegations_resolutions(()).get(&self.owner.def_id).copied();
165+
let Some(info) = self.tcx.resolutions(()).delegation_infos.get(&self.owner.def_id) else {
166+
self.dcx().span_delayed_bug(
167+
span,
168+
format!("delegation resolution record was not found for {:?}", self.owner.def_id),
169+
);
170+
171+
return self.generate_delegation_error(span, delegation);
172+
};
173+
174+
let sig_id = info.resolution_id.and_then(|id| self.check_for_cycles(id, span).map(|_| id));
199175

200176
// Delegation can be missing from the `delegations_resolutions` table
201177
// in illegal places such as function bodies in extern blocks (see #151356).
202-
let Some(Ok(sig_id)) = sig_id else {
178+
let Ok(sig_id) = sig_id else {
203179
self.dcx().span_delayed_bug(
204180
span,
205181
format!("LoweringContext: the delegation {:?} is unresolved", item_id),

compiler/rustc_ast_lowering/src/expr.rs

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

55
use rustc_ast::node_id::NodeMap;
66
use rustc_ast::*;
7-
use rustc_ast_pretty::pprust::expr_to_string;
87
use rustc_data_structures::stack::ensure_sufficient_stack;
98
use rustc_errors::msg;
109
use rustc_hir as hir;
@@ -557,13 +556,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
557556
let mut invalid_expr_error = |tcx: TyCtxt<'_>, span| {
558557
// Avoid emitting the error multiple times.
559558
if error.is_none() {
559+
let sm = tcx.sess.source_map();
560560
let mut const_args = vec![];
561561
let mut other_args = vec![];
562562
for (idx, arg) in args.iter().enumerate() {
563-
if legacy_args_idx.contains(&idx) {
564-
const_args.push(format!("{{ {} }}", expr_to_string(arg)));
565-
} else {
566-
other_args.push(expr_to_string(arg));
563+
if let Ok(arg) = sm.span_to_snippet(arg.span) {
564+
if legacy_args_idx.contains(&idx) {
565+
const_args.push(format!("{{ {} }}", arg));
566+
} else {
567+
other_args.push(arg);
568+
}
567569
}
568570
}
569571
let suggestion = UseConstGenericArg {

0 commit comments

Comments
 (0)