Skip to content

Commit b7d9de7

Browse files
authored
Rollup merge of #102194 - fee1-dead-contrib:improve-const-drop, r=oli-obk
Note the type when unable to drop values in compile time
2 parents 07467c5 + 2ce1cd5 commit b7d9de7

37 files changed

+158
-150
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
10091009

10101010
if needs_non_const_drop {
10111011
self.check_op_spanned(
1012-
ops::LiveDrop { dropped_at: Some(terminator.source_info.span) },
1012+
ops::LiveDrop {
1013+
dropped_at: Some(terminator.source_info.span),
1014+
dropped_ty: ty_of_dropped_place,
1015+
},
10131016
err_span,
10141017
);
10151018
}

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,11 @@ impl<'tcx> NonConstOp<'tcx> for InlineAsm {
422422
}
423423

424424
#[derive(Debug)]
425-
pub struct LiveDrop {
425+
pub struct LiveDrop<'tcx> {
426426
pub dropped_at: Option<Span>,
427+
pub dropped_ty: Ty<'tcx>,
427428
}
428-
impl<'tcx> NonConstOp<'tcx> for LiveDrop {
429+
impl<'tcx> NonConstOp<'tcx> for LiveDrop<'tcx> {
429430
fn build_error(
430431
&self,
431432
ccx: &ConstCx<'_, 'tcx>,
@@ -435,9 +436,13 @@ impl<'tcx> NonConstOp<'tcx> for LiveDrop {
435436
ccx.tcx.sess,
436437
span,
437438
E0493,
438-
"destructors cannot be evaluated at compile-time"
439+
"destructor of `{}` cannot be evaluated at compile-time",
440+
self.dropped_ty,
441+
);
442+
err.span_label(
443+
span,
444+
format!("the destructor for this type cannot be evaluated in {}s", ccx.const_kind()),
439445
);
440-
err.span_label(span, format!("{}s cannot evaluate destructors", ccx.const_kind()));
441446
if let Some(span) = self.dropped_at {
442447
err.span_label(span, "value is dropped here");
443448
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_middle::mir::visit::Visitor;
22
use rustc_middle::mir::{self, BasicBlock, Location};
3-
use rustc_middle::ty::TyCtxt;
3+
use rustc_middle::ty::{Ty, TyCtxt};
44
use rustc_span::{symbol::sym, Span};
55

66
use super::check::Qualifs;
@@ -58,9 +58,9 @@ impl<'mir, 'tcx> std::ops::Deref for CheckLiveDrops<'mir, 'tcx> {
5858
}
5959
}
6060

61-
impl CheckLiveDrops<'_, '_> {
62-
fn check_live_drop(&self, span: Span) {
63-
ops::LiveDrop { dropped_at: None }.build_error(self.ccx, span).emit();
61+
impl<'tcx> CheckLiveDrops<'_, 'tcx> {
62+
fn check_live_drop(&self, span: Span, dropped_ty: Ty<'tcx>) {
63+
ops::LiveDrop { dropped_at: None, dropped_ty }.build_error(self.ccx, span).emit();
6464
}
6565
}
6666

@@ -90,7 +90,7 @@ impl<'tcx> Visitor<'tcx> for CheckLiveDrops<'_, 'tcx> {
9090
}
9191

9292
if dropped_place.is_indirect() {
93-
self.check_live_drop(terminator.source_info.span);
93+
self.check_live_drop(terminator.source_info.span, dropped_ty);
9494
return;
9595
}
9696

@@ -101,7 +101,7 @@ impl<'tcx> Visitor<'tcx> for CheckLiveDrops<'_, 'tcx> {
101101
if self.qualifs.needs_non_const_drop(self.ccx, dropped_place.local, location) {
102102
// Use the span where the dropped local was declared for the error.
103103
let span = self.body.local_decls[dropped_place.local].source_info.span;
104-
self.check_live_drop(span);
104+
self.check_live_drop(span, dropped_ty);
105105
}
106106
}
107107

src/test/ui/check-static-values-constraints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static STATIC8: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
6363
// This example should fail because field1 in the base struct is not safe
6464
static STATIC9: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
6565
..SafeStruct{field1: SafeEnum::Variant3(WithDtor),
66-
//~^ ERROR destructors cannot be evaluated at compile-time
66+
//~^ ERROR destructor of
6767
field2: SafeEnum::Variant1}};
6868

6969
struct UnsafeStruct;

src/test/ui/check-static-values-constraints.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0493]: destructors cannot be evaluated at compile-time
1+
error[E0493]: destructor of `SafeStruct` cannot be evaluated at compile-time
22
--> $DIR/check-static-values-constraints.rs:65:43
33
|
44
LL | ..SafeStruct{field1: SafeEnum::Variant3(WithDtor),
@@ -7,7 +7,7 @@ LL | |
77
LL | | field2: SafeEnum::Variant1}};
88
| | ^- value is dropped here
99
| |________________________________________________________________________________|
10-
| statics cannot evaluate destructors
10+
| the destructor for this type cannot be evaluated in statics
1111

