Skip to content

Confusing error message when coercing an array reference to a pointer of the wrong type #138836

Closed
@yshui

Description

@yshui

Code

fn main() {
    let a: [u8; 3] = [1,2,3];
    let b = &a;
    let c = b as *const [u32; 3];
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:4:13
  |
4 |     let c = b as *const [u32; 3];
  |             ^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `[u32; 3]`

Desired output

error[E0308]: mismatched types
 --> src/main.rs:4:13
  |
4 |     let c = b as *const [u32; 3];
  |             ^^^^^^^^^^^^^^^^^^^^ expected `[u8; 3]`, found `[u32; 3]`

// Alternatively: expected `u8`, found `u32`

Rationale and extra context

Current error message seems to suggest I am trying to convert a &u8 to *const [u32; 3] when I am actually trying to convert a &[u8; 3].

Other cases

Rust Version

1.87.0-nightly

(2025-03-21 be73c1f4617c97bce81b)

Anything else?

No response

Metadata

Metadata

Assignees

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

Issue actions