Skip to content

Commit 519a0c6

Browse files
committed
Auto merge of #142830 - cjgillot:lower-incr-2, r=<try>
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 febb10d + 4f4e26e commit 519a0c6

File tree

112 files changed

+1180
-1272
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+1180
-1272
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3355,7 +3355,6 @@ version = "0.0.0"
33553355
dependencies = [
33563356
"rustc_abi",
33573357
"rustc_ast",
3358-
"rustc_ast_pretty",
33593358
"rustc_attr_data_structures",
33603359
"rustc_attr_parsing",
33613360
"rustc_data_structures",

compiler/rustc_ast/src/ast.rs

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

40554055
pub type ForeignItem = Item<ForeignItemKind>;
40564056

4057+
#[derive(Debug)]
4058+
pub enum AstOwner {
4059+
NonOwner,
4060+
Synthetic(rustc_span::def_id::LocalDefId),
4061+
Crate(P<Crate>),
4062+
Item(P<Item>),
4063+
TraitItem(P<AssocItem>),
4064+
ImplItem(P<AssocItem>),
4065+
ForeignItem(P<ForeignItem>),
4066+
}
4067+
40574068
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
40584069
#[cfg(target_pointer_width = "64")]
40594070
mod size_asserts {

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ pub fn walk_flat_map_stmt<T: MutVisitor>(
142142
stmts
143143
}
144144

145-
fn walk_flat_map_stmt_kind<T: MutVisitor>(vis: &mut T, kind: StmtKind) -> SmallVec<[StmtKind; 1]> {
145+
pub fn walk_flat_map_stmt_kind<T: MutVisitor>(
146+
vis: &mut T,
147+
kind: StmtKind,
148+
) -> SmallVec<[StmtKind; 1]> {
146149
match kind {
147150
StmtKind::Let(mut local) => smallvec![StmtKind::Let({
148151
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_data_structures = { path = "../rustc_attr_data_structures" }
1514
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
1615
rustc_data_structures = { path = "../rustc_data_structures" }

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,
@@ -200,7 +200,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
200200
},
201201
InlineAsmOperand::Sym { sym } => {
202202
let static_def_id = self
203-
.resolver
204203
.get_partial_res(sym.id)
205204
.and_then(|res| res.full_res())
206205
.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
@@ -5,7 +5,7 @@ use smallvec::SmallVec;
55

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

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

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +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;
50+
use rustc_span::symbol::kw;
5051
use rustc_span::{Ident, Span, Symbol};
5152
use {rustc_ast as ast, rustc_hir as hir};
5253

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

5659
pub(crate) struct DelegationResults<'hir> {
5760
pub body_id: hir::BodyId,
@@ -60,22 +63,7 @@ pub(crate) struct DelegationResults<'hir> {
6063
pub generics: &'hir hir::Generics<'hir>,
6164
}
6265

63-
impl<'hir> LoweringContext<'_, 'hir> {
64-
/// Defines whether the delegatee is an associated function whose first parameter is `self`.
65-
pub(crate) fn delegatee_is_method(
66-
&self,
67-
item_id: NodeId,
68-
path_id: NodeId,
69-
span: Span,
70-
is_in_trait_impl: bool,
71-
) -> bool {
72-
let sig_id = self.get_delegation_sig_id(item_id, path_id, span, is_in_trait_impl);
73-
let Ok(sig_id) = sig_id else {
74-
return false;
75-
};
76-
self.is_method(sig_id, span)
77-
}
78-
66+
impl<'hir> LoweringContext<'hir> {
7967
fn is_method(&self, def_id: DefId, span: Span) -> bool {
8068
match self.tcx.def_kind(def_id) {
8169
DefKind::Fn => false,
@@ -101,10 +89,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
10189
let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span, is_in_trait_impl);
10290
match sig_id {
10391
Ok(sig_id) => {
92+
let is_method = self.is_method(sig_id, span);
10493
let (param_count, c_variadic) = self.param_count(sig_id);
10594
let decl = self.lower_delegation_decl(sig_id, param_count, c_variadic, span);
10695
let sig = self.lower_delegation_sig(sig_id, decl, span);
107-
let body_id = self.lower_delegation_body(delegation, param_count, span);
96+
let body_id = self.lower_delegation_body(delegation, is_method, param_count, span);
10897
let ident = self.lower_ident(delegation.ident);
10998
let generics = self.lower_delegation_generics(span);
11099
DelegationResults { body_id, sig, ident, generics }
@@ -125,8 +114,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
125114
}
126115

127116
fn get_resolution_id(&self, node_id: NodeId, span: Span) -> Result<DefId, ErrorGuaranteed> {
128-
let def_id =
129-
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());
130118
def_id.ok_or_else(|| {
131119
self.tcx.dcx().span_delayed_bug(
132120
span,
@@ -234,10 +222,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
234222
hir::FnSig { decl, header, span }
235223
}
236224

237-
fn generate_param(&mut self, idx: usize, span: Span) -> (hir::Param<'hir>, NodeId) {
225+
fn generate_param(
226+
&mut self,
227+
is_method: bool,
228+
idx: usize,
229+
span: Span,
230+
) -> (hir::Param<'hir>, NodeId) {
238231
let pat_node_id = self.next_node_id();
239232
let pat_id = self.lower_node_id(pat_node_id);
240-
let ident = Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}")));
233+
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
234+
let name = if is_method && idx == 0 {
235+
kw::SelfLower
236+
} else {
237+
Symbol::intern(&format!("arg{idx}"))
238+
};
239+
let ident = Ident::with_dummy_span(name);
241240
let pat = self.arena.alloc(hir::Pat {
242241
hir_id: pat_id,
243242
kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),
@@ -248,9 +247,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
248247
(hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)
249248
}
250249

251-
fn generate_arg(&mut self, idx: usize, param_id: HirId, span: Span) -> hir::Expr<'hir> {
250+
fn generate_arg(
251+
&mut self,
252+
is_method: bool,
253+
idx: usize,
254+
param_id: HirId,
255+
span: Span,
256+
) -> hir::Expr<'hir> {
257+
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
258+
let name = if is_method && idx == 0 {
259+
kw::SelfLower
260+
} else {
261+
Symbol::intern(&format!("arg{idx}"))
262+
};
252263
let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {
253-
ident: Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}"))),
264+
ident: Ident::with_dummy_span(name),
254265
hir_id: self.next_id(),
255266
res: Res::Local(param_id),
256267
args: None,
@@ -264,6 +275,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
264275
fn lower_delegation_body(
265276
&mut self,
266277
delegation: &Delegation,
278+
is_method: bool,
267279
param_count: usize,
268280
span: Span,
269281
) -> BodyId {
@@ -274,14 +286,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
274286
let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);
275287

276288
for idx in 0..param_count {
277-
let (param, pat_node_id) = this.generate_param(idx, span);
289+
let (param, pat_node_id) = this.generate_param(is_method, idx, span);
278290
parameters.push(param);
279291

280292
let arg = if let Some(block) = block
281293
&& idx == 0
282294
{
283295
let mut self_resolver = SelfResolver {
284-
resolver: this.resolver,
296+
ctxt: this,
285297
path_id: delegation.id,
286298
self_param_id: pat_node_id,
287299
};
@@ -290,7 +302,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
290302
this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
291303
this.lower_target_expr(&block)
292304
} else {
293-
this.generate_arg(idx, param.pat.hir_id, span)
305+
this.generate_arg(is_method, idx, param.pat.hir_id, span)
294306
};
295307
args.push(arg);
296308
}
@@ -427,25 +439,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
427439
}
428440
}
429441

430-
struct SelfResolver<'a> {
431-
resolver: &'a mut ResolverAstLowering,
442+
struct SelfResolver<'r, 'hir> {
443+
ctxt: &'r mut LoweringContext<'hir>,
432444
path_id: NodeId,
433445
self_param_id: NodeId,
434446
}
435447

436-
impl<'a> SelfResolver<'a> {
448+
impl SelfResolver<'_, '_> {
437449
fn try_replace_id(&mut self, id: NodeId) {
438-
if let Some(res) = self.resolver.partial_res_map.get(&id)
450+
if let Some(res) = self.ctxt.get_partial_res(id)
439451
&& let Some(Res::Local(sig_id)) = res.full_res()
440452
&& sig_id == self.path_id
441453
{
442454
let new_res = PartialRes::new(Res::Local(self.self_param_id));
443-
self.resolver.partial_res_map.insert(id, new_res);
455+
self.ctxt.partial_res_overrides.insert(id, new_res);
444456
}
445457
}
446458
}
447459

448-
impl<'ast, 'a> Visitor<'ast> for SelfResolver<'a> {
460+
impl<'ast> Visitor<'ast> for SelfResolver<'_, '_> {
449461
fn visit_id(&mut self, id: NodeId) {
450462
self.try_replace_id(id);
451463
}

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use std::sync::Arc;
33

44
use rustc_ast::ptr::P as AstP;
55
use rustc_ast::*;
6-
use rustc_ast_pretty::pprust::expr_to_string;
76
use rustc_attr_data_structures::{AttributeKind, find_attr};
87
use rustc_data_structures::stack::ensure_sufficient_stack;
98
use rustc_hir as hir;
109
use rustc_hir::HirId;
1110
use rustc_hir::def::{DefKind, Res};
11+
use rustc_hir::definitions::DefPathData;
1212
use rustc_middle::span_bug;
1313
use rustc_middle::ty::TyCtxt;
1414
use rustc_session::errors::report_lit_error;
@@ -52,7 +52,7 @@ impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
5252
}
5353
}
5454

55-
impl<'hir> LoweringContext<'_, 'hir> {
55+
impl<'hir> LoweringContext<'hir> {
5656
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
5757
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
5858
}
@@ -462,13 +462,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
462462
let mut invalid_expr_error = |tcx: TyCtxt<'_>, span| {
463463
// Avoid emitting the error multiple times.
464464
if error.is_none() {
465+
let sm = tcx.sess.source_map();
465466
let mut const_args = vec![];
466467
let mut other_args = vec![];
467468
for (idx, arg) in args.iter().enumerate() {
468-
if legacy_args_idx.contains(&idx) {
469-
const_args.push(format!("{{ {} }}", expr_to_string(arg)));
470-
} else {
471-
other_args.push(expr_to_string(arg));
469+
if let Ok(arg) = sm.span_to_snippet(arg.span) {
470+
if legacy_args_idx.contains(&idx) {
471+
const_args.push(format!("{{ {} }}", arg));
472+
} else {
473+
other_args.push(arg);
474+
}
472475
}
473476
}
474477
let suggestion = UseConstGenericArg {
@@ -488,7 +491,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
488491
for (idx, arg) in args.iter().cloned().enumerate() {
489492
if legacy_args_idx.contains(&idx) {
490493
let node_id = self.next_node_id();
491-
self.create_def(node_id, None, DefKind::AnonConst, f.span);
494+
self.create_def(
495+
node_id,
496+
None,
497+
DefKind::AnonConst,
498+
DefPathData::LateAnonConst,
499+
f.span,
500+
);
492501
let mut visitor = WillCreateDefIdsVisitor {};
493502
let const_value = if let ControlFlow::Break(span) = visitor.visit_expr(&arg) {
494503
AstP(Expr {
@@ -1260,7 +1269,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12601269
whole_span: Span,
12611270
) -> hir::ExprKind<'hir> {
12621271
// Return early in case of an ordinary assignment.
1263-
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
1272+
fn is_ordinary(lower_ctx: &mut LoweringContext<'_>, lhs: &Expr) -> bool {
12641273
match &lhs.kind {
12651274
ExprKind::Array(..)
12661275
| ExprKind::Struct(..)
@@ -1320,7 +1329,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13201329
) -> Option<(&'a Option<AstP<QSelf>>, &'a Path)> {
13211330
if let ExprKind::Path(qself, path) = &expr.kind {
13221331
// Does the path resolve to something disallowed in a tuple struct/variant pattern?
1323-
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1332+
if let Some(partial_res) = self.get_partial_res(expr.id) {
13241333
if let Some(res) = partial_res.full_res()
13251334
&& !res.expected_in_tuple_struct_pat()
13261335
{
@@ -1342,7 +1351,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13421351
) -> Option<(&'a Option<AstP<QSelf>>, &'a Path)> {
13431352
if let ExprKind::Path(qself, path) = &expr.kind {
13441353
// Does the path resolve to something disallowed in a unit struct/variant pattern?
1345-
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1354+
if let Some(partial_res) = self.get_partial_res(expr.id) {
13461355
if let Some(res) = partial_res.full_res()
13471356
&& !res.expected_in_unit_struct_pat()
13481357
{

0 commit comments

Comments
 (0)