about summary refs log tree commit diff
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2024-10-13 12:35:52 +1100
committerZalathar <Zalathar@users.noreply.github.com>2024-10-22 21:47:51 +1100
commit997b7a6ed0630f097f83119d0ddc4e0d8f8d6ea2 (patch)
treebcb8786a67395f891b913a6147d0303edad52e56
parentc4016ea455c28574b6a5a4413d9c8950fefcb64c (diff)
downloadrust-997b7a6ed0630f097f83119d0ddc4e0d8f8d6ea2.tar.gz
rust-997b7a6ed0630f097f83119d0ddc4e0d8f8d6ea2.zip
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`.
-rw-r--r--src/tools/compiletest/src/header.rs35
1 files changed, 20 insertions, 15 deletions
diff --git a/src/tools/compiletest/src/header.rs b/src/tools/compiletest/src/header.rs
index fe1a500e177..d75cdefe635 100644
--- a/src/tools/compiletest/src/header.rs
+++ b/src/tools/compiletest/src/header.rs
@@ -678,28 +678,35 @@ impl TestProps {
     }
 }
 
-/// Extract an `(Option<line_revision>, directive)` directive from a line if comment is present.
-///
-/// See [`DirectiveLine`] for a diagram.
-pub fn line_directive<'line>(
+/// If the given line begins with the appropriate comment prefix for a directive,
+/// returns a struct containing various parts of the directive.
+fn line_directive<'line>(
+    line_number: usize,
     comment: &str,
     original_line: &'line str,
-) -> Option<(Option<&'line str>, &'line str)> {
+) -> Option<DirectiveLine<'line>> {
     // Ignore lines that don't start with the comment prefix.
     let after_comment = original_line.trim_start().strip_prefix(comment)?.trim_start();
 
+    let revision;
+    let raw_directive;
+
     if let Some(after_open_bracket) = after_comment.strip_prefix('[') {
         // A comment like `//@[foo]` only applies to revision `foo`.
-        let Some((line_revision, directive)) = after_open_bracket.split_once(']') else {
+        let Some((line_revision, after_close_bracket)) = after_open_bracket.split_once(']') else {
             panic!(
                 "malformed condition directive: expected `{comment}[foo]`, found `{original_line}`"
             )
         };
 
-        Some((Some(line_revision), directive.trim_start()))
+        revision = Some(line_revision);
+        raw_directive = after_close_bracket.trim_start();
     } else {
-        Some((None, after_comment))
-    }
+        revision = None;
+        raw_directive = after_comment;
+    };
+
+    Some(DirectiveLine { line_number, revision, raw_directive })
 }
 
 // To prevent duplicating the list of commmands between `compiletest`,`htmldocck` and `jsondocck`,
@@ -856,23 +863,21 @@ fn iter_header(
             return;
         }
 
-        let Some((revision, raw_directive)) = line_directive(comment, ln) else {
+        let Some(directive_line) = line_directive(line_number, comment, ln) else {
             continue;
         };
 
         // Perform unknown directive check on Rust files.
         if testfile.extension().map(|e| e == "rs").unwrap_or(false) {
-            let directive_ln = raw_directive.trim();
-
             let CheckDirectiveResult { is_known_directive, trailing_directive } =
-                check_directive(directive_ln, mode, ln);
+                check_directive(directive_line.raw_directive, mode, ln);
 
             if !is_known_directive {
                 *poisoned = true;
 
                 eprintln!(
                     "error: detected unknown compiletest test directive `{}` in {}:{}",
-                    directive_ln,
+                    directive_line.raw_directive,
                     testfile.display(),
                     line_number,
                 );
@@ -896,7 +901,7 @@ fn iter_header(
             }
         }
 
-        it(DirectiveLine { line_number, revision, raw_directive });
+        it(directive_line);
     }
 }