unused_associated_type_bounds are usable and useful · Issue #125560 · rust-lang/rust (original) (raw)

Code

pub trait AssocOptOut { type Foo where Self: Sized; fn foo(&self) -> Self::Foo where Self: Sized; }

impl<T: Default> AssocOptOut for Box<dyn AssocOptOut<Foo = T>> { type Foo = T; fn foo(&self) -> Self::Foo { T::default() } }

Current output

warning: unnecessary associated type bound for not object safe associated type --> src/lib.rs:6:54 | 6 | impl<T: Default> AssocOptOut for Box<dyn AssocOptOut<Foo = T>> { | ^^^^^^^ help: remove this bound | = note: this associated type has a where Self: Sized bound. Thus, while the associated type can be specified, it cannot be used in any way, because trait objects are not Sized. = note: #[warn(unused_associated_type_bounds)] on by default

Desired output

warning: associated type bound for not object safe associated type --> src/lib.rs:6:54 | 6 | impl<T: Default> AssocOptOut for Box<dyn AssocOptOut<Foo = T>> { | ^^^^^^^ help: remove this bound | = note: this associated type has a where Self: Sized bound. Thus, while the associated type can be specified, the trait objects itself does not define the associated type (as trait objects are not Sized). = note: #[warn(not_object_safe_associated_type_bounds)] on by default

Rationale and extra context

<dyn AssocOptOut as AssocOutput>::Foo is not defined and cannot be used in any way.

However, the specified associated type can still be used, as demonstrated by the blanket implementation for Box<dyn AssocType<Foo = T>>.

Moreover, it is useful, as now Box<dyn AssocOptOut<Foo = String>> implements AssocOptOut, for example.

(So ideally the warning could be renamed. Or if enough use-cases emerge, made allow or change in scope.)

Other cases

No response

Rust Version

Playground, all channels.

Stable channel: Build using the Stable version: 1.78.0

Beta channel: Build using the Beta version: 1.79.0-beta.6 (2024-05-23 66eb3e404b81e916f5e6)

Nightly channel: Build using the Nightly version: 1.80.0-nightly (2024-05-24 36153f1a4e3162f0a143)

Anything else?

The lint was introduced in #112319.