Skip to content

Commit 9455a55

Browse files
committed
Auto merge of #108700 - spastorino:new-rpitit-impl-side-2, r=compiler-errors
Make RPITITs simple cases work when using lower_impl_trait_in_trait_to_assoc_ty r? `@compiler-errors` It's probably best reviewed commit by commit.
2 parents 542ed2b + 5daa01e commit 9455a55

File tree

11 files changed

+164
-35
lines changed

11 files changed

+164
-35
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -3048,10 +3048,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
30483048
}
30493049
&hir::TyKind::OpaqueDef(item_id, lifetimes, in_trait) => {
30503050
let opaque_ty = tcx.hir().item(item_id);
3051-
let def_id = item_id.owner_id.to_def_id();
30523051

30533052
match opaque_ty.kind {
30543053
hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => {
3054+
let local_def_id = item_id.owner_id.def_id;
3055+
// If this is an RPITIT and we are using the new RPITIT lowering scheme, we
3056+
// generate the def_id of an associated type for the trait and return as
3057+
// type a projection.
3058+
let def_id = if in_trait && tcx.lower_impl_trait_in_trait_to_assoc_ty() {
3059+
tcx.associated_item_for_impl_trait_in_trait(local_def_id).to_def_id()
3060+
} else {
3061+
local_def_id.to_def_id()
3062+
};
30553063
self.impl_trait_ty_to_ty(def_id, lifetimes, origin, in_trait)
30563064
}
30573065
ref i => bug!("`impl Trait` pointed to non-opaque type?? {:#?}", i),

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ImplTraitInTraitCollector<'_, 'tcx> {
830830

831831
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
832832
if let ty::Alias(ty::Projection, proj) = ty.kind()
833-
&& self.interner().def_kind(proj.def_id) == DefKind::ImplTraitPlaceholder
833+
&& self.interner().is_impl_trait_in_trait(proj.def_id)
834834
{
835835
if let Some((ty, _)) = self.types.get(&proj.def_id) {
836836
return *ty;
@@ -1995,13 +1995,20 @@ pub(super) fn check_type_bounds<'tcx>(
19951995
let infcx = tcx.infer_ctxt().build();
19961996
let ocx = ObligationCtxt::new(&infcx);
19971997

1998-
let impl_ty_span = match tcx.hir().get_by_def_id(impl_ty_def_id) {
1999-
hir::Node::TraitItem(hir::TraitItem {
2000-
kind: hir::TraitItemKind::Type(_, Some(ty)),
2001-
..
2002-
}) => ty.span,
2003-
hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Type(ty), .. }) => ty.span,
2004-
_ => bug!(),
1998+
// A synthetic impl Trait for RPITIT desugaring has no HIR, which we currently use to get the
1999+
// span for an impl's associated type. Instead, for these, use the def_span for the synthesized
2000+
// associated type.
2001+
let impl_ty_span = if tcx.opt_rpitit_info(impl_ty.def_id).is_some() {
2002+
tcx.def_span(impl_ty_def_id)
2003+
} else {
2004+
match tcx.hir().get_by_def_id(impl_ty_def_id) {
2005+
hir::Node::TraitItem(hir::TraitItem {
2006+
kind: hir::TraitItemKind::Type(_, Some(ty)),
2007+
..
2008+
}) => ty.span,
2009+
hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Type(ty), .. }) => ty.span,
2010+
_ => bug!(),
2011+
}
20052012
};
20062013
let assumed_wf_types = ocx.assumed_wf_types(param_env, impl_ty_span, impl_ty_def_id);
20072014

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::astconv::AstConv;
33
use rustc_hir as hir;
44
use rustc_infer::traits::util;
55
use rustc_middle::ty::subst::InternalSubsts;
6-
use rustc_middle::ty::{self, TyCtxt};
6+
use rustc_middle::ty::{self, ImplTraitInTraitData, Ty, TyCtxt};
77
use rustc_span::def_id::DefId;
88
use rustc_span::Span;
99

@@ -58,17 +58,10 @@ fn opaque_type_bounds<'tcx>(
5858
tcx: TyCtxt<'tcx>,
5959
opaque_def_id: DefId,
6060
ast_bounds: &'tcx [hir::GenericBound<'tcx>],
61+
item_ty: Ty<'tcx>,
6162
span: Span,
62-
in_trait: bool,
6363
) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
6464
ty::print::with_no_queries!({
65-
let substs = InternalSubsts::identity_for_item(tcx, opaque_def_id);
66-
let item_ty = if in_trait {
67-
tcx.mk_projection(opaque_def_id, substs)
68-
} else {
69-
tcx.mk_opaque(opaque_def_id, substs)
70-
};
71-
7265
let icx = ItemCtxt::new(tcx, opaque_def_id);
7366
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds);
7467
// Opaque types are implicitly sized unless a `?Sized` bound is found
@@ -83,7 +76,18 @@ pub(super) fn explicit_item_bounds(
8376
tcx: TyCtxt<'_>,
8477
def_id: DefId,
8578
) -> &'_ [(ty::Predicate<'_>, Span)] {
86-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
79+
// If the def_id is about an RPITIT, delegate explicit_item_bounds to the opaque_def_id that
80+
// generated the synthesized associate type.
81+
let rpitit_info = if let Some(ImplTraitInTraitData::Trait { opaque_def_id, .. }) =
82+
tcx.opt_rpitit_info(def_id)
83+
{
84+
Some(opaque_def_id)
85+
} else {
86+
None
87+
};
88+
89+
let bounds_def_id = rpitit_info.unwrap_or(def_id);
90+
let hir_id = tcx.hir().local_def_id_to_hir_id(bounds_def_id.expect_local());
8791
match tcx.hir().get(hir_id) {
8892
hir::Node::TraitItem(hir::TraitItem {
8993
kind: hir::TraitItemKind::Type(bounds, _),
@@ -94,7 +98,15 @@ pub(super) fn explicit_item_bounds(
9498
kind: hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, in_trait, .. }),
9599
span,
96100
..
97-
}) => opaque_type_bounds(tcx, def_id, bounds, *span, *in_trait),
101+
}) => {
102+
let substs = InternalSubsts::identity_for_item(tcx, def_id);
103+
let item_ty = if *in_trait || rpitit_info.is_some() {
104+
tcx.mk_projection(def_id, substs)
105+
} else {
106+
tcx.mk_opaque(def_id, substs)
107+
};
108+
opaque_type_bounds(tcx, bounds_def_id, bounds, item_ty, *span)
109+
}
98110
_ => bug!("item_bounds called on {:?}", def_id),
99111
}
100112
}

