From 544d09132bef3d483a0e10b4bb25e25079107e37 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Wed, 21 Feb 2024 11:49:01 +1100 Subject: [PATCH] Flatten the parse logic in `line_directive` --- src/tools/compiletest/src/header.rs | 31 ++++++++++++----------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/tools/compiletest/src/header.rs b/src/tools/compiletest/src/header.rs index 50e8cfd60a9fb..44e5d8dea7dcc 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)) } }