Skip to content

Commit ec4833c

Browse files
Rollup merge of #164624 - dianne:fix-rustaceansareawesome-take-2-complete, r=oli-obk
make `RustaceansAreAwesome` satisfy projection clauses Followup to #163958 that restores the functionality it removed it removed from #163200 and clarifies/fixes some of their comments. The following variant of #161621 is now accepted again: ```rust trait HasAssoc { type Assoc; } trait Parent<T> {} // We need to assume `<(RustaceansAreAwesome,) as HasAssoc>::Assoc == str` // in order to prove `<(RustaceansAreAwesome,) as HasAssoc>::Assoc: ToOwned` trait NeedProjectionNotInExpansion where // The projection clause `<(Self,) as HasAssoc>::Assoc == str` isn't written // as part of a supertrait bound, so the expansion of `RustaceansAreAwesome: // NeedProjectionNotInExpansion` done by param-env normalization won't // include a version of it for `RustaceansAreAwesome`. (Self,): HasAssoc<Assoc = str>, Self: Parent<<<(Self,) as HasAssoc>::Assoc as ToOwned>::Owned>, { fn f(&self); } ``` It's unfortunate how tightly coupled this all is to particulars of param-env normalization. I'd love to remove these hacks eventually.
2 parents b12a9ee + e712498 commit ec4833c

6 files changed

Lines changed: 173 additions & 49 deletions

‎compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -749,19 +749,61 @@ fn receiver_is_dispatchable<'tcx>(
749749
let trait_predicate = ty::TraitRef::new_from_args(tcx, trait_def_id, args);
750750
clauses.push(trait_predicate.upcast(tcx));
751751

752-
// U satisfies Trait's trait bounds. When normalizing `U: Trait<Arg1, ..., ArgN>`, aliases
753-
// in the arguments of supertrait bounds can generate trait obligations which may only be
754-
// be provable using where-clauses from Trait (#161621).
752+
// U satisfies Trait's trait bounds. Supertrait bounds are already included by the expansion
753+
// of `U: Trait<...>` done by param-env normalization. However, normalizing aliases in the
754+
// arguments of those supertrait bounds can generate trait obligations which may only be
755+
// provable using other where-clauses from Trait (#161621).
755756
clauses.extend(
756757
tcx.clauses_of(trait_def_id)
757758
.instantiate(tcx, args)
758759
.clauses
759760
.into_iter()
760761
.map(Unnormalized::skip_norm_wip)
762+
// For simplicity, add all trait clauses, though strictly this should only need to
763+
// add non-supertrait clauses involving Self.
761764
.filter(|clause| matches!(clause.kind().skip_binder(), ty::ClauseKind::Trait(_))),
762765
);
763-
// FIXME(@dianne): we need projection clauses too in some form, but adding them all
764-
// unmodified can introduce ambiguity (#163550)
766+
767+
// When Self appears in the arguments of a projection clause, we also need a corresponding
768+
// projection for U. We don't want U in the targets of projections that doesn't also have
769+
// U in the alias, since that can lead to ambiguity (#163550). For example, if we have
770+
// `(): HasAssoc<Assoc = Self>` in Trait's clauses, `<() as HasAssoc>::Assoc` normalizes to
771+
// Self during dispatchability checking, since we assume `method`'s clauses. This should be
772+
// fine: if `dyn Trait<...>: Trait<...>` isn't satisfied, you can't call methods on such a
773+
// trait object anyway.
774+
//
775+
// HACK: This relies on param-env normalization jank. We don't normalize the left-hand side
776+
// of bounds like `<Self as HasAssoc1>::Assoc: HasAssoc2<Assoc = Self>` in the param-env we
777+
// use to normalize our param-env. If it's someday possible for `<Self as HasAssoc1>::Assoc`
778+
// to normalize there, we won't know whether Self is in the arguments of
779+
// `<<Self as HasAssoc1>::Assoc as HasAssoc2>::Assoc == Self` or not. In that case, this may
780+
// need first to normalize Trait's projection clauses or do additional hacks to avoid adding
781+
// ambiguity.
782+
clauses.extend(
783+
tcx.clauses_of(trait_def_id)
784+
.instantiate_identity(tcx)
785+
.clauses
786+
.into_iter()
787+
.map(Unnormalized::skip_norm_wip)
788+
.filter_map(|clause| -> Option<ty::Clause<'tcx>> {
789+
clause
790+
.as_projection_clause()
791+
// For each clause `Alias == ProjectionTarget` with Self in Alias, make a
792+
// copy of the clause with U in place of Self. Strictly, this should only
793+
// need to add clauses that didn't come from supertrait bounds.
794+
.filter(|proj| {
795+
proj.skip_binder().projection_term.args.iter().any(|arg| {
796+
arg.as_type().is_some_and(|arg| arg.contains(tcx.types.self_param))
797+
})
798+
})
799+
.map(|proj| {
800+
proj.map_bound(|proj| {
801+
EarlyBinder::bind(tcx, proj).instantiate(tcx, args).skip_norm_wip()
802+
})
803+
.upcast(tcx)
804+
})
805+
}),
806+
);
765807

