Skip to content

Commit 634022c

Browse files
authored
Rollup merge of rust-lang#58196 - varkor:const-fn-feature-gate-error, r=oli-obk
Add specific feature gate error for const-unstable features Before: ``` error: `impl Trait` in const fn is unstable --> src/lib.rs:7:19 | 7 | const fn foo() -> impl T { | ^^^^^^ error: aborting due to previous error ``` After: ``` error[E0723]: `impl Trait` in const fn is unstable (see issue rust-lang#57563) --> src/lib.rs:7:19 | 7 | const fn foo() -> impl T { | ^^^^^^ = help: add #![feature(const_fn)] to the crate attributes to enable error: aborting due to previous error ``` This improves the situation with rust-lang#57563. Fixes rust-lang#57544. Fixes rust-lang#54469. r? @oli-obk
2 parents b9efe60 + 8ca4406 commit 634022c

18 files changed

+352
-103
lines changed

src/librustc_mir/diagnostics.rs

+31
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,37 @@ let value = (&foo(), &foo());
23702370
```
23712371
"##,
23722372

2373+
E0723: r##"
2374+
An feature unstable in `const` contexts was used.
2375+
2376+
Erroneous code example:
2377+
2378+
```compile_fail,E0723
2379+
trait T {}
2380+
2381+
impl T for () {}
2382+
2383+
const fn foo() -> impl T { // error: `impl Trait` in const fn is unstable
2384+
()
2385+
}
2386+
```
2387+
2388+
To enable this feature on a nightly version of rustc, add the `const_fn`
2389+
feature flag:
2390+
2391+
```
2392+
#![feature(const_fn)]
2393+
2394+
trait T {}
2395+
2396+
impl T for () {}
2397+
2398+
const fn foo() -> impl T {
2399+
()
2400+
}
2401+
```
2402+
"##,
2403+
23732404
}
23742405

23752406
register_diagnostics! {

src/librustc_mir/transform/qualify_consts.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,17 @@ impl MirPass for QualifyAndPromoteConstants {
12061206
// enforce `min_const_fn` for stable const fns
12071207
use super::qualify_min_const_fn::is_min_const_fn;
12081208
if let Err((span, err)) = is_min_const_fn(tcx, def_id, mir) {
1209-
tcx.sess.span_err(span, &err);
1209+
let mut diag = struct_span_err!(
1210+
tcx.sess,
1211+
span,
1212+
E0723,
1213+
"{} (see issue #57563)",
1214+
err,
1215+
);
1216+
diag.help(
1217+
"add #![feature(const_fn)] to the crate attributes to enable",
1218+
);
1219+
diag.emit();
12101220
} else {
12111221
// this should not produce any errors, but better safe than sorry
12121222
// FIXME(#53819)
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
error: heap allocations are not allowed in const fn
1+
error[E0723]: heap allocations are not allowed in const fn (see issue #57563)
22
--> $DIR/bad_const_fn_body_ice.rs:2:5
33
|
44
LL | vec![1, 2, 3] //~ ERROR heap allocations are not allowed in const fn
55
| ^^^^^^^^^^^^^
66
|
7+
= help: add #![feature(const_fn)] to the crate attributes to enable
78
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
89

910
error: aborting due to previous error
1011

12+
For more information about this error, try `rustc --explain E0723`.
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
1-
error: unsizing casts are not allowed in const fn
1+
error[E0723]: unsizing casts are not allowed in const fn (see issue #57563)
22
--> $DIR/cast_errors.rs:3:41
33
|
44
LL | const fn unsize(x: &[u8; 3]) -> &[u8] { x }
55
| ^
6+
|
7+
= help: add #![feature(const_fn)] to the crate attributes to enable
68

7-
error: function pointers in const fn are unstable
9+
error[E0723]: function pointers in const fn are unstable (see issue #57563)
810
--> $DIR/cast_errors.rs:5:23
911
|
1012
LL | const fn closure() -> fn() { || {} }
1113
| ^^^^
14+
|
15+
= help: add #![feature(const_fn)] to the crate attributes to enable
1216

13-
error: function pointers in const fn are unstable
17+
error[E0723]: function pointers in const fn are unstable (see issue #57563)
1418
--> $DIR/cast_errors.rs:8:5
1519
|
1620
LL | (|| {}) as fn();
1721
| ^^^^^^^^^^^^^^^
22+
|
23+
= help: add #![feature(const_fn)] to the crate attributes to enable
1824

19-
error: function pointers in const fn are unstable
25+
error[E0723]: function pointers in const fn are unstable (see issue #57563)
2026
--> $DIR/cast_errors.rs:11:28
2127
|
2228
LL | const fn reify(f: fn()) -> unsafe fn() { f }
2329
| ^^^^^^^^^^^
30+
|
31+
= help: add #![feature(const_fn)] to the crate attributes to enable
2432

25-
error: function pointers in const fn are unstable
33+
error[E0723]: function pointers in const fn are unstable (see issue #57563)
2634
--> $DIR/cast_errors.rs:13:21
2735
|
2836
LL | const fn reify2() { main as unsafe fn(); }
2937
| ^^^^^^^^^^^^^^^^^^^
38+
|
39+
= help: add #![feature(const_fn)] to the crate attributes to enable
3040

3141
error: aborting due to 5 previous errors
3242

43+
For more information about this error, try `rustc --explain E0723`.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error: function pointers in const fn are unstable
1+
error[E0723]: function pointers in const fn are unstable (see issue #57563)
22
--> $DIR/cmp_fn_pointers.rs:1:14
33
|
44
LL | const fn cmp(x: fn(), y: fn()) -> bool { //~ ERROR function pointers in const fn are unstable
55
| ^
6+
|
7+
= help: add #![feature(const_fn)] to the crate attributes to enable
68

79
error: aborting due to previous error
810

11+
For more information about this error, try `rustc --explain E0723`.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error: loops are not allowed in const fn
1+
error[E0723]: loops are not allowed in const fn (see issue #57563)
22
--> $DIR/loop_ice.rs:2:5
33
|
44
LL | loop {} //~ ERROR loops are not allowed in const fn
55
| ^^^^^^^
6+
|
7+
= help: add #![feature(const_fn)] to the crate attributes to enable
68

79
error: aborting due to previous error
810

11+
For more information about this error, try `rustc --explain E0723`.

0 commit comments

Comments
 (0)