Only include associated type bounds for Self:Sized associated types if they are provided
This commit is contained in:
parent
fd5b1583fe
commit
2f71d0a243
5 changed files with 55 additions and 8 deletions
|
|
@ -178,9 +178,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
.filter(|item| item.kind == ty::AssocKind::Type)
|
||||
// No RPITITs -- they're not dyn-compatible for now.
|
||||
.filter(|item| !item.is_impl_trait_in_trait())
|
||||
// If the associated type has a `where Self: Sized` bound,
|
||||
// we do not need to constrain the associated type.
|
||||
.filter(|item| !tcx.generics_require_sized_self(item.def_id))
|
||||
.map(|item| (item.def_id, trait_ref)),
|
||||
);
|
||||
}
|
||||
|
|
@ -259,7 +256,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
if let Some(assoc) = projection_bounds.get(&key) {
|
||||
Some(*assoc)
|
||||
} else {
|
||||
missing_assoc_types.insert(key);
|
||||
// If the associated type has a `where Self: Sized` bound,
|
||||
// we do not need to constrain the associated type.
|
||||
if !tcx.generics_require_sized_self(key.0) {
|
||||
missing_assoc_types.insert(key);
|
||||
}
|
||||
None
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -720,7 +720,10 @@ impl<'tcx> Ty<'tcx> {
|
|||
repr: DynKind,
|
||||
) -> Ty<'tcx> {
|
||||
if cfg!(debug_assertions) {
|
||||
let projection_count = obj.projection_bounds().count();
|
||||
let projection_count = obj
|
||||
.projection_bounds()
|
||||
.filter(|item| !tcx.generics_require_sized_self(item.item_def_id()))
|
||||
.count();
|
||||
let expected_count: usize = obj
|
||||
.principal_def_id()
|
||||
.into_iter()
|
||||
|
|
|
|||
24
tests/ui/traits/object/constrain-via-unnecessary-bound.rs
Normal file
24
tests/ui/traits/object/constrain-via-unnecessary-bound.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
//@ check-pass
|
||||
|
||||
// Regression test for <https://github.com/rust-lang/rust/issues/140645>.
|
||||
// Test that we lower impossible-to-satisfy associated type bounds, which
|
||||
// may for example constrain impl parameters.
|
||||
|
||||
pub trait Other {}
|
||||
|
||||
pub trait Trait {
|
||||
type Assoc
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
impl Other for dyn Trait {}
|
||||
// `dyn Trait<Assoc = ()>` is a different "nominal type" than `dyn Traiat`.
|
||||
impl Other for dyn Trait<Assoc = ()> {}
|
||||
//~^ WARN unnecessary associated type bound for dyn-incompatible associated type
|
||||
|
||||
// I hope it's clear that `dyn Trait` (w/o `Assoc`) wouldn't match this impl.
|
||||
impl<T> dyn Trait<Assoc = T> {}
|
||||
//~^ WARN unnecessary associated type bound for dyn-incompatible associated type
|
||||
|
||||
fn main() {}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
warning: unnecessary associated type bound for dyn-incompatible associated type
|
||||
--> $DIR/constrain-via-unnecessary-bound.rs:17:26
|
||||
|
|
||||
LL | impl Other for dyn Trait<Assoc = ()> {}
|
||||
| ^^^^^^^^^^ help: remove this bound
|
||||
|
|
||||
= 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`
|
||||
= note: `#[warn(unused_associated_type_bounds)]` on by default
|
||||
|
||||
warning: unnecessary associated type bound for dyn-incompatible associated type
|
||||
--> $DIR/constrain-via-unnecessary-bound.rs:21:19
|
||||
|
|
||||
LL | impl<T> dyn Trait<Assoc = T> {}
|
||||
| ^^^^^^^^^ help: remove this bound
|
||||
|
|
||||
= 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`
|
||||
|
||||
warning: 2 warnings emitted
|
||||
|
||||
|
|
@ -154,12 +154,12 @@ error[E0308]: mismatched types
|
|||
--> $DIR/pretty.rs:41:56
|
||||
|
|
||||
LL | fn dyn_has_gat(x: &dyn HasGat<u8, Assoc<bool> = ()>) { x }
|
||||
| - ^ expected `()`, found `&dyn HasGat<u8>`
|
||||
| - ^ expected `()`, found `&dyn HasGat<u8, Assoc<bool> = ()>`
|
||||
| |
|
||||
| help: try adding a return type: `-> &dyn HasGat<u8>`
|
||||
| help: try adding a return type: `-> &dyn HasGat<u8, Assoc<bool> = ()>`
|
||||
|
|
||||
= note: expected unit type `()`
|
||||
found reference `&dyn HasGat<u8>`
|
||||
found reference `&dyn HasGat<u8, Assoc<bool> = ()>`
|
||||
|
||||
error: aborting due to 14 previous errors; 1 warning emitted
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue