Skip to content

compiletest: Support matching diagnostics on lines below #139100

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

Merged
merged 1 commit into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/doc/rustc-dev-guide/src/tests/ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ several ways to match the message with the line (see the examples below):
* `~|`: Associates the error level and message with the *same* line as the
*previous comment*. This is more convenient than using multiple carets when
there are multiple messages associated with the same line.
* `~v`: Associates the error level and message with the *next* error
annotation line. Each symbol (`v`) that you add adds a line to this, so `~vvv`
is three lines below the error annotation line.
* `~?`: Used to match error levels and messages with errors not having line
information. These can be placed on any line in the test file, but are
conventionally placed at the end.
Expand Down Expand Up @@ -273,6 +276,18 @@ fn main() {
//~| ERROR this pattern has 1 field, but the corresponding tuple struct has 3 fields [E0023]
```

#### Positioned above error line

Use the `//~v` idiom with number of v's in the string to indicate the number
of lines below. This is typically used in lexer or parser tests matching on errors like unclosed
delimiter or unclosed literal happening at the end of file.

```rust,ignore
// ignore-tidy-trailing-newlines
//~v ERROR this file contains an unclosed delimiter
fn main((ؼ
```

#### Error without line information

Use `//~?` to match an error without line information.
Expand Down
8 changes: 7 additions & 1 deletion src/tools/compiletest/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,17 @@ fn parse_expected(
// //~|
// //~^
// //~^^^^^
// //~v
// //~vvvvv
// //~?
// //[rev1]~
// //[rev1,rev2]~^^
static RE: OnceLock<Regex> = OnceLock::new();

let captures = RE
.get_or_init(|| Regex::new(r"//(?:\[(?P<revs>[\w\-,]+)])?~(?P<adjust>\?|\||\^*)").unwrap())
.get_or_init(|| {
Regex::new(r"//(?:\[(?P<revs>[\w\-,]+)])?~(?P<adjust>\?|\||[v\^]*)").unwrap()
})
.captures(line)?;

match (test_revision, captures.name("revs")) {
Expand Down Expand Up @@ -164,6 +168,8 @@ fn parse_expected(
(true, Some(last_nonfollow_error.expect("encountered //~| without preceding //~^ line")))
} else if line_num_adjust == "?" {
(false, None)
} else if line_num_adjust.starts_with('v') {
(false, Some(line_num + line_num_adjust.len()))
} else {
(false, Some(line_num - line_num_adjust.len()))
};
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/issues/issue-103451.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ error-pattern: this file contains an unclosed delimiter
struct R { }
//~vv ERROR this file contains an unclosed delimiter
struct S {
x: [u8; R
2 changes: 1 addition & 1 deletion tests/ui/parser/issues/issue-10636-2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ error-pattern: mismatched closing delimiter: `}`
// FIXME(31528) we emit a bunch of silly errors here due to continuing past the
// first one. This would be easy-ish to address by better recovery in tokenisation.

//~vvvvv ERROR mismatched closing delimiter: `}`
pub fn trace_option(option: Option<isize>) {
option.map(|some| 42;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Fixed in #66054.
// ignore-tidy-trailing-newlines
//@ error-pattern: this file contains an unclosed delimiter
//@ error-pattern: aborting due to 1 previous error
//~v ERROR this file contains an unclosed delimiter
#[Ѕ
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this file contains an unclosed delimiter
--> $DIR/issue-58094-missing-right-square-bracket.rs:5:4
--> $DIR/issue-58094-missing-right-square-bracket.rs:4:4
|
LL | #[Ѕ
| - ^
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/parser/issues/issue-62524.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore-tidy-trailing-newlines
//@ error-pattern: aborting due to 1 previous error

#![allow(uncommon_codepoints)]

//~vv ERROR this file contains an unclosed delimiter
y![
Ϥ,
2 changes: 1 addition & 1 deletion tests/ui/parser/issues/issue-62524.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this file contains an unclosed delimiter
--> $DIR/issue-62524.rs:6:3
--> $DIR/issue-62524.rs:7:3
|
LL | y![
| - unclosed delimiter
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/parser/issues/issue-62554.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@ error-pattern:this file contains an unclosed delimiter

fn main() {}

//~v ERROR this file contains an unclosed delimiter
fn foo(u: u8) { if u8 macro_rules! u8 { (u6) => { fn uuuuuuuuuuu() { use s loo mod u8 {
2 changes: 1 addition & 1 deletion tests/ui/parser/issues/issue-62554.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this file contains an unclosed delimiter
--> $DIR/issue-62554.rs:5:89
--> $DIR/issue-62554.rs:4:89
|
LL | fn foo(u: u8) { if u8 macro_rules! u8 { (u6) => { fn uuuuuuuuuuu() { use s loo mod u8 {
| - - - - -^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/issues/issue-62894.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Regression test for #62894, shouldn't crash.
//@ error-pattern: this file contains an unclosed delimiter

//~vvv ERROR this file contains an unclosed delimiter
fn f() { assert_eq!(f(), (), assert_eq!(assert_eq!

fn main() {}
4 changes: 3 additions & 1 deletion tests/ui/parser/issues/issue-62973.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// ignore-tidy-trailing-newlines
//@ error-pattern: aborting due to 3 previous errors

fn main() {}

//~vvv ERROR mismatched closing delimiter: `)`
//~vv ERROR mismatched closing delimiter: `)`
//~vvv ERROR this file contains an unclosed delimiter
fn p() { match s { v, E { [) {) }


6 changes: 3 additions & 3 deletions tests/ui/parser/issues/issue-62973.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
error: mismatched closing delimiter: `)`
--> $DIR/issue-62973.rs:6:27
--> $DIR/issue-62973.rs:8:27
|
LL | fn p() { match s { v, E { [) {) }
| ^^ mismatched closing delimiter
| |
| unclosed delimiter

error: mismatched closing delimiter: `)`
--> $DIR/issue-62973.rs:6:30
--> $DIR/issue-62973.rs:8:30
|
LL | fn p() { match s { v, E { [) {) }
| ^^ mismatched closing delimiter
| |
| unclosed delimiter

error: this file contains an unclosed delimiter
--> $DIR/issue-62973.rs:8:2
--> $DIR/issue-62973.rs:10:2
|
LL | fn p() { match s { v, E { [) {) }
| - - - - missing open `(` for this delimiter
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/parser/issues/issue-63116.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// fixed by #66361
//@ error-pattern: aborting due to 2 previous errors
//~vv ERROR mismatched closing delimiter: `]`
//~v ERROR this file contains an unclosed delimiter
impl W <s(f;Y(;]
4 changes: 2 additions & 2 deletions tests/ui/parser/issues/issue-63116.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error: mismatched closing delimiter: `]`
--> $DIR/issue-63116.rs:3:14
--> $DIR/issue-63116.rs:4:14
|
LL | impl W <s(f;Y(;]
| ^ ^ mismatched closing delimiter
| |
| unclosed delimiter

error: this file contains an unclosed delimiter
--> $DIR/issue-63116.rs:3:18
--> $DIR/issue-63116.rs:4:18
|
LL | impl W <s(f;Y(;]
| - -^
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/parser/issues/issue-63135.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
//@ error-pattern: this file contains an unclosed delimiter
//@ error-pattern: aborting due to 1 previous error
//~v ERROR this file contains an unclosed delimiter
fn i(n{...,f #
2 changes: 1 addition & 1 deletion tests/ui/parser/issues/issue-63135.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this file contains an unclosed delimiter
--> $DIR/issue-63135.rs:3:16
--> $DIR/issue-63135.rs:2:16
|
LL | fn i(n{...,f #
| - - ^
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/parser/issues/issue-81804.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//@ error-pattern: this file contains an unclosed delimiter
//@ error-pattern: this file contains an unclosed delimiter

fn main() {}

//~vv ERROR mismatched closing delimiter: `}`
//~v ERROR this file contains an unclosed delimiter
fn p([=(}
4 changes: 2 additions & 2 deletions tests/ui/parser/issues/issue-81804.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error: mismatched closing delimiter: `}`
--> $DIR/issue-81804.rs:6:8
--> $DIR/issue-81804.rs:5:8
|
LL | fn p([=(}
| ^^ mismatched closing delimiter
| |
| unclosed delimiter

error: this file contains an unclosed delimiter
--> $DIR/issue-81804.rs:6:11
--> $DIR/issue-81804.rs:5:11
|
LL | fn p([=(}
| -- ^
Expand Down
7 changes: 2 additions & 5 deletions tests/ui/parser/issues/issue-81827.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//@ error-pattern: this file contains an unclosed delimiter
//@ error-pattern: mismatched closing delimiter: `]`

#![crate_name="0"]



fn main() {}

//~vv ERROR mismatched closing delimiter: `]`
//~v ERROR this file contains an unclosed delimiter
fn r()->i{0|{#[cfg(r(0{]0
4 changes: 2 additions & 2 deletions tests/ui/parser/issues/issue-81827.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: mismatched closing delimiter: `]`
--> $DIR/issue-81827.rs:10:23
--> $DIR/issue-81827.rs:7:23
|
LL | fn r()->i{0|{#[cfg(r(0{]0
| - ^^ mismatched closing delimiter
Expand All @@ -8,7 +8,7 @@ LL | fn r()->i{0|{#[cfg(r(0{]0
| closing delimiter possibly meant for this

error: this file contains an unclosed delimiter
--> $DIR/issue-81827.rs:10:27
--> $DIR/issue-81827.rs:7:27
|
LL | fn r()->i{0|{#[cfg(r(0{]0
| - - - ^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/issues/issue-84104.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//@ error-pattern: this file contains an unclosed delimiter
//~v ERROR this file contains an unclosed delimiter
#[i=i::<ښܖ<
2 changes: 1 addition & 1 deletion tests/ui/parser/issues/issue-84148-2.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//@ error-pattern: this file contains an unclosed delimiter
//~v ERROR this file contains an unclosed delimiter
fn f(t:for<>t?
3 changes: 1 addition & 2 deletions tests/ui/parser/issues/issue-88770.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Regression test for the ICE described in #88770.

//@ error-pattern:this file contains an unclosed delimiter

//~vvvv ERROR this file contains an unclosed delimiter
fn m(){print!("",(c for&g
u
e
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/issues/issue-88770.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this file contains an unclosed delimiter
--> $DIR/issue-88770.rs:8:3
--> $DIR/issue-88770.rs:7:3
|
LL | fn m(){print!("",(c for&g
| - - - unclosed delimiter
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/mbe_missing_right_paren.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// ignore-tidy-trailing-newlines
//@ error-pattern: this file contains an unclosed delimiter
//~v ERROR this file contains an unclosed delimiter
macro_rules! abc(ؼ
3 changes: 1 addition & 2 deletions tests/ui/parser/missing_right_paren.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ignore-tidy-trailing-newlines
//@ error-pattern: this file contains an unclosed delimiter
//@ error-pattern: aborting due to 1 previous error
//~v ERROR this file contains an unclosed delimiter
fn main((ؼ
2 changes: 1 addition & 1 deletion tests/ui/parser/missing_right_paren.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this file contains an unclosed delimiter
--> $DIR/missing_right_paren.rs:4:11
--> $DIR/missing_right_paren.rs:3:11
|
LL | fn main((ؼ
| -- ^
Expand Down
4 changes: 1 addition & 3 deletions tests/ui/parser/unbalanced-doublequote.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//@ error-pattern: unterminated double quote string


//~vv ERROR unterminated double quote string
fn main() {
"
}
2 changes: 1 addition & 1 deletion tests/ui/parser/unbalanced-doublequote.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0765]: unterminated double quote string
--> $DIR/unbalanced-doublequote.rs:5:5
--> $DIR/unbalanced-doublequote.rs:3:5
|
LL | / "
LL | | }
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/use-unclosed-brace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//@ error-pattern: this file contains an unclosed delimiter
use foo::{bar, baz;

use std::fmt::Display;
Expand All @@ -7,4 +6,5 @@ mod bar { }

mod baz { }

//~v ERROR this file contains an unclosed delimiter
fn main() {}
Loading