Skip to content

Commit 49b7294

Browse files
committed
compiletest: Support optional error annotations
that are not required to annotate an actual error, which is sometimes useful for target-dependent errors. The syntax is `//~ ERROR? message`.
1 parent f97b3c6 commit 49b7294

File tree

8 files changed

+26
-34
lines changed

8 files changed

+26
-34
lines changed

src/doc/rustc-dev-guide/src/tests/ui.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ fn meow(_: [u8]) {}
218218
//~| ERROR anonymous parameters
219219
```
220220

221+
A question mark after the error kind (`//~ ERROR?`) means that the annotation error is not actually
222+
required to be produced by the compiler, this is useful for target-dependent errors.
223+
221224
The space character between `//~` (or other variants) and the subsequent text is
222225
negligible (i.e. there is no semantic difference between `//~ ERROR` and
223226
`//~ERROR` although the former is more common in the codebase).
@@ -335,7 +338,9 @@ Use of `error-pattern` is not recommended in general.
335338
For strict testing of compile time output, try to use the line annotations `//~` as much as
336339
possible, including `//~?` annotations for diagnostics without spans.
337340

338-
If the compile time output is target dependent or too verbose, use directive
341+
If the compile time output is target dependent, use optional annotations `//~ ERROR?`.
342+
343+
If the compile time output is too verbose, use directive
339344
`//@ dont-require-annotations: <diagnostic-kind>` to make the line annotation checking
340345
non-exhaustive.
341346
Some of the compiler messages can stay uncovered by annotations in this mode.

src/tools/compiletest/src/errors.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ pub struct Error {
6565
/// What kind of message we expect (e.g., warning, error, suggestion).
6666
pub kind: ErrorKind,
6767
pub msg: String,
68-
/// For some `Error`s, like secondary lines of multi-line diagnostics, line annotations
69-
/// are not mandatory, even if they would otherwise be mandatory for primary errors.
70-
/// Only makes sense for "actual" errors, not for "expected" errors.
68+
/// For `Error`s from the compiler, like secondary lines of multi-line diagnostics, line
69+
/// annotations are not mandatory, even if they would otherwise be mandatory for primary errors.
70+
/// For `Error`s from annotations this can be false if the annotation doesn't require to
71+
/// annotate an actual error (useful for target-dependent errors).
7172
pub require_annotation: bool,
7273
}
7374

@@ -167,7 +168,9 @@ fn parse_expected(
167168
rest.split_once(|c: char| c != '_' && !c.is_ascii_alphabetic()).unwrap_or((rest, ""));
168169
let kind = ErrorKind::from_user_str(kind_str);
169170
let untrimmed_msg = &rest[kind_str.len()..];
170-
let msg = untrimmed_msg.strip_prefix(':').unwrap_or(untrimmed_msg).trim().to_owned();
171+
let require_annotation = !untrimmed_msg.starts_with('?');
172+
let msg = untrimmed_msg.strip_prefix('?').unwrap_or(untrimmed_msg);
173+
let msg = msg.strip_prefix(':').unwrap_or(msg).trim().to_owned();
171174

172175
let line_num_adjust = &captures["adjust"];
173176
let (follow_prev, line_num) = if line_num_adjust == "|" {
@@ -188,7 +191,7 @@ fn parse_expected(
188191
kind,
189192
msg
190193
);
191-
Some((follow_prev, Error { line_num, kind, msg, require_annotation: true }))
194+
Some((follow_prev, Error { line_num, kind, msg, require_annotation }))
192195
}
193196

194197
#[cfg(test)]

src/tools/compiletest/src/runtest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ impl<'test> TestCx<'test> {
753753
let mut not_found = Vec::new();
754754
// anything not yet found is a problem
755755
for (index, expected_error) in expected_errors.iter().enumerate() {
756-
if !found[index] {
756+
if expected_error.require_annotation && !found[index] {
757757
self.error(&format!(
758758
"{}:{}: expected {} not found: {}",
759759
file_name,

tests/ui/cfg/cfg_false_no_std-2.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Error, the linked empty library is `no_std` and doesn't provide a panic handler.
22

3-
//@ dont-require-annotations: ERROR
43
//@ dont-check-compiler-stderr
54
//@ aux-build: cfg_false_lib_no_std_before.rs
65

@@ -11,6 +10,4 @@ extern crate cfg_false_lib_no_std_before as _;
1110
fn main() {}
1211

1312
//~? ERROR `#[panic_handler]` function required, but not found
14-
// FIXME: This error is target-dependent, could be served by some "optional error" annotation
15-
// instead of `dont-require-annotations`.
16-
//FIXME~? ERROR unwinding panics are not supported without std
13+
//~? ERROR? unwinding panics are not supported without std
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
//FIXME~ ERROR values of the type `[u8; usize::MAX]` are too big for the target architecture
1+
//~ ERROR? values of the type `[u8; usize::MAX]` are too big for the target architecture
22
// Make sure the compiler does not ICE when trying to generate the debuginfo name of a type that
33
// causes a layout error.
44
// This version of the test already ICE'd before the commit that introduce the ICE described in
55
// https://github.com/rust-lang/rust/issues/94961.
66

7-
//@ compile-flags:-C debuginfo=2 --error-format=human
7+
//@ compile-flags:-C debuginfo=2
88
//@ build-fail
9-
//@ error-pattern: values of the type `[u8; usize::MAX]` are too big for the target architecture
109

1110
#![crate_type = "rlib"]
1211

@@ -18,5 +17,5 @@ pub fn foo() -> usize {
1817
std::mem::size_of::<Foo<u8>>()
1918
}
2019

21-
// FIXME: the error is reported on different lines on different targets
22-
//FIXME~? ERROR values of the type `[u8; usize::MAX]` are too big for the target architecture
20+
// The error is reported on different lines on different targets
21+
//~? ERROR? values of the type `[u8; usize::MAX]` are too big for the target architecture

tests/ui/panic-runtime/two-panic-runtimes.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// ignore-tidy-linelength
21
//@ build-fail
3-
//@ dont-require-annotations: ERROR
42
//@ dont-check-compiler-stderr
53
//@ aux-build:panic-runtime-unwind.rs
64
//@ aux-build:panic-runtime-unwind2.rs
@@ -16,7 +14,5 @@ extern crate panic_runtime_lang_items;
1614
fn main() {}
1715

1816
//~? ERROR cannot link together two panic runtimes: panic_runtime_unwind and panic_runtime_unwind2
19-
// FIXME: These errors are target-dependent, could be served by some "optional error" annotation
20-
// instead of `dont-require-annotations`.
21-
//FIXME~? ERROR the linked panic runtime `panic_runtime_unwind2` is not compiled with this crate's panic strategy `abort`
22-
//FIXME~? ERROR the crate `panic_runtime_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`
17+
//~? ERROR? the linked panic runtime `panic_runtime_unwind2` is not compiled with this crate's panic strategy `abort`
18+
//~? ERROR? the crate `panic_runtime_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// ignore-tidy-linelength
21
//@ build-fail
3-
//@ dont-require-annotations: ERROR
42
//@ dont-check-compiler-stderr
53
//@ aux-build:panic-runtime-unwind.rs
64
//@ compile-flags:-C panic=abort
@@ -10,7 +8,5 @@ extern crate panic_runtime_unwind;
108
fn main() {}
119

1210
//~? ERROR the linked panic runtime `panic_runtime_unwind` is not compiled with this crate's panic strategy `abort`
13-
// FIXME: These errors are target-dependent, could be served by some "optional error" annotation
14-
// instead of `dont-require-annotations`.
15-
//FIXME~? ERROR cannot link together two panic runtimes: panic_unwind and panic_runtime_unwind
16-
//FIXME~? ERROR the crate `panic_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`
11+
//~? ERROR? cannot link together two panic runtimes: panic_unwind and panic_runtime_unwind
12+
//~? ERROR? the crate `panic_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// ignore-tidy-linelength
21
//@ build-fail
3-
//@ dont-require-annotations: ERROR
42
//@ dont-check-compiler-stderr
53
//@ aux-build:panic-runtime-unwind.rs
64
//@ aux-build:wants-panic-runtime-unwind.rs
@@ -11,7 +9,5 @@ extern crate wants_panic_runtime_unwind;
119
fn main() {}
1210

1311
//~? ERROR the linked panic runtime `panic_runtime_unwind` is not compiled with this crate's panic strategy `abort`
14-
// FIXME: These errors are target-dependent, could be served by some "optional error" annotation
15-
// instead of `dont-require-annotations`.
16-
//FIXME~? ERROR cannot link together two panic runtimes: panic_unwind and panic_runtime_unwind
17-
//FIXME~? ERROR the crate `panic_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`
12+
//~? ERROR? cannot link together two panic runtimes: panic_unwind and panic_runtime_unwind
13+
//~? ERROR? the crate `panic_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`

0 commit comments

Comments
 (0)