From c2238527e69faa9f922fb5d3fde9494395b299ff Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Mar 2023 18:27:25 -0300 Subject: [PATCH 01/13] Add tcx::lower_impl_trait_in_trait_to_assoc_ty to avoid accessing through sess.opts.unstable_opts --- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 4 ++++ compiler/rustc_ty_utils/src/assoc.rs | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 3ab01f7809b04..238142a484527 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1104,7 +1104,7 @@ fn should_encode_const(def_kind: DefKind) -> bool { // We only encode impl trait in trait when using `lower-impl-trait-in-trait-to-assoc-ty` unstable // option. fn should_encode_fn_impl_trait_in_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool { - if tcx.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty + if tcx.lower_impl_trait_in_trait_to_assoc_ty() && let Some(assoc_item) = tcx.opt_associated_item(def_id) && assoc_item.container == ty::AssocItemContainer::TraitContainer && assoc_item.kind == ty::AssocKind::Fn diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 70091477e39f4..f87bc45e4f281 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2439,6 +2439,10 @@ impl<'tcx> TyCtxt<'tcx> { pub fn trait_solver_next(self) -> bool { self.sess.opts.unstable_opts.trait_solver == rustc_session::config::TraitSolver::Next } + + pub fn lower_impl_trait_in_trait_to_assoc_ty(self) -> bool { + self.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty + } } impl<'tcx> TyCtxtAt<'tcx> { diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 2fc55a2527dd2..050fd5660493b 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -23,7 +23,7 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] { let item = tcx.hir().expect_item(def_id.expect_local()); match item.kind { hir::ItemKind::Trait(.., ref trait_item_refs) => { - if tcx.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty { + if tcx.lower_impl_trait_in_trait_to_assoc_ty() { // We collect RPITITs for each trait method's return type and create a // corresponding associated item using associated_items_for_impl_trait_in_trait // query. @@ -54,7 +54,7 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] { } } hir::ItemKind::Impl(ref impl_) => { - if tcx.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty { + if tcx.lower_impl_trait_in_trait_to_assoc_ty() { // We collect RPITITs for each trait method's return type, on the impl side too and // create a corresponding associated item using // associated_items_for_impl_trait_in_trait query. From 47860ddf589349cd905b2d9101227c2d8d9ca63c Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 6 Mar 2023 11:33:51 -0300 Subject: [PATCH 02/13] Add is_impl_trait_in_trait --- compiler/rustc_middle/src/ty/context.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index f87bc45e4f281..4aa60f5eebfb4 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2443,6 +2443,14 @@ impl<'tcx> TyCtxt<'tcx> { pub fn lower_impl_trait_in_trait_to_assoc_ty(self) -> bool { self.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty } + + pub fn is_impl_trait_in_trait(self, def_id: DefId) -> bool { + if self.lower_impl_trait_in_trait_to_assoc_ty() { + self.def_kind(def_id) == DefKind::AssocTy && self.opt_rpitit_info(def_id).is_some() + } else { + self.def_kind(def_id) == DefKind::ImplTraitPlaceholder + } + } } impl<'tcx> TyCtxtAt<'tcx> { From 97eaa5dbf42dd69e37fe9f29ba428d8344f55ca3 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Mar 2023 12:29:56 -0300 Subject: [PATCH 03/13] Avoid accessing HIR for RPITITs on check_type_bounds --- .../src/check/compare_impl_item.rs | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index 5adc7a8732373..90c5a5d6a2095 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -1995,13 +1995,20 @@ pub(super) fn check_type_bounds<'tcx>( let infcx = tcx.infer_ctxt().build(); let ocx = ObligationCtxt::new(&infcx); - let impl_ty_span = match tcx.hir().get_by_def_id(impl_ty_def_id) { - hir::Node::TraitItem(hir::TraitItem { - kind: hir::TraitItemKind::Type(_, Some(ty)), - .. - }) => ty.span, - hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Type(ty), .. }) => ty.span, - _ => bug!(), + // A synthetic impl Trait for RPITIT desugaring has no HIR, which we currently use to get the + // span for an impl's associated type. Instead, for these, use the def_span for the synthesized + // associated type. + let impl_ty_span = if tcx.opt_rpitit_info(impl_ty.def_id).is_some() { + tcx.def_span(impl_ty_def_id) + } else { + match tcx.hir().get_by_def_id(impl_ty_def_id) { + hir::Node::TraitItem(hir::TraitItem { + kind: hir::TraitItemKind::Type(_, Some(ty)), + .. + }) => ty.span, + hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Type(ty), .. }) => ty.span, + _ => bug!(), + } }; let assumed_wf_types = ocx.assumed_wf_types(param_env, impl_ty_span, impl_ty_def_id); From 3ecb70125e813750dffcec78b4d5075f0d0deb6a Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Mar 2023 12:32:33 -0300 Subject: [PATCH 04/13] Collect newly synthesized RPITITs on ImplTraitInTraitCollector --- compiler/rustc_hir_analysis/src/check/compare_impl_item.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index 90c5a5d6a2095..af54d4eefb343 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -830,7 +830,7 @@ impl<'tcx> TypeFolder> for ImplTraitInTraitCollector<'_, 'tcx> { fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { if let ty::Alias(ty::Projection, proj) = ty.kind() - && self.interner().def_kind(proj.def_id) == DefKind::ImplTraitPlaceholder + && self.interner().is_impl_trait_in_trait(proj.def_id) { if let Some((ty, _)) = self.types.get(&proj.def_id) { return *ty; From 83c0ff8fa5eb1789b9d5caa1d5bb2b344165e897 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Mar 2023 12:34:16 -0300 Subject: [PATCH 05/13] Map to new synthesized assoc ty for RPITITs in astconv --- compiler/rustc_hir_analysis/src/astconv/mod.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 899029d98e0c4..2a64e09b9c605 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -3049,10 +3049,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } &hir::TyKind::OpaqueDef(item_id, lifetimes, in_trait) => { let opaque_ty = tcx.hir().item(item_id); - let def_id = item_id.owner_id.to_def_id(); match opaque_ty.kind { hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => { + let local_def_id = item_id.owner_id.def_id; + // If this is an RPITIT and we are using the new RPITIT lowering scheme, we + // generate the def_id of an associated type for the trait and return as + // type a projection. + let def_id = if in_trait && tcx.lower_impl_trait_in_trait_to_assoc_ty() { + tcx.associated_item_for_impl_trait_in_trait(local_def_id).to_def_id() + } else { + local_def_id.to_def_id() + }; self.impl_trait_ty_to_ty(def_id, lifetimes, origin, in_trait) } ref i => bug!("`impl Trait` pointed to non-opaque type?? {:#?}", i), From e10034c6360916c0fdddf092bb3b7db3c98e436f Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Mar 2023 12:35:53 -0300 Subject: [PATCH 06/13] Properly implement explicit_item_bounds for RPITITs trait assoc ty --- .../src/collect/item_bounds.rs | 34 +++++++++++++------ compiler/rustc_ty_utils/src/assoc.rs | 3 -- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect/item_bounds.rs b/compiler/rustc_hir_analysis/src/collect/item_bounds.rs index 6f6f993f72717..7dce29cc0bbe3 100644 --- a/compiler/rustc_hir_analysis/src/collect/item_bounds.rs +++ b/compiler/rustc_hir_analysis/src/collect/item_bounds.rs @@ -3,7 +3,7 @@ use crate::astconv::AstConv; use rustc_hir as hir; use rustc_infer::traits::util; use rustc_middle::ty::subst::InternalSubsts; -use rustc_middle::ty::{self, TyCtxt}; +use rustc_middle::ty::{self, ImplTraitInTraitData, Ty, TyCtxt}; use rustc_span::def_id::DefId; use rustc_span::Span; @@ -58,17 +58,10 @@ fn opaque_type_bounds<'tcx>( tcx: TyCtxt<'tcx>, opaque_def_id: DefId, ast_bounds: &'tcx [hir::GenericBound<'tcx>], + item_ty: Ty<'tcx>, span: Span, - in_trait: bool, ) -> &'tcx [(ty::Predicate<'tcx>, Span)] { ty::print::with_no_queries!({ - let substs = InternalSubsts::identity_for_item(tcx, opaque_def_id); - let item_ty = if in_trait { - tcx.mk_projection(opaque_def_id, substs) - } else { - tcx.mk_opaque(opaque_def_id, substs) - }; - let icx = ItemCtxt::new(tcx, opaque_def_id); let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds); // Opaque types are implicitly sized unless a `?Sized` bound is found @@ -83,7 +76,18 @@ pub(super) fn explicit_item_bounds( tcx: TyCtxt<'_>, def_id: DefId, ) -> &'_ [(ty::Predicate<'_>, Span)] { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); + // If the def_id is about an RPITIT, delegate explicit_item_bounds to the opaque_def_id that + // generated the synthesized associate type. + let rpitit_info = if let Some(ImplTraitInTraitData::Trait { opaque_def_id, .. }) = + tcx.opt_rpitit_info(def_id) + { + Some(opaque_def_id) + } else { + None + }; + + let bounds_def_id = rpitit_info.unwrap_or(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(bounds_def_id.expect_local()); match tcx.hir().get(hir_id) { hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(bounds, _), @@ -94,7 +98,15 @@ pub(super) fn explicit_item_bounds( kind: hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, in_trait, .. }), span, .. - }) => opaque_type_bounds(tcx, def_id, bounds, *span, *in_trait), + }) => { + let substs = InternalSubsts::identity_for_item(tcx, def_id); + let item_ty = if *in_trait || rpitit_info.is_some() { + tcx.mk_projection(def_id, substs) + } else { + tcx.mk_opaque(def_id, substs) + }; + opaque_type_bounds(tcx, bounds_def_id, bounds, item_ty, *span) + } _ => bug!("item_bounds called on {:?}", def_id), } } diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 050fd5660493b..f67566a7d0e06 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -302,9 +302,6 @@ fn associated_item_for_impl_trait_in_trait( // There are no inferred outlives for the synthesized associated type. trait_assoc_ty.inferred_outlives_of(&[]); - // FIXME implement this. - trait_assoc_ty.explicit_item_bounds(&[]); - local_def_id } From 290c638b5f8f03704744351fa3aa02fb04b00ffc Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Mar 2023 12:36:44 -0300 Subject: [PATCH 07/13] Implement type_of for RPITITs assoc type --- .../rustc_hir_analysis/src/collect/type_of.rs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index acd9f8a5c8eeb..045a8a57186d7 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -9,7 +9,8 @@ use rustc_middle::ty::print::with_forced_trimmed_paths; use rustc_middle::ty::subst::InternalSubsts; use rustc_middle::ty::util::IntTypeExt; use rustc_middle::ty::{ - self, IsSuggestable, Ty, TyCtxt, TypeFolder, TypeSuperFoldable, TypeVisitableExt, + self, ImplTraitInTraitData, IsSuggestable, Ty, TyCtxt, TypeFolder, TypeSuperFoldable, + TypeVisitableExt, }; use rustc_span::symbol::Ident; use rustc_span::{Span, DUMMY_SP}; @@ -244,6 +245,24 @@ fn get_path_containing_arg_in_pat<'hir>( } pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder> { + // If we are computing `type_of` the synthesized associated type for an RPITIT in the impl + // side, use `collect_return_position_impl_trait_in_trait_tys` to infer the value of the + // associated type in the impl. + if let Some(ImplTraitInTraitData::Impl { fn_def_id, .. }) = tcx.opt_rpitit_info(def_id) { + match tcx.collect_return_position_impl_trait_in_trait_tys(fn_def_id) { + Ok(map) => { + let assoc_item = tcx.associated_item(def_id); + return ty::EarlyBinder(map[&assoc_item.trait_item_def_id.unwrap()]); + } + Err(_) => { + return ty::EarlyBinder(tcx.ty_error_with_message( + DUMMY_SP, + "Could not collect return position impl trait in trait tys", + )); + } + } + } + let def_id = def_id.expect_local(); use rustc_hir::*; From bc9ffbeb50afbc9002f98f18ba06cdfcfc32d0a2 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Mar 2023 12:38:36 -0300 Subject: [PATCH 08/13] Implement param_env for RPITITs assoc type --- compiler/rustc_middle/src/query/mod.rs | 1 + compiler/rustc_ty_utils/src/assoc.rs | 4 ++++ compiler/rustc_ty_utils/src/ty.rs | 13 +++++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 5133da3429a9c..70ca1f17d62f0 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1324,6 +1324,7 @@ rustc_queries! { /// might want to use `reveal_all()` method to change modes. query param_env(def_id: DefId) -> ty::ParamEnv<'tcx> { desc { |tcx| "computing normalized predicates of `{}`", tcx.def_path_str(def_id) } + feedable } /// Like `param_env`, but returns the `ParamEnv` in `Reveal::All` mode. diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index f67566a7d0e06..80ed72bfaa1f9 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -342,6 +342,10 @@ fn impl_associated_item_for_impl_trait_in_trait( fn_has_self_parameter: false, }); + // Copy param_env of the containing function. The synthesized associated type doesn't have + // extra predicates to assume. + impl_assoc_ty.param_env(tcx.param_env(impl_fn_def_id)); + // Copy impl_defaultness of the containing function. impl_assoc_ty.impl_defaultness(tcx.impl_defaultness(impl_fn_def_id)); diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index 18159778a8e39..5caad0965bd75 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -3,8 +3,8 @@ use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_index::bit_set::BitSet; use rustc_middle::ty::{ - self, Binder, EarlyBinder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt, - TypeSuperVisitable, TypeVisitable, TypeVisitor, + self, Binder, EarlyBinder, ImplTraitInTraitData, Predicate, PredicateKind, ToPredicate, Ty, + TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor, }; use rustc_session::config::TraitSolver; use rustc_span::def_id::{DefId, CRATE_DEF_ID}; @@ -117,6 +117,15 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] { /// See `ParamEnv` struct definition for details. fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { + // When computing the param_env of an RPITIT, copy param_env of the containing function. The + // synthesized associated type doesn't have extra predicates to assume. + let def_id = + if let Some(ImplTraitInTraitData::Trait { fn_def_id, .. }) = tcx.opt_rpitit_info(def_id) { + fn_def_id + } else { + def_id + }; + // Compute the bounds on Self and the type parameters. let ty::InstantiatedPredicates { mut predicates, .. } = tcx.predicates_of(def_id).instantiate_identity(tcx); From e132d293b26214d7a9c1a86c6384188ae5ab2164 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Mar 2023 12:40:32 -0300 Subject: [PATCH 09/13] Implement generics_of for impl side RPITITs assoc type --- compiler/rustc_ty_utils/src/assoc.rs | 40 +++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 80ed72bfaa1f9..efc353c5c73e1 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -313,11 +313,12 @@ fn impl_associated_item_for_impl_trait_in_trait( trait_assoc_def_id: LocalDefId, impl_fn_def_id: LocalDefId, ) -> LocalDefId { - let impl_def_id = tcx.local_parent(impl_fn_def_id); + let impl_local_def_id = tcx.local_parent(impl_fn_def_id); + let impl_def_id = impl_local_def_id.to_def_id(); // FIXME fix the span, we probably want the def_id of the return type of the function let span = tcx.def_span(impl_fn_def_id); - let impl_assoc_ty = tcx.at(span).create_def(impl_def_id, DefPathData::ImplTraitAssocTy); + let impl_assoc_ty = tcx.at(span).create_def(impl_local_def_id, DefPathData::ImplTraitAssocTy); let local_def_id = impl_assoc_ty.def_id(); let def_id = local_def_id.to_def_id(); @@ -349,10 +350,37 @@ fn impl_associated_item_for_impl_trait_in_trait( // Copy impl_defaultness of the containing function. impl_assoc_ty.impl_defaultness(tcx.impl_defaultness(impl_fn_def_id)); - // Copy generics_of the trait's associated item. - // FIXME: This is not correct, in particular the parent is going to be wrong. So we would need - // to copy from trait_assoc_def_id and adjust things. - impl_assoc_ty.generics_of(tcx.generics_of(trait_assoc_def_id).clone()); + // Copy generics_of the trait's associated item but the impl as the parent. + impl_assoc_ty.generics_of({ + let trait_assoc_generics = tcx.generics_of(trait_assoc_def_id); + let trait_assoc_parent_count = trait_assoc_generics.parent_count; + let mut params = trait_assoc_generics.params.clone(); + + let parent_generics = tcx.generics_of(impl_def_id); + let parent_count = parent_generics.parent_count + parent_generics.params.len(); + + let mut impl_fn_params = tcx.generics_of(impl_fn_def_id).params.clone(); + + for param in &mut params { + param.index = param.index + parent_count as u32 + impl_fn_params.len() as u32 + - trait_assoc_parent_count as u32; + } + + impl_fn_params.extend(params); + params = impl_fn_params; + + let param_def_id_to_index = + params.iter().map(|param| (param.def_id, param.index)).collect(); + + ty::Generics { + parent: Some(impl_def_id), + parent_count, + params, + param_def_id_to_index, + has_self: false, + has_late_bound_regions: trait_assoc_generics.has_late_bound_regions, + } + }); local_def_id } From 31a0d6721b540f5613b1ac1ec2568f245fd00b06 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Mar 2023 12:41:10 -0300 Subject: [PATCH 10/13] Implement explicit_predicates_of for impl side RPITITs assoc type --- compiler/rustc_ty_utils/src/assoc.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index efc353c5c73e1..b08c7c365da27 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -382,5 +382,11 @@ fn impl_associated_item_for_impl_trait_in_trait( } }); + // There are no predicates for the synthesized associated type. + impl_assoc_ty.explicit_predicates_of(ty::GenericPredicates { + parent: Some(impl_def_id), + predicates: &[], + }); + local_def_id } From 34ca2dc0915e90f3ce170b0bb87d36127b127c36 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Mar 2023 12:41:22 -0300 Subject: [PATCH 11/13] Implement inferred_outlives_of for impl side RPITITs assoc type --- compiler/rustc_ty_utils/src/assoc.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index b08c7c365da27..e0f569dc68a7a 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -388,5 +388,8 @@ fn impl_associated_item_for_impl_trait_in_trait( predicates: &[], }); + // There are no inferred outlives for the synthesized associated type. + impl_assoc_ty.inferred_outlives_of(&[]); + local_def_id } From b1efed4b6486c28e661a5020b736b5b3cd671e2b Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Mar 2023 12:41:52 -0300 Subject: [PATCH 12/13] Avoid accessing HIR for RPITITs assoc type on mark_live_symbols --- compiler/rustc_passes/src/dead.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 28e06e5bb3ef2..13332a2b75793 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -243,6 +243,12 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { continue; } + // Avoid accessing the HIR for the synthesized associated type generated for RPITITs. + if self.tcx.opt_rpitit_info(id).is_some() { + self.live_symbols.insert(id); + continue; + } + // in the case of tuple struct constructors we want to check the item, not the generated // tuple struct constructor function let id = self.struct_constructors.get(&id).copied().unwrap_or(id); From 5daa01e4a62e7ba1ff58ed233983dfa3af3e44b5 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Mar 2023 12:42:11 -0300 Subject: [PATCH 13/13] Add simple impl trait test for RPITIT --- .../new-lowering-strategy/simple-impl-trait.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/ui/impl-trait/in-trait/new-lowering-strategy/simple-impl-trait.rs diff --git a/tests/ui/impl-trait/in-trait/new-lowering-strategy/simple-impl-trait.rs b/tests/ui/impl-trait/in-trait/new-lowering-strategy/simple-impl-trait.rs new file mode 100644 index 0000000000000..ae09d20f6f5b7 --- /dev/null +++ b/tests/ui/impl-trait/in-trait/new-lowering-strategy/simple-impl-trait.rs @@ -0,0 +1,17 @@ +// check-pass +// compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty + +#![feature(return_position_impl_trait_in_trait)] +#![allow(incomplete_features)] + +trait Foo { + fn foo() -> impl Sized; +} + +impl Foo for String { + fn foo() -> i32 { + 22 + } +} + +fn main() {}