Skip to content

Commit 19c29bb

Browse files
committed
Remap impl-trait lifetimes on HIR instead of AST lowering.
1 parent ef17eb7 commit 19c29bb

File tree

33 files changed

+506
-621
lines changed

33 files changed

+506
-621
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+9-283
Large diffs are not rendered by default.

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
830830
///
831831
/// [`OpaqueDef`]: hir::TyKind::OpaqueDef
832832
fn get_future_inner_return_ty(&self, hir_ty: &'tcx hir::Ty<'tcx>) -> &'tcx hir::Ty<'tcx> {
833-
let hir::TyKind::OpaqueDef(opaque_ty, _) = hir_ty.kind else {
833+
let hir::TyKind::OpaqueDef(opaque_ty) = hir_ty.kind else {
834834
span_bug!(
835835
hir_ty.span,
836836
"lowered return type of async fn is not OpaqueDef: {:?}",

compiler/rustc_hir/src/hir.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -2632,7 +2632,6 @@ impl<'hir> Ty<'hir> {
26322632
}
26332633
TyKind::Tup(tys) => tys.iter().any(Self::is_suggestable_infer_ty),
26342634
TyKind::Ptr(mut_ty) | TyKind::Ref(_, mut_ty) => mut_ty.ty.is_suggestable_infer_ty(),
2635-
TyKind::OpaqueDef(_, generic_args) => are_suggestable_generic_args(generic_args),
26362635
TyKind::Path(QPath::TypeRelative(ty, segment)) => {
26372636
ty.is_suggestable_infer_ty() || are_suggestable_generic_args(segment.args().args)
26382637
}
@@ -2751,19 +2750,8 @@ pub struct BareFnTy<'hir> {
27512750
pub struct OpaqueTy<'hir> {
27522751
pub hir_id: HirId,
27532752
pub def_id: LocalDefId,
2754-
pub generics: &'hir Generics<'hir>,
27552753
pub bounds: GenericBounds<'hir>,
27562754
pub origin: OpaqueTyOrigin,
2757-
/// Return-position impl traits (and async futures) must "reify" any late-bound
2758-
/// lifetimes that are captured from the function signature they originate from.
2759-
///
2760-
/// This is done by generating a new early-bound lifetime parameter local to the
2761-
/// opaque which is instantiated in the function signature with the late-bound
2762-
/// lifetime.
2763-
///
2764-
/// This mapping associated a captured lifetime (first parameter) with the new
2765-
/// early-bound lifetime that was generated for the opaque.
2766-
pub lifetime_mapping: &'hir [(&'hir Lifetime, LocalDefId)],
27672755
pub span: Span,
27682756
}
27692757

@@ -2871,7 +2859,7 @@ pub enum TyKind<'hir> {
28712859
/// possibly parameters) that are actually bound on the `impl Trait`.
28722860
///
28732861
/// The last parameter specifies whether this opaque appears in a trait definition.
2874-
OpaqueDef(&'hir OpaqueTy<'hir>, &'hir [GenericArg<'hir>]),
2862+
OpaqueDef(&'hir OpaqueTy<'hir>),
28752863
/// A trait object type `Bound1 + Bound2 + Bound3`
28762864
/// where `Bound` is a trait or a lifetime.
28772865
TraitObject(
@@ -3995,7 +3983,6 @@ impl<'hir> Node<'hir> {
39953983
| Node::TraitItem(TraitItem { generics, .. })
39963984
| Node::ImplItem(ImplItem { generics, .. }) => Some(generics),
39973985
Node::Item(item) => item.kind.generics(),
3998-
Node::OpaqueTy(opaque) => Some(opaque.generics),
39993986
_ => None,
40003987
}
40013988
}

compiler/rustc_hir/src/intravisit.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,8 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) -> V::Resul
896896
TyKind::Path(ref qpath) => {
897897
try_visit!(visitor.visit_qpath(qpath, typ.hir_id, typ.span));
898898
}
899-
TyKind::OpaqueDef(opaque, lifetimes) => {
899+
TyKind::OpaqueDef(opaque) => {
900900
try_visit!(visitor.visit_opaque_ty(opaque));
901-
walk_list!(visitor, visit_generic_arg, lifetimes);
902901
}
903902
TyKind::Array(ref ty, ref length) => {
904903
try_visit!(visitor.visit_ty(ty));
@@ -1188,10 +1187,8 @@ pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(
11881187
}
11891188

11901189
pub fn walk_opaque_ty<'v, V: Visitor<'v>>(visitor: &mut V, opaque: &'v OpaqueTy<'v>) -> V::Result {
1191-
let &OpaqueTy { hir_id, def_id: _, generics, bounds, origin: _, lifetime_mapping: _, span: _ } =
1192-
opaque;
1190+
let &OpaqueTy { hir_id, def_id: _, bounds, origin: _, span: _ } = opaque;
11931191
try_visit!(visitor.visit_id(hir_id));
1194-
try_visit!(walk_generics(visitor, generics));
11951192
walk_list!(visitor, visit_param_bound, bounds);
11961193
V::Result::output()
11971194
}

compiler/rustc_hir_analysis/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
13961396
}
13971397
}
13981398

1399-
#[instrument(level = "debug", skip(tcx))]
1399+
#[instrument(level = "debug", skip(tcx), ret)]
14001400
fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFnSig<'_>> {
14011401
use rustc_hir::Node::*;
14021402
use rustc_hir::*;

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+15
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,21 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
449449
});
450450
}
451451

452+
if let Node::OpaqueTy(&hir::OpaqueTy { .. }) = node {
453+
assert!(own_params.is_empty());
454+
455+
let lifetimes = tcx.opaque_captured_lifetimes(def_id);
456+
debug!(?lifetimes);
457+
458+
own_params.extend(lifetimes.iter().map(|&(_, param)| ty::GenericParamDef {
459+
name: tcx.item_name(param.to_def_id()),
460+
index: next_index(),
461+
def_id: param.to_def_id(),
462+
pure_wrt_drop: false,
463+
kind: ty::GenericParamDefKind::Lifetime,
464+
}))
465+
}
466+
452467
let param_def_id_to_index =
453468
own_params.iter().map(|param| (param.def_id, param.index)).collect();
454469

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

-7
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,6 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
331331
// We create bi-directional Outlives predicates between the original
332332
// and the duplicated parameter, to ensure that they do not get out of sync.
333333
if let Node::OpaqueTy(..) = node {
334-
let opaque_ty_node = tcx.parent_hir_node(hir_id);
335-
let Node::Ty(&hir::Ty { kind: TyKind::OpaqueDef(_, lifetimes), .. }) = opaque_ty_node
336-
else {
337-
bug!("unexpected {opaque_ty_node:?}")
338-
};
339-
debug!(?lifetimes);
340-
341334
compute_bidirectional_outlives_predicates(tcx, &generics.own_params, &mut predicates);
342335
debug!(?predicates);
343336
}

0 commit comments

Comments
 (0)