Skip to content

Commit e1acdf5

Browse files
authored
Rollup merge of #88907 - WaffleLapkin:targeted_const_fn_with_a_bound_in_impl_block_error, r=estebank
Highlight the `const fn` if error happened because of a bound on the impl block Currently, for the following code, the compiler produces the errors like the following: ```rust struct Type<T>(T); impl<T: Clone> Type<T> { const fn 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 PR 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 ``` --- I've originally wanted to point directly to `const` token, but couldn't find a way to get it's span. It seems like this span is lost during the AST -> HIR lowering. Also, since the errors for object casts in `const fn`s (`&T` -> `&dyn Trait`) seem to trigger the same error, this PR accidentally changes these errors too. Not sure if it's desired or how to fix this. P.S. it's my first time contributing to diagnostics, so feedback is very appreciated! --- r? ```@estebank``` ```@rustbot``` label: +A-diagnostics
2 parents 1bf94a1 + 6ec7255 commit e1acdf5

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)