From e55878b53c54c395bdf40181423535245d3b2967 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 12 Feb 2025 11:57:28 -0600 Subject: [PATCH 1/7] test: Show frontmatter behavior before impl --- .../ui/feature-gates/feature-gate-frontmatter.rs | 6 ++++++ .../feature-gates/feature-gate-frontmatter.stderr | 10 ++++++++++ tests/ui/frontmatter/frontmatter-escaped.rs | 12 ++++++++++++ tests/ui/frontmatter/frontmatter-escaped.stderr | 10 ++++++++++ tests/ui/frontmatter/frontmatter-ignored-space.rs | 13 +++++++++++++ .../frontmatter/frontmatter-ignored-space.stderr | 10 ++++++++++ .../frontmatter-invalid-after-attribute.rs | 8 ++++++++ .../frontmatter-invalid-after-attribute.stderr | 10 ++++++++++ .../frontmatter-invalid-after-comment.rs | 10 ++++++++++ .../frontmatter-invalid-after-comment.stderr | 10 ++++++++++ .../ui/frontmatter/frontmatter-invalid-escaped.rs | 12 ++++++++++++ .../frontmatter-invalid-escaped.stderr | 10 ++++++++++ tests/ui/frontmatter/frontmatter-invalid-in-fn.rs | 8 ++++++++ .../frontmatter/frontmatter-invalid-in-fn.stderr | 15 +++++++++++++++ .../frontmatter-invalid-indentation.rs | 8 ++++++++ .../frontmatter-invalid-indentation.stderr | 10 ++++++++++ .../frontmatter-invalid-mismatched-close.rs | 8 ++++++++ .../frontmatter-invalid-mismatched-close.stderr | 10 ++++++++++ .../frontmatter/frontmatter-invalid-multiple.rs | 11 +++++++++++ .../frontmatter-invalid-multiple.stderr | 10 ++++++++++ .../frontmatter-invalid-non-ident-infostring.rs | 8 ++++++++ ...rontmatter-invalid-non-ident-infostring.stderr | 10 ++++++++++ .../frontmatter/frontmatter-invalid-short-open.rs | 8 ++++++++ .../frontmatter-invalid-short-open.stderr | 10 ++++++++++ .../frontmatter-invalid-trailing-text.rs | 8 ++++++++ .../frontmatter-invalid-trailing-text.stderr | 10 ++++++++++ .../frontmatter/frontmatter-invalid-unclosed.rs | 7 +++++++ .../frontmatter-invalid-unclosed.stderr | 10 ++++++++++ tests/ui/frontmatter/frontmatter-with-body.rs | 9 +++++++++ tests/ui/frontmatter/frontmatter-with-body.stderr | 10 ++++++++++ .../frontmatter-with-infostring-spaces.rs | 8 ++++++++ .../frontmatter-with-infostring-spaces.stderr | 10 ++++++++++ .../ui/frontmatter/frontmatter-with-infostring.rs | 8 ++++++++ .../frontmatter-with-infostring.stderr | 10 ++++++++++ tests/ui/frontmatter/frontmatter.rs | 8 ++++++++ tests/ui/frontmatter/frontmatter.stderr | 10 ++++++++++ tests/ui/unpretty/frontmatter.rs | 10 ++++++++++ tests/ui/unpretty/frontmatter.stderr | 10 ++++++++++ 38 files changed, 365 insertions(+) create mode 100644 tests/ui/feature-gates/feature-gate-frontmatter.rs create mode 100644 tests/ui/feature-gates/feature-gate-frontmatter.stderr create mode 100644 tests/ui/frontmatter/frontmatter-escaped.rs create mode 100644 tests/ui/frontmatter/frontmatter-escaped.stderr create mode 100644 tests/ui/frontmatter/frontmatter-ignored-space.rs create mode 100644 tests/ui/frontmatter/frontmatter-ignored-space.stderr create mode 100644 tests/ui/frontmatter/frontmatter-invalid-after-attribute.rs create mode 100644 tests/ui/frontmatter/frontmatter-invalid-after-attribute.stderr create mode 100644 tests/ui/frontmatter/frontmatter-invalid-after-comment.rs create mode 100644 tests/ui/frontmatter/frontmatter-invalid-after-comment.stderr create mode 100644 tests/ui/frontmatter/frontmatter-invalid-escaped.rs create mode 100644 tests/ui/frontmatter/frontmatter-invalid-escaped.stderr create mode 100644 tests/ui/frontmatter/frontmatter-invalid-in-fn.rs create mode 100644 tests/ui/frontmatter/frontmatter-invalid-in-fn.stderr create mode 100644 tests/ui/frontmatter/frontmatter-invalid-indentation.rs create mode 100644 tests/ui/frontmatter/frontmatter-invalid-indentation.stderr create mode 100644 tests/ui/frontmatter/frontmatter-invalid-mismatched-close.rs create mode 100644 tests/ui/frontmatter/frontmatter-invalid-mismatched-close.stderr create mode 100644 tests/ui/frontmatter/frontmatter-invalid-multiple.rs create mode 100644 tests/ui/frontmatter/frontmatter-invalid-multiple.stderr create mode 100644 tests/ui/frontmatter/frontmatter-invalid-non-ident-infostring.rs create mode 100644 tests/ui/frontmatter/frontmatter-invalid-non-ident-infostring.stderr create mode 100644 tests/ui/frontmatter/frontmatter-invalid-short-open.rs create mode 100644 tests/ui/frontmatter/frontmatter-invalid-short-open.stderr create mode 100644 tests/ui/frontmatter/frontmatter-invalid-trailing-text.rs create mode 100644 tests/ui/frontmatter/frontmatter-invalid-trailing-text.stderr create mode 100644 tests/ui/frontmatter/frontmatter-invalid-unclosed.rs create mode 100644 tests/ui/frontmatter/frontmatter-invalid-unclosed.stderr create mode 100644 tests/ui/frontmatter/frontmatter-with-body.rs create mode 100644 tests/ui/frontmatter/frontmatter-with-body.stderr create mode 100644 tests/ui/frontmatter/frontmatter-with-infostring-spaces.rs create mode 100644 tests/ui/frontmatter/frontmatter-with-infostring-spaces.stderr create mode 100644 tests/ui/frontmatter/frontmatter-with-infostring.rs create mode 100644 tests/ui/frontmatter/frontmatter-with-infostring.stderr create mode 100644 tests/ui/frontmatter/frontmatter.rs create mode 100644 tests/ui/frontmatter/frontmatter.stderr create mode 100644 tests/ui/unpretty/frontmatter.rs create mode 100644 tests/ui/unpretty/frontmatter.stderr diff --git a/tests/ui/feature-gates/feature-gate-frontmatter.rs b/tests/ui/feature-gates/feature-gate-frontmatter.rs new file mode 100644 index 0000000000000..faff93ed73740 --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-frontmatter.rs @@ -0,0 +1,6 @@ +--- +//~^ ERROR expected item, found `-` +--- + +pub fn main() { +} diff --git a/tests/ui/feature-gates/feature-gate-frontmatter.stderr b/tests/ui/feature-gates/feature-gate-frontmatter.stderr new file mode 100644 index 0000000000000..f3aa21afc723e --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-frontmatter.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/feature-gate-frontmatter.rs:1:1 + | +LL | --- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-escaped.rs b/tests/ui/frontmatter/frontmatter-escaped.rs new file mode 100644 index 0000000000000..d7b0b7816c774 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-escaped.rs @@ -0,0 +1,12 @@ +----- +//~^ ERROR expected item, found `-` + +--- +--- + +----- + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-escaped.stderr b/tests/ui/frontmatter/frontmatter-escaped.stderr new file mode 100644 index 0000000000000..f2dbf5535ddcd --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-escaped.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-escaped.rs:1:1 + | +LL | ----- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-ignored-space.rs b/tests/ui/frontmatter/frontmatter-ignored-space.rs new file mode 100644 index 0000000000000..3188514d2eaff --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-ignored-space.rs @@ -0,0 +1,13 @@ + + +--- +//~^ ERROR expected item, found `-` + +--- + +#![feature(frontmatter)] +// ignore-tidy-end-whitespace +// ignore-tidy-leading-newlines + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-ignored-space.stderr b/tests/ui/frontmatter/frontmatter-ignored-space.stderr new file mode 100644 index 0000000000000..a2f307eea85d9 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-ignored-space.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-ignored-space.rs:3:1 + | +LL | --- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-invalid-after-attribute.rs b/tests/ui/frontmatter/frontmatter-invalid-after-attribute.rs new file mode 100644 index 0000000000000..c3b77a9d05d09 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-after-attribute.rs @@ -0,0 +1,8 @@ +#![feature(frontmatter)] + +--- +//~^ ERROR expected item, found `-` +--- + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-invalid-after-attribute.stderr b/tests/ui/frontmatter/frontmatter-invalid-after-attribute.stderr new file mode 100644 index 0000000000000..b8398a46e9edd --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-after-attribute.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-invalid-after-attribute.rs:3:1 + | +LL | --- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-invalid-after-comment.rs b/tests/ui/frontmatter/frontmatter-invalid-after-comment.rs new file mode 100644 index 0000000000000..27960bd6149b2 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-after-comment.rs @@ -0,0 +1,10 @@ +// Comment + +--- +//~^ ERROR expected item, found `-` +--- + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-invalid-after-comment.stderr b/tests/ui/frontmatter/frontmatter-invalid-after-comment.stderr new file mode 100644 index 0000000000000..09d1813e7efb8 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-after-comment.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-invalid-after-comment.rs:3:1 + | +LL | --- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-invalid-escaped.rs b/tests/ui/frontmatter/frontmatter-invalid-escaped.rs new file mode 100644 index 0000000000000..1d705d58cca59 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-escaped.rs @@ -0,0 +1,12 @@ +--- +//~^ ERROR expected item, found `-` + +----- +----- + +--- + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-invalid-escaped.stderr b/tests/ui/frontmatter/frontmatter-invalid-escaped.stderr new file mode 100644 index 0000000000000..1ff363015478e --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-escaped.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-invalid-escaped.rs:1:1 + | +LL | --- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-invalid-in-fn.rs b/tests/ui/frontmatter/frontmatter-invalid-in-fn.rs new file mode 100644 index 0000000000000..1996c9af9fe49 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-in-fn.rs @@ -0,0 +1,8 @@ +#![feature(frontmatter)] +//~^ unknown feature `frontmatter` + +fn main() { +--- +--- +} +//~^ ERROR expected expression, found `}` diff --git a/tests/ui/frontmatter/frontmatter-invalid-in-fn.stderr b/tests/ui/frontmatter/frontmatter-invalid-in-fn.stderr new file mode 100644 index 0000000000000..b3e28219aeedb --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-in-fn.stderr @@ -0,0 +1,15 @@ +error: expected expression, found `}` + --> $DIR/frontmatter-invalid-in-fn.rs:7:1 + | +LL | } + | ^ expected expression + +error[E0635]: unknown feature `frontmatter` + --> $DIR/frontmatter-invalid-in-fn.rs:1:12 + | +LL | #![feature(frontmatter)] + | ^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0635`. diff --git a/tests/ui/frontmatter/frontmatter-invalid-indentation.rs b/tests/ui/frontmatter/frontmatter-invalid-indentation.rs new file mode 100644 index 0000000000000..b7aa9a87e764e --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-indentation.rs @@ -0,0 +1,8 @@ + --- +//~^ ERROR expected item, found `-` + --- + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-invalid-indentation.stderr b/tests/ui/frontmatter/frontmatter-invalid-indentation.stderr new file mode 100644 index 0000000000000..99b04ea14ef45 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-indentation.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-invalid-indentation.rs:1:3 + | +LL | --- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-invalid-mismatched-close.rs b/tests/ui/frontmatter/frontmatter-invalid-mismatched-close.rs new file mode 100644 index 0000000000000..d6afe90fad31b --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-mismatched-close.rs @@ -0,0 +1,8 @@ +--- +//~^ ERROR expected item, found `-` +---- + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-invalid-mismatched-close.stderr b/tests/ui/frontmatter/frontmatter-invalid-mismatched-close.stderr new file mode 100644 index 0000000000000..58468ef498b0f --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-mismatched-close.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-invalid-mismatched-close.rs:1:1 + | +LL | --- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-invalid-multiple.rs b/tests/ui/frontmatter/frontmatter-invalid-multiple.rs new file mode 100644 index 0000000000000..c39c63be35758 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-multiple.rs @@ -0,0 +1,11 @@ +---cargo +//~^ ERROR expected item, found `-` +--- + +---buck +--- + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-invalid-multiple.stderr b/tests/ui/frontmatter/frontmatter-invalid-multiple.stderr new file mode 100644 index 0000000000000..94c877b4ef8e6 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-multiple.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-invalid-multiple.rs:1:1 + | +LL | ---cargo + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-invalid-non-ident-infostring.rs b/tests/ui/frontmatter/frontmatter-invalid-non-ident-infostring.rs new file mode 100644 index 0000000000000..57db85f72ae0b --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-non-ident-infostring.rs @@ -0,0 +1,8 @@ +---cargo,hello-world +//~^ ERROR expected item, found `-` +--- + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-invalid-non-ident-infostring.stderr b/tests/ui/frontmatter/frontmatter-invalid-non-ident-infostring.stderr new file mode 100644 index 0000000000000..3f9fb7bebc7a2 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-non-ident-infostring.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-invalid-non-ident-infostring.rs:1:1 + | +LL | ---cargo,hello-world + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-invalid-short-open.rs b/tests/ui/frontmatter/frontmatter-invalid-short-open.rs new file mode 100644 index 0000000000000..cf7363d668859 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-short-open.rs @@ -0,0 +1,8 @@ +-- +//~^ ERROR expected item, found `-` +-- + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-invalid-short-open.stderr b/tests/ui/frontmatter/frontmatter-invalid-short-open.stderr new file mode 100644 index 0000000000000..e1ea652c73243 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-short-open.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-invalid-short-open.rs:1:1 + | +LL | -- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-invalid-trailing-text.rs b/tests/ui/frontmatter/frontmatter-invalid-trailing-text.rs new file mode 100644 index 0000000000000..ee11c56f9ebc1 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-trailing-text.rs @@ -0,0 +1,8 @@ +--- +//~^ ERROR expected item, found `-` +--- text + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-invalid-trailing-text.stderr b/tests/ui/frontmatter/frontmatter-invalid-trailing-text.stderr new file mode 100644 index 0000000000000..bdf3438e86374 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-trailing-text.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-invalid-trailing-text.rs:1:1 + | +LL | --- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-invalid-unclosed.rs b/tests/ui/frontmatter/frontmatter-invalid-unclosed.rs new file mode 100644 index 0000000000000..3166e73a40e8e --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-unclosed.rs @@ -0,0 +1,7 @@ +--- +//~^ ERROR expected item, found `-` + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-invalid-unclosed.stderr b/tests/ui/frontmatter/frontmatter-invalid-unclosed.stderr new file mode 100644 index 0000000000000..e3fb7a685d0cf --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-invalid-unclosed.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-invalid-unclosed.rs:1:1 + | +LL | --- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-with-body.rs b/tests/ui/frontmatter/frontmatter-with-body.rs new file mode 100644 index 0000000000000..55f1a7785cf20 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-with-body.rs @@ -0,0 +1,9 @@ +--- +//~^ ERROR expected item, found `-` +[dependencies] +--- + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-with-body.stderr b/tests/ui/frontmatter/frontmatter-with-body.stderr new file mode 100644 index 0000000000000..1417e6e0ed21f --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-with-body.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-with-body.rs:1:1 + | +LL | --- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-with-infostring-spaces.rs b/tests/ui/frontmatter/frontmatter-with-infostring-spaces.rs new file mode 100644 index 0000000000000..d05800bbd418b --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-with-infostring-spaces.rs @@ -0,0 +1,8 @@ +--- cargo +//~^ ERROR expected item, found `-` +--- + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-with-infostring-spaces.stderr b/tests/ui/frontmatter/frontmatter-with-infostring-spaces.stderr new file mode 100644 index 0000000000000..18635b5e91aed --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-with-infostring-spaces.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-with-infostring-spaces.rs:1:1 + | +LL | --- cargo + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter-with-infostring.rs b/tests/ui/frontmatter/frontmatter-with-infostring.rs new file mode 100644 index 0000000000000..ded13fcce42c7 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-with-infostring.rs @@ -0,0 +1,8 @@ +---cargo +//~^ ERROR expected item, found `-` +--- + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter-with-infostring.stderr b/tests/ui/frontmatter/frontmatter-with-infostring.stderr new file mode 100644 index 0000000000000..33dfb7f78f316 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter-with-infostring.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter-with-infostring.rs:1:1 + | +LL | ---cargo + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/frontmatter/frontmatter.rs b/tests/ui/frontmatter/frontmatter.rs new file mode 100644 index 0000000000000..117dce3247f9d --- /dev/null +++ b/tests/ui/frontmatter/frontmatter.rs @@ -0,0 +1,8 @@ +--- +//~^ ERROR expected item, found `-` +--- + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/frontmatter/frontmatter.stderr b/tests/ui/frontmatter/frontmatter.stderr new file mode 100644 index 0000000000000..d5780d8eb4fd7 --- /dev/null +++ b/tests/ui/frontmatter/frontmatter.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter.rs:1:1 + | +LL | --- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + diff --git a/tests/ui/unpretty/frontmatter.rs b/tests/ui/unpretty/frontmatter.rs new file mode 100644 index 0000000000000..249e690c77c80 --- /dev/null +++ b/tests/ui/unpretty/frontmatter.rs @@ -0,0 +1,10 @@ +--- +//~^ ERROR expected item, found `-` +--- + +//@ compile-flags: -Zunpretty=normal + +#![feature(frontmatter)] + +fn main() { +} diff --git a/tests/ui/unpretty/frontmatter.stderr b/tests/ui/unpretty/frontmatter.stderr new file mode 100644 index 0000000000000..d5780d8eb4fd7 --- /dev/null +++ b/tests/ui/unpretty/frontmatter.stderr @@ -0,0 +1,10 @@ +error: expected item, found `-` + --> $DIR/frontmatter.rs:1:1 + | +LL | --- + | ^ expected item + | + = note: for a full list of items that can appear in modules, see + +error: aborting due to 1 previous error + From b5d2a6cc07ef85c981a04e4521adecdcb752c38f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 11 Feb 2025 15:50:28 -0600 Subject: [PATCH 2/7] feat: Add 'frontmatter' unstable feature --- compiler/rustc_feature/src/unstable.rs | 2 ++ compiler/rustc_span/src/symbol.rs | 1 + .../src/language-features/frontmatter.md | 10 ++++++++++ tests/ui/frontmatter/frontmatter-invalid-in-fn.rs | 1 - tests/ui/frontmatter/frontmatter-invalid-in-fn.stderr | 11 ++--------- 5 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 src/doc/unstable-book/src/language-features/frontmatter.md diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index 7c931e32f63fd..9c023fdef9edb 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -501,6 +501,8 @@ declare_features! ( (incomplete, fn_delegation, "1.76.0", Some(118212)), /// Allows impls for the Freeze trait. (internal, freeze_impls, "1.78.0", Some(121675)), + /// Frontmatter blocks for build tool metadata. + (unstable, frontmatter, "CURRENT_RUSTC_VERSION", Some(136889)), /// Allows defining gen blocks and `gen fn`. (unstable, gen_blocks, "1.75.0", Some(117078)), /// Infer generic args for both consts and types. diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index c6e570e524f59..28a9234bcfb51 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1044,6 +1044,7 @@ symbols! { from_u16, from_usize, from_yeet, + frontmatter, fs_create_dir, fsub_algebraic, fsub_fast, diff --git a/src/doc/unstable-book/src/language-features/frontmatter.md b/src/doc/unstable-book/src/language-features/frontmatter.md new file mode 100644 index 0000000000000..95329f8efa753 --- /dev/null +++ b/src/doc/unstable-book/src/language-features/frontmatter.md @@ -0,0 +1,10 @@ +# `frontmatter` + +The tracking issue for this feature is: [#136889] + +[#136889]: https://github.com/rust-lang/rust/issues/136889 + +------------------------ + +The `frontmatter` feature adds support for a specialized and simplified attribute syntax +intended for external tools to consume. diff --git a/tests/ui/frontmatter/frontmatter-invalid-in-fn.rs b/tests/ui/frontmatter/frontmatter-invalid-in-fn.rs index 1996c9af9fe49..c5809e074607b 100644 --- a/tests/ui/frontmatter/frontmatter-invalid-in-fn.rs +++ b/tests/ui/frontmatter/frontmatter-invalid-in-fn.rs @@ -1,5 +1,4 @@ #![feature(frontmatter)] -//~^ unknown feature `frontmatter` fn main() { --- diff --git a/tests/ui/frontmatter/frontmatter-invalid-in-fn.stderr b/tests/ui/frontmatter/frontmatter-invalid-in-fn.stderr index b3e28219aeedb..55cc130b211b2 100644 --- a/tests/ui/frontmatter/frontmatter-invalid-in-fn.stderr +++ b/tests/ui/frontmatter/frontmatter-invalid-in-fn.stderr @@ -1,15 +1,8 @@ error: expected expression, found `}` - --> $DIR/frontmatter-invalid-in-fn.rs:7:1 + --> $DIR/frontmatter-invalid-in-fn.rs:6:1 | LL | } | ^ expected expression -error[E0635]: unknown feature `frontmatter` - --> $DIR/frontmatter-invalid-in-fn.rs:1:12 - | -LL | #![feature(frontmatter)] - | ^^^^^^^^^^^ - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error -For more information about this error, try `rustc --explain E0635`. From 05973f4480b648728d6282d4353f268520e0c751 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 14 Feb 2025 09:55:19 -0600 Subject: [PATCH 3/7] feat(lexer): Add frontmatter stripping --- compiler/rustc_lexer/src/lib.rs | 52 ++++++++++++++ compiler/rustc_lexer/src/tests.rs | 113 ++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) diff --git a/compiler/rustc_lexer/src/lib.rs b/compiler/rustc_lexer/src/lib.rs index 61638e45253fd..afa1ecc6475b7 100644 --- a/compiler/rustc_lexer/src/lib.rs +++ b/compiler/rustc_lexer/src/lib.rs @@ -276,6 +276,58 @@ pub fn strip_shebang(input: &str) -> Option { None } +/// Frontmatter is a special attribute type reserved for use by external tools +/// +/// This must be called after [`strip_shebang`] +pub fn strip_frontmatter(input: &str) -> Option { + // Whitespace may precede a frontmatter but must end with a newline + let rest = input.trim_start_matches(is_whitespace); + if rest.len() != input.len() { + let trimmed_len = input.len() - rest.len(); + let last_trimmed_index = trimmed_len - 1; + if input.as_bytes()[last_trimmed_index] != b'\n' { + // either not a frontmatter or invalid opening + return None; + } + } + + // Opens with a line that starts with 3 or more `-` followed by an optional identifier + const FENCE_CHAR: char = '-'; + let fence_length = + rest.char_indices().find_map(|(i, c)| (c != FENCE_CHAR).then_some(i)).unwrap_or(rest.len()); + if fence_length < 3 { + // either not a frontmatter or invalid frontmatter opening + return None; + } + let (fence_pattern, rest) = rest.split_at(fence_length); + let Some(info_end_index) = rest.find('\n') else { + // frontmatter close is required + return None; + }; + let (info, rest) = rest.split_at(info_end_index); + let info = info.trim_matches(is_whitespace); + if !info.is_empty() && !is_ident(info) { + // optional infostring is not an identifier + return None; + } + + // Ends with a line that starts with a matching number of `-` only followed by whitespace + let nl_fence_pattern = format!("\n{fence_pattern}"); + let Some(frontmatter_nl) = rest.find(&nl_fence_pattern) else { + // frontmatter close is required + return None; + }; + let rest = &rest[frontmatter_nl + nl_fence_pattern.len()..]; + + let (after_closing_fence, rest) = rest.split_once("\n").unwrap_or((rest, "")); + let after_closing_fence = after_closing_fence.trim_matches(is_whitespace); + if !after_closing_fence.is_empty() { + // extra characters beyond the original fence pattern, even if they are extra `-` + return None; + } + Some(input.len() - rest.len()) +} + /// Validates a raw string literal. Used for getting more information about a /// problem with a `RawStr`/`RawByteStr` with a `None` field. #[inline] diff --git a/compiler/rustc_lexer/src/tests.rs b/compiler/rustc_lexer/src/tests.rs index 8203ae70b0700..1c3fcc3b5e92a 100644 --- a/compiler/rustc_lexer/src/tests.rs +++ b/compiler/rustc_lexer/src/tests.rs @@ -124,6 +124,119 @@ fn test_valid_shebang() { assert_eq!(strip_shebang(input), None); } +#[test] +fn test_frontmatter() { + let input = "--- +--- +"; + assert_eq!(strip_frontmatter(input), Some(input.len())); + + let input = "--- +package.edition = '2024' + +[dependencies] +regex = '1' +--- +"; + assert_eq!(strip_frontmatter(input), Some(input.len())); + + // allow ident infostring + let input = "---cargo + +--- +"; + assert_eq!(strip_frontmatter(input), Some(input.len())); + + // disallow non-ident infostring + let input = "---cargo hello + +--- +"; + assert_eq!(strip_frontmatter(input), None); + + // ignore extra whitespace + let input = " + + +---\u{0020} + +---\u{0020} +"; + assert_eq!(strip_frontmatter(input), Some(input.len())); + + // disallow indented opening/close + let input = " --- + --- +"; + assert_eq!(strip_frontmatter(input), None); + + // ignore inner dashes not at line start + let input = "--- + + --- + --- + +--- +"; + assert_eq!(strip_frontmatter(input), Some(input.len())); + + // ignore fewer dashes inside + let input = "----- + +--- +--- + +----- +"; + assert_eq!(strip_frontmatter(input), Some(input.len())); + + // disallow more dashes inside + let input = "--- + +----- +----- + +--- +"; + assert_eq!(strip_frontmatter(input), None); + + // disallow mismatch close + let input = "---- + +--- +"; + assert_eq!(strip_frontmatter(input), None); + + // disallow unclosed + let input = "--- + +"; + assert_eq!(strip_frontmatter(input), None); + + // disallow short open/close + let input = "-- + +-- +"; + assert_eq!(strip_frontmatter(input), None); + + // disallow content before + let input = "#![feature(frontmatter)] + +--- +--- +"; + assert_eq!(strip_frontmatter(input), None); + + // disallow trailing text + let input = "#![feature(frontmatter)] + +--- +---cargo +"; + assert_eq!(strip_frontmatter(input), None); +} + fn check_lexing(src: &str, expect: Expect) { let actual: String = tokenize(src).map(|token| format!("{:?}\n", token)).collect(); expect.assert_eq(&actual) From 359eb206c1e64ca154a70d86c0020b134a117850 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 14 Feb 2025 13:29:30 -0600 Subject: [PATCH 4/7] feat(parse): Strip frontmatter --- compiler/rustc_parse/src/lexer/mod.rs | 6 ++++ .../src/language-features/frontmatter.md | 30 +++++++++++++++++++ .../feature-gates/feature-gate-frontmatter.rs | 3 +- .../feature-gate-frontmatter.stderr | 10 ------- tests/ui/frontmatter/frontmatter-escaped.rs | 3 +- .../ui/frontmatter/frontmatter-escaped.stderr | 10 ------- .../frontmatter/frontmatter-ignored-space.rs | 2 +- .../frontmatter-ignored-space.stderr | 10 ------- .../frontmatter-invalid-multiple.rs | 2 +- .../frontmatter-invalid-multiple.stderr | 4 +-- tests/ui/frontmatter/frontmatter-with-body.rs | 3 +- .../frontmatter/frontmatter-with-body.stderr | 10 ------- .../frontmatter-with-infostring-spaces.rs | 3 +- .../frontmatter-with-infostring-spaces.stderr | 10 ------- .../frontmatter-with-infostring.rs | 3 +- .../frontmatter-with-infostring.stderr | 10 ------- tests/ui/frontmatter/frontmatter.rs | 3 +- tests/ui/frontmatter/frontmatter.stderr | 10 ------- tests/ui/unpretty/frontmatter.rs | 2 +- tests/ui/unpretty/frontmatter.stderr | 10 ------- tests/ui/unpretty/frontmatter.stdout | 7 +++++ 21 files changed, 60 insertions(+), 91 deletions(-) delete mode 100644 tests/ui/feature-gates/feature-gate-frontmatter.stderr delete mode 100644 tests/ui/frontmatter/frontmatter-escaped.stderr delete mode 100644 tests/ui/frontmatter/frontmatter-ignored-space.stderr delete mode 100644 tests/ui/frontmatter/frontmatter-with-body.stderr delete mode 100644 tests/ui/frontmatter/frontmatter-with-infostring-spaces.stderr delete mode 100644 tests/ui/frontmatter/frontmatter-with-infostring.stderr delete mode 100644 tests/ui/frontmatter/frontmatter.stderr delete mode 100644 tests/ui/unpretty/frontmatter.stderr create mode 100644 tests/ui/unpretty/frontmatter.stdout diff --git a/compiler/rustc_parse/src/lexer/mod.rs b/compiler/rustc_parse/src/lexer/mod.rs index 1d17290e1c706..db14036dd2505 100644 --- a/compiler/rustc_parse/src/lexer/mod.rs +++ b/compiler/rustc_parse/src/lexer/mod.rs @@ -55,6 +55,12 @@ pub(crate) fn lex_token_trees<'psess, 'src>( start_pos = start_pos + BytePos::from_usize(shebang_len); } + // Skip frontmatter, if present. + if let Some(frontmatter_len) = rustc_lexer::strip_frontmatter(src) { + src = &src[frontmatter_len..]; + start_pos = start_pos + BytePos::from_usize(frontmatter_len); + } + let cursor = Cursor::new(src); let mut lexer = Lexer { psess, diff --git a/src/doc/unstable-book/src/language-features/frontmatter.md b/src/doc/unstable-book/src/language-features/frontmatter.md index 95329f8efa753..3abfdc89a517a 100644 --- a/src/doc/unstable-book/src/language-features/frontmatter.md +++ b/src/doc/unstable-book/src/language-features/frontmatter.md @@ -8,3 +8,33 @@ The tracking issue for this feature is: [#136889] The `frontmatter` feature adds support for a specialized and simplified attribute syntax intended for external tools to consume. + +For example, when used with Cargo: +```rust,ignore (frontmatter/shebang are not intended for doctests) +#!/usr/bin/env -S cargo -Zscript + +--- +[dependencies] +clap = "4" +--- + +use clap::Parser; + +#[derive(Parser)] +struct Cli { +} + +fn main () { + Cli::parse(); +} +``` + +A frontmatter may come after a shebang and must come before any other syntax except whitespace. +The open delimiter is three or more dashes (`-`) at the start of a new line. +The open delimiter may be followed by whitespace and / or an identifier to mark the interpretation of the frontmatter within an external tool. +It is then concluded at a newline. +The close delimiter is a series of dashes that matches the open delimiter, at the start of a line. +The close delimiter may be followed by whitespace. +Any other trailing content, including more dashes than the open delimiter, is an error. +It is then concluded at a newline. +All content between the open and close delimiter lines is ignored. diff --git a/tests/ui/feature-gates/feature-gate-frontmatter.rs b/tests/ui/feature-gates/feature-gate-frontmatter.rs index faff93ed73740..f1fabd1db9843 100644 --- a/tests/ui/feature-gates/feature-gate-frontmatter.rs +++ b/tests/ui/feature-gates/feature-gate-frontmatter.rs @@ -1,6 +1,7 @@ --- -//~^ ERROR expected item, found `-` --- +//@ check-pass + pub fn main() { } diff --git a/tests/ui/feature-gates/feature-gate-frontmatter.stderr b/tests/ui/feature-gates/feature-gate-frontmatter.stderr deleted file mode 100644 index f3aa21afc723e..0000000000000 --- a/tests/ui/feature-gates/feature-gate-frontmatter.stderr +++ /dev/null @@ -1,10 +0,0 @@ -error: expected item, found `-` - --> $DIR/feature-gate-frontmatter.rs:1:1 - | -LL | --- - | ^ expected item - | - = note: for a full list of items that can appear in modules, see - -error: aborting due to 1 previous error - diff --git a/tests/ui/frontmatter/frontmatter-escaped.rs b/tests/ui/frontmatter/frontmatter-escaped.rs index d7b0b7816c774..b882bcab0d6d1 100644 --- a/tests/ui/frontmatter/frontmatter-escaped.rs +++ b/tests/ui/frontmatter/frontmatter-escaped.rs @@ -1,11 +1,12 @@ ----- -//~^ ERROR expected item, found `-` --- --- ----- +//@ check-pass + #![feature(frontmatter)] fn main() { diff --git a/tests/ui/frontmatter/frontmatter-escaped.stderr b/tests/ui/frontmatter/frontmatter-escaped.stderr deleted file mode 100644 index f2dbf5535ddcd..0000000000000 --- a/tests/ui/frontmatter/frontmatter-escaped.stderr +++ /dev/null @@ -1,10 +0,0 @@ -error: expected item, found `-` - --> $DIR/frontmatter-escaped.rs:1:1 - | -LL | ----- - | ^ expected item - | - = note: for a full list of items that can appear in modules, see - -error: aborting due to 1 previous error - diff --git a/tests/ui/frontmatter/frontmatter-ignored-space.rs b/tests/ui/frontmatter/frontmatter-ignored-space.rs index 3188514d2eaff..0063cca5aba24 100644 --- a/tests/ui/frontmatter/frontmatter-ignored-space.rs +++ b/tests/ui/frontmatter/frontmatter-ignored-space.rs @@ -1,10 +1,10 @@ --- -//~^ ERROR expected item, found `-` --- +//@ check-pass #![feature(frontmatter)] // ignore-tidy-end-whitespace // ignore-tidy-leading-newlines diff --git a/tests/ui/frontmatter/frontmatter-ignored-space.stderr b/tests/ui/frontmatter/frontmatter-ignored-space.stderr deleted file mode 100644 index a2f307eea85d9..0000000000000 --- a/tests/ui/frontmatter/frontmatter-ignored-space.stderr +++ /dev/null @@ -1,10 +0,0 @@ -error: expected item, found `-` - --> $DIR/frontmatter-ignored-space.rs:3:1 - | -LL | --- - | ^ expected item - | - = note: for a full list of items that can appear in modules, see - -error: aborting due to 1 previous error - diff --git a/tests/ui/frontmatter/frontmatter-invalid-multiple.rs b/tests/ui/frontmatter/frontmatter-invalid-multiple.rs index c39c63be35758..adb08ecb6bb26 100644 --- a/tests/ui/frontmatter/frontmatter-invalid-multiple.rs +++ b/tests/ui/frontmatter/frontmatter-invalid-multiple.rs @@ -1,8 +1,8 @@ ---cargo -//~^ ERROR expected item, found `-` --- ---buck +//~^ ERROR expected item, found `-` --- #![feature(frontmatter)] diff --git a/tests/ui/frontmatter/frontmatter-invalid-multiple.stderr b/tests/ui/frontmatter/frontmatter-invalid-multiple.stderr index 94c877b4ef8e6..65f733823f2ea 100644 --- a/tests/ui/frontmatter/frontmatter-invalid-multiple.stderr +++ b/tests/ui/frontmatter/frontmatter-invalid-multiple.stderr @@ -1,7 +1,7 @@ error: expected item, found `-` - --> $DIR/frontmatter-invalid-multiple.rs:1:1 + --> $DIR/frontmatter-invalid-multiple.rs:4:1 | -LL | ---cargo +LL | ---buck | ^ expected item | = note: for a full list of items that can appear in modules, see diff --git a/tests/ui/frontmatter/frontmatter-with-body.rs b/tests/ui/frontmatter/frontmatter-with-body.rs index 55f1a7785cf20..c5350cebcda39 100644 --- a/tests/ui/frontmatter/frontmatter-with-body.rs +++ b/tests/ui/frontmatter/frontmatter-with-body.rs @@ -1,8 +1,9 @@ --- -//~^ ERROR expected item, found `-` [dependencies] --- +//@ check-pass + #![feature(frontmatter)] fn main() { diff --git a/tests/ui/frontmatter/frontmatter-with-body.stderr b/tests/ui/frontmatter/frontmatter-with-body.stderr deleted file mode 100644 index 1417e6e0ed21f..0000000000000 --- a/tests/ui/frontmatter/frontmatter-with-body.stderr +++ /dev/null @@ -1,10 +0,0 @@ -error: expected item, found `-` - --> $DIR/frontmatter-with-body.rs:1:1 - | -LL | --- - | ^ expected item - | - = note: for a full list of items that can appear in modules, see - -error: aborting due to 1 previous error - diff --git a/tests/ui/frontmatter/frontmatter-with-infostring-spaces.rs b/tests/ui/frontmatter/frontmatter-with-infostring-spaces.rs index d05800bbd418b..5038382e1da5d 100644 --- a/tests/ui/frontmatter/frontmatter-with-infostring-spaces.rs +++ b/tests/ui/frontmatter/frontmatter-with-infostring-spaces.rs @@ -1,7 +1,8 @@ --- cargo -//~^ ERROR expected item, found `-` --- +//@ check-pass + #![feature(frontmatter)] fn main() { diff --git a/tests/ui/frontmatter/frontmatter-with-infostring-spaces.stderr b/tests/ui/frontmatter/frontmatter-with-infostring-spaces.stderr deleted file mode 100644 index 18635b5e91aed..0000000000000 --- a/tests/ui/frontmatter/frontmatter-with-infostring-spaces.stderr +++ /dev/null @@ -1,10 +0,0 @@ -error: expected item, found `-` - --> $DIR/frontmatter-with-infostring-spaces.rs:1:1 - | -LL | --- cargo - | ^ expected item - | - = note: for a full list of items that can appear in modules, see - -error: aborting due to 1 previous error - diff --git a/tests/ui/frontmatter/frontmatter-with-infostring.rs b/tests/ui/frontmatter/frontmatter-with-infostring.rs index ded13fcce42c7..88be7a278e7ff 100644 --- a/tests/ui/frontmatter/frontmatter-with-infostring.rs +++ b/tests/ui/frontmatter/frontmatter-with-infostring.rs @@ -1,7 +1,8 @@ ---cargo -//~^ ERROR expected item, found `-` --- +//@ check-pass + #![feature(frontmatter)] fn main() { diff --git a/tests/ui/frontmatter/frontmatter-with-infostring.stderr b/tests/ui/frontmatter/frontmatter-with-infostring.stderr deleted file mode 100644 index 33dfb7f78f316..0000000000000 --- a/tests/ui/frontmatter/frontmatter-with-infostring.stderr +++ /dev/null @@ -1,10 +0,0 @@ -error: expected item, found `-` - --> $DIR/frontmatter-with-infostring.rs:1:1 - | -LL | ---cargo - | ^ expected item - | - = note: for a full list of items that can appear in modules, see - -error: aborting due to 1 previous error - diff --git a/tests/ui/frontmatter/frontmatter.rs b/tests/ui/frontmatter/frontmatter.rs index 117dce3247f9d..ac77a3de0bd36 100644 --- a/tests/ui/frontmatter/frontmatter.rs +++ b/tests/ui/frontmatter/frontmatter.rs @@ -1,7 +1,8 @@ --- -//~^ ERROR expected item, found `-` --- +//@ check-pass + #![feature(frontmatter)] fn main() { diff --git a/tests/ui/frontmatter/frontmatter.stderr b/tests/ui/frontmatter/frontmatter.stderr deleted file mode 100644 index d5780d8eb4fd7..0000000000000 --- a/tests/ui/frontmatter/frontmatter.stderr +++ /dev/null @@ -1,10 +0,0 @@ -error: expected item, found `-` - --> $DIR/frontmatter.rs:1:1 - | -LL | --- - | ^ expected item - | - = note: for a full list of items that can appear in modules, see - -error: aborting due to 1 previous error - diff --git a/tests/ui/unpretty/frontmatter.rs b/tests/ui/unpretty/frontmatter.rs index 249e690c77c80..1971808e2a866 100644 --- a/tests/ui/unpretty/frontmatter.rs +++ b/tests/ui/unpretty/frontmatter.rs @@ -1,8 +1,8 @@ --- -//~^ ERROR expected item, found `-` --- //@ compile-flags: -Zunpretty=normal +//@ check-pass #![feature(frontmatter)] diff --git a/tests/ui/unpretty/frontmatter.stderr b/tests/ui/unpretty/frontmatter.stderr deleted file mode 100644 index d5780d8eb4fd7..0000000000000 --- a/tests/ui/unpretty/frontmatter.stderr +++ /dev/null @@ -1,10 +0,0 @@ -error: expected item, found `-` - --> $DIR/frontmatter.rs:1:1 - | -LL | --- - | ^ expected item - | - = note: for a full list of items that can appear in modules, see - -error: aborting due to 1 previous error - diff --git a/tests/ui/unpretty/frontmatter.stdout b/tests/ui/unpretty/frontmatter.stdout new file mode 100644 index 0000000000000..9ad6bbbe5774b --- /dev/null +++ b/tests/ui/unpretty/frontmatter.stdout @@ -0,0 +1,7 @@ + +//@ compile-flags: -Zunpretty=normal +//@ check-pass + +#![feature(frontmatter)] + +fn main() {} From 1ccba030125290fa9e9d132cbc2a6f4c9e43da86 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 17 Feb 2025 15:36:56 -0600 Subject: [PATCH 5/7] fix(parse): Feature gate frontmatter --- compiler/rustc_ast_passes/src/feature_gate.rs | 1 + compiler/rustc_parse/src/lexer/mod.rs | 6 +++++- .../src/language-features/frontmatter.md | 2 ++ .../ui/feature-gates/feature-gate-frontmatter.rs | 2 +- .../feature-gate-frontmatter.stderr | 16 ++++++++++++++++ 5 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/ui/feature-gates/feature-gate-frontmatter.stderr diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index ea60e083c4ced..85a5a4393a6b0 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -511,6 +511,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) { gate_all!(contracts, "contracts are incomplete"); gate_all!(contracts_internals, "contract internal machinery is for internal use only"); gate_all!(where_clause_attrs, "attributes in `where` clause are unstable"); + gate_all!(frontmatter, "frontmatter syntax is unstable"); if !visitor.features.never_patterns() { if let Some(spans) = spans.get(&sym::never_patterns) { diff --git a/compiler/rustc_parse/src/lexer/mod.rs b/compiler/rustc_parse/src/lexer/mod.rs index db14036dd2505..89938889cc358 100644 --- a/compiler/rustc_parse/src/lexer/mod.rs +++ b/compiler/rustc_parse/src/lexer/mod.rs @@ -14,7 +14,7 @@ use rustc_session::lint::builtin::{ TEXT_DIRECTION_CODEPOINT_IN_COMMENT, }; use rustc_session::parse::ParseSess; -use rustc_span::{BytePos, Pos, Span, Symbol}; +use rustc_span::{BytePos, Pos, Span, Symbol, sym}; use tracing::debug; use crate::lexer::diagnostics::TokenTreeDiagInfo; @@ -58,7 +58,11 @@ pub(crate) fn lex_token_trees<'psess, 'src>( // Skip frontmatter, if present. if let Some(frontmatter_len) = rustc_lexer::strip_frontmatter(src) { src = &src[frontmatter_len..]; + let lo = start_pos; start_pos = start_pos + BytePos::from_usize(frontmatter_len); + let hi = start_pos; + let span = Span::with_root_ctxt(lo, hi); + psess.gated_spans.gate(sym::frontmatter, span); } let cursor = Cursor::new(src); diff --git a/src/doc/unstable-book/src/language-features/frontmatter.md b/src/doc/unstable-book/src/language-features/frontmatter.md index 3abfdc89a517a..795ce5bfcde3a 100644 --- a/src/doc/unstable-book/src/language-features/frontmatter.md +++ b/src/doc/unstable-book/src/language-features/frontmatter.md @@ -18,6 +18,8 @@ For example, when used with Cargo: clap = "4" --- +#![feature(frontmatter)] + use clap::Parser; #[derive(Parser)] diff --git a/tests/ui/feature-gates/feature-gate-frontmatter.rs b/tests/ui/feature-gates/feature-gate-frontmatter.rs index f1fabd1db9843..647ca10f36c1d 100644 --- a/tests/ui/feature-gates/feature-gate-frontmatter.rs +++ b/tests/ui/feature-gates/feature-gate-frontmatter.rs @@ -1,7 +1,7 @@ --- +//~^ frontmatter syntax is unstable [E0658] --- -//@ check-pass pub fn main() { } diff --git a/tests/ui/feature-gates/feature-gate-frontmatter.stderr b/tests/ui/feature-gates/feature-gate-frontmatter.stderr new file mode 100644 index 0000000000000..2e40090c5a3f6 --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-frontmatter.stderr @@ -0,0 +1,16 @@ +error[E0658]: frontmatter syntax is unstable + --> $DIR/feature-gate-frontmatter.rs:1:1 + | +LL | / --- +LL | | +LL | | --- +LL | | + | |_^ + | + = note: see issue #136889 for more information + = help: add `#![feature(frontmatter)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0658`. From a7cd78f8a5aace7fa2594dfd6ae2bb3cb857def3 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 18 Feb 2025 10:59:19 -0600 Subject: [PATCH 6/7] test(fmt): Verify frontmatter is not stripped --- src/tools/rustfmt/tests/source/frontmatter.rs | 11 +++++++++++ src/tools/rustfmt/tests/target/frontmatter.rs | 9 +++++++++ 2 files changed, 20 insertions(+) create mode 100644 src/tools/rustfmt/tests/source/frontmatter.rs create mode 100644 src/tools/rustfmt/tests/target/frontmatter.rs diff --git a/src/tools/rustfmt/tests/source/frontmatter.rs b/src/tools/rustfmt/tests/source/frontmatter.rs new file mode 100644 index 0000000000000..45bee64dbd885 --- /dev/null +++ b/src/tools/rustfmt/tests/source/frontmatter.rs @@ -0,0 +1,11 @@ + + +--- identifier +[dependencies] +regex = "1" + +--- + +#![feature(frontmatter)] + +fn main() {} diff --git a/src/tools/rustfmt/tests/target/frontmatter.rs b/src/tools/rustfmt/tests/target/frontmatter.rs new file mode 100644 index 0000000000000..8e4257b7d3d49 --- /dev/null +++ b/src/tools/rustfmt/tests/target/frontmatter.rs @@ -0,0 +1,9 @@ +--- identifier +[dependencies] +regex = "1" + +--- + +#![feature(frontmatter)] + +fn main() {} From 8cd8298670720c4d52c797c176c386bc460a509d Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 20 Feb 2025 09:57:54 -0600 Subject: [PATCH 7/7] feat(pretty): Preserve the frontmatter --- compiler/rustc_ast_pretty/src/pprust/state.rs | 9 +++++++++ tests/ui/unpretty/frontmatter.stdout | 2 ++ 2 files changed, 11 insertions(+) diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 3dbfc191f8f50..a76acd006339a 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -120,6 +120,15 @@ fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec pos += shebang_len; } + if let Some(frontmatter_len) = rustc_lexer::strip_frontmatter(&text[pos..]) { + comments.push(Comment { + style: CommentStyle::Isolated, + lines: vec![text[pos..pos + frontmatter_len].to_string()], + pos: start_bpos + BytePos(pos as u32), + }); + pos += frontmatter_len; + } + for token in rustc_lexer::tokenize(&text[pos..]) { let token_text = &text[pos..pos + token.len as usize]; match token.kind { diff --git a/tests/ui/unpretty/frontmatter.stdout b/tests/ui/unpretty/frontmatter.stdout index 9ad6bbbe5774b..2ccbb1b258255 100644 --- a/tests/ui/unpretty/frontmatter.stdout +++ b/tests/ui/unpretty/frontmatter.stdout @@ -1,3 +1,5 @@ +--- +--- //@ compile-flags: -Zunpretty=normal //@ check-pass