Skip to content

type inference failure confusing diagnostic let Some(x) = None for [u8] #141336

Open
@midnightveil

Description

@midnightveil

Code

fn func(x: &[u8]) {
    std::hint::black_box(x);
}

fn test() {
    if let Some(path) = None {
        func(&path);
    }
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/lib.rs:6:17
  |
6 |     if let Some(path) = None {
  |                 ^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
   --> src/lib.rs:6:12
    |
6   |     if let Some(path) = None {
    |            ^^^^^^^^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `[u8]`
note: required by a bound in `Some`
   --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:589:17
    |
589 | pub enum Option<T> {
    |                 ^ required by this bound in `Some`
...
597 |     Some(#[stable(feature = "rust1", since = "1.0.0")] T),
    |     ---- required by a bound in this tuple variant

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
   --> src/lib.rs:6:25
    |
6   |     if let Some(path) = None {
    |                         ^^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `[u8]`
note: required by a bound in `None`
   --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:589:17
    |
589 | pub enum Option<T> {
    |                 ^ required by this bound in `None`
...
593 |     None,
    |     ---- required by a bound in this unit variant

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 3 previous errors

Desired output

I'm not sure; but the issue is not that path doesn't have a size known at compile time but more that we can't infer an appropriate type for path

Rationale and extra context

No response

Other cases

Rust Version

nightly: 2025-05-20 bc821528634632b4ff8d from rust playground

Anything else?

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=ac6219162fb8bfab6a527be73b0db480

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceD-confusingDiagnostics: Confusing error or lint that should be reworked.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