Skip to content

Commit 88fa8f4

Browse files
something
1 parent afd7548 commit 88fa8f4

File tree

6 files changed

+82
-39
lines changed

6 files changed

+82
-39
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
134134
.into_iter()
135135
.filter(|(trait_ref, _)| !tcx.trait_is_auto(trait_ref.def_id()));
136136

137+
let mut implied_projection_bounds = vec![];
137138
for (base_trait_ref, original_span) in regular_traits_refs_spans {
138139
let base_pred: ty::Predicate<'tcx> = base_trait_ref.upcast(tcx);
139140
for ClauseWithSupertraitSpan { pred, supertrait_span } in
@@ -188,7 +189,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
188189
// the discussion in #56288 for alternatives.
189190
if !references_self {
190191
// Include projections defined on supertraits.
191-
projection_bounds.push((pred, original_span));
192+
implied_projection_bounds.push(pred);
192193
}
193194

194195
self.check_elaborated_projection_mentions_input_lifetimes(
@@ -221,6 +222,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
221222
);
222223
}
223224
}
225+
for projection_bound in &implied_projection_bounds {
226+
let def_id = projection_bound.projection_def_id();
227+
let trait_ref = tcx.anonymize_bound_vars(
228+
projection_bound.map_bound(|p| p.projection_term.trait_ref(tcx)),
229+
);
230+
needed_associated_types.swap_remove(&(def_id, trait_ref));
231+
}
224232

