Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
gate_all!(contracts_internals, "contract internal machinery is for internal use only");
Copy link
Member

@fmease fmease Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Commenting on an arbitrary line to make this discussion threaded)

From the PR description (emphasis and ellipses mine):

Post-RFC changes

[…]

  • acceptable places for frontmatter
    • […]
    • rustc: support removed from […] include! […]
    • […]
    • include! can be used to inject code into the middle of a file which becomes ambiguous between a frontmatter start and a very negated number (----x)

In #146340 I actually struck a (temporary) compromise: We do still strip/lex/recognize frontmatter (and shebang) in item-context include!s. However, we no longer strip frontmatter in expression/statement-context include!s like in let _ = include!(…); (to prevent the manifold negation backcompat issue you've rightly mentioned).

Now, I don't know what's best here. I'm eager to know your and T-lang's stance on the matter. Note that we do strip shebang in expression/statement-context include!s which I consider to be quite odd (but still understandable on a lexical level). I have an open PR #146377 which would stop stripping shebang in that position, too. I still need to rebase+polish, lang-nominate and possibly crater it. The outcome of that T-lang discussion likely affects the decision mentioned in the first paragraph (†).

(†): If that PR was accepted it would mean that we would strip shebang+frontmatter in item-ctxt includes and wouldn't strip either(!) shebang or frontmatter in expr/stmt-ctxt includes which would make the behavior of shebang+frontmatter consistent thereby fulfilling the original goal of "This applies anywhere shebang stripping is performed." in this specific case (obviously the goal wasn't achieved in other cases, like in the proc_macro API case).

gate_all!(where_clause_attrs, "attributes in `where` clause are unstable");
gate_all!(super_let, "`super let` is experimental");
gate_all!(frontmatter, "frontmatters are experimental");
gate_all!(coroutines, "coroutine syntax is experimental");

