Skip to content

Commit 7ab8a5f

Browse files
committed
E0379: Make diagnostic more precise
1 parent 43bbe57 commit 7ab8a5f

File tree

13 files changed

+31
-20
lines changed

13 files changed

+31
-20
lines changed

compiler/rustc_ast_passes/messages.ftl

+8-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,14 @@ ast_passes_tilde_const_disallowed = `~const` is not allowed here
233233
.item = this item cannot have `~const` trait bounds
234234
235235
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
238244
239245
ast_passes_trait_object_single_bound = only a single explicit lifetime bound is permitted
240246

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl<'a> AstValidator<'a> {
293293

294294
fn check_trait_fn_not_const(&self, constness: Const) {
295295
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 });
297297
}
298298
}
299299

compiler/rustc_ast_passes/src/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub struct TraitFnConst {
4949
#[primary_span]
5050
#[label]
5151
pub span: Span,
52+
pub in_impl: bool,
5253
}
5354

5455
#[derive(Diagnostic)]

compiler/rustc_error_codes/src/error_codes/E0379.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Erroneous code example:
66
trait Foo {
77
const fn bar() -> u32; // error!
88
}
9+
10+
impl Foo for () {
11+
const fn bar() -> u32 { 0 } // error!
12+
}
913
```
1014

1115
Trait methods cannot be declared `const` by design. For more information, see

tests/ui/consts/const-fn-mismatch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ trait Foo {
99

1010
impl Foo for u32 {
1111
const fn f() -> u32 {
12-
//~^ ERROR functions in traits cannot be declared const
12+
//~^ ERROR functions in trait impls cannot be declared const
1313
22
1414
}
1515
}

tests/ui/consts/const-fn-mismatch.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0379]: functions in traits cannot be declared const
1+
error[E0379]: functions in trait impls cannot be declared const
22
--> $DIR/const-fn-mismatch.rs:11:5
33
|
44
LL | const fn f() -> u32 {
5-
| ^^^^^ functions in traits cannot be const
5+
| ^^^^^ functions in trait impls cannot be const
66

77
error: aborting due to 1 previous error
88

tests/ui/feature-gates/feature-gate-min_const_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ trait Foo {
88
}
99

1010
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
1212
}
1313

1414
trait Bar {}

tests/ui/feature-gates/feature-gate-min_const_fn.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ error[E0379]: functions in traits cannot be declared const
1010
LL | const fn bar() -> u32 { 0 }
1111
| ^^^^^ functions in traits cannot be const
1212

13-
error[E0379]: functions in traits cannot be declared const
13+
error[E0379]: functions in trait impls cannot be declared const
1414
--> $DIR/feature-gate-min_const_fn.rs:11:5
1515
|
1616
LL | const fn foo() -> u32 { 0 }
17-
| ^^^^^ functions in traits cannot be const
17+
| ^^^^^ functions in trait impls cannot be const
1818

1919
error: aborting due to 3 previous errors
2020

tests/ui/mismatched_types/const-fn-in-trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0379]: functions in traits cannot be declared const
44
LL | const fn g();
55
| ^^^^^ functions in traits cannot be const
66

7-
error[E0379]: functions in traits cannot be declared const
7+
error[E0379]: functions in trait impls cannot be declared const
88
--> $DIR/const-fn-in-trait.rs:7:5
99
|
1010
LL | const fn f() -> u32 { 22 }
11-
| ^^^^^ functions in traits cannot be const
11+
| ^^^^^ functions in trait impls cannot be const
1212

1313
error: aborting due to 2 previous errors
1414

tests/ui/parser/fn-header-semantic-fail.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ fn main() {
2626
impl X for Y {
2727
async fn ft1() {} // OK.
2828
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
3030
extern "C" fn ft4() {}
3131
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
3333
//~| ERROR functions cannot be both `const` and `async`
3434
}
3535

tests/ui/parser/fn-header-semantic-fail.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ LL | const async unsafe extern "C" fn ft5();
2828
| | `async` because of this
2929
| `const` because of this
3030

31-
error[E0379]: functions in traits cannot be declared const
31+
error[E0379]: functions in trait impls cannot be declared const
3232
--> $DIR/fn-header-semantic-fail.rs:29:9
3333
|
3434
LL | const fn ft3() {}
35-
| ^^^^^ functions in traits cannot be const
35+
| ^^^^^ functions in trait impls cannot be const
3636

37-
error[E0379]: functions in traits cannot be declared const
37+
error[E0379]: functions in trait impls cannot be declared const
3838
--> $DIR/fn-header-semantic-fail.rs:31:9
3939
|
4040
LL | const async unsafe extern "C" fn ft5() {}
41-
| ^^^^^ functions in traits cannot be const
41+
| ^^^^^ functions in trait impls cannot be const
4242

4343
error: functions cannot be both `const` and `async`
4444
--> $DIR/fn-header-semantic-fail.rs:31:9

tests/ui/rfcs/rfc-2632-const-trait-impl/effects/trait-fn-const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait Trait {
77
}
88

99
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
1111
}
1212

1313
fn main() {}

tests/ui/rfcs/rfc-2632-const-trait-impl/effects/trait-fn-const.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0379]: functions in traits cannot be declared const
44
LL | const fn fun();
55
| ^^^^^ functions in traits cannot be const
66

7-
error[E0379]: functions in traits cannot be declared const
7+
error[E0379]: functions in trait impls cannot be declared const
88
--> $DIR/trait-fn-const.rs:10:5
99
|
1010
LL | const fn fun() {}
11-
| ^^^^^ functions in traits cannot be const
11+
| ^^^^^ functions in trait impls cannot be const
1212

1313
error: aborting due to 2 previous errors
1414

0 commit comments

Comments
 (0)