Skip to content

compiletest: Support optional error annotations #140586

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
wants to merge 1 commit into from
Closed
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
7 changes: 6 additions & 1 deletion src/doc/rustc-dev-guide/src/tests/ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ fn meow(_: [u8]) {}
//~| ERROR anonymous parameters
```

A question mark after the error kind (`//~ ERROR?`) means that the annotation error is not actually
required to be produced by the compiler, this is useful for target-dependent errors.

The space character between `//~` (or other variants) and the subsequent text is
negligible (i.e. there is no semantic difference between `//~ ERROR` and
`//~ERROR` although the former is more common in the codebase).
Expand Down Expand Up @@ -335,7 +338,9 @@ Use of `error-pattern` is not recommended in general.
For strict testing of compile time output, try to use the line annotations `//~` as much as
possible, including `//~?` annotations for diagnostics without spans.

If the compile time output is target dependent or too verbose, use directive
If the compile time output is target dependent, use optional annotations `//~ ERROR?`.

If the compile time output is too verbose, use directive
`//@ dont-require-annotations: <diagnostic-kind>` to make the line annotation checking
non-exhaustive.
Some of the compiler messages can stay uncovered by annotations in this mode.
Expand Down
13 changes: 8 additions & 5 deletions src/tools/compiletest/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ pub struct Error {
/// What kind of message we expect (e.g., warning, error, suggestion).
pub kind: ErrorKind,
pub msg: String,
/// For some `Error`s, like secondary lines of multi-line diagnostics, line annotations
/// are not mandatory, even if they would otherwise be mandatory for primary errors.
/// Only makes sense for "actual" errors, not for "expected" errors.
/// For `Error`s from the compiler, like secondary lines of multi-line diagnostics, line
/// annotations are not mandatory, even if they would otherwise be mandatory for primary errors.
/// For `Error`s from annotations this can be false if the annotation doesn't require to
/// annotate an actual error (useful for target-dependent errors).
pub require_annotation: bool,
}

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

let line_num_adjust = &captures["adjust"];
let (follow_prev, line_num) = if line_num_adjust == "|" {
Expand All @@ -188,7 +191,7 @@ fn parse_expected(
kind,
msg
);
Some((follow_prev, Error { line_num, kind, msg, require_annotation: true }))
Some((follow_prev, Error { line_num, kind, msg, require_annotation }))
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ impl<'test> TestCx<'test> {
let mut not_found = Vec::new();
// anything not yet found is a problem
for (index, expected_error) in expected_errors.iter().enumerate() {
if !found[index] {
if expected_error.require_annotation && !found[index] {
self.error(&format!(
"{}:{}: expected {} not found: {}",
file_name,
Expand Down
5 changes: 1 addition & 4 deletions tests/ui/cfg/cfg_false_no_std-2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Error, the linked empty library is `no_std` and doesn't provide a panic handler.

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

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

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

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

#![crate_type = "rlib"]

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

// FIXME: the error is reported on different lines on different targets
//FIXME~? ERROR values of the type `[u8; usize::MAX]` are too big for the target architecture
// The error is reported on different lines on different targets
//~? ERROR? values of the type `[u8; usize::MAX]` are too big for the target architecture
8 changes: 2 additions & 6 deletions tests/ui/panic-runtime/two-panic-runtimes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// ignore-tidy-linelength
//@ build-fail
//@ dont-require-annotations: ERROR
//@ dont-check-compiler-stderr
//@ aux-build:panic-runtime-unwind.rs
//@ aux-build:panic-runtime-unwind2.rs
Expand All @@ -16,7 +14,5 @@ extern crate panic_runtime_lang_items;
fn main() {}

//~? ERROR cannot link together two panic runtimes: panic_runtime_unwind and panic_runtime_unwind2
// FIXME: These errors are target-dependent, could be served by some "optional error" annotation
// instead of `dont-require-annotations`.
//FIXME~? ERROR the linked panic runtime `panic_runtime_unwind2` is not compiled with this crate's panic strategy `abort`
//FIXME~? ERROR the crate `panic_runtime_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`
//~? ERROR? the linked panic runtime `panic_runtime_unwind2` is not compiled with this crate's panic strategy `abort`
//~? ERROR? the crate `panic_runtime_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`
8 changes: 2 additions & 6 deletions tests/ui/panic-runtime/want-abort-got-unwind.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// ignore-tidy-linelength
//@ build-fail
//@ dont-require-annotations: ERROR
//@ dont-check-compiler-stderr
//@ aux-build:panic-runtime-unwind.rs
//@ compile-flags:-C panic=abort
Expand All @@ -10,7 +8,5 @@ extern crate panic_runtime_unwind;
fn main() {}

//~? ERROR the linked panic runtime `panic_runtime_unwind` is not compiled with this crate's panic strategy `abort`
// FIXME: These errors are target-dependent, could be served by some "optional error" annotation
// instead of `dont-require-annotations`.
//FIXME~? ERROR cannot link together two panic runtimes: panic_unwind and panic_runtime_unwind
//FIXME~? ERROR the crate `panic_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`
//~? ERROR? cannot link together two panic runtimes: panic_unwind and panic_runtime_unwind
//~? ERROR? the crate `panic_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`
8 changes: 2 additions & 6 deletions tests/ui/panic-runtime/want-abort-got-unwind2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// ignore-tidy-linelength
//@ build-fail
//@ dont-require-annotations: ERROR
//@ dont-check-compiler-stderr
//@ aux-build:panic-runtime-unwind.rs
//@ aux-build:wants-panic-runtime-unwind.rs
Expand All @@ -11,7 +9,5 @@ extern crate wants_panic_runtime_unwind;
fn main() {}

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