Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Note the type when unable to drop values in compile time #102194

Merged
merged 1 commit into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {

if needs_non_const_drop {
self.check_op_spanned(
ops::LiveDrop { dropped_at: Some(terminator.source_info.span) },
ops::LiveDrop {
dropped_at: Some(terminator.source_info.span),
dropped_ty: ty_of_dropped_place,
},
err_span,
);
}
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_const_eval/src/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,11 @@ impl<'tcx> NonConstOp<'tcx> for InlineAsm {
}

#[derive(Debug)]
pub struct LiveDrop {
pub struct LiveDrop<'tcx> {
pub dropped_at: Option<Span>,
pub dropped_ty: Ty<'tcx>,
}
impl<'tcx> NonConstOp<'tcx> for LiveDrop {
impl<'tcx> NonConstOp<'tcx> for LiveDrop<'tcx> {
fn build_error(
&self,
ccx: &ConstCx<'_, 'tcx>,
Expand All @@ -435,9 +436,13 @@ impl<'tcx> NonConstOp<'tcx> for LiveDrop {
ccx.tcx.sess,
span,
E0493,
"destructors cannot be evaluated at compile-time"
"destructor of `{}` cannot be evaluated at compile-time",
self.dropped_ty,
);
err.span_label(
span,
format!("the destructor for this type cannot be evaluated in {}s", ccx.const_kind()),
);
err.span_label(span, format!("{}s cannot evaluate destructors", ccx.const_kind()));
if let Some(span) = self.dropped_at {
err.span_label(span, "value is dropped here");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::{self, BasicBlock, Location};
use rustc_middle::ty::TyCtxt;
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_span::{symbol::sym, Span};

use super::check::Qualifs;
Expand Down Expand Up @@ -58,9 +58,9 @@ impl<'mir, 'tcx> std::ops::Deref for CheckLiveDrops<'mir, 'tcx> {
}
}

impl CheckLiveDrops<'_, '_> {
fn check_live_drop(&self, span: Span) {
ops::LiveDrop { dropped_at: None }.build_error(self.ccx, span).emit();
impl<'tcx> CheckLiveDrops<'_, 'tcx> {
fn check_live_drop(&self, span: Span, dropped_ty: Ty<'tcx>) {
ops::LiveDrop { dropped_at: None, dropped_ty }.build_error(self.ccx, span).emit();
}
}

Expand Down Expand Up @@ -90,7 +90,7 @@ impl<'tcx> Visitor<'tcx> for CheckLiveDrops<'_, 'tcx> {
}

if dropped_place.is_indirect() {
self.check_live_drop(terminator.source_info.span);
self.check_live_drop(terminator.source_info.span, dropped_ty);
return;
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/check-static-values-constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static STATIC8: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
// This example should fail because field1 in the base struct is not safe
static STATIC9: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
..SafeStruct{field1: SafeEnum::Variant3(WithDtor),
//~^ ERROR destructors cannot be evaluated at compile-time
//~^ ERROR destructor of
field2: SafeEnum::Variant1}};

struct UnsafeStruct;
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/check-static-values-constraints.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0493]: destructors cannot be evaluated at compile-time
error[E0493]: destructor of `SafeStruct` cannot be evaluated at compile-time
--> $DIR/check-static-values-constraints.rs:65:43
|
LL | ..SafeStruct{field1: SafeEnum::Variant3(WithDtor),
Expand All @@ -7,7 +7,7 @@ LL | |
LL | | field2: SafeEnum::Variant1}};
| | ^- value is dropped here
| |________________________________________________________________________________|
| statics cannot evaluate destructors
| the destructor for this type cannot be evaluated in statics

error[E0010]: allocations are not allowed in statics
--> $DIR/check-static-values-constraints.rs:79:33
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/consts/const-eval/const_let.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ const X2: FakeNeedsDrop = { let x; x = FakeNeedsDrop; x };

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

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

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

// error
const Z2: () = { let mut x; x = None; x = Some(FakeNeedsDrop); };
//~^ ERROR destructors cannot be evaluated at compile-time
//~^ ERROR destructor of
16 changes: 8 additions & 8 deletions src/test/ui/consts/const-eval/const_let.stderr
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
error[E0493]: destructors cannot be evaluated at compile-time
error[E0493]: destructor of `FakeNeedsDrop` cannot be evaluated at compile-time
--> $DIR/const_let.rs:16:32
|
LL | const Y: FakeNeedsDrop = { let mut x = FakeNeedsDrop; x = FakeNeedsDrop; x };
| ^^^^^ - value is dropped here
| |
| constants cannot evaluate destructors
| the destructor for this type cannot be evaluated in constants

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

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

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

error: aborting due to 4 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-eval/issue-65394.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// We will likely have to change this behavior before we allow `&mut` in a `const`.

const _: Vec<i32> = {
let mut x = Vec::<i32>::new(); //~ ERROR destructors cannot be evaluated at compile-time
let mut x = Vec::<i32>::new(); //~ ERROR destructor of
let r = &mut x; //~ ERROR mutable references are not allowed in constants
let y = x;
y
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/consts/const-eval/issue-65394.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ LL | let r = &mut x;
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable

error[E0493]: destructors cannot be evaluated at compile-time
error[E0493]: destructor of `Vec<i32>` cannot be evaluated at compile-time
--> $DIR/issue-65394.rs:7:9
|
LL | let mut x = Vec::<i32>::new();
| ^^^^^ constants cannot evaluate destructors
| ^^^^^ the destructor for this type cannot be evaluated in constants
...
LL | };
| - value is dropped here
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-eval/livedrop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _: Option<Vec<i32>> = {
let mut never_returned = Some(Vec::new());
let mut always_returned = None; //~ ERROR destructors cannot be evaluated at compile-time
let mut always_returned = None; //~ ERROR destructor of

let mut i = 0;
loop {
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/consts/const-eval/livedrop.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0493]: destructors cannot be evaluated at compile-time
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
--> $DIR/livedrop.rs:3:9
|
LL | let mut always_returned = None;
| ^^^^^^^^^^^^^^^^^^^ constants cannot evaluate destructors
| ^^^^^^^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constants
...
LL | always_returned = never_returned;
| --------------- value is dropped here
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/consts/control-flow/drop-fail.precise.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0493]: destructors cannot be evaluated at compile-time
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:8:9
|
LL | let x = Some(Vec::new());
| ^ constants cannot evaluate destructors
| ^ the destructor for this type cannot be evaluated in constants

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

error: aborting due to 2 previous errors

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/consts/control-flow/drop-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const _: Option<Vec<i32>> = {
let y: Option<Vec<i32>> = None;
let x = Some(Vec::new());
//[stock,precise]~^ ERROR destructors cannot be evaluated at compile-time
//[stock,precise]~^ ERROR destructor of

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

vec_tuple.0
};

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

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

let mut i = 0;
while i < 10 {
Expand Down
16 changes: 8 additions & 8 deletions src/test/ui/consts/control-flow/drop-fail.stock.stderr
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
error[E0493]: destructors cannot be evaluated at compile-time
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:8:9
|
LL | let x = Some(Vec::new());
| ^ constants cannot evaluate destructors
| ^ the destructor for this type cannot be evaluated in constants
...
LL | };
| - value is dropped here

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

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

error[E0493]: destructors cannot be evaluated at compile-time
error[E0493]: destructor of `Option<Vec<i32>>` cannot be evaluated at compile-time
--> $DIR/drop-fail.rs:39:9
|
LL | let mut tmp = None;
| ^^^^^^^ constants cannot evaluate destructors
| ^^^^^^^ the destructor for this type cannot be evaluated in constants
...
LL | };
| - value is dropped here
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/drop_box.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fn f<T>(_: Box<T>) {}
//~^ ERROR destructors cannot be evaluated at compile-time
//~^ ERROR destructor of

fn main() {}
4 changes: 2 additions & 2 deletions src/test/ui/consts/drop_box.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0493]: destructors cannot be evaluated at compile-time
error[E0493]: destructor of `Box<T>` cannot be evaluated at compile-time
--> $DIR/drop_box.rs:1:15
|
LL | const fn f<T>(_: Box<T>) {}
| ^ - value is dropped here
| |
| constant functions cannot evaluate destructors
| the destructor for this type cannot be evaluated in constant functions

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/consts/drop_zst.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0493]: destructors cannot be evaluated at compile-time
error[E0493]: destructor of `S` cannot be evaluated at compile-time
--> $DIR/drop_zst.rs:14:9
|
LL | let s = S;
| ^ constant functions cannot evaluate destructors
| ^ the destructor for this type cannot be evaluated in constant functions

error: aborting due to previous error

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/consts/min_const_fn/min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const fn foo35(a: bool, b: bool) -> bool { a ^ b }
struct Foo<T: ?Sized>(T);
impl<T> Foo<T> {
const fn new(t: T) -> Self { Foo(t) }
const fn into_inner(self) -> T { self.0 } //~ destructors cannot be evaluated
const fn into_inner(self) -> T { self.0 } //~ destructor of
const fn get(&self) -> &T { &self.0 }
const fn get_mut(&mut self) -> &mut T { &mut self.0 }
//~^ mutable references
Expand All @@ -43,7 +43,7 @@ impl<T> Foo<T> {
}
impl<'a, T> Foo<T> {
const fn new_lt(t: T) -> Self { Foo(t) }
const fn into_inner_lt(self) -> T { self.0 } //~ destructors cannot be evaluated
const fn into_inner_lt(self) -> T { self.0 } //~ destructor of
const fn get_lt(&'a self) -> &T { &self.0 }
const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
//~^ mutable references
Expand All @@ -52,7 +52,7 @@ impl<'a, T> Foo<T> {
}
impl<T: Sized> Foo<T> {
const fn new_s(t: T) -> Self { Foo(t) }
const fn into_inner_s(self) -> T { self.0 } //~ ERROR destructors
const fn into_inner_s(self) -> T { self.0 } //~ ERROR destructor
const fn get_s(&self) -> &T { &self.0 }
const fn get_mut_s(&mut self) -> &mut T { &mut self.0 }
//~^ mutable references
Expand Down
Loading