225233
self.complain_about_missing_assoc_tys(
226234
principal_span,

compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
pub(super) mod structural_traits;
44

55
use derive_where::derive_where;
6+
use rustc_type_ir::data_structures::HashSet;
67
use rustc_type_ir::fold::TypeFoldable;
78
use rustc_type_ir::inherent::*;
89
use rustc_type_ir::lang_items::TraitSolverLangItem;
@@ -664,13 +665,22 @@ where
664665
// Consider all of the auto-trait and projection bounds, which don't
665666
// need to be recorded as a `BuiltinImplSource::Object` since they don't
666667
// really have a vtable base...
668+
let mut shadowed = HashSet::default();
667669
for bound in bounds.iter() {
668670
match bound.skip_binder() {
669671
ty::ExistentialPredicate::Trait(_) => {
670672
// Skip principal
671673
}
672-
ty::ExistentialPredicate::Projection(_)
673-
| ty::ExistentialPredicate::AutoTrait(_) => {
674+
ty::ExistentialPredicate::Projection(proj) => {
675+
shadowed.insert(proj.def_id);
676+
candidates.extend(G::probe_and_consider_object_bound_candidate(
677+
self,
678+
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
679+
goal,
680+
bound.with_self_ty(cx, self_ty),
681+
));
682+
}
683+
ty::ExistentialPredicate::AutoTrait(_) => {
674684
candidates.extend(G::probe_and_consider_object_bound_candidate(
675685
self,
676686
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
@@ -686,12 +696,19 @@ where
686696
// a projection goal.
687697
if let Some(principal) = bounds.principal() {
688698
let principal_trait_ref = principal.with_self_ty(cx, self_ty);
689-
for (idx, assumption) in elaborate::supertraits(cx, principal_trait_ref).enumerate() {
699+
let principal_clause: I::Clause = principal_trait_ref.upcast(cx);
700+
for (idx, assumption) in elaborate::elaborate(cx, [principal_clause]).enumerate() {
701+
if let Some(proj) = assumption.as_projection_clause() {
702+
if shadowed.contains(&proj.projection_def_id()) {
703+
continue;
704+
}
705+
}
706+
690707
candidates.extend(G::probe_and_consider_object_bound_candidate(
691708
self,
692709
CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
693710
goal,
694-
assumption.upcast(cx),
711+
assumption,
695712
));
696713
}
697714
}

compiler/rustc_trait_selection/src/traits/project.rs

+29-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_middle::ty::visit::{MaxUniverse, TypeVisitable, TypeVisitableExt};
1919
use rustc_middle::ty::{self, Term, Ty, TyCtxt, TypingMode, Upcast};
2020
use rustc_middle::{bug, span_bug};
2121
use rustc_span::symbol::sym;
22+
use rustc_type_ir::elaborate;
2223
use tracing::{debug, instrument};
2324

2425
use super::{
@@ -60,7 +61,7 @@ enum ProjectionCandidate<'tcx> {
6061
TraitDef(ty::PolyProjectionPredicate<'tcx>),
6162

6263
/// Bounds specified on an object type
63-
Object(ty::PolyProjectionPredicate<'tcx>),
64+
Object(ty::PolyProjectionPredicate<'tcx>, bool),
6465

6566
/// From an "impl" (or a "pseudo-impl" returned by select)
6667
Select(Selection<'tcx>),
@@ -683,7 +684,7 @@ fn project<'cx, 'tcx>(
683684

684685
assemble_candidates_from_object_ty(selcx, obligation, &mut candidates);
685686

686-
if let ProjectionCandidateSet::Single(ProjectionCandidate::Object(_)) = candidates {
687+
if let ProjectionCandidateSet::Single(ProjectionCandidate::Object(..)) = candidates {
687688
// Avoid normalization cycle from selection (see
688689
// `assemble_candidates_from_object_ty`).
689690
// FIXME(lazy_normalization): Lazy normalization should save us from
@@ -839,6 +840,7 @@ fn assemble_candidates_from_object_ty<'cx, 'tcx>(
839840
}
840841
_ => return,
841842
};
843+
842844
let env_predicates = data
843845
.projection_bounds()
844846
.filter(|bound| bound.item_def_id() == obligation.predicate.def_id)
@@ -848,10 +850,30 @@ fn assemble_candidates_from_object_ty<'cx, 'tcx>(
848850
selcx,
849851
obligation,
850852
candidate_set,
851-
ProjectionCandidate::Object,
853+
|c| ProjectionCandidate::Object(c, false),
852854
env_predicates,
853855
false,
854856
);
857+
858+
let shadowed =
859+
data.projection_bounds().any(|bound| bound.item_def_id() == obligation.predicate.def_id);
860+
861+
if !shadowed && let Some(principal) = data.principal() {
862+
let principal: ty::Clause<'tcx> = principal.with_self_ty(tcx, self_ty).upcast(tcx);
863+
let supertrait_projections = elaborate::elaborate(tcx, [principal]).filter(|clause| {
864+
clause
865+
.as_projection_clause()
866+
.is_some_and(|proj| proj.projection_def_id() == obligation.predicate.def_id)
867+
});
868+
assemble_candidates_from_predicates(
869+
selcx,
870+
obligation,
871+
candidate_set,
872+
|c| ProjectionCandidate::Object(c, true),
873+
supertrait_projections,
874+
true,
875+
);
876+
}
855877
}
856878

857879
#[instrument(
@@ -1258,10 +1280,12 @@ fn confirm_candidate<'cx, 'tcx>(
12581280
) -> Progress<'tcx> {
12591281
debug!(?obligation, ?candidate, "confirm_candidate");
12601282
let mut progress = match candidate {
1261-
ProjectionCandidate::ParamEnv(poly_projection)
1262-
| ProjectionCandidate::Object(poly_projection) => {
1283+
ProjectionCandidate::ParamEnv(poly_projection) => {
12631284
confirm_param_env_candidate(selcx, obligation, poly_projection, false)
12641285
}
1286+
ProjectionCandidate::Object(poly_projection, from_super) => {
1287+
confirm_param_env_candidate(selcx, obligation, poly_projection, from_super)
1288+
}
12651289

12661290
ProjectionCandidate::TraitDef(poly_projection) => {
12671291
confirm_param_env_candidate(selcx, obligation, poly_projection, true)

tests/ui/associated-types/issue-59324.stderr

+3-27
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,6 @@ help: consider further restricting this bound
4343
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
4444
| +++++
4545

46-
error[E0277]: the trait bound `(): Foo` is not satisfied
47-
--> $DIR/issue-59324.rs:23:29
48-
|
49-
LL | fn with_factory<H>(factory: dyn ThriftService<()>) {}
50-
| ^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `()`
51-
|
52-
help: this trait has no implementations, consider adding one
53-
--> $DIR/issue-59324.rs:3:1
54-
|
55-
LL | pub trait Foo: NotFoo {
56-
| ^^^^^^^^^^^^^^^^^^^^^
57-
5846
error[E0277]: the trait bound `Bug: Foo` is not satisfied
5947
--> $DIR/issue-59324.rs:19:10
6048
|
@@ -66,25 +54,13 @@ help: consider further restricting this bound
6654
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
6755
| +++++
6856

69-
error[E0277]: the trait bound `(): Foo` is not satisfied
70-
--> $DIR/issue-59324.rs:23:52
71-
|
72-
LL | fn with_factory<H>(factory: dyn ThriftService<()>) {}
73-
| ^^ the trait `Foo` is not implemented for `()`
74-
|
75-
help: this trait has no implementations, consider adding one
76-
--> $DIR/issue-59324.rs:3:1
77-
|
78-
LL | pub trait Foo: NotFoo {
79-
| ^^^^^^^^^^^^^^^^^^^^^
80-
81-
error[E0277]: the size for values of type `(dyn ThriftService<(), AssocType = _> + 'static)` cannot be known at compilation time
57+
error[E0277]: the size for values of type `(dyn ThriftService<()> + 'static)` cannot be known at compilation time
8258
--> $DIR/issue-59324.rs:23:29
8359
|
8460
LL | fn with_factory<H>(factory: dyn ThriftService<()>) {}
8561
| ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
8662
|
87-
= help: the trait `Sized` is not implemented for `(dyn ThriftService<(), AssocType = _> + 'static)`
63+
= help: the trait `Sized` is not implemented for `(dyn ThriftService<()> + 'static)`
8864
= help: unsized fn params are gated as an unstable feature
8965
help: you can use `impl Trait` as the argument type
9066
|
@@ -95,6 +71,6 @@ help: function arguments must have a statically known size, borrowed types alway
9571
LL | fn with_factory<H>(factory: &dyn ThriftService<()>) {}
9672
| +
9773

98-
error: aborting due to 7 previous errors
74+
error: aborting due to 5 previous errors
9975

10076
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ check-pass
2+
3+
trait Sup<T> {
4+
type Assoc;
5+
}
6+
7+
impl<T> Sup<T> for () {
8+
type Assoc = T;
9+
}
10+
11+
trait Trait<A, B>: Sup<A, Assoc = A> + Sup<B, Assoc = B> {}
12+
13+
impl<T, U> Trait<T, U> for () {}
14+
15+
fn main() {
16+
let x: &dyn Trait<(), _> = &();
17+
let y: &dyn Trait<_, ()> = x;
18+
}

tests/ui/traits/alias/dont-elaborate-non-self.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0277]: the size for values of type `(dyn Fn() -> Fut + 'static)` cannot be known at compilation time
1+
error[E0277]: the size for values of type `(dyn Fn<()> + 'static)` cannot be known at compilation time
22
--> $DIR/dont-elaborate-non-self.rs:7:14
33
|
44
LL | fn f<Fut>(a: dyn F<Fut>) {}
55
| ^^^^^^^^^^ doesn't have a size known at compile-time
66
|
7-
= help: the trait `Sized` is not implemented for `(dyn Fn() -> Fut + 'static)`
7+
= help: the trait `Sized` is not implemented for `(dyn Fn<()> + 'static)`
88
= help: unsized fn params are gated as an unstable feature
99
help: you can use `impl Trait` as the argument type
1010
|

0 commit comments

Comments
 (0)