Skip to content

Commit 68386dc

Browse files
committed
Auto merge of rust-lang#120752 - compiler-errors:more-relevant-bounds, r=lcnr
Collect relevant item bounds from trait clauses for nested rigid projections Rust currently considers trait where-clauses that bound the trait's *own* associated types to act like an item bound: ```rust trait Foo where Self::Assoc: Bar { type Assoc; } // acts as if: trait Foo { type Assoc: Bar; } ``` ### Background This behavior has existed since essentially forever (i.e. before Rust 1.0), since we originally started out by literally looking at the where clauses written on the trait when assembling `SelectionCandidate::ProjectionCandidate` for projections. However, looking at the predicates of the associated type themselves was not sound, since it was unclear which predicates were *assumed* and which predicates were *implied*, and therefore this was reworked in rust-lang#72788 (which added a query for the predicates we consider for `ProjectionCandidate`s), and then finally item bounds and predicates were split in rust-lang#73905. ### Problem 1: GATs don't uplift bounds correctly All the while, we've still had logic to uplift associated type bounds from a trait's where clauses. However, with the introduction of GATs, this logic was never really generalized correctly for them, since we were using simple equality to test if the self type of a trait where clause is a projection. This leads to shortcomings, such as: ```rust trait Foo where for<'a> Self::Gat<'a>: Debug, { type Gat<'a>; } fn test<T: Foo>(x: T::Gat<'static>) { //~^ ERROR `<T as Foo>::Gat<'a>` doesn't implement `Debug` println!("{:?}", x); } ``` ### Problem 2: Nested associated type bounds are not uplifted We also don't attempt to uplift bounds on nested associated types, something that we couldn't really support until rust-lang#120584. This can be demonstrated best with an example: ```rust trait A where Self::Assoc: B, where <Self::Assoc as B>::Assoc2: C, { type Assoc; // <~ The compiler *should* treat this like it has an item bound `B<Assoc2: C>`. } trait B { type Assoc2; } trait C {} fn is_c<T: C>() {} fn test<T: A>() { is_c::<<Self::Assoc as B>::Assoc2>(); //~^ ERROR the trait bound `<<T as A>::Assoc as B>::Assoc2: C` is not satisfied } ``` Why does this matter? Well, generalizing this behavior bridges a gap between the associated type bounds (ATB) feature and trait where clauses. Currently, all bounds that can be stably written on associated types can also be expressed as where clauses on traits; however, with the stabilization of ATB, there are now bounds that can't be desugared in the same way. This fixes that. ## How does this PR fix things? First, when scraping item bounds from the trait's where clauses, given a trait predicate, we'll loop of the self type of the predicate as long as it's a projection. If we find a projection whose trait ref matches, we'll uplift the bound. This allows us to uplift, for example `<Self as Trait>::Assoc: Bound` (pre-existing), but also `<<Self as Trait>::Assoc as Iterator>::Item: Bound` (new). If that projection is a GAT, we will check if all of the GAT's *own* args are all unique late-bound vars. We then map the late-bound vars to early-bound vars from the GAT -- this allows us to uplift `for<'a, 'b> Self::Assoc<'a, 'b>: Trait` into an item bound, but we will leave `for<'a> Self::Assoc<'a, 'a>: Trait` and `Self::Assoc<'static, 'static>: Trait` alone. ### Okay, but does this *really* matter? I consider this to be an improvement of the status quo because it makes GATs a bit less magical, and makes rigid projections a bit more expressive.
2 parents b511753 + 517208f commit 68386dc

7 files changed

+382
-10
lines changed

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+237-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use rustc_data_structures::fx::FxIndexSet;
1+
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
22
use rustc_hir as hir;
33
use rustc_infer::traits::util;
4+
use rustc_middle::ty::fold::shift_vars;
45
use rustc_middle::ty::{
5-
self, GenericArgs, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
6+
self, GenericArgs, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
67
};
78
use rustc_middle::{bug, span_bug};
89
use rustc_span::Span;
@@ -42,14 +43,18 @@ fn associated_type_bounds<'tcx>(
4243
let trait_def_id = tcx.local_parent(assoc_item_def_id);
4344
let trait_predicates = tcx.trait_explicit_predicates_and_bounds(trait_def_id);
4445

45-
let bounds_from_parent = trait_predicates.predicates.iter().copied().filter(|(pred, _)| {
46-
match pred.kind().skip_binder() {
47-
ty::ClauseKind::Trait(tr) => tr.self_ty() == item_ty,
48-
ty::ClauseKind::Projection(proj) => proj.projection_term.self_ty() == item_ty,
49-
ty::ClauseKind::TypeOutlives(outlives) => outlives.0 == item_ty,
50-
_ => false,
51-
}
52-
});
46+
let item_trait_ref = ty::TraitRef::identity(tcx, tcx.parent(assoc_item_def_id.to_def_id()));
47+
let bounds_from_parent =
48+
trait_predicates.predicates.iter().copied().filter_map(|(clause, span)| {
49+
remap_gat_vars_and_recurse_into_nested_projections(
50+
tcx,
51+
filter,
52+
item_trait_ref,
53+
assoc_item_def_id,
54+
span,
55+
clause,
56+
)
57+
});
5358

5459
let all_bounds = tcx.arena.alloc_from_iter(bounds.clauses(tcx).chain(bounds_from_parent));
5560
debug!(
@@ -63,6 +68,228 @@ fn associated_type_bounds<'tcx>(
6368
all_bounds
6469
}
6570

71+
/// The code below is quite involved, so let me explain.
72+
///
73+
/// We loop here, because we also want to collect vars for nested associated items as
74+
/// well. For example, given a clause like `Self::A::B`, we want to add that to the
75+
/// item bounds for `A`, so that we may use that bound in the case that `Self::A::B` is
76+
/// rigid.
77+
///
78+
/// Secondly, regarding bound vars, when we see a where clause that mentions a GAT
79+
/// like `for<'a, ...> Self::Assoc<'a, ...>: Bound<'b, ...>`, we want to turn that into
80+
/// an item bound on the GAT, where all of the GAT args are substituted with the GAT's
81+
/// param regions, and then keep all of the other late-bound vars in the bound around.
82+
/// We need to "compress" the binder so that it doesn't mention any of those vars that
83+
/// were mapped to params.
84+
fn remap_gat_vars_and_recurse_into_nested_projections<'tcx>(
85+
tcx: TyCtxt<'tcx>,
86+
filter: PredicateFilter,
87+
item_trait_ref: ty::TraitRef<'tcx>,
88+
assoc_item_def_id: LocalDefId,
89+
span: Span,
90+
clause: ty::Clause<'tcx>,
91+
) -> Option<(ty::Clause<'tcx>, Span)> {
92+
let mut clause_ty = match clause.kind().skip_binder() {
93+
ty::ClauseKind::Trait(tr) => tr.self_ty(),
94+
ty::ClauseKind::Projection(proj) => proj.projection_term.self_ty(),
95+
ty::ClauseKind::TypeOutlives(outlives) => outlives.0,
96+
_ => return None,
97+
};
98+
99+
let gat_vars = loop {
100+
if let ty::Alias(ty::Projection, alias_ty) = *clause_ty.kind() {
101+
if alias_ty.trait_ref(tcx) == item_trait_ref
102+
&& alias_ty.def_id == assoc_item_def_id.to_def_id()
103+
{
104+
// We have found the GAT in question...
105+
// Return the vars, since we may need to remap them.
106+
break &alias_ty.args[item_trait_ref.args.len()..];
107+
} else {
108+
// Only collect *self* type bounds if the filter is for self.
109+
match filter {
110+
PredicateFilter::SelfOnly | PredicateFilter::SelfThatDefines(_) => {
111+
return None;
112+
}
113+
PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {}
114+
}
115+
116+
clause_ty = alias_ty.self_ty();
117+
continue;
118+
}
119+
}
120+
121+
return None;
122+
};
123+
124+
// Special-case: No GAT vars, no mapping needed.
125+
if gat_vars.is_empty() {
126+
return Some((clause, span));
127+
}
128+
129+
// First, check that all of the GAT args are substituted with a unique late-bound arg.
130+
// If we find a duplicate, then it can't be mapped to the definition's params.
131+
let mut mapping = FxIndexMap::default();
132+
let generics = tcx.generics_of(assoc_item_def_id);
133+
for (param, var) in std::iter::zip(&generics.own_params, gat_vars) {
134+
let existing = match var.unpack() {
135+
ty::GenericArgKind::Lifetime(re) => {
136+
if let ty::RegionKind::ReBound(ty::INNERMOST, bv) = re.kind() {
137+
mapping.insert(bv.var, tcx.mk_param_from_def(param))
138+
} else {
139+
return None;
140+
}
141+
}
142+
ty::GenericArgKind::Type(ty) => {
143+
if let ty::Bound(ty::INNERMOST, bv) = *ty.kind() {
144+
mapping.insert(bv.var, tcx.mk_param_from_def(param))
145+
} else {
146+
return None;
147+
}
148+
}
149+
ty::GenericArgKind::Const(ct) => {
150+
if let ty::ConstKind::Bound(ty::INNERMOST, bv) = ct.kind() {
151+
mapping.insert(bv, tcx.mk_param_from_def(param))
152+
} else {
153+
return None;
154+
}
155+
}
156+
};
157+
158+
if existing.is_some() {
159+
return None;
160+
}
161+
}
162+
163+
// Finally, map all of the args in the GAT to the params we expect, and compress
164+
// the remaining late-bound vars so that they count up from var 0.
165+
let mut folder =
166+
MapAndCompressBoundVars { tcx, binder: ty::INNERMOST, still_bound_vars: vec![], mapping };
167+
let pred = clause.kind().skip_binder().fold_with(&mut folder);
168+
169+
Some((
170+
ty::Binder::bind_with_vars(pred, tcx.mk_bound_variable_kinds(&folder.still_bound_vars))
171+
.upcast(tcx),
172+
span,
173+
))
174+
}
175+
176+
/// Given some where clause like `for<'b, 'c> <Self as Trait<'a_identity>>::Gat<'b>: Bound<'c>`,
177+
/// the mapping will map `'b` back to the GAT's `'b_identity`. Then we need to compress the
178+
/// remaining bound var `'c` to index 0.
179+
///
180+
/// This folder gives us: `for<'c> <Self as Trait<'a_identity>>::Gat<'b_identity>: Bound<'c>`,
181+
/// which is sufficient for an item bound for `Gat`, since all of the GAT's args are identity.
182+
struct MapAndCompressBoundVars<'tcx> {
183+
tcx: TyCtxt<'tcx>,
184+
/// How deep are we? Makes sure we don't touch the vars of nested binders.
185+
binder: ty::DebruijnIndex,
186+
/// List of bound vars that remain unsubstituted because they were not
187+
/// mentioned in the GAT's args.
188+
still_bound_vars: Vec<ty::BoundVariableKind>,
189+
/// Subtle invariant: If the `GenericArg` is bound, then it should be
190+
/// stored with the debruijn index of `INNERMOST` so it can be shifted
191+
/// correctly during substitution.
192+
mapping: FxIndexMap<ty::BoundVar, ty::GenericArg<'tcx>>,
193+
}
194+
195+
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for MapAndCompressBoundVars<'tcx> {
196+
fn cx(&self) -> TyCtxt<'tcx> {
197+
self.tcx
198+
}
199+
200+
fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
201+
where
202+
ty::Binder<'tcx, T>: TypeSuperFoldable<TyCtxt<'tcx>>,
203+
{
204+
self.binder.shift_in(1);
205+
let out = t.super_fold_with(self);
206+
self.binder.shift_out(1);
207+
out
208+
}
209+
210+
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
211+
if !ty.has_bound_vars() {
212+
return ty;
213+
}
214+
215+
if let ty::Bound(binder, old_bound) = *ty.kind()
216+
&& self.binder == binder
217+
{
218+
let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
219+
mapped.expect_ty()
220+
} else {
221+
// If we didn't find a mapped generic, then make a new one.
222+
// Allocate a new var idx, and insert a new bound ty.
223+
let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
224+
self.still_bound_vars.push(ty::BoundVariableKind::Ty(old_bound.kind));
225+
let mapped = Ty::new_bound(
226+
self.tcx,
227+
ty::INNERMOST,
228+
ty::BoundTy { var, kind: old_bound.kind },
229+
);
230+
self.mapping.insert(old_bound.var, mapped.into());
231+
mapped
232+
};
233+
234+
shift_vars(self.tcx, mapped, self.binder.as_u32())
235+
} else {
236+
ty.super_fold_with(self)
237+
}
238+
}
239+
240+
fn fold_region(&mut self, re: ty::Region<'tcx>) -> ty::Region<'tcx> {
241+
if let ty::ReBound(binder, old_bound) = re.kind()
242+
&& self.binder == binder
243+
{
244+
let mapped = if let Some(mapped) = self.mapping.get(&old_bound.var) {
245+
mapped.expect_region()
246+
} else {
247+
let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
248+
self.still_bound_vars.push(ty::BoundVariableKind::Region(old_bound.kind));
249+
let mapped = ty::Region::new_bound(
250+
self.tcx,
251+
ty::INNERMOST,
252+
ty::BoundRegion { var, kind: old_bound.kind },
253+
);
254+
self.mapping.insert(old_bound.var, mapped.into());
255+
mapped
256+
};
257+
258+
shift_vars(self.tcx, mapped, self.binder.as_u32())
259+
} else {
260+
re
261+
}
262+
}
263+
264+
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
265+
if !ct.has_bound_vars() {
266+
return ct;
267+
}
268+
269+
if let ty::ConstKind::Bound(binder, old_var) = ct.kind()
270+
&& self.binder == binder
271+
{
272+
let mapped = if let Some(mapped) = self.mapping.get(&old_var) {
273+
mapped.expect_const()
274+
} else {
275+
let var = ty::BoundVar::from_usize(self.still_bound_vars.len());
276+
self.still_bound_vars.push(ty::BoundVariableKind::Const);
277+
let mapped = ty::Const::new_bound(self.tcx, ty::INNERMOST, var);
278+
self.mapping.insert(old_var, mapped.into());
279+
mapped
280+
};
281+
282+
shift_vars(self.tcx, mapped, self.binder.as_u32())
283+
} else {
284+
ct.super_fold_with(self)
285+
}
286+
}
287+
288+
fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
289+
if !p.has_bound_vars() { p } else { p.super_fold_with(self) }
290+
}
291+
}
292+
66293
/// Opaque types don't inherit bounds from their parent: for return position
67294
/// impl trait it isn't possible to write a suitable predicate on the
68295
/// containing function and for type-alias impl trait we don't have a backwards
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Demonstrates a mostly-theoretical inference guidance now that we turn the where
2+
// clause on `Trait` into an item bound, given that we prefer item bounds somewhat
3+
// greedily in trait selection.
4+
5+
trait Bound<T> {}
6+
impl<T, U> Bound<T> for U {}
7+
8+
trait Trait
9+
where
10+
<<Self as Trait>::Assoc as Other>::Assoc: Bound<u32>,
11+
{
12+
type Assoc: Other;
13+
}
14+
15+
trait Other {
16+
type Assoc;
17+
}
18+
19+
fn impls_trait<T: Bound<U>, U>() -> Vec<U> { vec![] }
20+
21+
fn foo<T: Trait>() {
22+
let mut vec_u = impls_trait::<<<T as Trait>::Assoc as Other>::Assoc, _>();
23+
vec_u.sort();
24+
drop::<Vec<u8>>(vec_u);
25+
//~^ ERROR mismatched types
26+
}
27+
28+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/nested-associated-type-bound-incompleteness.rs:24:21
3+
|
4+
LL | drop::<Vec<u8>>(vec_u);
5+
| --------------- ^^^^^ expected `Vec<u8>`, found `Vec<u32>`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: expected struct `Vec<u8>`
10+
found struct `Vec<u32>`
11+
note: function defined here
12+
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@ check-pass
2+
3+
trait Trait
4+
where
5+
for<'a> Self::Gat<'a>: OtherTrait,
6+
for<'a, 'b, 'c> <Self::Gat<'a> as OtherTrait>::OtherGat<'b>: HigherRanked<'c>,
7+
{
8+
type Gat<'a>;
9+
}
10+
11+
trait OtherTrait {
12+
type OtherGat<'b>;
13+
}
14+
15+
trait HigherRanked<'c> {}
16+
17+
fn lower_ranked<T: for<'b, 'c> OtherTrait<OtherGat<'b>: HigherRanked<'c>>>() {}
18+
19+
fn higher_ranked<T: Trait>()
20+
where
21+
for<'a> T::Gat<'a>: OtherTrait,
22+
for<'a, 'b, 'c> <T::Gat<'a> as OtherTrait>::OtherGat<'b>: HigherRanked<'c>,
23+
{
24+
}
25+
26+
fn test<T: Trait>() {
27+
lower_ranked::<T::Gat<'_>>();
28+
higher_ranked::<T>();
29+
}
30+
31+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@[next] compile-flags: -Znext-solver
4+
5+
trait Trait
6+
where
7+
Self::Assoc: Clone,
8+
{
9+
type Assoc;
10+
}
11+
12+
fn foo<T: Trait>(x: &T::Assoc) -> T::Assoc {
13+
x.clone()
14+
}
15+
16+
trait Trait2
17+
where
18+
Self::Assoc: Iterator,
19+
<Self::Assoc as Iterator>::Item: Clone,
20+
{
21+
type Assoc;
22+
}
23+
24+
fn foo2<T: Trait2>(x: &<T::Assoc as Iterator>::Item) -> <T::Assoc as Iterator>::Item {
25+
x.clone()
26+
}
27+
28+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ check-pass
2+
3+
// Test that `for<'a> Self::Gat<'a>: Debug` is implied in the definition of `Foo`,
4+
// just as it would be if it weren't a GAT but just a regular associated type.
5+
6+
use std::fmt::Debug;
7+
8+
trait Foo
9+
where
10+
for<'a> Self::Gat<'a>: Debug,
11+
{
12+
type Gat<'a>;
13+
}
14+
15+
fn test<T: Foo>(x: T::Gat<'static>) {
16+
println!("{:?}", x);
17+
}
18+
19+
fn main() {}

0 commit comments

Comments
 (0)