Skip to content

Commit 0130c3a

Browse files
committed
Auto merge of #113215 - compiler-errors:rpitit-predicates-tweaks, r=spastorino
Make RPITITs assume/require their parent method's predicates Removes a FIXME from the `param_env` query where we were manually adding the parent function's predicates to the RPITIT's assumptions. r? `@spastorino`
2 parents e728b5b + b0ab37e commit 0130c3a

File tree

8 files changed

+76
-41
lines changed

8 files changed

+76
-41
lines changed

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,26 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
6363
use rustc_hir::*;
6464

6565
match tcx.opt_rpitit_info(def_id.to_def_id()) {
66-
Some(ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
66+
Some(ImplTraitInTraitData::Trait { opaque_def_id, fn_def_id }) => {
6767
let opaque_ty_id = tcx.hir().local_def_id_to_hir_id(opaque_def_id.expect_local());
6868
let opaque_ty_node = tcx.hir().get(opaque_ty_id);
6969
let Node::Item(&Item { kind: ItemKind::OpaqueTy(OpaqueTy { lifetime_mapping, .. }), .. }) = opaque_ty_node else {
7070
bug!("unexpected {opaque_ty_node:?}")
7171
};
7272

7373
let mut predicates = Vec::new();
74+
75+
// RPITITs should inherit the predicates of their parent. This is
76+
// both to ensure that the RPITITs are only instantiated when the
77+
// parent predicates would hold, and also so that the param-env
78+
// inherits these predicates as assumptions.
79+
let identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
80+
predicates.extend(
81+
tcx.explicit_predicates_of(fn_def_id).instantiate_own(tcx, identity_substs),
82+
);
83+
84+
// We also install bidirectional outlives predicates for the RPITIT
85+
// to keep the duplicates lifetimes from opaque lowering in sync.
7486
compute_bidirectional_outlives_predicates(
7587
tcx,
7688
def_id,
@@ -89,12 +101,13 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
89101

90102
Some(ImplTraitInTraitData::Impl { fn_def_id }) => {
91103
let assoc_item = tcx.associated_item(def_id);
92-
let trait_assoc_predicates = tcx.predicates_of(assoc_item.trait_item_def_id.unwrap());
104+
let trait_assoc_predicates =
105+
tcx.explicit_predicates_of(assoc_item.trait_item_def_id.unwrap());
93106

94107
let impl_assoc_identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
95108
let impl_def_id = tcx.parent(fn_def_id);
96109
let impl_trait_ref_substs =
97-
tcx.impl_trait_ref(impl_def_id).unwrap().skip_binder().substs;
110+
tcx.impl_trait_ref(impl_def_id).unwrap().subst_identity().substs;
98111

99112
let impl_assoc_substs =
100113
impl_assoc_identity_substs.rebase_onto(tcx, impl_def_id, impl_trait_ref_substs);

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,9 @@ fn foo(&self) -> Self::T { String::new() }
574574
let Some(hir_id) = body_owner_def_id.as_local() else {
575575
return false;
576576
};
577-
let hir_id = tcx.hir().local_def_id_to_hir_id(hir_id);
577+
let Some(hir_id) = tcx.opt_local_def_id_to_hir_id(hir_id) else {
578+
return false;
579+
};
578580
// When `body_owner` is an `impl` or `trait` item, look in its associated types for
579581
// `expected` and point at it.
580582
let parent_id = tcx.hir().get_parent_item(hir_id);

compiler/rustc_metadata/src/rmeta/encoder.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1151,10 +1151,10 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
11511151
let assoc_item = tcx.associated_item(def_id);
11521152
match assoc_item.container {
11531153
ty::AssocItemContainer::ImplContainer => true,
1154-
// FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty) always encode RPITITs,
1155-
// since we need to be able to "project" from an RPITIT associated item
1156-
// to an opaque when installing the default projection predicates in
1157-
// default trait methods with RPITITs.
1154+
// Always encode RPITITs, since we need to be able to project
1155+
// from an RPITIT associated item to an opaque when installing
1156+
// the default projection predicates in default trait methods
1157+
// with RPITITs.
11581158
ty::AssocItemContainer::TraitContainer => {
11591159
assoc_item.defaultness(tcx).has_value() || assoc_item.opt_rpitit_info.is_some()
11601160
}

compiler/rustc_ty_utils/src/ty.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use rustc_hir::def::DefKind;
44
use rustc_index::bit_set::BitSet;
55
use rustc_middle::query::Providers;
66
use rustc_middle::ty::{
7-
self, EarlyBinder, ImplTraitInTraitData, ToPredicate, Ty, TyCtxt, TypeSuperVisitable,
8-
TypeVisitable, TypeVisitor,
7+
self, EarlyBinder, ToPredicate, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor,
98
};
109
use rustc_session::config::TraitSolver;
1110
use rustc_span::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
@@ -120,22 +119,6 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
120119
let ty::InstantiatedPredicates { mut predicates, .. } =
121120
tcx.predicates_of(def_id).instantiate_identity(tcx);
122121

123-
// When computing the param_env of an RPITIT, use predicates of the containing function,
124-
// *except* for the additional assumption that the RPITIT normalizes to the trait method's
125-
// default opaque type. This is needed to properly check the item bounds of the assoc
126-
// type hold (`check_type_bounds`), since that method already installs a similar projection
127-
// bound, so they will conflict.
128-
// FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty): I don't like this, we should
129-
// at least be making sure that the generics in RPITITs and their parent fn don't
130-
// get out of alignment, or else we do actually need to substitute these predicates.
131-
if let Some(ImplTraitInTraitData::Trait { fn_def_id, .. })
132-
| Some(ImplTraitInTraitData::Impl { fn_def_id, .. }) = tcx.opt_rpitit_info(def_id)
133-
{
134-
// FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty): Should not need to add the predicates
135-
// from the parent fn to our assumptions
136-
predicates.extend(tcx.predicates_of(fn_def_id).instantiate_identity(tcx).predicates);
137-
}
138-
139122
// Finally, we have to normalize the bounds in the environment, in
140123
// case they contain any associated type projections. This process
141124
// can yield errors if the put in illegal associated types, like

tests/ui/impl-trait/in-trait/issue-102140.next.stderr

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ LL | MyTrait::foo(&self)
66
| |
77
| required by a bound introduced by this call
88
|
9-
help: consider removing the leading `&`-reference
10-
|
11-
LL - MyTrait::foo(&self)
12-
LL + MyTrait::foo(self)
13-
|
9+
= help: the trait `MyTrait` is implemented for `Outer`
1410

1511
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
1612
--> $DIR/issue-102140.rs:26:9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
2-
--> $DIR/wf-bounds.rs:13:22
2+
--> $DIR/wf-bounds.rs:17:22
33
|
44
LL | fn nya() -> impl Wf<Vec<[u8]>>;
55
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
@@ -9,14 +9,14 @@ note: required by a bound in `Vec`
99
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
1010

1111
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
12-
--> $DIR/wf-bounds.rs:16:23
12+
--> $DIR/wf-bounds.rs:20:23
1313
|
1414
LL | fn nya2() -> impl Wf<[u8]>;
1515
| ^^^^^^^^ doesn't have a size known at compile-time
1616
|
1717
= help: the trait `Sized` is not implemented for `[u8]`
1818
note: required by a bound in `Wf`
19-
--> $DIR/wf-bounds.rs:8:10
19+
--> $DIR/wf-bounds.rs:10:10
2020
|
2121
LL | trait Wf<T> {
2222
| ^ required by this bound in `Wf`
@@ -26,7 +26,7 @@ LL | trait Wf<T: ?Sized> {
2626
| ++++++++
2727

2828
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
29-
--> $DIR/wf-bounds.rs:19:44
29+
--> $DIR/wf-bounds.rs:23:44
3030
|
3131
LL | fn nya3() -> impl Wf<(), Output = impl Wf<Vec<[u8]>>>;
3232
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
@@ -35,6 +35,23 @@ LL | fn nya3() -> impl Wf<(), Output = impl Wf<Vec<[u8]>>>;
3535
note: required by a bound in `Vec`
3636
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
3737

38-
error: aborting due to 3 previous errors
38+
error[E0277]: `T` doesn't implement `std::fmt::Display`
39+
--> $DIR/wf-bounds.rs:26:26
40+
|
41+
LL | fn nya4<T>() -> impl Wf<NeedsDisplay<T>>;
42+
| ^^^^^^^^^^^^^^^^^^^ `T` cannot be formatted with the default formatter
43+
|
44+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
45+
note: required by a bound in `NeedsDisplay`
46+
--> $DIR/wf-bounds.rs:14:24
47+
|
48+
LL | struct NeedsDisplay<T: Display>(T);
49+
| ^^^^^^^ required by this bound in `NeedsDisplay`
50+
help: consider restricting type parameter `T`
51+
|
52+
LL | fn nya4<T: std::fmt::Display>() -> impl Wf<NeedsDisplay<T>>;
53+
| +++++++++++++++++++
54+
55+
error: aborting due to 4 previous errors
3956

4057
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
2-
--> $DIR/wf-bounds.rs:13:22
2+
--> $DIR/wf-bounds.rs:17:22
33
|
44
LL | fn nya() -> impl Wf<Vec<[u8]>>;
55
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
@@ -9,14 +9,14 @@ note: required by a bound in `Vec`
99
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
1010

1111
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
12-
--> $DIR/wf-bounds.rs:16:23
12+
--> $DIR/wf-bounds.rs:20:23
1313
|
1414
LL | fn nya2() -> impl Wf<[u8]>;
1515
| ^^^^^^^^ doesn't have a size known at compile-time
1616
|
1717
= help: the trait `Sized` is not implemented for `[u8]`
1818
note: required by a bound in `Wf`
19-
--> $DIR/wf-bounds.rs:8:10
19+
--> $DIR/wf-bounds.rs:10:10
2020
|
2121
LL | trait Wf<T> {
2222
| ^ required by this bound in `Wf`
@@ -26,7 +26,7 @@ LL | trait Wf<T: ?Sized> {
2626
| ++++++++
2727

2828
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
29-
--> $DIR/wf-bounds.rs:19:44
29+
--> $DIR/wf-bounds.rs:23:44
3030
|
3131
LL | fn nya3() -> impl Wf<(), Output = impl Wf<Vec<[u8]>>>;
3232
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
@@ -35,6 +35,23 @@ LL | fn nya3() -> impl Wf<(), Output = impl Wf<Vec<[u8]>>>;
3535
note: required by a bound in `Vec`
3636
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
3737

38-
error: aborting due to 3 previous errors
38+
error[E0277]: `T` doesn't implement `std::fmt::Display`
39+
--> $DIR/wf-bounds.rs:26:26
40+
|
41+
LL | fn nya4<T>() -> impl Wf<NeedsDisplay<T>>;
42+
| ^^^^^^^^^^^^^^^^^^^ `T` cannot be formatted with the default formatter
43+
|
44+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
45+
note: required by a bound in `NeedsDisplay`
46+
--> $DIR/wf-bounds.rs:14:24
47+
|
48+
LL | struct NeedsDisplay<T: Display>(T);
49+
| ^^^^^^^ required by this bound in `NeedsDisplay`
50+
help: consider restricting type parameter `T`
51+
|
52+
LL | fn nya4<T: std::fmt::Display>() -> impl Wf<NeedsDisplay<T>>;
53+
| +++++++++++++++++++
54+
55+
error: aborting due to 4 previous errors
3956

4057
For more information about this error, try `rustc --explain E0277`.

tests/ui/impl-trait/in-trait/wf-bounds.rs

+7
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
#![feature(return_position_impl_trait_in_trait)]
66
#![allow(incomplete_features)]
77

8+
use std::fmt::Display;
9+
810
trait Wf<T> {
911
type Output;
1012
}
1113

14+
struct NeedsDisplay<T: Display>(T);
15+
1216
trait Uwu {
1317
fn nya() -> impl Wf<Vec<[u8]>>;
1418
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
@@ -18,6 +22,9 @@ trait Uwu {
1822

1923
fn nya3() -> impl Wf<(), Output = impl Wf<Vec<[u8]>>>;
2024
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
25+
26+
fn nya4<T>() -> impl Wf<NeedsDisplay<T>>;
27+
//~^ ERROR `T` doesn't implement `std::fmt::Display`
2128
}
2229

2330
fn main() {}

0 commit comments

Comments
 (0)