Skip to content

Changing order of trait bounds changes available impls #75691

Closed
@Pratyush

Description

@Pratyush

I tried this code (playground link):

use core::ops::AddAssign;

trait TraitA<B>: Copy + AddAssign<B> + AddAssign<Self> {}

trait TraitC<B> { type Thing: TraitA<B>; }

fn use_c<B, C: TraitC<B>>(mut a: C::Thing, b: B) {
	a += b; // errors
	a += a;
}

I expected this to compile, because the trait bounds on TraitA are sufficient. Instead, rustc fails to find the AddAssign<B> bound for C::Thing, and errors out with the following error message:

  |
7 | fn use_c<B, C: TraitC<B>>(mut a: C::Thing, b: B) {
  |          - this type parameter
8 |     a += b; // errors
  |          ^ expected associated type, found type parameter `B`
  |
  = note: expected associated type `<C as TraitC<B>>::Thing`
              found type parameter `B`
help: consider further restricting this bound
  |
7 | fn use_c<B, C: TraitC<B> + TraitC<B, Thing = B>>(mut a: C::Thing, b: B) {
  |                          ^^^^^^^^^^^^^^^^^^^^^^

(Note that the help message, while technically correct, is actually unhelpful here.)

Meta

This bug occurs in the latest stable, beta, and nightly (this can be verified on the playground link).

This might be related to #41756.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler 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