Skip to content

Commit b6e1214

Browse files
committed
Remap impl-trait lifetimes on HIR instead of AST lowering.
1 parent 298c746 commit b6e1214

File tree

35 files changed

+507
-596
lines changed

35 files changed

+507
-596
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+9-246
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
@@ -2627,7 +2627,6 @@ impl<'hir> Ty<'hir> {
26272627
}
26282628
TyKind::Tup(tys) => tys.iter().any(Self::is_suggestable_infer_ty),
26292629
TyKind::Ptr(mut_ty) | TyKind::Ref(_, mut_ty) => mut_ty.ty.is_suggestable_infer_ty(),
2630-
TyKind::OpaqueDef(_, generic_args) => are_suggestable_generic_args(generic_args),
26312630
TyKind::Path(QPath::TypeRelative(ty, segment)) => {
26322631
ty.is_suggestable_infer_ty() || are_suggestable_generic_args(segment.args().args)
26332632
}
@@ -2746,19 +2745,8 @@ pub struct BareFnTy<'hir> {
27462745
pub struct OpaqueTy<'hir> {
27472746
pub hir_id: HirId,
27482747
pub def_id: LocalDefId,
2749-
pub generics: &'hir Generics<'hir>,
27502748
pub bounds: GenericBounds<'hir>,
27512749
pub origin: OpaqueTyOrigin,
2752-
/// Return-position impl traits (and async futures) must "reify" any late-bound
2753-
/// lifetimes that are captured from the function signature they originate from.
2754-
///
2755-
/// This is done by generating a new early-bound lifetime parameter local to the
2756-
/// opaque which is instantiated in the function signature with the late-bound
2757-
/// lifetime.
2758-
///
2759-
/// This mapping associated a captured lifetime (first parameter) with the new
2760-
/// early-bound lifetime that was generated for the opaque.
2761-
pub lifetime_mapping: &'hir [(&'hir Lifetime, LocalDefId)],
27622750
pub span: Span,
27632751
}
27642752

@@ -2866,7 +2854,7 @@ pub enum TyKind<'hir> {
28662854
/// possibly parameters) that are actually bound on the `impl Trait`.
28672855
///
28682856
/// The last parameter specifies whether this opaque appears in a trait definition.
2869-
OpaqueDef(&'hir OpaqueTy<'hir>, &'hir [GenericArg<'hir>]),
2857+
OpaqueDef(&'hir OpaqueTy<'hir>),
28702858
/// A trait object type `Bound1 + Bound2 + Bound3`
28712859
/// where `Bound` is a trait or a lifetime.
28722860
TraitObject(&'hir [PolyTraitRef<'hir>], &'hir Lifetime, TraitObjectSyntax),
@@ -3991,7 +3979,6 @@ impl<'hir> Node<'hir> {
39913979
| Node::TraitItem(TraitItem { generics, .. })
39923980
| Node::ImplItem(ImplItem { generics, .. }) => Some(generics),
39933981
Node::Item(item) => item.kind.generics(),
3994-
Node::OpaqueTy(opaque) => Some(opaque.generics),
39953982
_ => None,
39963983
}
39973984
}

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
@@ -1302,7 +1302,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
13021302
}
13031303
}
13041304

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

compiler/rustc_hir_analysis/src/collect/generics_of.rs

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

429+
if let Node::OpaqueTy(&hir::OpaqueTy { .. }) = node {
430+
assert!(own_params.is_empty());
431+
432+
let lifetimes = tcx.opaque_captured_lifetimes(def_id);
433+
debug!(?lifetimes);
434+
435+
own_params.extend(lifetimes.iter().map(|&(_, param)| ty::GenericParamDef {
436+
name: tcx.item_name(param.to_def_id()),
437+
index: next_index(),
438+
def_id: param.to_def_id(),
439+
pure_wrt_drop: false,
440+
kind: ty::GenericParamDefKind::Lifetime,
441+
}))
442+
}
443+
429444
let param_def_id_to_index =
430445
own_params.iter().map(|param| (param.def_id, param.index)).collect();
431446

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

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

0 commit comments

Comments
 (0)