Skip to content

Commit edbbdd7

Browse files
Rollup merge of #152926 - makai410:final-on-assoc-type, r=fmease
Fix ICE when an associated type is wrongly marked as `final` Fixes: #152797. cc @mu001999 .
2 parents 4136302 + bde075b commit edbbdd7

5 files changed

Lines changed: 52 additions & 39 deletions

File tree

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,9 +1085,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
10851085
}
10861086
};
10871087

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

10921101
let item = hir::TraitItem {
10931102
owner_id: trait_item_def_id,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This is a regression test for <https://github.com/rust-lang/rust/issues/152797>.
2+
#![feature(final_associated_functions)]
3+
#![feature(min_generic_const_args)]
4+
#![expect(incomplete_features)]
5+
trait Uwu {
6+
final type Ovo;
7+
//~^ error: `final` is only allowed on associated functions in traits
8+
final type const QwQ: ();
9+
//~^ error: `final` is only allowed on associated functions in traits
10+
}
11+
12+
fn main() {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: `final` is only allowed on associated functions in traits
2+
--> $DIR/final-on-assoc-type-const.rs:6:5
3+
|
4+
LL | final type Ovo;
5+
| -----^^^^^^^^^^
6+
| |
7+
| `final` because of this
8+
9+
error: `final` is only allowed on associated functions in traits
10+
--> $DIR/final-on-assoc-type-const.rs:8:5
11+
|
12+
LL | final type const QwQ: ();
13+
| -----^^^^^^^^^^^^^^^^^^^^
14+
| |
15+
| `final` because of this
16+
17+
error: aborting due to 2 previous errors
18+

tests/ui/traits/final/positions.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ final impl Trait for Foo {
3838

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

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

4846

tests/ui/traits/final/positions.stderr

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ LL | final trait Trait {
1515
= note: only associated functions in traits can be `final`
1616

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

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

8989
error: `final` is only allowed on associated functions in traits
90-
--> $DIR/positions.rs:43:5
90+
--> $DIR/positions.rs:42:5
9191
|
9292
LL | final const FOO: usize = 1;
9393
| -----^^^^^^^^^^^^^^^^^^^^^^
9494
| |
9595
| `final` because of this
9696

9797
error: `final` is only allowed on associated functions in traits
98-
--> $DIR/positions.rs:49:1
98+
--> $DIR/positions.rs:47:1
9999
|
100100
LL | final fn foo() {}
101101
| -----^^^^^^^^^
102102
| |
103103
| `final` because of this
104104

105105
error: `final` is only allowed on associated functions in traits
106-
--> $DIR/positions.rs:52:1
106+
--> $DIR/positions.rs:50:1
107107
|
108108
LL | final type FooTy = ();
109109
| -----^^^^^^^^^^^^^^^^^
110110
| |
111111
| `final` because of this
112112

113113
error: `final` is only allowed on associated functions in traits
114-
--> $DIR/positions.rs:55:1
114+
--> $DIR/positions.rs:53:1
115115
|
116116
LL | final const FOO: usize = 0;
117117
| -----^^^^^^^^^^^^^^^^^^^^^^
118118
| |
119119
| `final` because of this
120120

121121
error: `final` is only allowed on associated functions in traits
122-
--> $DIR/positions.rs:61:5
122+
--> $DIR/positions.rs:59:5
123123
|
124124
LL | final fn foo_extern();
125125
| -----^^^^^^^^^^^^^^^^^
126126
| |
127127
| `final` because of this
128128

129129
error: `final` is only allowed on associated functions in traits
130-
--> $DIR/positions.rs:64:5
130+
--> $DIR/positions.rs:62:5
131131
|
132132
LL | final type FooExtern;
133133
| -----^^^^^^^^^^^^^^^^
134134
| |
135135
| `final` because of this
136136

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

162-
error: cannot override `Foo` because it already has a `final` definition in the trait
163-
--> $DIR/positions.rs:39:5
164-
|
165-
LL | final type Foo = ();
166-
| ^^^^^^^^^^^^^^
167-
|
168-
note: `Foo` is marked final here
169-
--> $DIR/positions.rs:16:5
170-
|
171-
LL | final type Foo = ();
172-
| ^^^^^^^^^^^^^^
173-
174-
error: cannot override `FOO` because it already has a `final` definition in the trait
175-
--> $DIR/positions.rs:43:5
176-
|
177-
LL | final const FOO: usize = 1;
178-
| ^^^^^^^^^^^^^^^^^^^^^^
179-
|
180-
note: `FOO` is marked final here
181-
--> $DIR/positions.rs:19:5
182-
|
183-
LL | final const FOO: usize = 1;
184-
| ^^^^^^^^^^^^^^^^^^^^^^
185-
186-
error: aborting due to 21 previous errors
162+
error: aborting due to 19 previous errors
187163

0 commit comments

Comments
 (0)