rust-lang / rust-lang/rust

unused_associated_type_bounds are usable and useful

Open
#125,560 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-dyn-trait A-lints T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the supplied Rust Playground example first and compare the current and desired diagnostics. Then trace the unused_associated_type_bounds lint introduced in #112319. Done means the diagnostic distinguishes a specified associated type from one usable through a trait object and uses the agreed lint name.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.