Skip to content

Commit 3fce417

Browse files
committed
fix(embedded): Disallow ambiguous multiple frontmatters
We could say that we delegate this to rustc but if they add support for multiple frontmatters, we need to update to be able to know which we should read, so its better to error on our side.
1 parent 4e90b12 commit 3fce417

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/cargo/util/toml/embedded.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ impl<'s> ScriptSource<'s> {
211211
let frontmatter_len = input.len() - rest.len();
212212
source.content = &input[frontmatter_len..];
213213

214+
let repeat = Self::parse(source.content)?;
215+
if repeat.frontmatter.is_some() {
216+
anyhow::bail!("only one frontmatter is supported");
217+
}
218+
214219
Ok(source)
215220
}
216221

@@ -430,6 +435,7 @@ content: "\n//@ check-pass\n\n// check that frontmatter blocks can have tokens t
430435

431436
#[test]
432437
fn rustc_frontmatter_whitespace_1() {
438+
// Deferred to rustc since this requires knowledge of Rust grammar
433439
assert_source(
434440
r#" ---
435441
//~^ ERROR: invalid preceding whitespace for frontmatter opening
@@ -607,8 +613,9 @@ content: "\nfn main() {}\n"
607613

608614
#[test]
609615
fn rustc_multifrontmatter() {
610-
assert_source(
611-
r#"---
616+
assert_err(
617+
ScriptSource::parse(
618+
r#"---
612619
---
613620
614621
---
@@ -620,13 +627,8 @@ content: "\nfn main() {}\n"
620627
621628
fn main() {}
622629
"#,
623-
str![[r#"
624-
shebang: None
625-
info: None
626-
frontmatter: ""
627-
content: "\n---\n//~^ ERROR: expected item, found `-`\n// FIXME(frontmatter): make this diagnostic better\n---\n\n// test that we do not parse another frontmatter block after the first one.\n\nfn main() {}\n"
628-
629-
"#]],
630+
),
631+
str!["only one frontmatter is supported"],
630632
);
631633
}
632634

0 commit comments

Comments
 (0)