Skip to content

false positive unused_associated_type_bounds bounds needed for code to compile #143132

Open
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn unused_associated_type_bounds

this code:

//@ 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 Trait`.
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() {}

caused the following diagnostics:

    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
    Checking _constrain-via-unnecessary-bound v0.1.0 (/tmp/icemaker_global_tempdir.GjvWA1sVwdV5/icemaker_clippyfix_tempdir.kMucq7kHadM8/_constrain-via-unnecessary-bound)
warning: unnecessary associated type bound for dyn-incompatible associated type
  --> src/main.rs:17:26
   |
17 | 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: requested on the command line with `--force-warn unused-associated-type-bounds`

warning: unnecessary associated type bound for dyn-incompatible associated type
  --> src/main.rs:21:19
   |
21 | 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: `_constrain-via-unnecessary-bound` (bin "_constrain-via-unnecessary-bound") generated 2 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.31s

However after applying these diagnostics, the resulting code:

//@ 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 Trait`.
impl Other for dyn Trait<> {}
//~^ 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<> {}
//~^ WARN unnecessary associated type bound for dyn-incompatible associated type

fn main() {}

no longer compiled:

    Checking _constrain-via-unnecessary-bound v0.1.0 (/tmp/icemaker_global_tempdir.GjvWA1sVwdV5/icemaker_clippyfix_tempdir.kMucq7kHadM8/_constrain-via-unnecessary-bound)
error[E0119]: conflicting implementations of trait `Other` for type `(dyn Trait + 'static)`
  --> src/main.rs:17:1
   |
15 | impl Other for dyn Trait {}
   | ------------------------ first implementation here
16 | // `dyn Trait<Assoc = ()>` is a different "nominal type" than `dyn Trait`.
17 | impl Other for dyn Trait<> {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(dyn Trait + 'static)`

error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
  --> src/main.rs:21:6
   |
21 | impl<T> dyn Trait<> {}
   |      ^ unconstrained type parameter

Some errors have detailed explanations: E0119, E0207.
For more information about an error, try `rustc --explain E0119`.
error: could not compile `_constrain-via-unnecessary-bound` (bin "_constrain-via-unnecessary-bound") due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_constrain-via-unnecessary-bound` (bin "_constrain-via-unnecessary-bound" test) due to 2 previous errors

Version:

rustc 1.90.0-nightly (bdaba05a9 2025-06-27)
binary: rustc
commit-hash: bdaba05a953eb5abeba0011cdda2560d157aed2e
commit-date: 2025-06-27
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-dyn-traitArea: trait objects, vtable layoutA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions