rust-lang / rust-lang/rust

false positive `unused_associated_type_bounds` bounds needed for code to compile

Open
#143,132 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-dyn-trait A-suggestion-diagnostics C-bug D-invalid-suggestion T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

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

Start with the provided check-pass reproducer and the unused_associated_type_bounds lint, then run it with --force-warn unused_associated_type_bounds. Investigate why removing the suggested associated type bounds changes impl identity and causes E0119 or E0207; done means the diagnostic no longer proposes a change that makes this valid reproducer fail to compile.

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
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.