Skip to content

Commit 6ec7255

Browse files
committed
Highlight the const function if error happened because of a bound on the impl block
Currently, for the following code, the compiler produces the errors like the following error: ```rust struct Type<T> impl<T: Clone> Type<T> { fn const f() {} } ``` ```text error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable --> ./test.rs:3:6 | 3 | impl<T: Clone> Type<T> { | ^ | = note: see issue #57563 <#57563> for more information = help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable ``` This can be confusing (especially to newcomers) since the error mentions "const fn parameters", but highlights only the impl. This commits adds function highlighting, changing the error to the following: ```text error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable --> ./test.rs:3:6 | 3 | impl<T: Clone> Type<T> { | ^ 4 | pub const fn f() {} | ---------------- function declared as const here | = note: see issue #57563 <#57563> for more information = help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable ```
1 parent d2dfb0e commit 6ec7255

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,21 @@ pub mod ty {
599599
}
600600

601601
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
602-
feature_err(
602+
let mut builder = feature_err(
603603
&ccx.tcx.sess.parse_sess,
604604
sym::const_fn_trait_bound,
605605
span,
606606
"trait bounds other than `Sized` on const fn parameters are unstable",
607-
)
607+
);
608+
609+
match ccx.fn_sig() {
610+
Some(fn_sig) if !fn_sig.span.contains(span) => {
611+
builder.span_label(fn_sig.span, "function declared as const here");
612+
}
613+
_ => {}
614+
}
615+
616+
builder
608617
}
609618
}
610619

src/test/ui/consts/min_const_fn/min_const_fn.stderr

+18-3
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ error[E0658]: trait bounds other than `Sized` on const fn parameters are unstabl
214214
|
215215
LL | impl<T: std::fmt::Debug> Foo<T> {
216216
| ^
217+
LL |
218+
LL | const fn foo(&self) {}
219+
| ------------------- function declared as const here
217220
|
218221
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
219222
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
@@ -223,6 +226,9 @@ error[E0658]: trait bounds other than `Sized` on const fn parameters are unstabl
223226
|
224227
LL | impl<T: std::fmt::Debug + Sized> Foo<T> {
225228
| ^
229+
LL |
230+
LL | const fn foo2(&self) {}
231+
| -------------------- function declared as const here
226232
|
227233
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
228234
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
@@ -232,6 +238,9 @@ error[E0658]: trait bounds other than `Sized` on const fn parameters are unstabl
232238
|
233239
LL | impl<T: Sync + Sized> Foo<T> {
234240
| ^
241+
LL |
242+
LL | const fn foo3(&self) {}
243+
| -------------------- function declared as const here
235244
|
236245
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
237246
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
@@ -292,7 +301,9 @@ error[E0658]: trait bounds other than `Sized` on const fn parameters are unstabl
292301
--> $DIR/min_const_fn.rs:139:41
293302
|
294303
LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
295-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
304+
| ------------------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
305+
| |
306+
| function declared as const here
296307
|
297308
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
298309
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
@@ -301,7 +312,9 @@ error[E0658]: trait bounds other than `Sized` on const fn parameters are unstabl
301312
--> $DIR/min_const_fn.rs:139:42
302313
|
303314
LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
304-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
315+
| ------------------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^
316+
| |
317+
| function declared as const here
305318
|
306319
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
307320
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
@@ -310,7 +323,9 @@ error[E0658]: trait bounds other than `Sized` on const fn parameters are unstabl
310323
--> $DIR/min_const_fn.rs:139:42
311324
|
312325
LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
313-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
326+
| ------------------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^
327+
| |
328+
| function declared as const here
314329
|
315330
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
316331
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable

src/test/ui/consts/min_const_fn/min_const_fn_dyn.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
22
--> $DIR/min_const_fn_dyn.rs:9:5
33
|
4+
LL | const fn no_inner_dyn_trait2(x: Hide) {
5+
| ------------------------------------- function declared as const here
46
LL | x.0.field;
57
| ^^^^^^^^^
68
|
@@ -11,7 +13,9 @@ error[E0658]: trait bounds other than `Sized` on const fn parameters are unstabl
1113
--> $DIR/min_const_fn_dyn.rs:12:66
1214
|
1315
LL | const fn no_inner_dyn_trait_ret() -> Hide { Hide(HasDyn { field: &0 }) }
14-
| ^^
16+
| ----------------------------------------- ^^
17+
| |
18+
| function declared as const here
1519
|
1620
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
1721
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable

0 commit comments

Comments
 (0)