Skip to content

Commit 591ec04

Browse files
committed
compiletest: Turn TestProps::require_annotations into a set
and further simplify its checking in runtest
1 parent f91308a commit 591ec04

File tree

2 files changed

+19
-46
lines changed

2 files changed

+19
-46
lines changed

src/tools/compiletest/src/header.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{HashMap, HashSet};
1+
use std::collections::HashSet;
22
use std::env;
33
use std::fs::File;
44
use std::io::BufReader;
@@ -198,7 +198,7 @@ pub struct TestProps {
198198
/// that don't otherwise want/need `-Z build-std`.
199199
pub add_core_stubs: bool,
200200
/// Whether line annotatins are required for the given error kind.
201-
pub require_annotations: HashMap<ErrorKind, bool>,
201+
pub dont_require_annotations: HashSet<ErrorKind>,
202202
}
203203

204204
mod directives {
@@ -301,13 +301,7 @@ impl TestProps {
301301
no_auto_check_cfg: false,
302302
has_enzyme: false,
303303
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, true),
310-
]),
304+
dont_require_annotations: Default::default(),
311305
}
312306
}
313307

@@ -585,8 +579,8 @@ impl TestProps {
585579
if let Some(err_kind) =
586580
config.parse_name_value_directive(ln, DONT_REQUIRE_ANNOTATIONS)
587581
{
588-
self.require_annotations
589-
.insert(ErrorKind::expect_from_user_str(&err_kind), false);
582+
self.dont_require_annotations
583+
.insert(ErrorKind::expect_from_user_str(&err_kind));
590584
}
591585
},
592586
);

src/tools/compiletest/src/runtest.rs

+14-35
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::common::{
2222
output_base_dir, output_base_name, output_testname_unique,
2323
};
2424
use crate::compute_diff::{DiffLine, make_diff, write_diff, write_filtered_diff};
25-
use crate::errors::{self, Error, ErrorKind};
25+
use crate::errors::{Error, ErrorKind};
2626
use crate::header::TestProps;
2727
use crate::read2::{Truncated, read2_abbreviated};
2828
use crate::util::{PathBufExt, add_dylib_path, logv, static_regex};
@@ -674,7 +674,7 @@ impl<'test> TestCx<'test> {
674674
}
675675
}
676676

677-
fn check_expected_errors(&self, expected_errors: Vec<errors::Error>, proc_res: &ProcRes) {
677+
fn check_expected_errors(&self, expected_errors: Vec<Error>, proc_res: &ProcRes) {
678678
debug!(
679679
"check_expected_errors: expected_errors={:?} proc_res.status={:?}",
680680
expected_errors, proc_res.status
@@ -709,9 +709,12 @@ impl<'test> TestCx<'test> {
709709
self.testpaths.file.display().to_string()
710710
};
711711

712-
let expect_help = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Help));
713-
let expect_note = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Note));
714-
let expect_sugg = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Suggestion));
712+
// Errors and warnings are always expected, other diagnostics are only expected
713+
// if one of them actually occurs in the test.
714+
let expected_kinds: HashSet<_> = [ErrorKind::Error, ErrorKind::Warning]
715+
.into_iter()
716+
.chain(expected_errors.iter().filter_map(|e| e.kind))
717+
.collect();
715718

716719
// Parse the JSON output from the compiler and extract out the messages.
717720
let actual_errors = json::parse_output(&diagnostic_file_name, &proc_res.stderr, proc_res);
@@ -737,13 +740,12 @@ impl<'test> TestCx<'test> {
737740
}
738741

739742
None => {
740-
// If the test is a known bug, don't require that the error is annotated
741-
if self.is_unexpected_compiler_message(
742-
&actual_error,
743-
expect_help,
744-
expect_note,
745-
expect_sugg,
746-
) {
743+
if actual_error.require_annotation
744+
&& actual_error.kind.map_or(false, |kind| {
745+
expected_kinds.contains(&kind)
746+
&& !self.props.dont_require_annotations.contains(&kind)
747+
})
748+
{
747749
self.error(&format!(
748750
"{}:{}: unexpected {}: '{}'",
749751
file_name,
@@ -800,29 +802,6 @@ impl<'test> TestCx<'test> {
800802
}
801803
}
802804

803-
/// Returns `true` if we should report an error about `actual_error`,
804-
/// which did not match any of the expected error.
805-
fn is_unexpected_compiler_message(
806-
&self,
807-
actual_error: &Error,
808-
expect_help: bool,
809-
expect_note: bool,
810-
expect_sugg: bool,
811-
) -> bool {
812-
actual_error.require_annotation
813-
&& actual_error.kind.map_or(false, |err_kind| {
814-
// If the test being checked doesn't contain any "help" or "note" annotations, then
815-
// we don't require annotating "help" or "note" (respecively) diagnostics at all.
816-
let default_require_annotations = self.props.require_annotations[&err_kind];
817-
match err_kind {
818-
ErrorKind::Help => expect_help && default_require_annotations,
819-
ErrorKind::Note => expect_note && default_require_annotations,
820-
ErrorKind::Suggestion => expect_sugg && default_require_annotations,
821-
_ => default_require_annotations,
822-
}
823-
})
824-
}
825-
826805
fn should_emit_metadata(&self, pm: Option<PassMode>) -> Emit {
827806
match (pm, self.props.fail_mode, self.config.mode) {
828807
(Some(PassMode::Check), ..) | (_, Some(FailMode::Check), Ui) => Emit::Metadata,

0 commit comments

Comments
 (0)