1212
error[E0010]: allocations are not allowed in statics
1313
--> $DIR/check-static-values-constraints.rs:79:33

src/test/ui/consts/const-eval/const_let.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ const X2: FakeNeedsDrop = { let x; x = FakeNeedsDrop; x };
1414

1515
// error
1616
const Y: FakeNeedsDrop = { let mut x = FakeNeedsDrop; x = FakeNeedsDrop; x };
17-
//~^ ERROR destructors cannot be evaluated at compile-time
17+
//~^ ERROR destructor of
1818

1919
// error
2020
const Y2: FakeNeedsDrop = { let mut x; x = FakeNeedsDrop; x = FakeNeedsDrop; x };
21-
//~^ ERROR destructors cannot be evaluated at compile-time
21+
//~^ ERROR destructor of
2222

2323
// error
2424
const Z: () = { let mut x = None; x = Some(FakeNeedsDrop); };
25-
//~^ ERROR destructors cannot be evaluated at compile-time
25+
//~^ ERROR destructor of
2626

2727
// error
2828
const Z2: () = { let mut x; x = None; x = Some(FakeNeedsDrop); };
29-
//~^ ERROR destructors cannot be evaluated at compile-time
29+
//~^ ERROR destructor of

src/test/ui/consts/const-eval/const_let.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
error[E0493]: destructors cannot be evaluated at compile-time
1+
error[E0493]: destructor of `FakeNeedsDrop` cannot be evaluated at compile-time
22
--> $DIR/const_let.rs:16:32
33
|
44
LL | const Y: FakeNeedsDrop = { let mut x = FakeNeedsDrop; x = FakeNeedsDrop; x };
55
| ^^^^^ - value is dropped here
66
| |
7-
| constants cannot evaluate destructors
7+
| the destructor for this type cannot be evaluated in constants
88

9-
error[E0493]: destructors cannot be evaluated at compile-time
9+
error[E0493]: destructor of `FakeNeedsDrop` cannot be evaluated at compile-time
1010
--> $DIR/const_let.rs:20:33
1111
|
1212
LL | const Y2: FakeNeedsDrop = { let mut x; x = FakeNeedsDrop; x = FakeNeedsDrop; x };
1313
| ^^^^^ - value is dropped here
1414
| |
15-
| constants cannot evaluate destructors
15+
| the destructor for this type cannot be evaluated in constants
1616

17-
error[E0493]: destructors cannot be evaluated at compile-time
17+
error[E0493]: destructor of `Option<FakeNeedsDrop>` cannot be evaluated at compile-time
1818
--> $DIR/const_let.rs:24:21
1919
|
2020
LL | const Z: () = { let mut x = None; x = Some(FakeNeedsDrop); };
2121
| ^^^^^ - value is dropped here
2222
| |
23-
| constants cannot evaluate destructors
23+
| the destructor for this type cannot be evaluated in constants
2424

25-
error[E0493]: destructors cannot be evaluated at compile-time
25+
error[E0493]: destructor of `Option<FakeNeedsDrop>` cannot be evaluated at compile-time
2626
--> $DIR/const_let.rs:28:22
2727
|
2828
LL | const Z2: () = { let mut x; x = None; x = Some(FakeNeedsDrop); };
2929
| ^^^^^ - value is dropped here
3030
| |
31-
| constants cannot evaluate destructors
31+
| the destructor for this type cannot be evaluated in constants
3232

3333
error: aborting due to 4 previous errors
3434

src/test/ui/consts/const-eval/issue-65394.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// We will likely have to change this behavior before we allow `&mut` in a `const`.
55

