Skip to content

somewhat surprising match default binding interaction #46688

Closed
@nikomatsakis

Description

@nikomatsakis

It took me a while to figure out the compilation error in this example (try it yourself):

#![feature(match_default_bindings)]

fn surprise(x: i32) { }

fn main() {
  let x = &(1, &2);
  let (_, &b) = x;
  surprise(b);
}

Here you get:

error[E0308]: mismatched types
 --> src/main.rs:9:12
  |
9 |   surprise(b);
  |            ^
  |            |
  |            expected i32, found &{integer}
  |            help: consider dereferencing the borrow: `*b`
  |
  = note: expected type `i32`
             found type `&{integer}`

Naturally I tried changing the pattern &&b. That gives you:

error[E0308]: mismatched types
 --> src/main.rs:7:12
  |
7 |   let (_, &&b) = x;
  |            ^^ expected integral variable, found reference
  |
  = note: expected type `{integer}`
             found type `&_`
  = help: did you mean `b: &{integer}`?

What the heck?

What's going on here is that the filter is giving us an &(i32, &i32). When we skip the first &, we get into "ref by default" mode, but when we explicitly acknowledge the second one, we do not get back into "by value" mode. That's kind of annoying.

Metadata

Metadata

Assignees

No one assigned

    Labels

    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