Skip to content

Commit 517b3e6

Browse files
committed
Only offer the [const] Destruct suggestion on a nightly compiler
The suggested bound requires the unstable `const_destruct` feature, so it is not actionable on a stable compiler. Add a run-make test that verifies the suggestion is only emitted on nightly.
1 parent 714aea0 commit 517b3e6

5 files changed

Lines changed: 60 additions & 1 deletion

File tree

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,10 @@ impl<'tcx> NonConstOp<'tcx> for LiveDrop<'tcx> {
614614
};
615615

616616
// If the dropped type is a type parameter, suggest adding a `[const] Destruct` bound.
617-
if let Param(param_ty) = self.dropped_ty.kind() {
617+
// The suggestion is only offered on nightly, since `[const]` bounds are unstable.
618+
if let Param(param_ty) = self.dropped_ty.kind()
619+
&& ccx.tcx.sess.is_nightly_build()
620+
{
618621
let tcx = ccx.tcx;
619622
let caller = ccx.def_id();
620623
if let Some(generics) = tcx.hir_node_by_def_id(caller).generics() {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0493]: destructor of `T` cannot be evaluated at compile-time
2+
--> const-drop.rs:1:24
3+
|
4+
LL | const fn const_drop<T>(_: T) {}
5+
| ^ - value is dropped here
6+
| |
7+
| the destructor for this type cannot be evaluated in constant functions
8+
|
9+
help: consider restricting type parameter `T` with unstable trait `Destruct`
10+
|
11+
LL | const fn const_drop<T: [const] Destruct>(_: T) {}
12+
| ++++++++++++++++++
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0493`.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0493]: destructor of `T` cannot be evaluated at compile-time
2+
--> const-drop.rs:1:24
3+
|
4+
1 | const fn const_drop<T>(_: T) {}
5+
| ^ - value is dropped here
6+
| |
7+
| the destructor for this type cannot be evaluated in constant functions
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0493`.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const fn const_drop<T>(_: T) {}
2+
3+
fn main() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ needs-target-std
2+
//
3+
// Test that the suggestion to constrain a type parameter that is dropped in a const
4+
// function with a `[const] Destruct` bound is only offered on nightly, since the bound
5+
// requires an unstable feature.
6+
7+
use run_make_support::{diff, rustc};
8+
9+
fn main() {
10+
let out = rustc()
11+
.input("const-drop.rs")
12+
.env("RUSTC_BOOTSTRAP", "-1")
13+
.run_fail()
14+
.assert_stderr_not_contains("consider restricting type parameter `T`")
15+
.stderr_utf8();
16+
diff().expected_file("const-drop-stable.stderr").actual_text("(rustc)", &out).run();
17+
let out = rustc()
18+
.input("const-drop.rs")
19+
.ui_testing()
20+
.run_fail()
21+
.assert_stderr_contains(
22+
"consider restricting type parameter `T` with unstable trait `Destruct`",
23+
)
24+
.stderr_utf8();
25+
diff().expected_file("const-drop-nightly.stderr").actual_text("(rustc)", &out).run();
26+
}

0 commit comments

Comments
 (0)