Skip to content

Confusing Error when accidentally constructing a type alias with array of fixed size for struct that is generic over an array size #129484

Not planned
@DaniD3v

Description

@DaniD3v

Code

pub type ChunkBlocks = GenericChunkBlocks<123>;

pub struct GenericChunkBlocks<const SIZE: usize> {
    blocks: [u16; SIZE],
}

impl<const BLOCK_COUNT: usize> Default for GenericChunkBlocks<BLOCK_COUNT> {
    fn default() -> Self {
        ChunkBlocks { // Note how I accidentally used ChunkBlocks instead of Self.
            blocks: [0; BLOCK_COUNT],
        }
    }
}

Current output

Compiling compiler-error v0.1.0 (/home/notyou/devel/compiler-error)
error[E0308]: mismatched types
  --> src/lib.rs:10:21
   |
10 |             blocks: [0; BLOCK_COUNT],
   |                     ^^^^^^^^^^^^^^^^ expected `123`, found `BLOCK_COUNT`
   |
   = note: expected array `[u16; 123]`
              found array `[u16; BLOCK_COUNT]`

error[E0308]: mismatched types
  --> src/lib.rs:9:9
   |
8  |       fn default() -> Self {
   |                       ---- expected `GenericChunkBlocks<BLOCK_COUNT>` because of return type
9  | /         ChunkBlocks {
10 | |             blocks: [0; BLOCK_COUNT],
11 | |         }
   | |_________^ expected `BLOCK_COUNT`, found `123`
   |
   = note: expected struct `GenericChunkBlocks<BLOCK_COUNT>`
              found struct `GenericChunkBlocks<123>`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `compiler-error` (lib) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `compiler-error` (lib test) due to 2 previous errors

Desired output

error[E0308]: mismatched types
  --> src/lib.rs:9:9
   |
8  |       fn default() -> Self {
   |                       ---- expected `GenericChunkBlocks<BLOCK_COUNT>` because of return type
9  | /         ChunkBlocks {
10 | |             blocks: [0; BLOCK_COUNT],
11 | |         }
   | |_________^ expected `BLOCK_COUNT`, found `123`
   |
   = note: expected struct `GenericChunkBlocks<BLOCK_COUNT>`
              found struct `ChunkBlocks`

or
   = note: GenericChunkBlocks<123> is a type alias: `ChunkBlocks`

Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.82.0-nightly (13a52890d 2024-08-14)
binary: rustc
commit-hash: 13a52890dde8cfeb95069d77c223ac37c0cf3a46
commit-date: 2024-08-14
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 19.1.0

Anything else?

No response

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Aug 23, 2024
fmease

fmease commented on Aug 24, 2024

@fmease
Member

Duplicate of #17164

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-diagnosticsArea: Messages for errors, warnings, and lintsT-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

        @fmease@DaniD3v

        Issue actions

          Confusing Error when accidentally constructing a type alias with array of fixed size for struct that is generic over an array size · Issue #129484 · rust-lang/rust