Skip to content

Confusing error with doc comments in parameter list #54801

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

Closed
lzybkr opened this issue Oct 3, 2018 · 1 comment
Closed

Confusing error with doc comments in parameter list #54801

lzybkr opened this issue Oct 3, 2018 · 1 comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST

Comments

@lzybkr
Copy link
Contributor

lzybkr commented Oct 3, 2018

The following code:

pub fn f(
    /// Comment
    id: u8)
{}

Results in the following error:

error: expected pattern, found `/// Comment`
 --> src/lib.rs:2:5
  |
2 |     /// Comment
  |     ^^^^^^^^^^^ expected pattern

At best, the error is confusing. Ideally it should not be an error, but if doc comments are not allowed in a parameter list, the error should be clearer.

@Havvy Havvy added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST labels Oct 4, 2018
@Havvy
Copy link
Contributor

Havvy commented Oct 4, 2018

Attributes, including doc comments, are not allowed on function parameters. Two more test cases:

fn foo(#[allow(dead_code)] id: i32) {}

fn bar(id: #[allow(dead_code)] i32) {}

with current output of

error: expected pattern, found `#`
 --> src/lib.rs:3:8
  |
L | fn foo(#[allow(dead_code)] id: i32) {}
  |        ^ expected pattern

error: expected type, found `#`
 --> src/lib.rs:5:12
  |
L | fn bar(id: #[allow(dead_code)] i32) {}
  |            ^

Ideally it should instead say:

error: expected pattern, found `#`
 --> src/lib.rs:3:8
  |
3 | fn foo(#[allow(dead_code)] id: i32) {}
  |        ^ attributes are not allowed on function parameters.

error: expected type, found `#`
 --> src/lib.rs:5:12
  |
5 | fn bar(id: #[allow(dead_code)] i32) {}
  |            ^ attributes are not allowed on function parameter types.

And then s/attributes/doc comments/ if it's a doc comment.

@Havvy Havvy added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Oct 4, 2018
bors added a commit that referenced this issue Oct 29, 2018
Custom diagnostic when trying to doc comment argument

When writing

```
pub fn f(
    /// Comment
    id: u8,
) {}
```

Produce a targeted diagnostic

```
error: documentation comments cannot be applied to method arguments
  --> $DIR/fn-arg-doc-comment.rs:2:5
   |
LL |     /// Comment
   |     ^^^^^^^^^^^ doc comments are not allowed here
```

Fix #54801.
bors added a commit that referenced this issue Nov 5, 2018
Custom diagnostic when trying to doc comment argument

When writing

```
pub fn f(
    /// Comment
    id: u8,
) {}
```

Produce a targeted diagnostic

```
error: documentation comments cannot be applied to method arguments
  --> $DIR/fn-arg-doc-comment.rs:2:5
   |
LL |     /// Comment
   |     ^^^^^^^^^^^ doc comments are not allowed here
```

Fix #54801.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST
Projects
None yet
Development

No branches or pull requests

2 participants