rust-lang / rust-lang/rust

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

Open
#141,336 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-inference D-confusing T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example in the linked Rust Playground using the stated nightly version and compare the current E0277 messages with the desired diagnostic. Trace the compiler's type-inference and diagnostic path for if let Some(path) = None; done when this case reports that no appropriate type can be inferred rather than attributing the problem to an unsized local.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.