Skip to content

unused_associated_type_bounds are usable and useful #125560

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
QuineDot opened this issue May 25, 2024 · 0 comments
Open

unused_associated_type_bounds are usable and useful #125560

QuineDot opened this issue May 25, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-dyn-trait Area: trait objects, vtable layout A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@QuineDot
Copy link

QuineDot commented May 25, 2024

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.

@QuineDot QuineDot added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 25, 2024
@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-dyn-trait Area: trait objects, vtable layout labels May 4, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-dyn-trait Area: trait objects, vtable layout A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants