Skip to content

somewhat surprising match default binding interaction #46688

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
nikomatsakis opened this issue Dec 12, 2017 · 2 comments
Closed

somewhat surprising match default binding interaction #46688

nikomatsakis opened this issue Dec 12, 2017 · 2 comments
Labels
T-lang Relevant to the language team

Comments

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Dec 12, 2017

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.

@durka
Copy link
Contributor

durka commented Dec 12, 2017

To check my understanding, the #![feature(match_default_bindings)] turns (_, &b) into &(_, &ref b)?

@nikomatsakis
Copy link
Contributor Author

effectively yes

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
T-lang Relevant to the language team
Projects
None yet
Development

No branches or pull requests

2 participants