Skip to content

Destructuring arguments in trait methods with bodies fails with a syntax error #55036

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
ivanbakel opened this issue Oct 13, 2018 · 5 comments
Closed

Comments

@ivanbakel
Copy link
Contributor

You can't destructure an argument in a trait method which has code in the body.

Minimal example

struct Foo { 
    bar: Bar
}

struct Bar;

trait Baz {
    fn foo_in_trait(Foo { bar }: Foo) {
        do_nothing();
    }
}

fn foo(Foo { bar }: Foo) {
    do_nothing();
}

fn do_nothing() {
}

Expectation

This should work fine. Destructuring an argument in a trait method seems totally reasonable.

Reality

The line fn foo_in_trait(Foo { bar }: Foo) { gives errors

error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `{`
 --> test.rs:8:25
  |
8 |     fn foo_in_trait(Foo { bar }: Foo) {
  |                         ^ expected one of 7 possible tokens here

error: expected one of `!`, `(`, `)`, `+`, `-`, `::`, `<`, `_`, `box`, `false`, `mut`, `ref`, or `true`, found `{`
 --> test.rs:8:25
  |
8 |     fn foo_in_trait(Foo { bar }: Foo) {
  |                         ^ expected one of 13 possible tokens here

error: expected one of `async`, `const`, `extern`, `fn`, `type`, `unsafe`, or `}`, found `:`
 --> test.rs:8:32
  |
8 |     fn foo_in_trait(Foo { bar }: Foo) {
  |                                ^ expected one of 7 possible tokens here

Meta

rustc version: rustc 1.31.0-nightly (5af0bb830 2018-10-10)

Let's speculate

This might be related to #53051, maybe? In particular, I would expect to see the error message from that pull request in this case, if this were validly invalid code.

@ehuss
Copy link
Contributor

ehuss commented Oct 13, 2018

This should work fine.

FWIW, your sample code is now valid in the 2018 edition.

@ivanbakel
Copy link
Contributor Author

I think it might be a regression brought about by the recent PR.

@ehuss
Copy link
Contributor

ehuss commented Oct 13, 2018

Oh, you're right, my bad!

It looks like the error was regressed by #54415.

@Centril
Copy link
Contributor

Centril commented Oct 13, 2018

cc rust-lang/rfcs#2522

@petrochenkov
Copy link
Contributor

Fixed in #57251

bors added a commit that referenced this issue Jan 2, 2019
syntax: Fix regression in diagnostics for patterns in trait method parameters

Fixes #55036
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants