The type_alias_bounds lint misfires when the trait bound is used by a const generic parameter · Issue #94398 · rust-lang/rust (original) (raw)

#![feature(generic_const_exprs)]

trait Trait { const N: usize; }

struct Struct;

type Alias<T: Trait> = Struct<{::N}>;

Compiling this code (Playground) produces the following diagnostics:

warning: bounds on generic parameters are not enforced in type aliases
 --> src/lib.rs:9:19
  |
9 | pub type Alias<T: Trait> = Struct<{<T as Trait>::N}>;
  |                   ^^^^^
  |
  = 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
  |
9 - pub type Alias<T: Trait> = Struct<{<T as Trait>::N}>;
9 + pub type Alias<T> = Struct<{<T as Trait>::N}>;
  | 

I expected to see this happen: The compiler should not have suggested removing : Trait from the type alias.

Instead, this happened: Removing : Trait as per the compiler's suggestion breaks the code.

error[E0277]: the trait bound `T: Trait` is not satisfied
 --> src/lib.rs:9:29
  |
9 | pub type Alias<T> = Struct<{<T as Trait>::N}>;
  |                             ^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `T`
  |
help: consider restricting type parameter `T`
  |
9 | pub type Alias<T: Trait> = Struct<{<T as Trait>::N}>;
  |                 +++++++

Meta

rustc --version --verbose:

rustc 1.61.0-nightly (532d3cda9 2022-02-23)
binary: rustc
commit-hash: 532d3cda90b8a729cd982548649d32803d265052
commit-date: 2022-02-23
host: aarch64-apple-darwin
release: 1.61.0-nightly
LLVM version: 14.0.0