Skip to content

Commit

Permalink
fix(parse): Feature gate frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 17, 2025
1 parent 060c012 commit 8d4336e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
gate_all!(unsafe_binders, "unsafe binder types are experimental");
gate_all!(contracts, "contracts are incomplete");
gate_all!(contracts_internals, "contract internal machinery is for internal use only");
gate_all!(frontmatter, "frontmatter syntax is unstable");

if !visitor.features.never_patterns() {
if let Some(spans) = spans.get(&sym::never_patterns) {
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/doc/unstable-book/src/language-features/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ For example, when used with Cargo:
clap = "4"
---

#![feature(frontmatter)]

use clap::Parser;

#[derive(Parser)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/feature-gates/feature-gate-frontmatter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
//~^ frontmatter syntax is unstable [E0658]
---

//@ check-pass

pub fn main() {
}
16 changes: 16 additions & 0 deletions tests/ui/feature-gates/feature-gate-frontmatter.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/rust-lang/rust/issues/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`.

0 comments on commit 8d4336e

Please sign in to comment.