From 59781157a7d45720399076311095732ecfd79606 Mon Sep 17 00:00:00 2001 From: mu001999 Date: Fri, 20 Feb 2026 12:12:21 +0800 Subject: [PATCH] Deny final not followed by item --- compiler/rustc_parse/src/errors.rs | 9 ++++++++ compiler/rustc_parse/src/parser/item.rs | 2 ++ tests/ui/traits/final/bad-positions.rs | 7 ++++++ tests/ui/traits/final/bad-positions.stderr | 26 ++++++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 tests/ui/traits/final/bad-positions.rs create mode 100644 tests/ui/traits/final/bad-positions.stderr diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 136963a2594ed..2a084a888e330 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -2203,6 +2203,15 @@ pub(crate) struct DefaultNotFollowedByItem { pub span: Span, } +#[derive(Diagnostic)] +#[diag("`final` is not followed by an item")] +#[note("only associated functions in traits may be prefixed by `final`")] +pub(crate) struct FinalNotFollowedByItem { + #[primary_span] + #[label("the `final` qualifier")] + pub span: Span, +} + #[derive(Diagnostic)] pub(crate) enum MissingKeywordForItemDefinition { #[diag("missing `enum` for enum definition")] diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index c03a237239e07..69610d062919d 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -197,6 +197,8 @@ impl<'a> Parser<'a> { if let Defaultness::Default(span) = def { this.dcx().emit_err(errors::DefaultNotFollowedByItem { span }); + } else if let Defaultness::Final(span) = def { + this.dcx().emit_err(errors::FinalNotFollowedByItem { span }); } if !attrs_allowed { diff --git a/tests/ui/traits/final/bad-positions.rs b/tests/ui/traits/final/bad-positions.rs new file mode 100644 index 0000000000000..3ee62985a13b1 --- /dev/null +++ b/tests/ui/traits/final/bad-positions.rs @@ -0,0 +1,7 @@ +#![feature(final_associated_functions)] + +fn main() { + final; //~ ERROR `final` is not followed by an item + final 1 + 1; //~ ERROR `final` is not followed by an item + final { println!("text"); }; //~ ERROR `final` is not followed by an item +} diff --git a/tests/ui/traits/final/bad-positions.stderr b/tests/ui/traits/final/bad-positions.stderr new file mode 100644 index 0000000000000..00dded85d0b79 --- /dev/null +++ b/tests/ui/traits/final/bad-positions.stderr @@ -0,0 +1,26 @@ +error: `final` is not followed by an item + --> $DIR/bad-positions.rs:4:5 + | +LL | final; + | ^^^^^ the `final` qualifier + | + = note: only associated functions in traits may be prefixed by `final` + +error: `final` is not followed by an item + --> $DIR/bad-positions.rs:5:5 + | +LL | final 1 + 1; + | ^^^^^ the `final` qualifier + | + = note: only associated functions in traits may be prefixed by `final` + +error: `final` is not followed by an item + --> $DIR/bad-positions.rs:6:5 + | +LL | final { println!("text"); }; + | ^^^^^ the `final` qualifier + | + = note: only associated functions in traits may be prefixed by `final` + +error: aborting due to 3 previous errors +