66
const _: Vec<i32> = {
7-
let mut x = Vec::<i32>::new(); //~ ERROR destructors cannot be evaluated at compile-time
7+
let mut x = Vec::<i32>::new(); //~ ERROR destructor of
88
let r = &mut x; //~ ERROR mutable references are not allowed in constants
99
let y = x;
1010
y

src/test/ui/consts/const-eval/issue-65394.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | let r = &mut x;
77
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
88
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
99

10-
error[E0493]: destructors cannot be evaluated at compile-time
10+
error[E0493]: destructor of `Vec<i32>` cannot be evaluated at compile-time
1111
--> $DIR/issue-65394.rs:7:9
1212
|
1313
LL | let mut x = Vec::<i32>::new();
14-
| ^^^^^ constants cannot evaluate destructors
14+
| ^^^^^ the destructor for this type cannot be evaluated in constants
1515
...
1616
LL | };
1717
| - value is dropped here

src/test/ui/consts/const-eval/livedrop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const _: Option<Vec<i32>> = {
22
let mut never_returned = Some(Vec::new());
3-
let mut always_returned = None; //~ ERROR destructors cannot be evaluated at compile-time
3+
let mut always_returned = None; //~ ERROR destructor of
44

55
let mut i = 0;
66
loop {

src/test/ui/consts/const-eval/livedrop.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0493]: destructors cannot be evaluated at compile-time
1+
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
22
--> $DIR/livedrop.rs:3:9
33
|
44
LL | let mut always_returned = None;
5-
| ^^^^^^^^^^^^^^^^^^^ constants cannot evaluate destructors
5+
| ^^^^^^^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constants
66
...
77
LL | always_returned = never_returned;
88
| --------------- value is dropped here

src/test/ui/consts/control-flow/drop-fail.precise.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error[E0493]: destructors cannot be evaluated at compile-time
1+
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
22
--> $DIR/drop-fail.rs:8:9
33
|
44
LL | let x = Some(Vec::new());
5-
| ^ constants cannot evaluate destructors
5+
| ^ the destructor for this type cannot be evaluated in constants
66

7-
error[E0493]: destructors cannot be evaluated at compile-time
7+
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
88
--> $DIR/drop-fail.rs:39:9
99
|
1010
LL | let mut tmp = None;
11-
| ^^^^^^^ constants cannot evaluate destructors
11+
| ^^^^^^^ the destructor for this type cannot be evaluated in constants
1212

1313
error: aborting due to 2 previous errors
1414

src/test/ui/consts/control-flow/drop-fail.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
const _: Option<Vec<i32>> = {
77
let y: Option<Vec<i32>> = None;
88
let x = Some(Vec::new());
9-
//[stock,precise]~^ ERROR destructors cannot be evaluated at compile-time
9+
//[stock,precise]~^ ERROR destructor of
1010

1111
if true {
1212
x
@@ -19,15 +19,15 @@ const _: Option<Vec<i32>> = {
1919
// existing analysis.
2020
const _: Vec<i32> = {
2121
let vec_tuple = (Vec::new(),);
22-
//[stock]~^ ERROR destructors cannot be evaluated at compile-time
22+
//[stock]~^ ERROR destructor of
2323

2424
vec_tuple.0
2525
};
2626

2727
// This applies to single-field enum variants as well.
2828
const _: Vec<i32> = {
2929
let x: Result<_, Vec<i32>> = Ok(Vec::new());
30-
//[stock]~^ ERROR destructors cannot be evaluated at compile-time
30+
//[stock]~^ ERROR destructor of
3131

3232
match x {
3333
Ok(x) | Err(x) => x,
@@ -37,7 +37,7 @@ const _: Vec<i32> = {
3737
const _: Option<Vec<i32>> = {
3838
let mut some = Some(Vec::new());
3939
let mut tmp = None;
40-
//[stock,precise]~^ ERROR destructors cannot be evaluated at compile-time
40+
//[stock,precise]~^ ERROR destructor of
4141

4242
let mut i = 0;
4343
while i < 10 {

src/test/ui/consts/control-flow/drop-fail.stock.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
error[E0493]: destructors cannot be evaluated at compile-time
1+
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
22
--> $DIR/drop-fail.rs:8:9
33
|
44
LL | let x = Some(Vec::new());
5-
| ^ constants cannot evaluate destructors
5+
| ^ the destructor for this type cannot be evaluated in constants
66
...
77
LL | };
88
| - value is dropped here
99

10-
error[E0493]: destructors cannot be evaluated at compile-time
10+
error[E0493]: destructor of `(Vec<i32>,)` cannot be evaluated at compile-time
1111
--> $DIR/drop-fail.rs:21:9
1212
|
1313
LL | let vec_tuple = (Vec::new(),);
14-
| ^^^^^^^^^ constants cannot evaluate destructors
14+
| ^^^^^^^^^ the destructor for this type cannot be evaluated in constants
1515
...
1616
LL | };
1717
| - value is dropped here
1818

19-
error[E0493]: destructors cannot be evaluated at compile-time
19+
error[E0493]: destructor of `Result<Vec<i32>, Vec<i32>>` cannot be evaluated at compile-time
2020
--> $DIR/drop-fail.rs:29:9
2121
|
2222
LL | let x: Result<_, Vec<i32>> = Ok(Vec::new());
23-
| ^ constants cannot evaluate destructors
23+
| ^ the destructor for this type cannot be evaluated in constants
2424
...
2525
LL | };
2626
| - value is dropped here
2727

28-
error[E0493]: destructors cannot be evaluated at compile-time
28+
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
2929
--> $DIR/drop-fail.rs:39:9
3030
|
3131
LL | let mut tmp = None;
32-
| ^^^^^^^ constants cannot evaluate destructors
32+
| ^^^^^^^ the destructor for this type cannot be evaluated in constants
3333
...
3434
LL | };
3535
| - value is dropped here

src/test/ui/consts/drop_box.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
const fn f<T>(_: Box<T>) {}
2-
//~^ ERROR destructors cannot be evaluated at compile-time
2+
//~^ ERROR destructor of
33

44
fn main() {}

src/test/ui/consts/drop_box.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0493]: destructors cannot be evaluated at compile-time
1+
error[E0493]: destructor of `Box<T>` cannot be evaluated at compile-time
22
--> $DIR/drop_box.rs:1:15
33
|
44
LL | const fn f<T>(_: Box<T>) {}
55
| ^ - value is dropped here
66
| |
7-
| constant functions cannot evaluate destructors
7+
| the destructor for this type cannot be evaluated in constant functions
88

99
error: aborting due to previous error
1010

src/test/ui/consts/drop_zst.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0493]: destructors cannot be evaluated at compile-time
1+
error[E0493]: destructor of `S` cannot be evaluated at compile-time
22
--> $DIR/drop_zst.rs:14:9
33
|
44
LL | let s = S;
5-
| ^ constant functions cannot evaluate destructors
5+
| ^ the destructor for this type cannot be evaluated in constant functions
66

77
error: aborting due to previous error
88

src/test/ui/consts/min_const_fn/min_const_fn.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const fn foo35(a: bool, b: bool) -> bool { a ^ b }
3434
struct Foo<T: ?Sized>(T);
3535
impl<T> Foo<T> {
3636
const fn new(t: T) -> Self { Foo(t) }
37-
const fn into_inner(self) -> T { self.0 } //~ destructors cannot be evaluated
37+
const fn into_inner(self) -> T { self.0 } //~ destructor of
3838
const fn get(&self) -> &T { &self.0 }
3939
const fn get_mut(&mut self) -> &mut T { &mut self.0 }
4040
//~^ mutable references
@@ -43,7 +43,7 @@ impl<T> Foo<T> {
4343
}
4444
impl<'a, T> Foo<T> {
4545
const fn new_lt(t: T) -> Self { Foo(t) }
46-
const fn into_inner_lt(self) -> T { self.0 } //~ destructors cannot be evaluated
46+
const fn into_inner_lt(self) -> T { self.0 } //~ destructor of
4747
const fn get_lt(&'a self) -> &T { &self.0 }
4848
const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
4949
//~^ mutable references
@@ -52,7 +52,7 @@ impl<'a, T> Foo<T> {
5252
}
5353
impl<T: Sized> Foo<T> {
5454
const fn new_s(t: T) -> Self { Foo(t) }
55-
const fn into_inner_s(self) -> T { self.0 } //~ ERROR destructors
55+
const fn into_inner_s(self) -> T { self.0 } //~ ERROR destructor
5656
const fn get_s(&self) -> &T { &self.0 }
5757
const fn get_mut_s(&mut self) -> &mut T { &mut self.0 }
5858
//~^ mutable references

0 commit comments

Comments
 (0)