Skip to content

Commit a77c1ae

Browse files
committed
Auto merge of #153489 - aerooneqq:two-phase-hir, r=<try>
TY-aware delayed HIR lowering
2 parents f824853 + 13219bc commit a77c1ae

30 files changed

Lines changed: 518 additions & 530 deletions

File tree

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, ParamMode, ResolverAstLoweringExt,
2424
};
2525

26-
impl<'a, 'hir> LoweringContext<'a, 'hir> {
26+
impl<'a, 'b, 'hir> LoweringContext<'a, 'b, 'hir> {
2727
pub(crate) fn lower_inline_asm(
2828
&mut self,
2929
sp: Span,

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 1 addition & 1 deletion
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<'a, 'b, 'hir> LoweringContext<'a, 'b, 'hir> {
1010
pub(super) fn lower_block(
1111
&mut self,
1212
b: &Block,

compiler/rustc_ast_lowering/src/contract.rs

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

55
use crate::LoweringContext;
66

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

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 59 additions & 200 deletions
Large diffs are not rendered by default.

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_span::symbol::kw;
1010
use rustc_span::{DUMMY_SP, Ident, Span};
1111
use thin_vec::{ThinVec, thin_vec};
1212

13-
use crate::{AstOwner, LoweringContext};
13+
use crate::{LoweringContext, ResolverAstLoweringExt};
1414

1515
pub(super) enum DelegationGenerics<T> {
1616
/// User-specified args are present: `reuse foo::<String>;`.
@@ -60,7 +60,7 @@ impl<T> DelegationGenerics<T> {
6060
impl<'hir> HirOrAstGenerics<'hir> {
6161
pub(super) fn into_hir_generics(
6262
&mut self,
63-
ctx: &mut LoweringContext<'_, 'hir>,
63+
ctx: &mut LoweringContext<'_, '_, 'hir>,
6464
item_id: NodeId,
6565
span: Span,
6666
) -> &mut HirOrAstGenerics<'hir> {
@@ -100,7 +100,7 @@ impl<'hir> HirOrAstGenerics<'hir> {
100100

101101
pub(super) fn into_generic_args(
102102
&self,
103-
ctx: &mut LoweringContext<'_, 'hir>,
103+
ctx: &mut LoweringContext<'_, '_, 'hir>,
104104
add_lifetimes: bool,
105105
span: Span,
106106
) -> Option<&'hir hir::GenericArgs<'hir>> {
@@ -140,7 +140,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
140140
&mut self,
141141
item_id: NodeId,
142142
span: Span,
143-
ctx: &mut LoweringContext<'_, 'hir>,
143+
ctx: &mut LoweringContext<'_, '_, 'hir>,
144144
) -> impl Iterator<Item = hir::GenericParam<'hir>> {
145145
// Now we always call `into_hir_generics` both on child and parent,
146146
// however in future we would not do that, when scenarios like
@@ -182,7 +182,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
182182
&mut self,
183183
item_id: NodeId,
184184
span: Span,
185-
ctx: &mut LoweringContext<'_, 'hir>,
185+
ctx: &mut LoweringContext<'_, '_, 'hir>,
186186
) -> impl Iterator<Item = hir::WherePredicate<'hir>> {
187187
// Now we always call `into_hir_generics` both on child and parent,
188188
// however in future we would not do that, when scenarios like
@@ -207,11 +207,11 @@ impl<'hir> GenericsGenerationResults<'hir> {
207207
}
208208
}
209209

210-
impl<'hir> LoweringContext<'_, 'hir> {
210+
impl<'hir> LoweringContext<'_, '_, 'hir> {
211211
pub(super) fn lower_delegation_generics(
212212
&mut self,
213213
delegation: &Delegation,
214-
root_fn_id: DefId,
214+
delegee_id: DefId,
215215
item_id: NodeId,
216216
span: Span,
217217
) -> GenericsGenerationResults<'hir> {
@@ -221,13 +221,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
221221
);
222222

223223
let root_function_in_trait =
224-
matches!(self.tcx.def_kind(self.tcx.parent(root_fn_id)), DefKind::Trait);
224+
matches!(self.tcx.def_kind(self.tcx.parent(delegee_id)), DefKind::Trait);
225225

226226
let generate_self = delegation_in_free_ctx && root_function_in_trait;
227227

228228
let parent_generics_factory = |this: &mut Self, user_specified: bool| {
229229
this.get_parent_generics(
230-
this.tcx.parent(root_fn_id),
230+
this.tcx.parent(delegee_id),
231231
generate_self,
232232
user_specified,
233233
span,
@@ -259,7 +259,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
259259
let child_generics = if segments[len - 1].args.is_some() {
260260
DelegationGenerics::UserSpecified
261261
} else {
262-
DelegationGenerics::Default(self.get_fn_like_generics(root_fn_id, span))
262+
DelegationGenerics::Default(self.get_fn_like_generics(delegee_id, span))
263263
};
264264

265265
GenericsGenerationResults {
@@ -289,11 +289,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
289289

290290
// Note that we use self.disambiguator here, if we will create new every time
291291
// we will get ICE if params have the same name.
292-
self.resolver.node_id_to_def_id.insert(
292+
self.resolver.mut_part.node_id_to_def_id.insert(
293293
p.id,
294294
self.tcx
295295
.create_def(
296-
self.resolver.node_id_to_def_id[&item_id],
296+
self.resolver.def_id(item_id).unwrap(),
297297
Some(p.ident.name),
298298
match p.kind {
299299
GenericParamKind::Lifetime => DefKind::LifetimeParam,
@@ -394,7 +394,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
394394
res,
395395
}]),
396396
res,
397-
span: p.span,
397+
span,
398398
}),
399399
)
400400
};
@@ -415,15 +415,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
415415
hir::GenericParamKind::Type { .. } => {
416416
Some(hir::GenericArg::Type(self.arena.alloc(hir::Ty {
417417
hir_id: self.next_id(),
418-
span: p.span,
418+
span,
419419
kind: hir::TyKind::Path(create_path(self)),
420420
})))
421421
}
422422
hir::GenericParamKind::Const { .. } => {
423423
Some(hir::GenericArg::Const(self.arena.alloc(hir::ConstArg {
424424
hir_id: self.next_id(),
425425
kind: hir::ConstArgKind::Path(create_path(self)),
426-
span: p.span,
426+
span,
427427
})))
428428
}
429429
}
@@ -435,19 +435,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
435435
}
436436

437437
fn get_fn_like_generics(&mut self, id: DefId, span: Span) -> Option<Generics> {
438-
if let Some(local_id) = id.as_local() {
439-
match self.ast_index.get(local_id) {
440-
Some(AstOwner::Item(item)) if let ItemKind::Fn(f) = &item.kind => {
441-
Some(f.generics.clone())
442-
}
443-
Some(AstOwner::AssocItem(item, _)) if let AssocItemKind::Fn(f) = &item.kind => {
444-
Some(f.generics.clone())
445-
}
446-
_ => None,
447-
}
448-
} else {
449-
self.get_external_generics(id, false, span)
450-
}
438+
self.get_external_generics(id, false, span)
451439
}
452440

453441
fn get_external_generics(
@@ -509,7 +497,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
509497

510498
let node_id = self.next_node_id();
511499

512-
self.resolver.partial_res_map.insert(node_id, hir::def::PartialRes::new(res));
500+
self.resolver.mut_part.partial_res_map.insert(node_id, hir::def::PartialRes::new(res));
513501

514502
GenericParamKind::Const {
515503
ty: Box::new(Ty {
@@ -542,21 +530,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
542530
span: Span,
543531
) -> Option<Generics> {
544532
// If args are user-specified we still maybe need to add self.
545-
let mut generics = if user_specified {
546-
None
547-
} else {
548-
if let Some(local_id) = id.as_local() {
549-
if let Some(AstOwner::Item(item)) = self.ast_index.get(local_id)
550-
&& matches!(item.kind, ItemKind::Trait(..))
551-
{
552-
item.opt_generics().cloned()
553-
} else {
554-
None
555-
}
556-
} else {
557-
self.get_external_generics(id, true, span)
558-
}
559-
};
533+
let mut generics =
534+
if user_specified { None } else { self.get_external_generics(id, true, span) };
560535

561536
if add_self {
562537
generics.get_or_insert_default().params.insert(

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
5353
}
5454
}
5555

56-
impl<'hir> LoweringContext<'_, 'hir> {
56+
impl<'hir> LoweringContext<'_, '_, 'hir> {
5757
fn lower_exprs(&mut self, exprs: &[Box<Expr>]) -> &'hir [hir::Expr<'hir>] {
5858
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
5959
}
@@ -1232,7 +1232,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12321232
whole_span: Span,
12331233
) -> hir::ExprKind<'hir> {
12341234
// Return early in case of an ordinary assignment.
1235-
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
1235+
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_, '_>, lhs: &Expr) -> bool {
12361236
match &lhs.kind {
12371237
ExprKind::Array(..)
12381238
| ExprKind::Struct(..)

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_span::{ByteSymbol, 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.
@@ -230,7 +230,7 @@ enum ArgumentType {
230230
/// <core::fmt::Argument>::new_…(arg)
231231
/// ```
232232
fn make_argument<'hir>(
233-
ctx: &mut LoweringContext<'_, 'hir>,
233+
ctx: &mut LoweringContext<'_, '_, 'hir>,
234234
sp: Span,
235235
arg: &'hir hir::Expr<'hir>,
236236
ty: ArgumentType,
@@ -277,7 +277,7 @@ fn make_count(
277277
}
278278

279279
fn expand_format_args<'hir>(
280-
ctx: &mut LoweringContext<'_, 'hir>,
280+
ctx: &mut LoweringContext<'_, '_, 'hir>,
281281
macsp: Span,
282282
fmt: &FormatArgs,
283283
allow_const: bool,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_abi::ExternAbi;
22
use rustc_ast::visit::AssocCtxt;
33
use rustc_ast::*;
4+
use rustc_data_structures::fx::FxIndexMap;
45
use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
56
use rustc_hir::attrs::{AttributeKind, EiiImplResolution};
67
use rustc_hir::def::{DefKind, PerNS, Res};
@@ -10,7 +11,7 @@ use rustc_hir::{
1011
};
1112
use rustc_index::{IndexSlice, IndexVec};
1213
use rustc_middle::span_bug;
13-
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
14+
use rustc_middle::ty::TyCtxt;
1415
use rustc_span::def_id::DefId;
1516
use rustc_span::edit_distance::find_best_match_for_name;
1617
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
@@ -24,12 +25,18 @@ use super::{
2425
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
2526
RelaxedBoundForbiddenReason, RelaxedBoundPolicy, ResolverAstLoweringExt,
2627
};
28+
use crate::CombinedResolverForLowering;
2729

28-
pub(super) struct ItemLowerer<'a, 'hir> {
30+
pub(super) enum Owners<'b, 'hir> {
31+
IndexVec(&'b mut IndexVec<LocalDefId, hir::MaybeOwner<'hir>>),
32+
Map(&'b mut FxIndexMap<LocalDefId, hir::MaybeOwner<'hir>>),
33+
}
34+
35+
pub(super) struct ItemLowerer<'a, 'b, 'hir> {
2936
pub(super) tcx: TyCtxt<'hir>,
30-
pub(super) resolver: &'a mut ResolverAstLowering,
31-
pub(super) ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
32-
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<'hir>>,
37+
pub(super) resolver: &'b mut CombinedResolverForLowering<'a, 'hir>,
38+
pub(super) ast_index: &'b IndexSlice<LocalDefId, AstOwner<'a>>,
39+
pub(super) owners: Owners<'b, 'hir>,
3340
}
3441

3542
/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
@@ -51,17 +58,23 @@ fn add_ty_alias_where_clause(
5158
if before.0 || !after.0 { before } else { after };
5259
}
5360

54-
impl<'a, 'hir> ItemLowerer<'a, 'hir> {
61+
impl<'a, 'b, 'hir> ItemLowerer<'a, 'b, 'hir> {
5562
fn with_lctx(
56-
&mut self,
63+
&'b mut self,
5764
owner: NodeId,
58-
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
65+
f: impl FnOnce(&mut LoweringContext<'a, 'b, 'hir>) -> hir::OwnerNode<'hir>,
5966
) {
60-
let mut lctx = LoweringContext::new(self.tcx, self.ast_index, self.resolver);
67+
let mut lctx = LoweringContext::new(self.tcx, self.resolver);
6168
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
6269

6370
for (def_id, info) in lctx.children {
64-
let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
71+
let owner = match &mut self.owners {
72+
Owners::IndexVec(index_vec) => {
73+
index_vec.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom)
74+
}
75+
Owners::Map(hash_map) => hash_map.entry(def_id).or_insert(hir::MaybeOwner::Phantom),
76+
};
77+
6578
assert!(
6679
matches!(owner, hir::MaybeOwner::Phantom),
6780
"duplicate copy of {def_id:?} in lctx.children"
@@ -70,14 +83,20 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
7083
}
7184
}
7285

73-
pub(super) fn lower_node(&mut self, def_id: LocalDefId) {
74-
let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
86+
pub(super) fn lower_node(&'b mut self, def_id: LocalDefId) {
87+
let owner = match &mut self.owners {
88+
Owners::IndexVec(index_vec) => {
89+
index_vec.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom)
90+
}
91+
Owners::Map(hash_map) => hash_map.get(&def_id).unwrap_or(&hir::MaybeOwner::Phantom),
92+
};
93+
7594
if let hir::MaybeOwner::Phantom = owner {
7695
let node = self.ast_index[def_id];
7796
match node {
7897
AstOwner::NonOwner => {}
7998
AstOwner::Crate(c) => {
80-
assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
99+
assert_eq!(self.resolver.def_id(CRATE_NODE_ID).unwrap(), CRATE_DEF_ID);
81100
self.with_lctx(CRATE_NODE_ID, |lctx| {
82101
let module = lctx.lower_mod(&c.items, &c.spans);
83102
// FIXME(jdonszelman): is dummy span ever a problem here?
@@ -99,7 +118,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
99118
}
100119
}
101120

102-
impl<'hir> LoweringContext<'_, 'hir> {
121+
impl<'hir> LoweringContext<'_, '_, 'hir> {
103122
pub(super) fn lower_mod(
104123
&mut self,
105124
items: &[Box<Item>],
@@ -1475,7 +1494,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14751494
pub(crate) fn lower_coroutine_body_with_moved_arguments(
14761495
&mut self,
14771496
decl: &FnDecl,
1478-
lower_body: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::Expr<'hir>,
1497+
lower_body: impl FnOnce(&mut LoweringContext<'_, '_, 'hir>) -> hir::Expr<'hir>,
14791498
fn_decl_span: Span,
14801499
body_span: Span,
14811500
coroutine_kind: CoroutineKind,
@@ -1612,7 +1631,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16121631
parameters.push(new_parameter);
16131632
}
16141633

1615-
let mkbody = |this: &mut LoweringContext<'_, 'hir>| {
1634+
let mkbody = |this: &mut LoweringContext<'_, '_, 'hir>| {
16161635
// Create a block from the user's function body:
16171636
let user_body = lower_body(this);
16181637

@@ -1830,7 +1849,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
18301849
.collect();
18311850

18321851
// Introduce extra lifetimes if late resolution tells us to.
1833-
let extra_lifetimes = self.resolver.extra_lifetime_params(parent_node_id);
1852+
let extra_lifetimes =
1853+
self.resolver.extra_lifetime_params(parent_node_id).cloned().unwrap_or_default();
18341854
params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
18351855
self.lifetime_res_to_generic_param(
18361856
ident,

0 commit comments

Comments
 (0)