compiler/rustc_hir_analysis/src/collect/type_of.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use rustc_middle::ty::print::with_forced_trimmed_paths;
99
use rustc_middle::ty::subst::InternalSubsts;
1010
use rustc_middle::ty::util::IntTypeExt;
1111
use rustc_middle::ty::{
12-
self, IsSuggestable, Ty, TyCtxt, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
12+
self, ImplTraitInTraitData, IsSuggestable, Ty, TyCtxt, TypeFolder, TypeSuperFoldable,
13+
TypeVisitableExt,
1314
};
1415
use rustc_span::symbol::Ident;
1516
use rustc_span::{Span, DUMMY_SP};
@@ -244,6 +245,24 @@ fn get_path_containing_arg_in_pat<'hir>(
244245
}
245246

246247
pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Ty<'_>> {
248+
// If we are computing `type_of` the synthesized associated type for an RPITIT in the impl
249+
// side, use `collect_return_position_impl_trait_in_trait_tys` to infer the value of the
250+
// associated type in the impl.
251+
if let Some(ImplTraitInTraitData::Impl { fn_def_id, .. }) = tcx.opt_rpitit_info(def_id) {
252+
match tcx.collect_return_position_impl_trait_in_trait_tys(fn_def_id) {
253+
Ok(map) => {
254+
let assoc_item = tcx.associated_item(def_id);
255+
return ty::EarlyBinder(map[&assoc_item.trait_item_def_id.unwrap()]);
256+
}
257+
Err(_) => {
258+
return ty::EarlyBinder(tcx.ty_error_with_message(
259+
DUMMY_SP,
260+
"Could not collect return position impl trait in trait tys",
261+
));
262+
}
263+
}
264+
}
265+
247266
let def_id = def_id.expect_local();
248267
use rustc_hir::*;
249268

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ fn should_encode_const(def_kind: DefKind) -> bool {
11041104
// We only encode impl trait in trait when using `lower-impl-trait-in-trait-to-assoc-ty` unstable
11051105
// option.
11061106
fn should_encode_fn_impl_trait_in_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
1107-
if tcx.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty
1107+
if tcx.lower_impl_trait_in_trait_to_assoc_ty()
11081108
&& let Some(assoc_item) = tcx.opt_associated_item(def_id)
11091109
&& assoc_item.container == ty::AssocItemContainer::TraitContainer
11101110
&& assoc_item.kind == ty::AssocKind::Fn

compiler/rustc_middle/src/query/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,7 @@ rustc_queries! {
13331333
/// might want to use `reveal_all()` method to change modes.
13341334
query param_env(def_id: DefId) -> ty::ParamEnv<'tcx> {
13351335
desc { |tcx| "computing normalized predicates of `{}`", tcx.def_path_str(def_id) }
1336+
feedable
13361337
}
13371338

13381339
/// Like `param_env`, but returns the `ParamEnv` in `Reveal::All` mode.

compiler/rustc_middle/src/ty/context.rs

+12
Original file line numberDiff line numberDiff line change
@@ -2445,6 +2445,18 @@ impl<'tcx> TyCtxt<'tcx> {
24452445
pub fn trait_solver_next(self) -> bool {
24462446
self.sess.opts.unstable_opts.trait_solver == rustc_session::config::TraitSolver::Next
24472447
}
2448+
2449+
pub fn lower_impl_trait_in_trait_to_assoc_ty(self) -> bool {
2450+
self.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty
2451+
}
2452+
2453+
pub fn is_impl_trait_in_trait(self, def_id: DefId) -> bool {
2454+
if self.lower_impl_trait_in_trait_to_assoc_ty() {
2455+
self.def_kind(def_id) == DefKind::AssocTy && self.opt_rpitit_info(def_id).is_some()
2456+
} else {
2457+
self.def_kind(def_id) == DefKind::ImplTraitPlaceholder
2458+
}
2459+
}
24482460
}
24492461

24502462
impl<'tcx> TyCtxtAt<'tcx> {

compiler/rustc_passes/src/dead.rs

+6
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
243243
continue;
244244
}
245245

246+
// Avoid accessing the HIR for the synthesized associated type generated for RPITITs.
247+
if self.tcx.opt_rpitit_info(id).is_some() {
248+
self.live_symbols.insert(id);
249+
continue;
250+
}
251+
246252
// in the case of tuple struct constructors we want to check the item, not the generated
247253
// tuple struct constructor function
248254
let id = self.struct_constructors.get(&id).copied().unwrap_or(id);

compiler/rustc_ty_utils/src/assoc.rs

+49-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
2222
let item = tcx.hir().expect_item(def_id.expect_local());
2323
match item.kind {
2424
hir::ItemKind::Trait(.., ref trait_item_refs) => {
25-
if tcx.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty {
25+
if tcx.lower_impl_trait_in_trait_to_assoc_ty() {
2626
// We collect RPITITs for each trait method's return type and create a
2727
// corresponding associated item using associated_items_for_impl_trait_in_trait
2828
// query.
@@ -53,7 +53,7 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
5353
}
5454
}
5555
hir::ItemKind::Impl(ref impl_) => {
56-
if tcx.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty {
56+
if tcx.lower_impl_trait_in_trait_to_assoc_ty() {
5757
// We collect RPITITs for each trait method's return type, on the impl side too and
5858
// create a corresponding associated item using
5959
// associated_items_for_impl_trait_in_trait query.
@@ -301,9 +301,6 @@ fn associated_item_for_impl_trait_in_trait(
301301
// There are no inferred outlives for the synthesized associated type.
302302
trait_assoc_ty.inferred_outlives_of(&[]);
303303

304-
// FIXME implement this.
305-
trait_assoc_ty.explicit_item_bounds(&[]);
306-
307304
local_def_id
308305
}
309306

@@ -315,11 +312,12 @@ fn impl_associated_item_for_impl_trait_in_trait(
315312
trait_assoc_def_id: LocalDefId,
316313
impl_fn_def_id: LocalDefId,
317314
) -> LocalDefId {
318-
let impl_def_id = tcx.local_parent(impl_fn_def_id);
315+
let impl_local_def_id = tcx.local_parent(impl_fn_def_id);
316+
let impl_def_id = impl_local_def_id.to_def_id();
319317

320318
// FIXME fix the span, we probably want the def_id of the return type of the function
321319
let span = tcx.def_span(impl_fn_def_id);
322-
let impl_assoc_ty = tcx.at(span).create_def(impl_def_id, DefPathData::ImplTraitAssocTy);
320+
let impl_assoc_ty = tcx.at(span).create_def(impl_local_def_id, DefPathData::ImplTraitAssocTy);
323321

324322
let local_def_id = impl_assoc_ty.def_id();
325323
let def_id = local_def_id.to_def_id();
@@ -344,13 +342,53 @@ fn impl_associated_item_for_impl_trait_in_trait(
344342
fn_has_self_parameter: false,
345343
});
346344

345+
// Copy param_env of the containing function. The synthesized associated type doesn't have
346+
// extra predicates to assume.
347+
impl_assoc_ty.param_env(tcx.param_env(impl_fn_def_id));
348+
347349
// Copy impl_defaultness of the containing function.
348350
impl_assoc_ty.impl_defaultness(tcx.impl_defaultness(impl_fn_def_id));
349351

350-
// Copy generics_of the trait's associated item.
351-
// FIXME: This is not correct, in particular the parent is going to be wrong. So we would need
352-
// to copy from trait_assoc_def_id and adjust things.
353-
impl_assoc_ty.generics_of(tcx.generics_of(trait_assoc_def_id).clone());
352+
// Copy generics_of the trait's associated item but the impl as the parent.
353+
impl_assoc_ty.generics_of({
354+
let trait_assoc_generics = tcx.generics_of(trait_assoc_def_id);
355+
let trait_assoc_parent_count = trait_assoc_generics.parent_count;
356+
let mut params = trait_assoc_generics.params.clone();
357+
358+
let parent_generics = tcx.generics_of(impl_def_id);
359+
let parent_count = parent_generics.parent_count + parent_generics.params.len();
360+
361+
let mut impl_fn_params = tcx.generics_of(impl_fn_def_id).params.clone();
362+
363+
for param in &mut params {
364+
param.index = param.index + parent_count as u32 + impl_fn_params.len() as u32
365+
- trait_assoc_parent_count as u32;
366+
}
367+
368+
impl_fn_params.extend(params);
369+
params = impl_fn_params;
370+
371+
let param_def_id_to_index =
372+
params.iter().map(|param| (param.def_id, param.index)).collect();
373+
374+
ty::Generics {
375+
parent: Some(impl_def_id),
376+
parent_count,
377+
params,
378+
param_def_id_to_index,
379+
has_self: false,
380+
has_late_bound_regions: trait_assoc_generics.has_late_bound_regions,
381+
}
382+
});
383+
384+
// There are no predicates for the synthesized associated type.
385+
impl_assoc_ty.explicit_predicates_of(ty::GenericPredicates {
386+
parent: Some(impl_def_id),
387+
predicates: &[],
388+
});
389+
390+
// There are no inferred outlives for the synthesized associated type.
391+
impl_assoc_ty.inferred_outlives_of(&[]);
354392

355393
local_def_id
356394
}

compiler/rustc_ty_utils/src/ty.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use rustc_hir as hir;
33
use rustc_hir::def::DefKind;
44
use rustc_index::bit_set::BitSet;
55
use rustc_middle::ty::{
6-
self, Binder, EarlyBinder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt,
7-
TypeSuperVisitable, TypeVisitable, TypeVisitor,
6+
self, Binder, EarlyBinder, ImplTraitInTraitData, Predicate, PredicateKind, ToPredicate, Ty,
7+
TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor,
88
};
99
use rustc_session::config::TraitSolver;
1010
use rustc_span::def_id::{DefId, CRATE_DEF_ID};
@@ -117,6 +117,15 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] {
117117

118118
/// See `ParamEnv` struct definition for details.
119119
fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
120+
// When computing the param_env of an RPITIT, copy param_env of the containing function. The
121+
// synthesized associated type doesn't have extra predicates to assume.
122+
let def_id =
123+
if let Some(ImplTraitInTraitData::Trait { fn_def_id, .. }) = tcx.opt_rpitit_info(def_id) {
124+
fn_def_id
125+
} else {
126+
def_id
127+
};
128+
120129
// Compute the bounds on Self and the type parameters.
121130
let ty::InstantiatedPredicates { mut predicates, .. } =
122131
tcx.predicates_of(def_id).instantiate_identity(tcx);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// check-pass
2+
// compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
3+
4+
#![feature(return_position_impl_trait_in_trait)]
5+
#![allow(incomplete_features)]
6+
7+
trait Foo {
8+
fn foo() -> impl Sized;
9+
}
10+
11+
impl Foo for String {
12+
fn foo() -> i32 {
13+
22
14+
}
15+
}
16+
17+
fn main() {}

0 commit comments

Comments
 (0)