File tree 13 files changed +31
-20
lines changed
rustc_error_codes/src/error_codes
rfcs/rfc-2632-const-trait-impl/effects
13 files changed +31
-20
lines changed Original file line number Diff line number Diff line change @@ -233,8 +233,14 @@ ast_passes_tilde_const_disallowed = `~const` is not allowed here
233
233
.item = this item cannot have `~const` trait bounds
234
234
235
235
ast_passes_trait_fn_const =
236
- functions in traits cannot be declared const
237
- .label = functions in traits cannot be const
236
+ functions in { $in_impl ->
237
+ [ true ] trait impls
238
+ *[ false ] traits
239
+ } cannot be declared const
240
+ .label = functions in { $in_impl ->
241
+ [ true ] trait impls
242
+ *[ false ] traits
243
+ } cannot be const
238
244
239
245
ast_passes_trait_object_single_bound = only a single explicit lifetime bound is permitted
240
246
Original file line number Diff line number Diff line change @@ -293,7 +293,7 @@ impl<'a> AstValidator<'a> {
293
293
294
294
fn check_trait_fn_not_const ( & self , constness : Const ) {
295
295
if let Const :: Yes ( span) = constness {
296
- self . dcx ( ) . emit_err ( errors:: TraitFnConst { span } ) ;
296
+ self . dcx ( ) . emit_err ( errors:: TraitFnConst { span, in_impl : self . in_trait_impl } ) ;
297
297
}
298
298
}
299
299
Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ pub struct TraitFnConst {
49
49
#[ primary_span]
50
50
#[ label]
51
51
pub span : Span ,
52
+ pub in_impl : bool ,
52
53
}
53
54
54
55
#[ derive( Diagnostic ) ]
Original file line number Diff line number Diff line change @@ -6,6 +6,10 @@ Erroneous code example:
6
6
trait Foo {
7
7
const fn bar() -> u32; // error!
8
8
}
9
+
10
+ impl Foo for () {
11
+ const fn bar() -> u32 { 0 } // error!
12
+ }
9
13
```
10
14
11
15
Trait methods cannot be declared ` const ` by design. For more information, see
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ trait Foo {
9
9
10
10
impl Foo for u32 {
11
11
const fn f ( ) -> u32 {
12
- //~^ ERROR functions in traits cannot be declared const
12
+ //~^ ERROR functions in trait impls cannot be declared const
13
13
22
14
14
}
15
15
}
Original file line number Diff line number Diff line change 1
- error[E0379]: functions in traits cannot be declared const
1
+ error[E0379]: functions in trait impls cannot be declared const
2
2
--> $DIR/const-fn-mismatch.rs:11:5
3
3
|
4
4
LL | const fn f() -> u32 {
5
- | ^^^^^ functions in traits cannot be const
5
+ | ^^^^^ functions in trait impls cannot be const
6
6
7
7
error: aborting due to 1 previous error
8
8
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ trait Foo {
8
8
}
9
9
10
10
impl Foo for u32 {
11
- const fn foo ( ) -> u32 { 0 } //~ ERROR functions in traits cannot be declared const
11
+ const fn foo ( ) -> u32 { 0 } //~ ERROR functions in trait impls cannot be declared const
12
12
}
13
13
14
14
trait Bar { }
Original file line number Diff line number Diff line change @@ -10,11 +10,11 @@ error[E0379]: functions in traits cannot be declared const
10
10
LL | const fn bar() -> u32 { 0 }
11
11
| ^^^^^ functions in traits cannot be const
12
12
13
- error[E0379]: functions in traits cannot be declared const
13
+ error[E0379]: functions in trait impls cannot be declared const
14
14
--> $DIR/feature-gate-min_const_fn.rs:11:5
15
15
|
16
16
LL | const fn foo() -> u32 { 0 }
17
- | ^^^^^ functions in traits cannot be const
17
+ | ^^^^^ functions in trait impls cannot be const
18
18
19
19
error: aborting due to 3 previous errors
20
20
Original file line number Diff line number Diff line change @@ -4,11 +4,11 @@ error[E0379]: functions in traits cannot be declared const
4
4
LL | const fn g();
5
5
| ^^^^^ functions in traits cannot be const
6
6
7
- error[E0379]: functions in traits cannot be declared const
7
+ error[E0379]: functions in trait impls cannot be declared const
8
8
--> $DIR/const-fn-in-trait.rs:7:5
9
9
|
10
10
LL | const fn f() -> u32 { 22 }
11
- | ^^^^^ functions in traits cannot be const
11
+ | ^^^^^ functions in trait impls cannot be const
12
12
13
13
error: aborting due to 2 previous errors
14
14
Original file line number Diff line number Diff line change @@ -26,10 +26,10 @@ fn main() {
26
26
impl X for Y {
27
27
async fn ft1 ( ) { } // OK.
28
28
unsafe fn ft2 ( ) { } // OK.
29
- const fn ft3 ( ) { } //~ ERROR functions in traits cannot be declared const
29
+ const fn ft3 ( ) { } //~ ERROR functions in trait impls cannot be declared const
30
30
extern "C" fn ft4 ( ) { }
31
31
const async unsafe extern "C" fn ft5 ( ) { }
32
- //~^ ERROR functions in traits cannot be declared const
32
+ //~^ ERROR functions in trait impls cannot be declared const
33
33
//~| ERROR functions cannot be both `const` and `async`
34
34
}
35
35
Original file line number Diff line number Diff line change @@ -28,17 +28,17 @@ LL | const async unsafe extern "C" fn ft5();
28
28
| | `async` because of this
29
29
| `const` because of this
30
30
31
- error[E0379]: functions in traits cannot be declared const
31
+ error[E0379]: functions in trait impls cannot be declared const
32
32
--> $DIR/fn-header-semantic-fail.rs:29:9
33
33
|
34
34
LL | const fn ft3() {}
35
- | ^^^^^ functions in traits cannot be const
35
+ | ^^^^^ functions in trait impls cannot be const
36
36
37
- error[E0379]: functions in traits cannot be declared const
37
+ error[E0379]: functions in trait impls cannot be declared const
38
38
--> $DIR/fn-header-semantic-fail.rs:31:9
39
39
|
40
40
LL | const async unsafe extern "C" fn ft5() {}
41
- | ^^^^^ functions in traits cannot be const
41
+ | ^^^^^ functions in trait impls cannot be const
42
42
43
43
error: functions cannot be both `const` and `async`
44
44
--> $DIR/fn-header-semantic-fail.rs:31:9
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ trait Trait {
7
7
}
8
8
9
9
impl const Trait for ( ) {
10
- const fn fun ( ) { } //~ ERROR functions in traits cannot be declared const
10
+ const fn fun ( ) { } //~ ERROR functions in trait impls cannot be declared const
11
11
}
12
12
13
13
fn main ( ) { }
Original file line number Diff line number Diff line change @@ -4,11 +4,11 @@ error[E0379]: functions in traits cannot be declared const
4
4
LL | const fn fun();
5
5
| ^^^^^ functions in traits cannot be const
6
6
7
- error[E0379]: functions in traits cannot be declared const
7
+ error[E0379]: functions in trait impls cannot be declared const
8
8
--> $DIR/trait-fn-const.rs:10:5
9
9
|
10
10
LL | const fn fun() {}
11
- | ^^^^^ functions in traits cannot be const
11
+ | ^^^^^ functions in trait impls cannot be const
12
12
13
13
error: aborting due to 2 previous errors
14
14
You can’t perform that action at this time.
0 commit comments