Skip to content

Commit 997b7a6

Browse files
committed
Make line_directive return a DirectiveLine
This reduces the need to juggle raw tuples, and opens up the possibility of moving more parts of directive parsing into `line_directive`.
1 parent c4016ea commit 997b7a6

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/tools/compiletest/src/header.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -678,28 +678,35 @@ impl TestProps {
678678
}
679679
}
680680

681-
/// Extract an `(Option<line_revision>, directive)` directive from a line if comment is present.
682-
///
683-
/// See [`DirectiveLine`] for a diagram.
684-
pub fn line_directive<'line>(
681+
/// If the given line begins with the appropriate comment prefix for a directive,
682+
/// returns a struct containing various parts of the directive.
683+
fn line_directive<'line>(
684+
line_number: usize,
685685
comment: &str,
686686
original_line: &'line str,
687-
) -> Option<(Option<&'line str>, &'line str)> {
687+
) -> Option<DirectiveLine<'line>> {
688688
// Ignore lines that don't start with the comment prefix.
689689
let after_comment = original_line.trim_start().strip_prefix(comment)?.trim_start();
690690

691+
let revision;
692+
let raw_directive;
693+
691694
if let Some(after_open_bracket) = after_comment.strip_prefix('[') {
692695
// A comment like `//@[foo]` only applies to revision `foo`.
693-
let Some((line_revision, directive)) = after_open_bracket.split_once(']') else {
696+
let Some((line_revision, after_close_bracket)) = after_open_bracket.split_once(']') else {
694697
panic!(
695698
"malformed condition directive: expected `{comment}[foo]`, found `{original_line}`"
696699
)
697700
};
698701

699-
Some((Some(line_revision), directive.trim_start()))
702+
revision = Some(line_revision);
703+
raw_directive = after_close_bracket.trim_start();
700704
} else {
701-
Some((None, after_comment))
702-
}
705+
revision = None;
706+
raw_directive = after_comment;
707+
};
708+
709+
Some(DirectiveLine { line_number, revision, raw_directive })
703710
}
704711

705712
// To prevent duplicating the list of commmands between `compiletest`,`htmldocck` and `jsondocck`,
@@ -856,23 +863,21 @@ fn iter_header(
856863
return;
857864
}
858865

859-
let Some((revision, raw_directive)) = line_directive(comment, ln) else {
866+
let Some(directive_line) = line_directive(line_number, comment, ln) else {
860867
continue;
861868
};
862869

863870
// Perform unknown directive check on Rust files.
864871
if testfile.extension().map(|e| e == "rs").unwrap_or(false) {
865-
let directive_ln = raw_directive.trim();
866-
867872
let CheckDirectiveResult { is_known_directive, trailing_directive } =
868-
check_directive(directive_ln, mode, ln);
873+
check_directive(directive_line.raw_directive, mode, ln);
869874

870875
if !is_known_directive {
871876
*poisoned = true;
872877

873878
eprintln!(
874879
"error: detected unknown compiletest test directive `{}` in {}:{}",
875-
directive_ln,
880+
directive_line.raw_directive,
876881
testfile.display(),
877882
line_number,
878883
);
@@ -896,7 +901,7 @@ fn iter_header(
896901
}
897902
}
898903

899-
it(DirectiveLine { line_number, revision, raw_directive });
904+
it(directive_line);
900905
}
901906
}
902907

0 commit comments

Comments
 (0)