Skip to content

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

Closed
@ivanbakel

Description

@ivanbakel

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions