Skip to content

Commit 84a2e23

Browse files
authored
Unrolled build for rust-lang#123753
Rollup merge of rust-lang#123753 - Urgau:compiletest-trailing-directive, r=jieyouxu compiletest: error when finding a trailing directive This PR introduce a supplementary check that when checking for a compiletest directive, will also check that the next "word" after that directive is not also by itself a directive. This is done to avoid situations like this `//@ only-linux only-x86_64` where one might think that both directives are being taken into account while in fact the second in a comment, and so was ignored, until now. Related to rust-lang#123730 cc ``@scottmcm`` r? ``@jieyouxu``
2 parents 05ccc49 + acd4e94 commit 84a2e23

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/tools/compiletest/src/header.rs

+31-5
Original file line numberDiff line numberDiff line change
@@ -935,16 +935,25 @@ struct HeaderLine<'ln> {
935935
pub(crate) struct CheckDirectiveResult<'ln> {
936936
is_known_directive: bool,
937937
directive_name: &'ln str,
938+
trailing_directive: Option<&'ln str>,
938939
}
939940

940-
// Returns `(is_known_directive, directive_name)`.
941941
pub(crate) fn check_directive(directive_ln: &str) -> CheckDirectiveResult<'_> {
942-
let directive_name =
943-
directive_ln.split_once([':', ' ']).map(|(pre, _)| pre).unwrap_or(directive_ln);
942+
let (directive_name, post) = directive_ln.split_once([':', ' ']).unwrap_or((directive_ln, ""));
943+
944+
let trailing = post.trim().split_once(' ').map(|(pre, _)| pre).unwrap_or(post);
945+
let trailing_directive = {
946+
// 1. is the directive name followed by a space? (to exclude `:`)
947+
matches!(directive_ln.get(directive_name.len()..), Some(s) if s.starts_with(" "))
948+
// 2. is what is after that directive also a directive (ex: "only-x86 only-arm")
949+
&& KNOWN_DIRECTIVE_NAMES.contains(&trailing)
950+
}
951+
.then_some(trailing);
944952

945953
CheckDirectiveResult {
946954
is_known_directive: KNOWN_DIRECTIVE_NAMES.contains(&directive_name),
947955
directive_name: directive_ln,
956+
trailing_directive,
948957
}
949958
}
950959

@@ -1014,7 +1023,8 @@ fn iter_header(
10141023
if testfile.extension().map(|e| e == "rs").unwrap_or(false) {
10151024
let directive_ln = non_revisioned_directive_line.trim();
10161025

1017-
let CheckDirectiveResult { is_known_directive, .. } = check_directive(directive_ln);
1026+
let CheckDirectiveResult { is_known_directive, trailing_directive, .. } =
1027+
check_directive(directive_ln);
10181028

10191029
if !is_known_directive {
10201030
*poisoned = true;
@@ -1028,6 +1038,21 @@ fn iter_header(
10281038

10291039
return;
10301040
}
1041+
1042+
if let Some(trailing_directive) = &trailing_directive {
1043+
*poisoned = true;
1044+
1045+
eprintln!(
1046+
"error: detected trailing compiletest test directive `{}` in {}:{}\n \
1047+
help: put the trailing directive in it's own line: `//@ {}`",
1048+
trailing_directive,
1049+
testfile.display(),
1050+
line_number,
1051+
trailing_directive,
1052+
);
1053+
1054+
return;
1055+
}
10311056
}
10321057

10331058
it(HeaderLine {
@@ -1051,7 +1076,8 @@ fn iter_header(
10511076

10521077
let rest = rest.trim_start();
10531078

1054-
let CheckDirectiveResult { is_known_directive, directive_name } = check_directive(rest);
1079+
let CheckDirectiveResult { is_known_directive, directive_name, .. } =
1080+
check_directive(rest);
10551081

10561082
if is_known_directive {
10571083
*poisoned = true;

src/tools/compiletest/src/header/tests.rs

+21
Original file line numberDiff line numberDiff line change
@@ -667,3 +667,24 @@ fn test_non_rs_unknown_directive_not_checked() {
667667
);
668668
assert!(!poisoned);
669669
}
670+
671+
#[test]
672+
fn test_trailing_directive() {
673+
let mut poisoned = false;
674+
run_path(&mut poisoned, Path::new("a.rs"), b"//@ only-x86 only-arm");
675+
assert!(poisoned);
676+
}
677+
678+
#[test]
679+
fn test_trailing_directive_with_comment() {
680+
let mut poisoned = false;
681+
run_path(&mut poisoned, Path::new("a.rs"), b"//@ only-x86 only-arm with comment");
682+
assert!(poisoned);
683+
}
684+
685+
#[test]
686+
fn test_not_trailing_directive() {
687+
let mut poisoned = false;
688+
run_path(&mut poisoned, Path::new("a.rs"), b"//@ revisions: incremental");
689+
assert!(!poisoned);
690+
}

0 commit comments

Comments
 (0)