rust-lang / rust-lang/rust

match &[first, ..more] leads down a suggestion garden path

Open
#130,639 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-patterns A-resolve A-suggestion-diagnostics D-invalid-suggestion D-papercut T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
struct Algorithms<'a>(&'a Vec<String>);
impl<'a> fmt::Display for Algorithms<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.0[..] {
            &[] => f.write_str("<default>"),
            &[one] => f.write_str(&one),
            &[first, ..more] => {
                f.write_str(&first)?;
                for rec in more {
                    write!(f, ", then {}", rec)?;
                }
                Ok(())
            },
        }
    }
}
Current output
   Compiling zram-generator v1.1.2 (/home/nabijaczleweli/code/systemd-zram-generator)
error[E0425]: cannot find value `more` in this scope
   --> src/config.rs:205:24
    |
205 |             &[first, ..more] => {
    |                        ^^^^ not found in this scope
    |
help: if you meant to collect the rest of the slice in `more`, use the at operator
    |
205 |             &[first, more @ ..] => {
    |                      ~~~~~~~~~

okay so I do that and get

   Compiling zram-generator v1.1.2 (/home/nabijaczleweli/code/systemd-zram-generator)
error[E0277]: the size for values of type `[String]` cannot be known at compilation time
   --> src/config.rs:205:22
    |
205 |             &[first, more @ ..] => {
    |                      ^^^^^^^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `[String]`
    = note: all local variables must have a statically known size
    = help: unsized locals are gated as an unstable feature

..? definitely sub-optimal; the first thing I tried was

error: `..` patterns are not allowed here
   --> src/config.rs:205:30
    |
205 |             &[first, &more @ ..] => {
    |                              ^^
    |
    = note: only allowed in tuple, tuple struct, and slice patterns

so I did &[first, ref more @ ..] which worked

Desired output

idk. probably suggest with the ref if appropriate?

Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.80.1 (3f5fd8dd4 2024-08-06)
binary: rustc
commit-hash: 3f5fd8dd41153bc5fdca9427e9e05be2c767ba23
commit-date: 2024-08-06
host: x86_64-unknown-linux-gnu
release: 1.80.1
LLVM version: 18.1.7

Anything else?

No response

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 pattern in src/config.rs with rustc 1.80.1, comparing the suggestions for the three slice-pattern forms shown in the report. Trace the compiler diagnostic responsible for the suggestion; done means it points directly to a valid, compiling form without the unsized-local detour.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.