if !visitor.features.never_patterns() {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ declare_features! (
(accepted, fn_must_use, "1.27.0", Some(43302)),
/// Allows capturing variables in scope using format_args!
(accepted, format_args_capture, "1.58.0", Some(67984)),
/// Frontmatter `---` blocks for use by external tools.
(accepted, frontmatter, "CURRENT_RUSTC_VERSION", Some(136889)),
/// Infer generic args for both consts and types.
(accepted, generic_arg_infer, "1.89.0", Some(85077)),
/// Allows associated types to be generic, e.g., `type Foo<T>;` (RFC 1598).
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,6 @@ 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 use by external tools.
(unstable, frontmatter, "1.88.0", Some(136889)),
/// Allows defining gen blocks and `gen fn`.
(unstable, gen_blocks, "1.75.0", Some(117078)),
/// Allows non-trivial generic constants which have to have wfness manually propagated to callers
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_session::lint::builtin::{
TEXT_DIRECTION_CODEPOINT_IN_COMMENT, TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
};
use rustc_session::parse::ParseSess;
use rustc_span::{BytePos, Pos, Span, Symbol, sym};
use rustc_span::{BytePos, Pos, Span, Symbol};
use tracing::debug;

use crate::errors;
Expand Down Expand Up @@ -626,7 +626,6 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
let last_line_start_pos = frontmatter_opening_end_pos + BytePos(last_line_start as u32);

let frontmatter_span = self.mk_sp(frontmatter_opening_pos, self.pos);
self.psess.gated_spans.gate(sym::frontmatter, frontmatter_span);

if !last_line_trimmed.starts_with("---") {
let label_span = self.mk_sp(frontmatter_opening_pos, frontmatter_opening_end_pos);
Expand Down
32 changes: 32 additions & 0 deletions src/doc/style-guide/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,38 @@ given:

### [Types](types.md)

### Frontmatter

There should be no blank lines between the frontmatter and either the start of the file or a shebang.
There can be zero or one line between the frontmatter and any following content.

The frontmatter fences should use the minimum number of dashes necessary for the contained content (one more than the longest series of initial dashes in the
content, with a minimum of 3 to be recognized as frontmatter delimiters).
If an infostring is present after the opening fence, there should be one space separating them.
The frontmatter fence lines should not have trailing whitespace.

Examples:

```rust
#!/usr/bin/env cargo
---
[dependencies]
regex = "1"
---

fn main() {}
```

```rust
#!/usr/bin/env cargo
--- cargo
[dependencies]
regex = "1"
---

fn main() {}
```

### Comments

The following guidelines for comments are recommendations only, a mechanical
Expand Down
26 changes: 0 additions & 26 deletions src/doc/style-guide/src/nightly.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,3 @@ This chapter documents style and formatting for nightly-only syntax. The rest of
Style and formatting for nightly-only syntax should be removed from this chapter and integrated into the appropriate sections of the style guide at the time of stabilization.

There is no guarantee of the stability of this chapter in contrast to the rest of the style guide. Refer to the style team policy for nightly formatting procedure regarding breaking changes to this chapter.

### Frontmatter

*Location: Placed before comments and attributes in the [root](index.html).*

*Tracking issue: [#136889](https://github.com/rust-lang/rust/issues/136889)*

*Feature gate: `frontmatter`*

There should be no blank lines between the frontmatter and either the start of the file or a shebang.
There can be zero or one line between the frontmatter and any following content.

The frontmatter fences should use the minimum number of dashes necessary for the contained content (one more than the longest series of initial dashes in the
content, with a minimum of 3 to be recognized as frontmatter delimiters).
If an infostring is present after the opening fence, there should be one space separating them.
The frontmatter fence lines should not have trailing whitespace.

```rust
#!/usr/bin/env cargo
--- cargo
[dependencies]
regex = "1"
---

fn main() {}
```
25 changes: 0 additions & 25 deletions src/doc/unstable-book/src/language-features/frontmatter.md

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ Anything to do with loops and `for`, `loop` and `while` keywords to express them

## `tests/ui/frontmatter/`

Tests for `#![feature(frontmatter)]`. See [Tracking Issue for `frontmatter` #136889](https://github.com/rust-lang/rust/issues/136889).
Tests for Cargo Script's frontmatter.

## `tests/ui/fully-qualified-type/`

Expand Down
5 changes: 0 additions & 5 deletions tests/ui/feature-gates/feature-gate-frontmatter.rs

This file was deleted.

15 changes: 0 additions & 15 deletions tests/ui/feature-gates/feature-gate-frontmatter.stderr

This file was deleted.

2 changes: 0 additions & 2 deletions tests/ui/frontmatter/dot-in-infostring-leading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

// infostrings cannot have leading dots

#![feature(frontmatter)]

fn main() {}
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/dot-in-infostring-non-leading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@
// infostrings can contain dots as long as a dot isn't the first character.
//@ check-pass

#![feature(frontmatter)]

fn main() {}
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@
// This test checks that longer dashes for opening and closing can be used to
// escape sequences such as three dashes inside the frontmatter block.

#![feature(frontmatter)]

fn main() {}
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/extra-after-end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
---cargo
//~^ ERROR: extra characters after frontmatter close are not allowed

#![feature(frontmatter)]

fn main() {}
4 changes: 1 addition & 3 deletions tests/ui/frontmatter/frontmatter-after-tokens.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#![feature(frontmatter)]
// frontmatters must be at the start of a file. This test ensures that.

---
//~^ ERROR: expected item, found `-`
// FIXME(frontmatter): make this diagnostic better
---

// frontmatters must be at the start of a file. This test ensures that.

fn main() {}
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/frontmatter-contains-whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ package.description = """

// Ensure the frontmatter can contain any whitespace

#![feature(frontmatter)]

fn main() {}
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/frontmatter-crlf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ clap = "4"

// crlf line endings should be accepted

#![feature(frontmatter)]

fn main() {}
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/frontmatter-inner-hyphens-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ x ---🚧️
// Regression test for #141483
//@check-pass

#![feature(frontmatter)]

fn main() {}
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/frontmatter-inner-hyphens-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ x ---y
// non-whitespace character preceding them.
//@check-pass

#![feature(frontmatter)]

fn main() {}
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/frontmatter-non-lexible-tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

//@ check-pass

#![feature(frontmatter)]

// check that frontmatter blocks can have tokens that are otherwise not accepted by
// the lexer as Rust code.

Expand Down
4 changes: 1 addition & 3 deletions tests/ui/frontmatter/frontmatter-whitespace-1.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
---
//~^ ERROR: invalid preceding whitespace for frontmatter opening
//~^^ ERROR: unclosed frontmatter
---

#![feature(frontmatter)]
//~^ ERROR: invalid preceding whitespace for frontmatter close

// check that whitespaces should not precede the frontmatter opening or close.

Expand Down
18 changes: 7 additions & 11 deletions tests/ui/frontmatter/frontmatter-whitespace-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@ note: frontmatter opening should not be preceded by whitespace
LL | ---
| ^^

error: unclosed frontmatter
--> $DIR/frontmatter-whitespace-1.rs:1:3
error: invalid preceding whitespace for frontmatter close
--> $DIR/frontmatter-whitespace-1.rs:3:1
|
LL | / ---
LL | |
LL | |
LL | | ---
LL | |
| |_^
LL | ---
| ^^^^^
|
note: frontmatter opening here was not closed
--> $DIR/frontmatter-whitespace-1.rs:1:3
note: frontmatter close should not be preceded by whitespace
--> $DIR/frontmatter-whitespace-1.rs:3:1
|
LL | ---
| ^^^
| ^^

error: aborting due to 2 previous errors

8 changes: 3 additions & 5 deletions tests/ui/frontmatter/frontmatter-whitespace-2.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
---cargo
//~^ ERROR: unclosed frontmatter

//@ compile-flags: --crate-type lib

#![feature(frontmatter)]

fn foo(x: i32) -> i32 {
---x
//~^ WARNING: use of a double negation [double_negations]
}
//~^ ERROR: invalid preceding whitespace for frontmatter close
//~| ERROR: extra characters after frontmatter close are not allowed
} //~ ERROR: unexpected closing delimiter: `}`

// this test is for the weird case that valid Rust code can have three dashes
// within them and get treated as a frontmatter close.
38 changes: 17 additions & 21 deletions tests/ui/frontmatter/frontmatter-whitespace-2.stderr
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
error: unclosed frontmatter
--> $DIR/frontmatter-whitespace-2.rs:1:1
error: invalid preceding whitespace for frontmatter close
--> $DIR/frontmatter-whitespace-2.rs:6:1
|
LL | / ---cargo
... |
LL | |
| |_^
LL | ---x
| ^^^^^^^^
|
note: frontmatter opening here was not closed
--> $DIR/frontmatter-whitespace-2.rs:1:1
note: frontmatter close should not be preceded by whitespace
--> $DIR/frontmatter-whitespace-2.rs:6:1
|
LL | ---cargo
| ^^^
LL | ---x
| ^^^^

warning: use of a double negation
--> $DIR/frontmatter-whitespace-2.rs:9:6
error: extra characters after frontmatter close are not allowed
--> $DIR/frontmatter-whitespace-2.rs:6:1
|
LL | ---x
| ^^^
|
= note: the prefix `--` could be misinterpreted as a decrement operator which exists in other languages
= note: use `-= 1` if you meant to decrement the value
= note: `#[warn(double_negations)]` on by default
help: add parentheses for clarity
| ^^^^^^^^

error: unexpected closing delimiter: `}`
--> $DIR/frontmatter-whitespace-2.rs:9:1
|
LL | --(-x)
| + +
LL | }
| ^ unexpected closing delimiter

error: aborting due to 1 previous error; 1 warning emitted
error: aborting due to 3 previous errors

2 changes: 0 additions & 2 deletions tests/ui/frontmatter/frontmatter-whitespace-3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@
// ignore-tidy-leading-newlines
// ignore-tidy-tab

#![feature(frontmatter)]

fn main() {}
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/frontmatter-whitespace-4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@
// ignore-tidy-tab
// A frontmatter infostring can have leading whitespace.

#![feature(frontmatter)]

fn main() {}
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/hyphen-in-infostring-leading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

// infostrings cannot have leading hyphens

#![feature(frontmatter)]

fn main() {}
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/hyphen-in-infostring-non-leading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@
// infostrings can contain hyphens as long as a hyphen isn't the first character.
//@ check-pass

#![feature(frontmatter)]

fn main() {}
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/include-in-item-ctxt.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Ensure that in item ctxts we can `include` files that contain frontmatter.
//@ check-pass

#![feature(frontmatter)]

include!("auxiliary/lib.rs");

fn main() {
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/frontmatter/infostring-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

// infostrings can only be a single identifier.

#![feature(frontmatter)]

fn main() {}
Loading
Loading