766808
let meta_sized_predicate = {
767809
let meta_sized_did = tcx.require_lang_item(LangItem::MetaSized, DUMMY_SP);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! Tests for <https://github.com/rust-lang/rust/issues/161621> that currently don't pass due to
2+
//! other limitations. We at least shouldn't see `RustaceansAreAwesome` mentioned in error messages.
3+
//! If we do see that, and especially if that's all that prevents these from compiling, dyn-
4+
//! compatibility checking (specifically receiver dispatchability checking) needs to be updated.
5+
//!
6+
//! See `dispatchability-placeholder-satisfies-bounds.rs` for additional context.
7+
8+
trait HasAssoc1 {
9+
type Assoc;
10+
}
11+
12+
trait HasAssoc2 {
13+
type Assoc;
14+
}
15+
16+
trait Parent<T: ?Sized> {}
17+
18+
// Due to param-env normalization being done in an unnormalized param-env, neither of these examples
19+
// can prove `<Self as HasAssoc1>::Assoc: HasAssoc2`.
20+
21+
trait AliasNormalizesToSelf //~ ERROR: the trait bound `Self: HasAssoc2` is not satisfied
22+
where
23+
Self: HasAssoc1<Assoc = Self>,
24+
<Self as HasAssoc1>::Assoc: HasAssoc2<Assoc = Self>,
25+
Self: Parent<<<Self as HasAssoc1>::Assoc as HasAssoc2>::Assoc>,
26+
{
27+
fn f(&self); //~ ERROR: the trait bound `Self: HasAssoc2` is not satisfied
28+
}
29+
30+
trait AliasNormalizesToUnit //~ ERROR: the trait bound `(): HasAssoc2` is not satisfied
31+
where
32+
Self: HasAssoc1<Assoc = ()>,
33+
<Self as HasAssoc1>::Assoc: HasAssoc2<Assoc = Self>,
34+
Self: Parent<<<Self as HasAssoc1>::Assoc as HasAssoc2>::Assoc>,
35+
{
36+
fn f(&self); //~ ERROR: the trait bound `(): HasAssoc2` is not satisfied
37+
}
38+
39+
fn main() {}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error[E0277]: the trait bound `Self: HasAssoc2` is not satisfied
2+
--> $DIR/dispatchability-placeholder-does-not-appear-in-error.rs:21:1
3+
|
4+
LL | trait AliasNormalizesToSelf
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HasAssoc2` is not implemented for `Self`
6+
|
7+
help: consider further restricting `Self`
8+
|
9+
LL | trait AliasNormalizesToSelf: HasAssoc2
10+
| +++++++++++
11+
12+
error[E0277]: the trait bound `(): HasAssoc2` is not satisfied
13+
--> $DIR/dispatchability-placeholder-does-not-appear-in-error.rs:30:1
14+
|
15+
LL | trait AliasNormalizesToUnit
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HasAssoc2` is not implemented for `()`
17+
|
18+
help: this trait has no implementations, consider adding one
19+
--> $DIR/dispatchability-placeholder-does-not-appear-in-error.rs:12:1
20+
|
21+
LL | trait HasAssoc2 {
22+
| ^^^^^^^^^^^^^^^
23+
24+
error[E0277]: the trait bound `Self: HasAssoc2` is not satisfied
25+
--> $DIR/dispatchability-placeholder-does-not-appear-in-error.rs:27:5
26+
|
27+
LL | fn f(&self);
28+
| ^^^^^^^^^^^^ the trait `HasAssoc2` is not implemented for `Self`
29+
|
30+
help: consider further restricting `Self`
31+
|
32+
LL | fn f(&self) where Self: HasAssoc2;
33+
| +++++++++++++++++++++
34+
35+
error[E0277]: the trait bound `(): HasAssoc2` is not satisfied
36+
--> $DIR/dispatchability-placeholder-does-not-appear-in-error.rs:36:5
37+
|
38+
LL | fn f(&self);
39+
| ^^^^^^^^^^^^ the trait `HasAssoc2` is not implemented for `()`
40+
|
41+
help: this trait has no implementations, consider adding one
42+
--> $DIR/dispatchability-placeholder-does-not-appear-in-error.rs:12:1
43+
|
44+
LL | trait HasAssoc2 {
45+
| ^^^^^^^^^^^^^^^
46+
47+
error: aborting due to 4 previous errors
48+
49+
For more information about this error, try `rustc --explain E0277`.

‎tests/ui/dyn-compatibility/dispatchability-placeholder-does-not-satisfy-bounds.rs‎

Lines changed: 0 additions & 24 deletions
This file was deleted.

‎tests/ui/dyn-compatibility/dispatchability-placeholder-does-not-satisfy-bounds.stderr‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎tests/ui/dyn-compatibility/dispatchability-placeholder-satisfies-bounds.rs‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! supertraits that would be necessary for `RustaceansAreAwesome` to implement `Trait`.
99
//@ check-pass
1010

11-
// This works for `Self: Super<...Ty::<RustaceansAreAwesome>::Assoc...>` after the `where`.
11+
// This works for `Self: Super<...<Ty<RustaceansAreAwesome> as TraitWithAssoc>::Assoc...>`.
1212
// We need to assume `(Args, RustaceansAreAwesome::Output): SignatureToFnPtr`.
1313

1414
use std::ops::Deref;
@@ -26,7 +26,7 @@ where
2626
fn method(&self);
2727
}
2828

29-
// This works for `Trait: Super<...Ty<RustaceansAreAwesome>::Assoc...>` before the `where`.
29+
// This works for `Self: Super<...<Ty as TraitWithAssoc<RustaceansAreAwesome>>::Assoc...>`.
3030
// We need to assume `A: Associator<RustaceansAreAwesome>`.
3131

3232
trait Associator<S: ?Sized> {
@@ -58,24 +58,53 @@ pub trait Transposable<T: Matrix2D<RowIndex = Self::RowIndex>>: Matrix2D {
5858
fn transpose(&self) -> T;
5959
}
6060

61+
// Here, we need to assume `<(RustaceansAreAwesome,) as HasAssoc>::Assoc == str` in order to prove
62+
// `<(RustaceansAreAwesome,) as HasAssoc>::Assoc: ToOwned`; some projection clauses are necessary.
63+
64+
trait HasAssoc {
65+
type Assoc;
66+
}
67+
68+
trait Parent<T: ?Sized> {}
69+
70+
trait NeedProjectionNotInExpansion
71+
where
72+
// The projection clause `<(Self,) as HasAssoc>::Assoc == str` isn't written as part of a
73+
// supertrait bound, so the expansion of `RustaceansAreAwesome: NeedProjectionNotInExpansion`
74+
// done by param-env normalization won't include a version of it for `RustaceansAreAwesome`.
75+
(Self,): HasAssoc<Assoc = str>,
76+
Self: Parent<<<(Self,) as HasAssoc>::Assoc as ToOwned>::Owned>,
77+
{
78+
fn f(&self);
79+
}
80+
81+
// When adding projection clauses involving `RustaceansAreAwesome`, we shouldn't conflict with those
82+
// that will be added by expansion. Due to the suprtrait bound `Self:HasAssoc<Assoc = Self>`, param-
83+
// env normalization expands the assumption `RustaceansAreAwesome: CarefulOfProjectionFromExpansion`
84+
// to also assume `<RustaceansAreAwesome as HasAssoc>::Assoc == RustaceansAreAwesome`. Adding any
85+
// differing projection clause for it would lead to ambiguity here.
86+
87+
trait CarefulOfProjectionFromExpansion
88+
where
89+
Self: HasAssoc<Assoc = Self>,
90+
Self: Parent<<<Self as HasAssoc>::Assoc as HasAssoc>::Assoc>,
91+
{
92+
fn f(&self);
93+
}
94+
6195
// An example where we can't add any additional projections without introducing ambiguity: we don't
6296
// want to assume `<() as HasAssoc>::Assoc` is both `Self` and `RustaceansAreAwesome`. At the time
6397
// of writing, we let `<() as HasAssoc>::Assoc` normalize to `Self` during receiver dispatchability
6498
// checking. This should be fine, since to call methods on a `dyn TechnicallyDynCompatible`, we'd
6599
// need `<() as HasAssoc>::Assoc` to be `dyn TechnicallyDynCompatible`. This is the same reasoning
66100
// that lets us assume `RustaceansAreAwesome` implements its trait at all: to use the trait object,
67-
// it must implement the trait; otherwise, receiver dispatchability doesn't mean much.
68-
69-
trait HasAssoc {
70-
type Assoc;
71-
}
101+
// it must implement the trait; otherwise, receiver dispatchability doesn't mean much (except
102+
// insofar as it lets `TechnicallyDynCompatible` be dyn-compatible).
72103

73104
impl HasAssoc for () {
74105
type Assoc = ();
75106
}
76107

77-
trait Parent<T: ?Sized> {}
78-
79108
trait TechnicallyDynCompatible
80109
where
81110
(): HasAssoc<Assoc = Self>,

0 commit comments

Comments
 (0)