Skip to content

The type_alias_bounds lint misfires when the trait bound is used by a const generic parameter #94398

Closed

Description

#![feature(generic_const_exprs)]

trait Trait {
    const N: usize;
}

struct Struct<const N: usize>;

type Alias<T: Trait> = Struct<{<T as Trait>::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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

A-lintArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.F-generic_const_exprs`#![feature(generic_const_exprs)]`L-type_alias_boundsLint: type_alias_boundsrequires-incomplete-featuresrequires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions