Skip to content

Commit 98525ae

Browse files
Check object's supertrait and associated type bounds in new solver
1 parent 07c993e commit 98525ae

File tree

6 files changed

+193
-2
lines changed

6 files changed

+193
-2
lines changed

compiler/rustc_trait_selection/src/solve/assembly.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ pub(super) trait GoalKind<'tcx>: TypeFoldable<TyCtxt<'tcx>> + Copy + Eq {
9999
requirements: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
100100
) -> QueryResult<'tcx>;
101101

102+
// Consider a clause specifically for a `dyn Trait` self type. This requires
103+
// additionally checking all of the supertraits and object bounds to hold,
104+
// since they're not implied by the well-formedness of the object type.
105+
fn consider_object_bound_candidate(
106+
ecx: &mut EvalCtxt<'_, 'tcx>,
107+
goal: Goal<'tcx, Self>,
108+
assumption: ty::Predicate<'tcx>,
109+
) -> QueryResult<'tcx>;
110+
102111
fn consider_impl_candidate(
103112
ecx: &mut EvalCtxt<'_, 'tcx>,
104113
goal: Goal<'tcx, Self>,
@@ -455,7 +464,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
455464
for assumption in
456465
elaborate_predicates(tcx, bounds.iter().map(|bound| bound.with_self_ty(tcx, self_ty)))
457466
{
458-
match G::consider_implied_clause(self, goal, assumption.predicate, []) {
467+
match G::consider_object_bound_candidate(self, goal, assumption.predicate) {
459468
Ok(result) => {
460469
candidates.push(Candidate { source: CandidateSource::BuiltinImpl, result })
461470
}

compiler/rustc_trait_selection/src/solve/project_goals.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,50 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
128128
}
129129
}
130130

131+
fn consider_object_bound_candidate(
132+
ecx: &mut EvalCtxt<'_, 'tcx>,
133+
goal: Goal<'tcx, Self>,
134+
assumption: ty::Predicate<'tcx>,
135+
) -> QueryResult<'tcx> {
136+
if let Some(poly_projection_pred) = assumption.to_opt_poly_projection_pred()
137+
&& poly_projection_pred.projection_def_id() == goal.predicate.def_id()
138+
{
139+
ecx.probe(|ecx| {
140+
let assumption_projection_pred =
141+
ecx.instantiate_binder_with_infer(poly_projection_pred);
142+
let mut nested_goals = ecx.eq(
143+
goal.param_env,
144+
goal.predicate.projection_ty,
145+
assumption_projection_pred.projection_ty,
146+
)?;
147+
148+
let tcx = ecx.tcx();
149+
let ty::Dynamic(bounds, _, _) = *goal.predicate.self_ty().kind() else {
150+
bug!("expected object type in `consider_object_bound_candidate`");
151+
};
152+
nested_goals.extend(
153+
structural_traits::predicates_for_object_candidate(
154+
tcx,
155+
goal.predicate.projection_ty.trait_ref(tcx),
156+
bounds,
157+
)
158+
.into_iter()
159+
.map(|pred| goal.with(tcx, pred)),
160+
);
161+
162+
let subst_certainty = ecx.evaluate_all(nested_goals)?;
163+
164+
ecx.eq_term_and_make_canonical_response(
165+
goal,
166+
subst_certainty,
167+
assumption_projection_pred.term,
168+
)
169+
})
170+
} else {
171+
Err(NoSolution)
172+
}
173+
}
174+
131175
fn consider_impl_candidate(
132176
ecx: &mut EvalCtxt<'_, 'tcx>,
133177
goal: Goal<'tcx, ProjectionPredicate<'tcx>>,

compiler/rustc_trait_selection/src/solve/trait_goals.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,45 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
8686
}
8787
}
8888

89+
fn consider_object_bound_candidate(
90+
ecx: &mut EvalCtxt<'_, 'tcx>,
91+
goal: Goal<'tcx, Self>,
92+
assumption: ty::Predicate<'tcx>,
93+
) -> QueryResult<'tcx> {
94+
if let Some(poly_trait_pred) = assumption.to_opt_poly_trait_pred()
95+
&& poly_trait_pred.def_id() == goal.predicate.def_id()
96+
{
97+
// FIXME: Constness and polarity
98+
ecx.probe(|ecx| {
99+
let assumption_trait_pred =
100+
ecx.instantiate_binder_with_infer(poly_trait_pred);
101+
let mut nested_goals = ecx.eq(
102+
goal.param_env,
103+
goal.predicate.trait_ref,
104+
assumption_trait_pred.trait_ref,
105+
)?;
106+
107+
let tcx = ecx.tcx();
108+
let ty::Dynamic(bounds, _, _) = *goal.predicate.self_ty().kind() else {
109+
bug!("expected object type in `consider_object_bound_candidate`");
110+
};
111+
nested_goals.extend(
112+
structural_traits::predicates_for_object_candidate(
113+
tcx,
114+
goal.predicate.trait_ref,
115+
bounds,
116+
)
117+
.into_iter()
118+
.map(|pred| goal.with(tcx, pred)),
119+
);
120+
121+
ecx.evaluate_all_and_make_canonical_response(nested_goals)
122+
})
123+
} else {
124+
Err(NoSolution)
125+
}
126+
}
127+
89128
fn consider_auto_trait_candidate(
90129
ecx: &mut EvalCtxt<'_, 'tcx>,
91130
goal: Goal<'tcx, Self>,

compiler/rustc_trait_selection/src/solve/trait_goals/structural_traits.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use rustc_data_structures::fx::FxHashMap;
12
use rustc_hir::{Movability, Mutability};
23
use rustc_infer::traits::query::NoSolution;
3-
use rustc_middle::ty::{self, Ty, TyCtxt};
4+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable};
45

