Skip to content

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

Closed
@yvt

Description

@yvt
Contributor
#![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

Activity

ChayimFriedman2

ChayimFriedman2 commented on Feb 27, 2022

@ChayimFriedman2
Contributor

Should the compiler not warn or not err when the bound is missing?

added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
on Jun 5, 2024
self-assigned this
on Jun 5, 2024
fmease

fmease commented on Jun 5, 2024

@fmease
Member

This should relatively easy to fix. I've assigned myself to this issue as well as all(?) other L-type_alias_bounds Lint: type_alias_bounds issues because I consider it part of my work on lazy_type_alias.

added a commit that references this issue on Jul 26, 2024

Rollup merge of rust-lang#126575 - fmease:update-lint-type_alias_boun…

ceae371
added a commit that references this issue on Jul 26, 2024
eac4883
added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
and removed
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
on Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-lintsArea: 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-featuresThis issue requires the use of incomplete features.requires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @yvt@fmease@BoxyUwU@ChayimFriedman2@workingjubilee

    Issue actions

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