Skip to content

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

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
yshui opened this issue Mar 22, 2025 · 1 comment · Fixed by #138962
Closed
Assignees
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

@yshui
Copy link
Contributor

yshui commented Mar 22, 2025

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

@yshui yshui 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 Mar 22, 2025
@xizheyin
Copy link
Contributor

@rustbot claim

ChrisDenton added a commit to ChrisDenton/rust that referenced this issue Apr 13, 2025
…errors

Expect an array when expected and acutal types are both arrays during cast

Closes rust-lang#138836
ChrisDenton added a commit to ChrisDenton/rust that referenced this issue Apr 13, 2025
…errors

Expect an array when expected and acutal types are both arrays during cast

Closes rust-lang#138836
jhpratt added a commit to jhpratt/rust that referenced this issue Apr 13, 2025
…errors

Expect an array when expected and acutal types are both arrays during cast

Closes rust-lang#138836
@bors bors closed this as completed in 60c76b9 Apr 14, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 14, 2025
Rollup merge of rust-lang#138962 - xizheyin:issue-138836, r=compiler-errors

Expect an array when expected and acutal types are both arrays during cast

Closes rust-lang#138836
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

Successfully merging a pull request may close this issue.

2 participants