Skip to content
Merged
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
15 changes: 12 additions & 3 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,9 +1088,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
};

let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value, || {
hir::Defaultness::Default { has_value }
});
let defaultness = match i.kind.defaultness() {
// We do not yet support `final` on trait associated items other than functions.
// Even though we reject `final` on non-functions during AST validation, we still
// need to stop propagating it here because later compiler passes do not expect
// and cannot handle such items.
Defaultness::Final(..) if !matches!(i.kind, AssocItemKind::Fn(..)) => {
Defaultness::Implicit
}
defaultness => defaultness,
};
let (defaultness, _) = self
.lower_defaultness(defaultness, has_value, || hir::Defaultness::Default { has_value });

let item = hir::TraitItem {
owner_id: trait_item_def_id,
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/traits/final/final-on-assoc-type-const.rs
Copy link
Member

@fmease fmease Feb 21, 2026

Choose a reason for hiding this comment

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

Could you also add a (type-leve) assoc const case? They also ICEs on nightly (with no default value for trait assoc const in compiler/rustc_hir_analysis/src/collect.rs) & should be fixed by this PR. Maybe tests/ui/traits/final/final-on-assoc-type-const.rs (sic!) containing:

// See also <https://github.com/rust-lang/rust/issues/152797>..
#![feature(final_associated_functions, min_generic_const_items)]
#![expect(incomplete_features)]

trait Uwu {
    final type const OVO: ();
    //~^ error: `final` is only allowed on associated functions in traits
}

fn main() {}

Much appreciated!

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops I just combined the two tests :(( I've already force pushed 3 times so if that doesn't matter too much I'd prefer to leave it as is...

Copy link
Member

Choose a reason for hiding this comment

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

No worry, it's fine.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This is a regression test for <https://github.com/rust-lang/rust/issues/152797>.
#![feature(final_associated_functions)]
#![feature(min_generic_const_args)]
#![expect(incomplete_features)]
trait Uwu {
final type Ovo;
//~^ error: `final` is only allowed on associated functions in traits
final type const QwQ: ();
//~^ error: `final` is only allowed on associated functions in traits
}

fn main() {}
18 changes: 18 additions & 0 deletions tests/ui/traits/final/final-on-assoc-type-const.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: `final` is only allowed on associated functions in traits
--> $DIR/final-on-assoc-type-const.rs:6:5
|
LL | final type Ovo;
| -----^^^^^^^^^^
| |
| `final` because of this

error: `final` is only allowed on associated functions in traits
--> $DIR/final-on-assoc-type-const.rs:8:5
|
LL | final type const QwQ: ();
| -----^^^^^^^^^^^^^^^^^^^^
| |
| `final` because of this

error: aborting due to 2 previous errors

2 changes: 0 additions & 2 deletions tests/ui/traits/final/positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ final impl Trait for Foo {

final type Foo = ();
//~^ ERROR `final` is only allowed on associated functions in traits
//~^^ ERROR cannot override `Foo` because it already has a `final` definition in the trait

final const FOO: usize = 1;
//~^ ERROR `final` is only allowed on associated functions in traits
//~^^ ERROR cannot override `FOO` because it already has a `final` definition in the trait
}


Expand Down
44 changes: 10 additions & 34 deletions tests/ui/traits/final/positions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ LL | final trait Trait {
= note: only associated functions in traits can be `final`

error: a static item cannot be `final`
--> $DIR/positions.rs:67:5
--> $DIR/positions.rs:65:5
|
LL | final static FOO_EXTERN: usize = 0;
| ^^^^^ `final` because of this
|
= note: only associated functions in traits can be `final`

error: an extern block cannot be `final`
--> $DIR/positions.rs:58:1
--> $DIR/positions.rs:56:1
|
LL | final unsafe extern "C" {
| ^^^^^ `final` because of this
Expand Down Expand Up @@ -87,55 +87,55 @@ LL | final type Foo = ();
| `final` because of this

error: `final` is only allowed on associated functions in traits
--> $DIR/positions.rs:43:5
--> $DIR/positions.rs:42:5
|
LL | final const FOO: usize = 1;
| -----^^^^^^^^^^^^^^^^^^^^^^
| |
| `final` because of this

error: `final` is only allowed on associated functions in traits
--> $DIR/positions.rs:49:1
--> $DIR/positions.rs:47:1
|
LL | final fn foo() {}
| -----^^^^^^^^^
| |
| `final` because of this

error: `final` is only allowed on associated functions in traits
--> $DIR/positions.rs:52:1
--> $DIR/positions.rs:50:1
|
LL | final type FooTy = ();
| -----^^^^^^^^^^^^^^^^^
| |
| `final` because of this

error: `final` is only allowed on associated functions in traits
--> $DIR/positions.rs:55:1
--> $DIR/positions.rs:53:1
|
LL | final const FOO: usize = 0;
| -----^^^^^^^^^^^^^^^^^^^^^^
| |
| `final` because of this

error: `final` is only allowed on associated functions in traits
--> $DIR/positions.rs:61:5
--> $DIR/positions.rs:59:5
|
LL | final fn foo_extern();
| -----^^^^^^^^^^^^^^^^^
| |
| `final` because of this

error: `final` is only allowed on associated functions in traits
--> $DIR/positions.rs:64:5
--> $DIR/positions.rs:62:5
|
LL | final type FooExtern;
| -----^^^^^^^^^^^^^^^^
| |
| `final` because of this

error: incorrect `static` inside `extern` block
--> $DIR/positions.rs:67:18
--> $DIR/positions.rs:65:18
|
LL | final unsafe extern "C" {
| ----------------------- `extern` blocks define existing foreign statics and statics inside of them cannot have a body
Expand All @@ -159,29 +159,5 @@ note: `method` is marked final here
LL | final fn method() {}
| ^^^^^^^^^^^^^^^^^

error: cannot override `Foo` because it already has a `final` definition in the trait
--> $DIR/positions.rs:39:5
|
LL | final type Foo = ();
| ^^^^^^^^^^^^^^
|
note: `Foo` is marked final here
--> $DIR/positions.rs:16:5
|
LL | final type Foo = ();
| ^^^^^^^^^^^^^^

error: cannot override `FOO` because it already has a `final` definition in the trait
--> $DIR/positions.rs:43:5
|
LL | final const FOO: usize = 1;
| ^^^^^^^^^^^^^^^^^^^^^^
|
note: `FOO` is marked final here
--> $DIR/positions.rs:19:5
|
LL | final const FOO: usize = 1;
| ^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 21 previous errors
error: aborting due to 19 previous errors

Loading