Skip to content

Commit

Permalink
Rollup merge of rust-lang#58196 - varkor:const-fn-feature-gate-error,…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
kennytm committed Feb 16, 2019
2 parents f05e6bf + 8ca4406 commit 49107c3
Show file tree
Hide file tree
Showing 18 changed files with 352 additions and 103 deletions.
31 changes: 31 additions & 0 deletions src/librustc_mir/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,37 @@ let value = (&foo(), &foo());
```
"##,

E0723: r##"
An feature unstable in `const` contexts was used.
Erroneous code example:
```compile_fail,E0723
trait T {}
impl T for () {}
const fn foo() -> impl T { // error: `impl Trait` in const fn is unstable
()
}
```
To enable this feature on a nightly version of rustc, add the `const_fn`
feature flag:
```
#![feature(const_fn)]
trait T {}
impl T for () {}
const fn foo() -> impl T {
()
}
```
"##,

}

register_diagnostics! {
Expand Down
12 changes: 11 additions & 1 deletion src/librustc_mir/transform/qualify_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,17 @@ impl MirPass for QualifyAndPromoteConstants {
// enforce `min_const_fn` for stable const fns
use super::qualify_min_const_fn::is_min_const_fn;
if let Err((span, err)) = is_min_const_fn(tcx, def_id, mir) {
tcx.sess.span_err(span, &err);
let mut diag = struct_span_err!(
tcx.sess,
span,
E0723,
"{} (see issue #57563)",
err,
);
diag.help(
"add #![feature(const_fn)] to the crate attributes to enable",
);
diag.emit();
} else {
// this should not produce any errors, but better safe than sorry
// FIXME(#53819)
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
error: heap allocations are not allowed in const fn
error[E0723]: heap allocations are not allowed in const fn (see issue #57563)
--> $DIR/bad_const_fn_body_ice.rs:2:5
|
LL | vec![1, 2, 3] //~ ERROR heap allocations are not allowed in const fn
| ^^^^^^^^^^^^^
|
= help: add #![feature(const_fn)] to the crate attributes to enable
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0723`.
21 changes: 16 additions & 5 deletions src/test/ui/consts/min_const_fn/cast_errors.stderr
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
error: unsizing casts are not allowed in const fn
error[E0723]: unsizing casts are not allowed in const fn (see issue #57563)
--> $DIR/cast_errors.rs:3:41
|
LL | const fn unsize(x: &[u8; 3]) -> &[u8] { x }
| ^
|
= help: add #![feature(const_fn)] to the crate attributes to enable

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

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

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

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

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0723`.
5 changes: 4 additions & 1 deletion src/test/ui/consts/min_const_fn/cmp_fn_pointers.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
error: function pointers in const fn are unstable
error[E0723]: function pointers in const fn are unstable (see issue #57563)
--> $DIR/cmp_fn_pointers.rs:1:14
|
LL | const fn cmp(x: fn(), y: fn()) -> bool { //~ ERROR function pointers in const fn are unstable
| ^
|
= help: add #![feature(const_fn)] to the crate attributes to enable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0723`.
5 changes: 4 additions & 1 deletion src/test/ui/consts/min_const_fn/loop_ice.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
error: loops are not allowed in const fn
error[E0723]: loops are not allowed in const fn (see issue #57563)
--> $DIR/loop_ice.rs:2:5
|
LL | loop {} //~ ERROR loops are not allowed in const fn
| ^^^^^^^
|
= help: add #![feature(const_fn)] to the crate attributes to enable

error: aborting due to previous error

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

0 comments on commit 49107c3

Please sign in to comment.