diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index e5d8013058f65..ac754e2dfb9b2 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -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) { diff --git a/compiler/rustc_parse/src/lexer/mod.rs b/compiler/rustc_parse/src/lexer/mod.rs index 125509d5b0c87..98b64c7340c82 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 57b2fa439d157..33c99820513d7 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`.