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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DaniD3v opened this issue Aug 23, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@DaniD3v
Copy link

DaniD3v commented Aug 23, 2024

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

@DaniD3v DaniD3v added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 23, 2024
@fmease
Copy link
Member

fmease commented Aug 24, 2024

Duplicate of #17164

@fmease fmease marked this as a duplicate of #17164 Aug 24, 2024
@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants