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

Expand error annotations section with examples #724

Merged
merged 2 commits into from May 28, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 64 additions & 3 deletions src/tests/adding.md
Expand Up @@ -193,15 +193,76 @@ source.

Error annotations specify the errors that the compiler is expected to
emit. They are "attached" to the line in source where the error is
located.
located. Error annotations are considered during tidy lints of line
chrissimpkins marked this conversation as resolved.
Show resolved Hide resolved
length and should be formatted according to tidy requirements.

The error annotation definition and source line definition association
is defined with the following set of idioms:

* `~`: Associates the following error level and message with the
current line
* `~|`: Associates the following error level and message with the same
line as the previous comment
* `~^`: Associates the following error level and message with the
previous line. Each caret (`^`) that you add adds a line to this, so
`~^^^^^^^` is seven lines up.
previous error annotation line. Each caret (`^`) that you add adds
a line to this, so `~^^^` is three lines above the error annotation
line.

### Error annotation examples

Here are examples of error annotations on different lines of UI test
source.

#### Positioned on error line

Use the `//~ ERROR` idiom:

```rust, ignore
chrissimpkins marked this conversation as resolved.
Show resolved Hide resolved
fn main() {
let x = (1, 2, 3);
match x {
(_a, _x @ ..) => {} //~ ERROR `_x @` is not allowed in a tuple
_ => {}
}
}
```

#### Positioned below error line

Use the `//~^` idiom with number of carets in the string to indicate the
number of lines above. In the example below, the error line is four
lines above the error annotation line so four carets are included in
the annotation.

```rust, ignore
chrissimpkins marked this conversation as resolved.
Show resolved Hide resolved
fn main() {
let x = (1, 2, 3);
match x {
(_a, _x @ ..) => {} // <- the error is on this line
_ => {}
}
}
//~^^^^ ERROR `_x @` is not allowed in a tuple
```

#### Use same error line as defined on error annotation line above

Use the `//~|` idiom to define the same error line as
the error annotation line above:

```rust, ignore
chrissimpkins marked this conversation as resolved.
Show resolved Hide resolved
struct Binder(i32, i32, i32);

fn main() {
let x = Binder(1, 2, 3);
match x {
Binder(_a, _x @ ..) => {} // <- the error is on this line
_ => {}
}
}
//~^^^^ ERROR `_x @` is not allowed in a tuple struct
//~| ERROR this pattern has 1 field, but the corresponding tuple struct has 3 fields [E0023]
```

The error levels that you can have are:

Expand Down