Rollup merge of #152883 - mu001999-contrib:fix/final-bad-use, r=Kivooeo

Deny final not followed by item

Fixes rust-lang/rust#152820
This commit is contained in:
Jonathan Brouwer
2026-02-20 13:25:01 +01:00
committed by GitHub
4 changed files with 44 additions and 0 deletions
+9
View File
@@ -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")]
+2
View File
@@ -197,6 +197,8 @@ pub(super) fn parse_item_common(
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 {
+7
View File
@@ -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
}
@@ -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