Skip to content

Changing order of trait bounds changes available impls #75691

Closed
@Pratyush

Description

@Pratyush
Contributor

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.

Activity

Pratyush

Pratyush commented on Sep 17, 2020

@Pratyush
ContributorAuthor

Update: this compiles successfully with chalk; the output of

rustc +nightly --crate-name test_chalk --edition=2018 src/lib.rs --crate-type lib --emit=dep-info,metadata -C embed-bitcode=no -C debuginfo=2 -C metadata=1ff6265fd5bdefac -C extra-filename=-1ff6265fd5bdefac --out-dir /tmp/test_chalk/target/debug/deps -C incremental=/tmp/test_chalk/target/debug/incremental -L dependency=/tmp/test_chalk/target/debug/deps -C target-cpu=native -Z chalk=yes

is

warning: function is never used: `use_c`
 --> src/lib.rs:7:4
  |
7 | fn use_c<B, C: TraitC<B>>(mut a: C::Thing, b: B) {
  |    ^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted
tesuji

tesuji commented on Sep 17, 2020

@tesuji
added
A-associated-itemsArea: Associated items (types, constants & functions)
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Sep 17, 2020
Pratyush

Pratyush commented on Oct 10, 2020

@Pratyush
ContributorAuthor

Update: this compiles on the latest nightly. Is there a way to figure out what changed?

tesuji

tesuji commented on Oct 11, 2020

@tesuji
Contributor

You could try out bisect-rustc to find which nightly and possibly which PR fix the problem: https://github.com/rust-lang/cargo-bisect-rustc/blob/master/TUTORIAL.md

Pratyush

Pratyush commented on Oct 11, 2020

@Pratyush
ContributorAuthor

Ahh I think it was this (see “ambiguous projection bounds”) #73905 (comment)

Pratyush

Pratyush commented on Nov 16, 2020

@Pratyush
ContributorAuthor

Can confirm that this is fixed due to #73905; closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @Pratyush@tesuji@rustbot

        Issue actions

          Changing order of trait bounds changes available impls · Issue #75691 · rust-lang/rust