56
use crate::solve::EvalCtxt;
67

@@ -233,3 +234,62 @@ pub(crate) fn extract_tupled_inputs_and_output_from_callable<'tcx>(
233234
}
234235
}
235236
}
237+
238+
pub(crate) fn predicates_for_object_candidate<'tcx>(
239+
tcx: TyCtxt<'tcx>,
240+
trait_ref: ty::TraitRef<'tcx>,
241+
object_bound: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
242+
) -> Vec<ty::Predicate<'tcx>> {
243+
let mut requirements = vec![];
244+
requirements.extend(
245+
tcx.super_predicates_of(trait_ref.def_id).instantiate(tcx, trait_ref.substs).predicates,
246+
);
247+
for item in tcx.associated_items(trait_ref.def_id).in_definition_order() {
248+
if item.kind == ty::AssocKind::Type {
249+
requirements.extend(tcx.item_bounds(item.def_id).subst(tcx, trait_ref.substs));
250+
}
251+
}
252+
253+
let mut replace_projection_with = FxHashMap::default();
254+
for bound in object_bound {
255+
let bound = bound.no_bound_vars().expect("higher-ranked projections not supported, yet");
256+
if let ty::ExistentialPredicate::Projection(proj) = bound {
257+
let proj = proj.with_self_ty(tcx, trait_ref.self_ty());
258+
let old_ty = replace_projection_with.insert(
259+
proj.projection_ty,
260+
proj.term.ty().expect("expected only types in dyn right now"),
261+
);
262+
assert_eq!(
263+
old_ty,
264+
None,
265+
"{} has two substitutions: {} and {}",
266+
proj.projection_ty,
267+
proj.term,
268+
old_ty.unwrap()
269+
);
270+
}
271+
}
272+
273+
requirements.fold_with(&mut ReplaceProjectionWith { tcx, mapping: replace_projection_with })
274+
}
275+
276+
struct ReplaceProjectionWith<'tcx> {
277+
tcx: TyCtxt<'tcx>,
278+
mapping: FxHashMap<ty::AliasTy<'tcx>, Ty<'tcx>>,
279+
}
280+
281+
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReplaceProjectionWith<'tcx> {
282+
fn interner(&self) -> TyCtxt<'tcx> {
283+
self.tcx
284+
}
285+
286+
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
287+
if let ty::Alias(ty::Projection, alias_ty) = *ty.kind()
288+
&& let Some(replacement) = self.mapping.get(&alias_ty)
289+
{
290+
*replacement
291+
} else {
292+
ty.super_fold_with(self)
293+
}
294+
}
295+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// compile-flags: -Ztrait-solver=next
2+
3+
trait Setup {
4+
type From: Copy;
5+
}
6+
7+
fn copy<U: Setup + ?Sized>(from: &U::From) -> U::From {
8+
*from
9+
}
10+
11+
pub fn copy_any<T>(t: &T) -> T {
12+
copy::<dyn Setup<From=T>>(t)
13+
//~^ ERROR the trait bound `dyn Setup<From = T>: Setup` is not satisfied
14+
}
15+
16+
fn main() {
17+
let x = String::from("Hello, world");
18+
let y = copy_any(&x);
19+
println!("{y}");
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0277]: the trait bound `dyn Setup<From = T>: Setup` is not satisfied
2+
--> $DIR/object-unsafety.rs:12:12
3+
|
4+
LL | copy::<dyn Setup<From=T>>(t)
5+
| ^^^^^^^^^^^^^^^^^ the trait `Setup` is not implemented for `dyn Setup<From = T>`
6+
|
7+
note: required by a bound in `copy`
8+
--> $DIR/object-unsafety.rs:7:12
9+
|
10+
LL | fn copy<U: Setup + ?Sized>(from: &U::From) -> U::From {
11+
| ^^^^^ required by this bound in `copy`
12+
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
13+
|
14+
LL | pub fn copy_any<T>(t: &T) -> T where dyn Setup<From = T>: Setup {
15+
| ++++++++++++++++++++++++++++++++
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)