Skip to content

Suggest .try_into().unwrap() or as _ for const parameters of the wrong type (e.g., in array types) if applicable and possible on stable #104949

Open
@scvalex

Description

@scvalex

I tried this code:

pub struct MaxLengthString<const N: u8> {
    len: u8,
    buf: [u8; N],
}

And I got this compilation error:

error[E0308]: mismatched types
 --> src/lib.rs:3:15
  |
3 |     buf: [u8; N],
  |               ^ expected `usize`, found `u8`

For more information about this error, try `rustc --explain E0308`.

This works fine if the const parameter is of type usize, but I think it should work for the other unsigned types that are smaller than usize. Or at the very least, maybe there should be a different error message that says this is definitely not supported.

Meta

rustc --version --verbose:

rustc 1.67.0-nightly (8681d4cff 2022-11-25)
binary: rustc
commit-hash: 8681d4cffcd23bbe619984ab62772a91827a40dc
commit-date: 2022-11-25
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-arrayArea: `[T; N]`A-const-genericsArea: const generics (parameters and arguments)A-diagnosticsArea: Messages for errors, warnings, and lintsD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.S-blockedStatus: Blocked on something else such as an RFC or other implementation work.T-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