rustc breaks code by suggestion to remove bounds · Issue #104918 · rust-lang/rust (original) (raw)

Given the following code:

#![allow(dead_code)]

// However, bounds are taken into account when accessing associated types trait Bound { type Assoc; } type T1<U: Bound> = U::Assoc; //~ WARN not enforced in type aliases type T2 where U: Bound = U::Assoc; //~ WARN not enforced in type aliases

fn main() {}

The current output is:

warning: bounds on generic parameters are not enforced in type aliases
 --> src/main.rs:5:12
  |
5 | type T1<U: Bound> = U::Assoc; //~ WARN not enforced in type aliases
  |            ^^^^^
  |
help: use fully disambiguated paths (i.e., `<T as Trait>::Assoc`) to refer to associated types in type aliases
 --> src/main.rs:5:21
  |
5 | type T1<U: Bound> = U::Assoc; //~ WARN not enforced in type aliases
  |                     ^^^^^^^^
  = note: `#[warn(type_alias_bounds)]` on by default
help: the bound will not be checked when the type alias is used, and should be removed
  |
5 - type T1<U: Bound> = U::Assoc; //~ WARN not enforced in type aliases
5 + type T1<U> = U::Assoc; //~ WARN not enforced in type aliases
  |

warning: where clauses are not enforced in type aliases
 --> src/main.rs:6:18
  |
6 | type T2<U> where U: Bound = U::Assoc;  //~ WARN not enforced in type aliases
  |                  ^^^^^^^^
  |
help: use fully disambiguated paths (i.e., `<T as Trait>::Assoc`) to refer to associated types in type aliases
 --> src/main.rs:6:29
  |
6 | type T2<U> where U: Bound = U::Assoc;  //~ WARN not enforced in type aliases
  |                             ^^^^^^^^
help: the clause will not be checked when the type alias is used, and should be removed
  |
6 - type T2<U> where U: Bound = U::Assoc;  //~ WARN not enforced in type aliases
6 + type T2<U>  = U::Assoc;  //~ WARN not enforced in type aliases
  |

If I apply this via cargo fix, the code no longer compilers 🙃

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0220]: associated type `Assoc` not found for `U`
 --> src/main.rs:5:17
  |
5 | type T1<U> = U::Assoc; //~ WARN not enforced in type aliases
  |                 ^^^^^ there is a similarly named associated type `Assoc` in the trait `Bound`

error[E0220]: associated type `Assoc` not found for `U`
 --> src/main.rs:6:18
  |
6 | type T2<U>  = U::Assoc;  //~ WARN not enforced in type aliases
  |                  ^^^^^ there is a similarly named associated type `Assoc` in the trait `Bound`

error: aborting due to 2 previous errors