Skip to content

Commit 29ed096

Browse files
authored
Unrolled build for rust-lang#129383
Rollup merge of rust-lang#129383 - cjgillot:opaque-noremap, r=compiler-errors,petrochenkov Remap impl-trait lifetimes on HIR instead of AST lowering Current AST->HIR lowering goes out of its way to remap lifetimes for opaque types. This is complicated and leaks into upstream and downstream code. This PR stops trying to be clever during lowering, and prefers to do this remapping during the HIR->ty lowering. The remapping computation easily fits into the bound var resolution code. Its result can be used in by `generics_of` and `hir_ty_lowering::new_opaque` to add the proper parameters and arguments. See an example on the doc for query `opaque_captured_lifetimes`. Based on rust-lang#129244 Fixes rust-lang#125249 Fixes rust-lang#126850 cc `@compiler-errors` `@spastorino` r? `@petrochenkov`
2 parents 759e07f + 2d74d8f commit 29ed096

File tree

38 files changed

+512
-746
lines changed

38 files changed

+512
-746
lines changed

compiler/rustc_ast_lowering/src/lib.rs

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

compiler/rustc_ast_lowering/src/lifetime_collector.rs

-151
This file was deleted.

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-19
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

@@ -2861,12 +2849,7 @@ pub enum TyKind<'hir> {
28612849
/// Type parameters may be stored in each `PathSegment`.
28622850
Path(QPath<'hir>),
28632851
/// An opaque type definition itself. This is only used for `impl Trait`.
2864-
///
2865-
/// The generic argument list contains the lifetimes (and in the future
2866-
/// possibly parameters) that are actually bound on the `impl Trait`.
2867-
///
2868-
/// The last parameter specifies whether this opaque appears in a trait definition.
2869-
OpaqueDef(&'hir OpaqueTy<'hir>, &'hir [GenericArg<'hir>]),
2852+
OpaqueDef(&'hir OpaqueTy<'hir>),
28702853
/// A trait object type `Bound1 + Bound2 + Bound3`
28712854
/// where `Bound` is a trait or a lifetime.
28722855
TraitObject(&'hir [PolyTraitRef<'hir>], &'hir Lifetime, TraitObjectSyntax),
@@ -3991,7 +3974,6 @@ impl<'hir> Node<'hir> {
39913974
| Node::TraitItem(TraitItem { generics, .. })
39923975
| Node::ImplItem(ImplItem { generics, .. }) => Some(generics),
39933976
Node::Item(item) => item.kind.generics(),
3994-
Node::OpaqueTy(opaque) => Some(opaque.generics),
39953977
_ => None,
39963978
}
39973979
}

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)