Skip to content
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

Allow destructuring parameters in trait function declarations #53046

Open
algonomicon1 opened this issue Aug 3, 2018 · 3 comments
Open

Allow destructuring parameters in trait function declarations #53046

algonomicon1 opened this issue Aug 3, 2018 · 3 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@algonomicon1
Copy link

The current syntax rules do not allow for trait functions to destructure parameters:

#![allow(unused)]

// This works
fn foo((x, y): (i32, i32)) {
}

trait Bar {
    // This does not work
    fn bar((x, y): (i32, i32));
}

Playground
Compiling the above outputs this:

error: expected one of `)` or `,`, found `:`
 --> src/main.rs:7:18
  |
7 |     fn bar((x, y): (i32, i32));
  |                  ^ expected one of `)` or `,` here

error: expected one of `!`, `&&`, `&`, `(`, `)`, `*`, `<`, `?`, `[`, `_`, `dyn`, `extern`, `fn`, `for`, `impl`, `unsafe`, or lifetime, found `:`
 --> src/main.rs:7:18
  |
7 |     fn bar((x, y): (i32, i32));
  |                  ^ expected one of 17 possible tokens here

error[E0601]: `main` function not found in crate `playground`
  |
  = note: consider adding a `main` function to `src/main.rs`

The destructuring of trait function parameters should be allowed for consistency and readability.

@varkor
Copy link
Member

varkor commented Aug 3, 2018

I think this is a consequence of trait method declarations not requiring argument names. Patterns as arguments are only accepted if names are required. I imagine there's some parsing issue here. Perhaps something like:

trait Trait {
	fn foo(&Bar); // is `&Bar` a pattern or type here?
}

However, in the presence of :, it might be possible to disambiguate (as : is not allowed in patterns).

Edit: see rust-lang/rfcs#1351 (comment) and #41686.

@varkor
Copy link
Member

varkor commented Aug 4, 2018

Interestingly, there's a better error message for this, E0642:

error[E0642]: patterns aren't allowed in methods without bodies
 --> fn_inconsistency.rs:6:12
  |
6 |     fn bar((x, y): (i32, i32));
  |            ^^^^^^

error: aborting due to previous error

However, it's unlisted on the Error Index and isn't emitted because the parser fails before that point.

Edit: Looks like it wasn't being type-checked properly before it was forbidden.

@varkor varkor added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Aug 4, 2018
kennytm added a commit to kennytm/rust that referenced this issue Aug 12, 2018
…ts-error, r=petrochenkov

Emit error for pattern arguments in trait methods

The error and check for this already existed, but the parser didn't try to parse trait method arguments as patterns, so the error was never emitted. This surfaces the error, so we get better errors than simple parse errors.

This improves the error message described in rust-lang#53046.
bors added a commit that referenced this issue Aug 13, 2018
…=petrochenkov

Emit error for pattern arguments in trait methods

The error and check for this already existed, but the parser didn't try to parse trait method arguments as patterns, so the error was never emitted. This surfaces the error, so we get better errors than simple parse errors.

This improves the error message described in #53046.

r? @petrochenkov
@steveklabnik
Copy link
Member

Triage: there is a very small change now:

error[E0642]: patterns aren't allowed in functions without bodies
 --> src/lib.rs:9:12
  |
9 |     fn bar((x, y): (i32, i32));
  |            ^^^^^^ pattern not allowed in function without body

(now says function because it's not a method)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

No branches or pull requests

3 participants