diff options
| author | Zalathar <Zalathar@users.noreply.github.com> | 2024-02-21 11:49:01 +1100 |
|---|---|---|
| committer | Zalathar <Zalathar@users.noreply.github.com> | 2024-02-21 21:10:47 +1100 |
| commit | 544d09132bef3d483a0e10b4bb25e25079107e37 (patch) | |
| tree | 1940bcc162cdc4bacb70cec94b8df275c591600c | |
| parent | 99fb653d1d0f79961506413de549bebd7b798ba8 (diff) | |
| download | rust-544d09132bef3d483a0e10b4bb25e25079107e37.tar.gz rust-544d09132bef3d483a0e10b4bb25e25079107e37.zip | |
Flatten the parse logic in `line_directive`
| -rw-r--r-- | src/tools/compiletest/src/header.rs | 31 |
1 files changed, 13 insertions, 18 deletions
diff --git a/src/tools/compiletest/src/header.rs b/src/tools/compiletest/src/header.rs index 50e8cfd60a9..44e5d8dea7d 100644 --- a/src/tools/compiletest/src/header.rs +++ b/src/tools/compiletest/src/header.rs @@ -650,27 +650,22 @@ impl TestProps { /// See [`HeaderLine`] for a diagram. pub fn line_directive<'line>( comment: &str, - ln: &'line str, + original_line: &'line str, ) -> Option<(Option<&'line str>, &'line str)> { - let ln = ln.trim_start(); - if ln.starts_with(comment) { - let ln = ln[comment.len()..].trim_start(); - if ln.starts_with('[') { - // A comment like `//[foo]` is specific to revision `foo` - let Some(close_brace) = ln.find(']') else { - panic!( - "malformed condition directive: expected `{}[foo]`, found `{}`", - comment, ln - ); - }; + // Ignore lines that don't start with the comment prefix. + let after_comment = original_line.trim_start().strip_prefix(comment)?.trim_start(); + + 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 { + panic!( + "malformed condition directive: expected `{comment}[foo]`, found `{original_line}`" + ) + }; - let line_revision = &ln[1..close_brace]; - Some((Some(line_revision), ln[(close_brace + 1)..].trim_start())) - } else { - Some((None, ln)) - } + Some((Some(line_revision), directive.trim_start())) } else { - None + Some((None, after_comment)) } } |
