From ae8e401c9f8b51dc8ff3ac5914e0203ea95e26d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Tue, 2 Jan 2024 00:26:50 +0100 Subject: [PATCH] E0379: Make diagnostic more precise --- compiler/rustc_ast_passes/messages.ftl | 10 ++++++++-- compiler/rustc_ast_passes/src/ast_validation.rs | 2 +- compiler/rustc_ast_passes/src/errors.rs | 1 + compiler/rustc_error_codes/src/error_codes/E0379.md | 4 ++++ tests/ui/consts/const-fn-mismatch.rs | 2 +- tests/ui/consts/const-fn-mismatch.stderr | 4 ++-- tests/ui/feature-gates/feature-gate-min_const_fn.rs | 2 +- .../ui/feature-gates/feature-gate-min_const_fn.stderr | 4 ++-- tests/ui/mismatched_types/const-fn-in-trait.stderr | 4 ++-- tests/ui/parser/fn-header-semantic-fail.rs | 4 ++-- tests/ui/parser/fn-header-semantic-fail.stderr | 8 ++++---- .../effects/trait-fn-const.rs | 2 +- .../effects/trait-fn-const.stderr | 4 ++-- 13 files changed, 31 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_ast_passes/messages.ftl b/compiler/rustc_ast_passes/messages.ftl index b5612c1820d..b7274db029f 100644 --- a/compiler/rustc_ast_passes/messages.ftl +++ b/compiler/rustc_ast_passes/messages.ftl @@ -233,8 +233,14 @@ ast_passes_tilde_const_disallowed = `~const` is not allowed here .item = this item cannot have `~const` trait bounds ast_passes_trait_fn_const = - functions in traits cannot be declared const - .label = functions in traits cannot be const + functions in {$in_impl -> + [true] trait impls + *[false] traits + } cannot be declared const + .label = functions in {$in_impl -> + [true] trait impls + *[false] traits + } cannot be const ast_passes_trait_object_single_bound = only a single explicit lifetime bound is permitted diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index bc5cf463f12..39ea684087e 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -293,7 +293,7 @@ impl<'a> AstValidator<'a> { fn check_trait_fn_not_const(&self, constness: Const) { if let Const::Yes(span) = constness { - self.dcx().emit_err(errors::TraitFnConst { span }); + self.dcx().emit_err(errors::TraitFnConst { span, in_impl: self.in_trait_impl }); } } diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 0cec4374be2..ebaa7ccecc9 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -49,6 +49,7 @@ pub struct TraitFnConst { #[primary_span] #[label] pub span: Span, + pub in_impl: bool, } #[derive(Diagnostic)] diff --git a/compiler/rustc_error_codes/src/error_codes/E0379.md b/compiler/rustc_error_codes/src/error_codes/E0379.md index ab438e41447..35f546cfdb7 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0379.md +++ b/compiler/rustc_error_codes/src/error_codes/E0379.md @@ -6,6 +6,10 @@ Erroneous code example: trait Foo { const fn bar() -> u32; // error! } + +impl Foo for () { + const fn bar() -> u32 { 0 } // error! +} ``` Trait methods cannot be declared `const` by design. For more information, see diff --git a/tests/ui/consts/const-fn-mismatch.rs b/tests/ui/consts/const-fn-mismatch.rs index 3107b8128e6..b17e4cedd3f 100644 --- a/tests/ui/consts/const-fn-mismatch.rs +++ b/tests/ui/consts/const-fn-mismatch.rs @@ -9,7 +9,7 @@ trait Foo { impl Foo for u32 { const fn f() -> u32 { - //~^ ERROR functions in traits cannot be declared const + //~^ ERROR functions in trait impls cannot be declared const 22 } } diff --git a/tests/ui/consts/const-fn-mismatch.stderr b/tests/ui/consts/const-fn-mismatch.stderr index beaf52c0cfb..7d681c212b9 100644 --- a/tests/ui/consts/const-fn-mismatch.stderr +++ b/tests/ui/consts/const-fn-mismatch.stderr @@ -1,8 +1,8 @@ -error[E0379]: functions in traits cannot be declared const +error[E0379]: functions in trait impls cannot be declared const --> $DIR/const-fn-mismatch.rs:11:5 | LL | const fn f() -> u32 { - | ^^^^^ functions in traits cannot be const + | ^^^^^ functions in trait impls cannot be const error: aborting due to 1 previous error diff --git a/tests/ui/feature-gates/feature-gate-min_const_fn.rs b/tests/ui/feature-gates/feature-gate-min_const_fn.rs index 8f9b433009d..3d61a9eb937 100644 --- a/tests/ui/feature-gates/feature-gate-min_const_fn.rs +++ b/tests/ui/feature-gates/feature-gate-min_const_fn.rs @@ -8,7 +8,7 @@ trait Foo { } impl Foo for u32 { - const fn foo() -> u32 { 0 } //~ ERROR functions in traits cannot be declared const + const fn foo() -> u32 { 0 } //~ ERROR functions in trait impls cannot be declared const } trait Bar {} diff --git a/tests/ui/feature-gates/feature-gate-min_const_fn.stderr b/tests/ui/feature-gates/feature-gate-min_const_fn.stderr index d7a58591364..abc9ddabd51 100644 --- a/tests/ui/feature-gates/feature-gate-min_const_fn.stderr +++ b/tests/ui/feature-gates/feature-gate-min_const_fn.stderr @@ -10,11 +10,11 @@ error[E0379]: functions in traits cannot be declared const LL | const fn bar() -> u32 { 0 } | ^^^^^ functions in traits cannot be const -error[E0379]: functions in traits cannot be declared const +error[E0379]: functions in trait impls cannot be declared const --> $DIR/feature-gate-min_const_fn.rs:11:5 | LL | const fn foo() -> u32 { 0 } - | ^^^^^ functions in traits cannot be const + | ^^^^^ functions in trait impls cannot be const error: aborting due to 3 previous errors diff --git a/tests/ui/mismatched_types/const-fn-in-trait.stderr b/tests/ui/mismatched_types/const-fn-in-trait.stderr index 7d1fbe45c53..75122237bf5 100644 --- a/tests/ui/mismatched_types/const-fn-in-trait.stderr +++ b/tests/ui/mismatched_types/const-fn-in-trait.stderr @@ -4,11 +4,11 @@ error[E0379]: functions in traits cannot be declared const LL | const fn g(); | ^^^^^ functions in traits cannot be const -error[E0379]: functions in traits cannot be declared const +error[E0379]: functions in trait impls cannot be declared const --> $DIR/const-fn-in-trait.rs:7:5 | LL | const fn f() -> u32 { 22 } - | ^^^^^ functions in traits cannot be const + | ^^^^^ functions in trait impls cannot be const error: aborting due to 2 previous errors diff --git a/tests/ui/parser/fn-header-semantic-fail.rs b/tests/ui/parser/fn-header-semantic-fail.rs index f8b58cad7c1..f01e1c2277c 100644 --- a/tests/ui/parser/fn-header-semantic-fail.rs +++ b/tests/ui/parser/fn-header-semantic-fail.rs @@ -26,10 +26,10 @@ fn main() { impl X for Y { async fn ft1() {} // OK. unsafe fn ft2() {} // OK. - const fn ft3() {} //~ ERROR functions in traits cannot be declared const + const fn ft3() {} //~ ERROR functions in trait impls cannot be declared const extern "C" fn ft4() {} const async unsafe extern "C" fn ft5() {} - //~^ ERROR functions in traits cannot be declared const + //~^ ERROR functions in trait impls cannot be declared const //~| ERROR functions cannot be both `const` and `async` } diff --git a/tests/ui/parser/fn-header-semantic-fail.stderr b/tests/ui/parser/fn-header-semantic-fail.stderr index cdf01e0c5df..1f280424cf3 100644 --- a/tests/ui/parser/fn-header-semantic-fail.stderr +++ b/tests/ui/parser/fn-header-semantic-fail.stderr @@ -28,17 +28,17 @@ LL | const async unsafe extern "C" fn ft5(); | | `async` because of this | `const` because of this -error[E0379]: functions in traits cannot be declared const +error[E0379]: functions in trait impls cannot be declared const --> $DIR/fn-header-semantic-fail.rs:29:9 | LL | const fn ft3() {} - | ^^^^^ functions in traits cannot be const + | ^^^^^ functions in trait impls cannot be const -error[E0379]: functions in traits cannot be declared const +error[E0379]: functions in trait impls cannot be declared const --> $DIR/fn-header-semantic-fail.rs:31:9 | LL | const async unsafe extern "C" fn ft5() {} - | ^^^^^ functions in traits cannot be const + | ^^^^^ functions in trait impls cannot be const error: functions cannot be both `const` and `async` --> $DIR/fn-header-semantic-fail.rs:31:9 diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/trait-fn-const.rs b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/trait-fn-const.rs index 40e8bfdcc17..0d1c926fef5 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/trait-fn-const.rs +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/trait-fn-const.rs @@ -7,7 +7,7 @@ trait Trait { } impl const Trait for () { - const fn fun() {} //~ ERROR functions in traits cannot be declared const + const fn fun() {} //~ ERROR functions in trait impls cannot be declared const } fn main() {} diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/trait-fn-const.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/trait-fn-const.stderr index 09fe580ae5c..cf8a2772d89 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/trait-fn-const.stderr +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/trait-fn-const.stderr @@ -4,11 +4,11 @@ error[E0379]: functions in traits cannot be declared const LL | const fn fun(); | ^^^^^ functions in traits cannot be const -error[E0379]: functions in traits cannot be declared const +error[E0379]: functions in trait impls cannot be declared const --> $DIR/trait-fn-const.rs:10:5 | LL | const fn fun() {} - | ^^^^^ functions in traits cannot be const + | ^^^^^ functions in trait impls cannot be const error: aborting due to 2 previous errors