Skip to content

Commit cdfd675

Browse files
committed
Auto merge of #99867 - spastorino:refactor-remap-lifetimes, r=nikomatsakis
Split create_def and lowering of lifetimes for opaque types and bare async fns r? `@cjgillot` This work is kind of half-way, but I think it could be merged anyway. I think we should be able to remove all the vacant arms in `new_named_lifetime_with_res`, if I'm not wrong that requires visiting more nodes. We can do that as a follow up. In follow-up PRs, besides the thing mentioned previously, I'll be trying to remove `LifetimeCaptureContext`, `captured_lifetimes` as a global data structure, global `binders_to_ignore` and all their friends :). Also try to remap in a more general way based on def-ids.
2 parents 6bcf01a + 4170d73 commit cdfd675

File tree

8 files changed

+588
-296
lines changed

8 files changed

+588
-296
lines changed

compiler/rustc_ast/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl fmt::Debug for Label {
6464

6565
/// A "Lifetime" is an annotation of the scope in which variable
6666
/// can be used, e.g. `'a` in `&'a i32`.
67-
#[derive(Clone, Encodable, Decodable, Copy)]
67+
#[derive(Clone, Encodable, Decodable, Copy, PartialEq, Eq)]
6868
pub struct Lifetime {
6969
pub id: NodeId,
7070
pub ident: Ident,

compiler/rustc_ast_lowering/src/expr.rs

+31-32
Original file line numberDiff line numberDiff line change
@@ -864,22 +864,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
864864
(body_id, generator_option)
865865
});
866866

867-
self.with_lifetime_binder(closure_id, generic_params, |this, bound_generic_params| {
868-
// Lower outside new scope to preserve `is_in_loop_condition`.
869-
let fn_decl = this.lower_fn_decl(decl, None, FnDeclKind::Closure, None);
870-
871-
let c = self.arena.alloc(hir::Closure {
872-
binder: binder_clause,
873-
capture_clause,
874-
bound_generic_params,
875-
fn_decl,
876-
body: body_id,
877-
fn_decl_span: this.lower_span(fn_decl_span),
878-
movability: generator_option,
879-
});
867+
let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);
868+
// Lower outside new scope to preserve `is_in_loop_condition`.
869+
let fn_decl = self.lower_fn_decl(decl, None, FnDeclKind::Closure, None);
870+
871+
let c = self.arena.alloc(hir::Closure {
872+
binder: binder_clause,
873+
capture_clause,
874+
bound_generic_params,
875+
fn_decl,
876+
body: body_id,
877+
fn_decl_span: self.lower_span(fn_decl_span),
878+
movability: generator_option,
879+
});
880880

881-
hir::ExprKind::Closure(c)
882-
})
881+
hir::ExprKind::Closure(c)
883882
}
884883

885884
fn generator_movability_for_fn(
@@ -991,23 +990,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
991990
body_id
992991
});
993992

994-
self.with_lifetime_binder(closure_id, generic_params, |this, bound_generic_params| {
995-
// We need to lower the declaration outside the new scope, because we
996-
// have to conserve the state of being inside a loop condition for the
997-
// closure argument types.
998-
let fn_decl = this.lower_fn_decl(&outer_decl, None, FnDeclKind::Closure, None);
999-
1000-
let c = self.arena.alloc(hir::Closure {
1001-
binder: binder_clause,
1002-
capture_clause,
1003-
bound_generic_params,
1004-
fn_decl,
1005-
body,
1006-
fn_decl_span: this.lower_span(fn_decl_span),
1007-
movability: None,
1008-
});
1009-
hir::ExprKind::Closure(c)
1010-
})
993+
let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);
994+
995+
// We need to lower the declaration outside the new scope, because we
996+
// have to conserve the state of being inside a loop condition for the
997+
// closure argument types.
998+
let fn_decl = self.lower_fn_decl(&outer_decl, None, FnDeclKind::Closure, None);
999+
1000+
let c = self.arena.alloc(hir::Closure {
1001+
binder: binder_clause,
1002+
capture_clause,
1003+
bound_generic_params,
1004+
fn_decl,
1005+
body,
1006+
fn_decl_span: self.lower_span(fn_decl_span),
1007+
movability: None,
1008+
});
1009+
hir::ExprKind::Closure(c)
10111010
}
10121011

10131012
/// Destructure the LHS of complex assignments.

compiler/rustc_ast_lowering/src/item.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
8080
generator_kind: None,
8181
task_context: None,
8282
current_item: None,
83-
captured_lifetimes: None,
8483
impl_trait_defs: Vec::new(),
8584
impl_trait_bounds: Vec::new(),
8685
allow_try_trait: Some([sym::try_trait_v2, sym::yeet_desugar_details][..].into()),
@@ -1350,12 +1349,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
13501349

13511350
let mut predicates: SmallVec<[hir::WherePredicate<'hir>; 4]> = SmallVec::new();
13521351
predicates.extend(generics.params.iter().filter_map(|param| {
1353-
let bounds = self.lower_param_bounds(&param.bounds, itctx);
13541352
self.lower_generic_bound_predicate(
13551353
param.ident,
13561354
param.id,
13571355
&param.kind,
1358-
bounds,
1356+
&param.bounds,
1357+
itctx,
13591358
PredicateOrigin::GenericParam,
13601359
)
13611360
}));
@@ -1403,13 +1402,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
14031402
ident: Ident,
14041403
id: NodeId,
14051404
kind: &GenericParamKind,
1406-
bounds: &'hir [hir::GenericBound<'hir>],
1405+
bounds: &[GenericBound],
1406+
itctx: ImplTraitContext,
14071407
origin: PredicateOrigin,
14081408
) -> Option<hir::WherePredicate<'hir>> {
14091409
// Do not create a clause if we do not have anything inside it.
14101410
if bounds.is_empty() {
14111411
return None;
14121412
}
1413+
1414+
let bounds = self.lower_param_bounds(bounds, itctx);
1415+
14131416
let ident = self.lower_ident(ident);
14141417
let param_span = ident.span;
14151418
let span = bounds
@@ -1450,11 +1453,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
14501453
GenericParamKind::Lifetime => {
14511454
let ident_span = self.lower_span(ident.span);
14521455
let ident = self.lower_ident(ident);
1453-
let res = self.resolver.get_lifetime_res(id).unwrap_or_else(|| {
1454-
panic!("Missing resolution for lifetime {:?} at {:?}", id, ident.span)
1455-
});
14561456
let lt_id = self.next_node_id();
1457-
let lifetime = self.new_named_lifetime_with_res(lt_id, ident_span, ident, res);
1457+
let lifetime = self.new_named_lifetime(id, lt_id, ident_span, ident);
14581458
Some(hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
14591459
lifetime,
14601460
span,

0 commit comments

Comments
 (0)