Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

somewhat surprising match default binding interaction #46688

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, which will review and decide on the PR/issue.

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.

@nikomatsakis nikomatsakis added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Dec 12, 2017
@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants