Skip to content

Commit 2f71d0a

Browse files
compiler-errorspietroalbini
authored andcommitted
Only include associated type bounds for Self:Sized associated types if they are provided
1 parent fd5b158 commit 2f71d0a

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
@@ -178,9 +178,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
178178
.filter(|item| item.kind == ty::AssocKind::Type)
179179
// No RPITITs -- they're not dyn-compatible for now.
180180
.filter(|item| !item.is_impl_trait_in_trait())
181-
// If the associated type has a `where Self: Sized` bound,
182-
// we do not need to constrain the associated type.
183-
.filter(|item| !tcx.generics_require_sized_self(item.def_id))
184181
.map(|item| (item.def_id, trait_ref)),
185182
);
186183
}
@@ -259,7 +256,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
259256
if let Some(assoc) = projection_bounds.get(&key) {
260257
Some(*assoc)
261258
} else {
262-
missing_assoc_types.insert(key);
259+
// If the associated type has a `where Self: Sized` bound,
260+
// we do not need to constrain the associated type.
261+
if !tcx.generics_require_sized_self(key.0) {
262+
missing_assoc_types.insert(key);
263+
}
263264
None
264265
}
265266
})

compiler/rustc_middle/src/ty/sty.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,10 @@ impl<'tcx> Ty<'tcx> {
720720
repr: DynKind,
721721
) -> Ty<'tcx> {
722722
if cfg!(debug_assertions) {
723-
let projection_count = obj.projection_bounds().count();
723+
let projection_count = obj
724+
.projection_bounds()
725+
.filter(|item| !tcx.generics_require_sized_self(item.item_def_id()))
726+
.count();
724727
let expected_count: usize = obj
725728
.principal_def_id()
726729
.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)