Skip to content

mut keyword alone unexpectedly dereferences value in pattern matching #105647

Open
@yescallop

Description

@yescallop

I tried this code:

let [a] = &[()];
let [mut b] = &[()];
b = a;

I expected to see this happen: the mutable binding b has type &(), and thus the compilation succeeds without any error.

Instead, this happened: the mutable binding b has type (), and the compilation fails with error:

error[E0308]: mismatched types
  --> src\lib.rs:80:9
   |
79 |     let [mut b] = &[()];
   |          ----- expected due to the type of this binding
80 |     b = a;
   |         ^ expected `()`, found `&()`
   |
help: consider dereferencing the borrow
   |
80 |     b = *a;
   |         +

Meta

rustc --version --verbose:

rustc 1.68.0-nightly (37d7de337 2022-12-12)
binary: rustc
commit-hash: 37d7de337903a558dbeb1e82c844fe915ab8ff25
commit-date: 2022-12-12
host: x86_64-pc-windows-msvc
release: 1.68.0-nightly
LLVM version: 15.0.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.T-langRelevant to the language 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