Skip to content

Commit f595989

Browse files
Only include associated type bounds for Self:Sized associated types if they are provided
1 parent 0f73f0f commit f595989

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
204204
.filter(|item| item.is_type())
205205
// No RPITITs -- they're not dyn-compatible for now.
206206
.filter(|item| !item.is_impl_trait_in_trait())
207-
// If the associated type has a `where Self: Sized` bound,
208-
// we do not need to constrain the associated type.
209-
.filter(|item| !tcx.generics_require_sized_self(item.def_id))
210207
.map(|item| (item.def_id, trait_ref)),
211208
);
212209
}
@@ -285,7 +282,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
285282
if let Some(assoc) = projection_bounds.get(&key) {
286283
Some(*assoc)
287284
} else {
288-
missing_assoc_types.insert(key);
285+
// If the associated type has a `where Self: Sized` bound,
286+
// we do not need to constrain the associated type.
287+
if !tcx.generics_require_sized_self(key.0) {
288+
missing_assoc_types.insert(key);
289+
}
289290
None
290291
}
291292
})

compiler/rustc_middle/src/ty/sty.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,10 @@ impl<'tcx> Ty<'tcx> {
723723
repr: DynKind,
724724
) -> Ty<'tcx> {
725725
if cfg!(debug_assertions) {
726-
let projection_count = obj.projection_bounds().count();
726+
let projection_count = obj
727+
.projection_bounds()
728+
.filter(|item| !tcx.generics_require_sized_self(item.item_def_id()))
729+
.count();
727730
let expected_count: usize = obj
728731
.principal_def_id()
729732
.into_iter()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ check-pass
2+
3+
// Regression test for <https://github.com/rust-lang/rust/issues/140645>.
4+
// Test that we lower impossible-to-satisfy associated type bounds, which
5+
// may for example constrain impl parameters.
6+
7+
pub trait Other {}
8+
9+
pub trait Trait {
10+
type Assoc
11+
where
12+
Self: Sized;
13+
}
14+
15+
impl Other for dyn Trait {}
16+
// `dyn Trait<Assoc = ()>` is a different "nominal type" than `dyn Traiat`.
17+
impl Other for dyn Trait<Assoc = ()> {}
18+
//~^ WARN unnecessary associated type bound for dyn-incompatible associated type
19+
20+
// I hope it's clear that `dyn Trait` (w/o `Assoc`) wouldn't match this impl.
21+
impl<T> dyn Trait<Assoc = T> {}
22+
//~^ WARN unnecessary associated type bound for dyn-incompatible associated type
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
warning: unnecessary associated type bound for dyn-incompatible associated type
2+
--> $DIR/constrain-via-unnecessary-bound.rs:17:26
3+
|
4+
LL | impl Other for dyn Trait<Assoc = ()> {}
5+
| ^^^^^^^^^^ help: remove this bound
6+
|
7+
= note: this associated type has a `where Self: Sized` bound, and while the associated type can be specified, it cannot be used because trait objects are never `Sized`
8+
= note: `#[warn(unused_associated_type_bounds)]` on by default
9+
10+
warning: unnecessary associated type bound for dyn-incompatible associated type
11+
--> $DIR/constrain-via-unnecessary-bound.rs:21:19
12+
|
13+
LL | impl<T> dyn Trait<Assoc = T> {}
14+
| ^^^^^^^^^ help: remove this bound
15+
|
16+
= note: this associated type has a `where Self: Sized` bound, and while the associated type can be specified, it cannot be used because trait objects are never `Sized`
17+
18+
warning: 2 warnings emitted
19+

tests/ui/traits/object/pretty.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ error[E0308]: mismatched types
154154
--> $DIR/pretty.rs:41:56
155155
|
156156
LL | fn dyn_has_gat(x: &dyn HasGat<u8, Assoc<bool> = ()>) { x }
157-
| - ^ expected `()`, found `&dyn HasGat<u8>`
157+
| - ^ expected `()`, found `&dyn HasGat<u8, Assoc<bool> = ()>`
158158
| |
159-
| help: try adding a return type: `-> &dyn HasGat<u8>`
159+
| help: try adding a return type: `-> &dyn HasGat<u8, Assoc<bool> = ()>`
160160
|
161161
= note: expected unit type `()`
162-
found reference `&dyn HasGat<u8>`
162+
found reference `&dyn HasGat<u8, Assoc<bool> = ()>`
163163

164164
error: aborting due to 14 previous errors; 1 warning emitted
165165

0 commit comments

Comments
 (0)