Skip to content

Commit 9b3616c

Browse files
authored
Unrolled build for rust-lang#139489
Rollup merge of rust-lang#139489 - petrochenkov:noreqann, r=jieyouxu compiletest: Add directive `dont-require-annotations` for making matching on specific diagnostic kinds non-exhaustive. E.g. `//@ dont-require-annotations:ERROR`, like in the examples in this PR. cc rust-lang#139427 (comment) Closes rust-lang#132647 FYI `@BoxyUwU` since you've wanted this. r? `@jieyouxu`
2 parents 934880f + 1282912 commit 9b3616c

File tree

8 files changed

+57
-31
lines changed

8 files changed

+57
-31
lines changed

src/tools/compiletest/src/directive-list.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
2222
"dont-check-compiler-stderr",
2323
"dont-check-compiler-stdout",
2424
"dont-check-failure-status",
25+
"dont-require-annotations",
2526
"edition",
2627
"error-pattern",
2728
"exact-llvm-major-version",

src/tools/compiletest/src/errors.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::sync::OnceLock;
88
use regex::Regex;
99
use tracing::*;
1010

11-
#[derive(Copy, Clone, Debug, PartialEq)]
11+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
1212
pub enum ErrorKind {
1313
Help,
1414
Error,
@@ -40,6 +40,15 @@ impl ErrorKind {
4040
_ => return None,
4141
})
4242
}
43+
44+
pub fn expect_from_user_str(s: &str) -> ErrorKind {
45+
ErrorKind::from_user_str(s).unwrap_or_else(|| {
46+
panic!(
47+
"unexpected diagnostic kind `{s}`, expected \
48+
`ERROR`, `WARN`, `NOTE`, `HELP` or `SUGGESTION`"
49+
)
50+
})
51+
}
4352
}
4453

4554
impl fmt::Display for ErrorKind {

src/tools/compiletest/src/header.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashSet;
1+
use std::collections::{HashMap, HashSet};
22
use std::env;
33
use std::fs::File;
44
use std::io::BufReader;
@@ -11,6 +11,7 @@ use tracing::*;
1111

1212
use crate::common::{Config, Debugger, FailMode, Mode, PassMode};
1313
use crate::debuggers::{extract_cdb_version, extract_gdb_version};
14+
use crate::errors::ErrorKind;
1415
use crate::executor::{CollectedTestDesc, ShouldPanic};
1516
use crate::header::auxiliary::{AuxProps, parse_and_update_aux};
1617
use crate::header::needs::CachedNeedsConditions;
@@ -196,6 +197,8 @@ pub struct TestProps {
196197
/// Build and use `minicore` as `core` stub for `no_core` tests in cross-compilation scenarios
197198
/// that don't otherwise want/need `-Z build-std`.
198199
pub add_core_stubs: bool,
200+
/// Whether line annotatins are required for the given error kind.
201+
pub require_annotations: HashMap<ErrorKind, bool>,
199202
}
200203

201204
mod directives {
@@ -212,6 +215,7 @@ mod directives {
212215
pub const CHECK_RUN_RESULTS: &'static str = "check-run-results";
213216
pub const DONT_CHECK_COMPILER_STDOUT: &'static str = "dont-check-compiler-stdout";
214217
pub const DONT_CHECK_COMPILER_STDERR: &'static str = "dont-check-compiler-stderr";
218+
pub const DONT_REQUIRE_ANNOTATIONS: &'static str = "dont-require-annotations";
215219
pub const NO_PREFER_DYNAMIC: &'static str = "no-prefer-dynamic";
216220
pub const PRETTY_MODE: &'static str = "pretty-mode";
217221
pub const PRETTY_COMPARE_ONLY: &'static str = "pretty-compare-only";
@@ -297,6 +301,13 @@ impl TestProps {
297301
no_auto_check_cfg: false,
298302
has_enzyme: false,
299303
add_core_stubs: false,
304+
require_annotations: HashMap::from([
305+
(ErrorKind::Help, true),
306+
(ErrorKind::Note, true),
307+
(ErrorKind::Error, true),
308+
(ErrorKind::Warning, true),
309+
(ErrorKind::Suggestion, false),
310+
]),
300311
}
301312
}
302313

@@ -570,6 +581,13 @@ impl TestProps {
570581
config.set_name_directive(ln, NO_AUTO_CHECK_CFG, &mut self.no_auto_check_cfg);
571582

572583
self.update_add_core_stubs(ln, config);
584+
585+
if let Some(err_kind) =
586+
config.parse_name_value_directive(ln, DONT_REQUIRE_ANNOTATIONS)
587+
{
588+
self.require_annotations
589+
.insert(ErrorKind::expect_from_user_str(&err_kind), false);
590+
}
573591
},
574592
);
575593

src/tools/compiletest/src/runtest.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,6 @@ impl<'test> TestCx<'test> {
709709
self.testpaths.file.display().to_string()
710710
};
711711

712-
// If the testcase being checked contains at least one expected "help"
713-
// message, then we'll ensure that all "help" messages are expected.
714-
// Otherwise, all "help" messages reported by the compiler will be ignored.
715-
// This logic also applies to "note" messages.
716712
let expect_help = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Help));
717713
let expect_note = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Note));
718714

@@ -800,22 +796,24 @@ impl<'test> TestCx<'test> {
800796
}
801797

802798
/// Returns `true` if we should report an error about `actual_error`,
803-
/// which did not match any of the expected error. We always require
804-
/// errors/warnings to be explicitly listed, but only require
805-
/// helps/notes if there are explicit helps/notes given.
799+
/// which did not match any of the expected error.
806800
fn is_unexpected_compiler_message(
807801
&self,
808802
actual_error: &Error,
809803
expect_help: bool,
810804
expect_note: bool,
811805
) -> bool {
812806
actual_error.require_annotation
813-
&& match actual_error.kind {
814-
Some(ErrorKind::Help) => expect_help,
815-
Some(ErrorKind::Note) => expect_note,
816-
Some(ErrorKind::Error) | Some(ErrorKind::Warning) => true,
817-
Some(ErrorKind::Suggestion) | None => false,
818-
}
807+
&& actual_error.kind.map_or(false, |err_kind| {
808+
// If the test being checked doesn't contain any "help" or "note" annotations, then
809+
// we don't require annotating "help" or "note" (respecively) diagnostics at all.
810+
let default_require_annotations = self.props.require_annotations[&err_kind];
811+
match err_kind {
812+
ErrorKind::Help => expect_help && default_require_annotations,
813+
ErrorKind::Note => expect_note && default_require_annotations,
814+
_ => default_require_annotations,
815+
}
816+
})
819817
}
820818

821819
fn should_emit_metadata(&self, pm: Option<PassMode>) -> Emit {

tests/ui/cfg/cfg_false_no_std-2.rs

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

3-
//@ compile-flags: --error-format=human
4-
//@ error-pattern: `#[panic_handler]` function required, but not found
3+
//@ dont-require-annotations:ERROR
54
//@ dont-check-compiler-stderr
65
//@ aux-build: cfg_false_lib_no_std_before.rs
76

@@ -11,6 +10,7 @@ extern crate cfg_false_lib_no_std_before as _;
1110

1211
fn main() {}
1312

14-
// FIXME: The second error is target-dependent.
15-
//FIXME~? ERROR `#[panic_handler]` function required, but not found
13+
//~? 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`.
1616
//FIXME~? ERROR unwinding panics are not supported without std

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// ignore-tidy-linelength
22
//@ build-fail
3-
//@ compile-flags: --error-format=human
4-
//@ error-pattern: cannot link together two panic runtimes: panic_runtime_unwind and panic_runtime_unwind2
3+
//@ dont-require-annotations:ERROR
54
//@ dont-check-compiler-stderr
65
//@ aux-build:panic-runtime-unwind.rs
76
//@ aux-build:panic-runtime-unwind2.rs
@@ -16,7 +15,8 @@ extern crate panic_runtime_lang_items;
1615

1716
fn main() {}
1817

19-
// FIXME: The second and third errors are target-dependent.
20-
//FIXME~? ERROR cannot link together two panic runtimes: panic_runtime_unwind and panic_runtime_unwind2
18+
//~? 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`.
2121
//FIXME~? ERROR the linked panic runtime `panic_runtime_unwind2` is not compiled with this crate's panic strategy `abort`
2222
//FIXME~? ERROR the crate `panic_runtime_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// ignore-tidy-linelength
22
//@ build-fail
3-
//@ compile-flags: --error-format=human
4-
//@ error-pattern: the linked panic runtime `panic_runtime_unwind` is not compiled with this crate's panic strategy `abort`
3+
//@ dont-require-annotations:ERROR
54
//@ dont-check-compiler-stderr
65
//@ aux-build:panic-runtime-unwind.rs
76
//@ compile-flags:-C panic=abort
@@ -10,7 +9,8 @@ extern crate panic_runtime_unwind;
109

1110
fn main() {}
1211

13-
// FIXME: The first and third errors are target-dependent.
12+
//~? 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`.
1415
//FIXME~? ERROR cannot link together two panic runtimes: panic_unwind and panic_runtime_unwind
15-
//FIXME~? ERROR the linked panic runtime `panic_runtime_unwind` is not compiled with this crate's panic strategy `abort`
1616
//FIXME~? ERROR the crate `panic_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// ignore-tidy-linelength
22
//@ build-fail
3-
//@ compile-flags: --error-format=human
4-
//@ error-pattern: the linked panic runtime `panic_runtime_unwind` is not compiled with this crate's panic strategy `abort`
3+
//@ dont-require-annotations:ERROR
54
//@ dont-check-compiler-stderr
65
//@ aux-build:panic-runtime-unwind.rs
76
//@ aux-build:wants-panic-runtime-unwind.rs
@@ -11,7 +10,8 @@ extern crate wants_panic_runtime_unwind;
1110

1211
fn main() {}
1312

14-
// FIXME: The first and third errors are target-dependent.
13+
//~? 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`.
1516
//FIXME~? ERROR cannot link together two panic runtimes: panic_unwind and panic_runtime_unwind
16-
//FIXME~? ERROR the linked panic runtime `panic_runtime_unwind` is not compiled with this crate's panic strategy `abort`
1717
//FIXME~? ERROR the crate `panic_unwind` requires panic strategy `unwind` which is incompatible with this crate's strategy of `abort`

0 commit comments

Comments
 (0)