Auto merge of #55451 - estebank:arg-doc, r=pnkfelix

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.
This commit is contained in:
bors 2018-11-05 16:36:18 +00:00
commit af791bb8f4
15 changed files with 187 additions and 33 deletions

View file

@ -27,7 +27,9 @@ error: expected one of `.`, `?`, `{`, or an operator, found `'b`
--> $DIR/label_break_value_illegal_uses.rs:28:17
|
LL | match false 'b: {} //~ ERROR expected one of `.`, `?`, `{`, or an operator
| ^^ expected one of `.`, `?`, `{`, or an operator here
| ----- ^^ expected one of `.`, `?`, `{`, or an operator here
| |
| while parsing this match expression
error: aborting due to 4 previous errors

View file

@ -0,0 +1,37 @@
pub fn f(
/// Comment
//~^ ERROR documentation comments cannot be applied to method arguments
//~| NOTE doc comments are not allowed here
id: u8,
/// Other
//~^ ERROR documentation comments cannot be applied to method arguments
//~| NOTE doc comments are not allowed here
a: u8,
) {}
fn foo(#[allow(dead_code)] id: i32) {}
//~^ ERROR attributes cannot be applied to method arguments
//~| NOTE attributes are not allowed here
fn bar(id: #[allow(dead_code)] i32) {}
//~^ ERROR attributes cannot be applied to a method argument's type
//~| NOTE attributes are not allowed here
fn main() {
// verify that the parser recovered and properly typechecked the args
f("", "");
//~^ ERROR mismatched types
//~| NOTE expected u8, found reference
//~| NOTE expected
//~| ERROR mismatched types
//~| NOTE expected u8, found reference
//~| NOTE expected
foo("");
//~^ ERROR mismatched types
//~| NOTE expected i32, found reference
//~| NOTE expected
bar("");
//~^ ERROR mismatched types
//~| NOTE expected i32, found reference
//~| NOTE expected
}

View file

@ -0,0 +1,63 @@
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
error: documentation comments cannot be applied to method arguments
--> $DIR/fn-arg-doc-comment.rs:6:5
|
LL | /// Other
| ^^^^^^^^^ doc comments are not allowed here
error: attributes cannot be applied to method arguments
--> $DIR/fn-arg-doc-comment.rs:12:8
|
LL | fn foo(#[allow(dead_code)] id: i32) {}
| ^^^^^^^^^^^^^^^^^^^ attributes are not allowed here
error: attributes cannot be applied to a method argument's type
--> $DIR/fn-arg-doc-comment.rs:16:12
|
LL | fn bar(id: #[allow(dead_code)] i32) {}
| ^^^^^^^^^^^^^^^^^^^ attributes are not allowed here
error[E0308]: mismatched types
--> $DIR/fn-arg-doc-comment.rs:22:7
|
LL | f("", "");
| ^^ expected u8, found reference
|
= note: expected type `u8`
found type `&'static str`
error[E0308]: mismatched types
--> $DIR/fn-arg-doc-comment.rs:22:11
|
LL | f("", "");
| ^^ expected u8, found reference
|
= note: expected type `u8`
found type `&'static str`
error[E0308]: mismatched types
--> $DIR/fn-arg-doc-comment.rs:29:9
|
LL | foo("");
| ^^ expected i32, found reference
|
= note: expected type `i32`
found type `&'static str`
error[E0308]: mismatched types
--> $DIR/fn-arg-doc-comment.rs:33:9
|
LL | bar("");
| ^^ expected i32, found reference
|
= note: expected type `i32`
found type `&'static str`
error: aborting due to 8 previous errors
For more information about this error, try `rustc --explain E0308`.

View file

@ -11,5 +11,6 @@
// compile-flags: -Z parse-only
impl S {
fn f(*, a: u8) -> u8 {} //~ ERROR expected pattern, found `*`
fn f(*, a: u8) -> u8 {}
//~^ ERROR expected argument name, found `*`
}

View file

@ -1,8 +1,8 @@
error: expected pattern, found `*`
error: expected argument name, found `*`
--> $DIR/issue-33413.rs:14:10
|
LL | fn f(*, a: u8) -> u8 {} //~ ERROR expected pattern, found `*`
| ^ expected pattern
LL | fn f(*, a: u8) -> u8 {}
| ^ expected argument name
error: aborting due to previous error

View file

@ -12,7 +12,7 @@
fn main() {
let foo =
match
match //~ NOTE while parsing this match expression
Some(4).unwrap_or_else(5)
//~^ NOTE expected one of `.`, `?`, `{`, or an operator here
; //~ NOTE unexpected token

View file

@ -1,8 +1,11 @@
error: expected one of `.`, `?`, `{`, or an operator, found `;`
--> $DIR/match-refactor-to-expr.rs:18:9
|
LL | match
| ----- help: try removing this `match`
LL | match //~ NOTE while parsing this match expression
| -----
| |
| while parsing this match expression
| help: try removing this `match`
LL | Some(4).unwrap_or_else(5)
| - expected one of `.`, `?`, `{`, or an operator here
LL | //~^ NOTE expected one of `.`, `?`, `{`, or an operator here

View file

@ -10,4 +10,5 @@
// compile-flags: -Z parse-only
fn f(+x: isize) {} //~ ERROR expected pattern, found `+`
fn f(+x: isize) {}
//~^ ERROR expected argument name, found `+`

View file

@ -1,8 +1,8 @@
error: expected pattern, found `+`
error: expected argument name, found `+`
--> $DIR/removed-syntax-mode.rs:13:6
|
LL | fn f(+x: isize) {} //~ ERROR expected pattern, found `+`
| ^ expected pattern
LL | fn f(+x: isize) {}
| ^ expected argument name
error: aborting due to previous error

View file

@ -2,7 +2,9 @@ error: expected expression, found reserved keyword `try`
--> $DIR/try-block-in-match.rs:16:11
|
LL | match try { false } { _ => {} } //~ ERROR expected expression, found reserved keyword `try`
| ^^^ expected expression
| ----- ^^^ expected expression
| |
| while parsing this match expression
error: aborting due to previous error