Skip to content

Commit 5601ae4

Browse files
committed
Stabilize drop_types_in_const.
1 parent 18366f4 commit 5601ae4

14 files changed

+39
-211
lines changed

src/librustc_mir/diagnostics.rs

+1-23
Original file line numberDiff line numberDiff line change
@@ -431,29 +431,6 @@ Remember this solution is unsafe! You will have to ensure that accesses to the
431431
cell are synchronized.
432432
"##,
433433

434-
E0493: r##"
435-
A type with a destructor was assigned to an invalid type of variable. Erroneous
436-
code example:
437-
438-
```compile_fail,E0493
439-
struct Foo {
440-
a: u32
441-
}
442-
443-
impl Drop for Foo {
444-
fn drop(&mut self) {}
445-
}
446-
447-
const F : Foo = Foo { a : 0 };
448-
// error: constants are not allowed to have destructors
449-
static S : Foo = Foo { a : 0 };
450-
// error: destructors in statics are an unstable feature
451-
```
452-
453-
To solve this issue, please use a type which does allow the usage of type with
454-
destructors.
455-
"##,
456-
457434
E0494: r##"
458435
A reference of an interior static was assigned to another const/static.
459436
Erroneous code example:
@@ -991,6 +968,7 @@ fn print_fancy_ref(fancy_ref: &FancyNum){
991968
}
992969

993970
register_diagnostics! {
971+
E0493, // destructors cannot be evaluated at compile-time
994972
E0524, // two closures require unique access to `..` at the same time
995973
E0526, // shuffle indices are not constant
996974
E0625, // thread-local statics cannot be accessed at compile-time

src/librustc_mir/transform/qualify_consts.rs

+16-132
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc_data_structures::bitvec::BitVector;
1818
use rustc_data_structures::indexed_set::IdxSetBuf;
1919
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
2020
use rustc::hir;
21-
use rustc::hir::map as hir_map;
2221
use rustc::hir::def_id::DefId;
2322
use rustc::traits::{self, Reveal};
2423
use rustc::ty::{self, TyCtxt, Ty, TypeFoldable};
@@ -196,91 +195,6 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
196195
self.add(original);
197196
}
198197

199-
/// Check for NEEDS_DROP (from an ADT or const fn call) and
200-
/// error, unless we're in a function.
201-
fn always_deny_drop(&self) {
202-
self.deny_drop_with_feature_gate_override(false);
203-
}
204-
205-
/// Check for NEEDS_DROP (from an ADT or const fn call) and
206-
/// error, unless we're in a function, or the feature-gate
207-
/// for constant with destructors is enabled.
208-
fn deny_drop(&self) {
209-
self.deny_drop_with_feature_gate_override(true);
210-
}
211-
212-
fn deny_drop_with_feature_gate_override(&self, allow_gate: bool) {
213-
if self.mode == Mode::Fn || !self.qualif.intersects(Qualif::NEEDS_DROP) {
214-
return;
215-
}
216-
217-
// Constants allow destructors, but they're feature-gated.
218-
let msg = if allow_gate {
219-
// Feature-gate for constant with destructors is enabled.
220-
if self.tcx.sess.features.borrow().drop_types_in_const {
221-
return;
222-
}
223-
224-
// This comes from a macro that has #[allow_internal_unstable].
225-
if self.span.allows_unstable() {
226-
return;
227-
}
228-
229-
format!("destructors in {}s are an unstable feature",
230-
self.mode)
231-
} else {
232-
format!("{}s are not allowed to have destructors",
233-
self.mode)
234-
};
235-
236-
let mut err =
237-
struct_span_err!(self.tcx.sess, self.span, E0493, "{}", msg);
238-
239-
if allow_gate {
240-
help!(&mut err,
241-
"in Nightly builds, add `#![feature(drop_types_in_const)]` \
242-
to the crate attributes to enable");
243-
} else {
244-
// FIXME(eddyb) this looks up `self.mir.return_ty`.
245-
// We probably want the actual return type here, if at all.
246-
self.find_drop_implementation_method_span()
247-
.map(|span| err.span_label(span, "destructor defined here"));
248-
249-
err.span_label(self.span,
250-
format!("{}s cannot have destructors", self.mode));
251-
}
252-
253-
err.emit();
254-
}
255-
256-
fn find_drop_implementation_method_span(&self) -> Option<Span> {
257-
self.tcx.lang_items()
258-
.drop_trait()
259-
.and_then(|drop_trait_id| {
260-
let mut span = None;
261-
262-
self.tcx
263-
.for_each_relevant_impl(drop_trait_id, self.mir.return_ty, |impl_did| {
264-
self.tcx.hir
265-
.as_local_node_id(impl_did)
266-
.and_then(|impl_node_id| self.tcx.hir.find(impl_node_id))
267-
.map(|node| {
268-
if let hir_map::NodeItem(item) = node {
269-
if let hir::ItemImpl(.., ref impl_item_refs) = item.node {
270-
span = impl_item_refs.first()
271-
.map(|iiref| {
272-
self.tcx.hir.impl_item(iiref.id)
273-
.span
274-
});
275-
}
276-
}
277-
});
278-
});
279-
280-
span
281-
})
282-
}
283-
284198
/// Check if an Lvalue with the current qualifications could
285199
/// be consumed, by either an operand or a Deref projection.
286200
fn try_consume(&mut self) -> bool {
@@ -457,25 +371,17 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
457371
}
458372
}
459373

460-
let return_ty = mir.return_ty;
461374
self.qualif = self.return_qualif.unwrap_or(Qualif::NOT_CONST);
462375

463-
match self.mode {
464-
Mode::StaticMut => {
465-
// Check for destructors in static mut.
466-
self.add_type(return_ty);
467-
self.deny_drop();
468-
}
469-
_ => {
470-
// Account for errors in consts by using the
471-
// conservative type qualification instead.
472-
if self.qualif.intersects(Qualif::CONST_ERROR) {
473-
self.qualif = Qualif::empty();
474-
self.add_type(return_ty);
475-
}
476-
}
376+
// Account for errors in consts by using the
377+
// conservative type qualification instead.
378+
if self.qualif.intersects(Qualif::CONST_ERROR) {
379+
self.qualif = Qualif::empty();
380+
let return_ty = mir.return_ty;
381+
self.add_type(return_ty);
477382
}
478383

384+
479385
// Collect all the temps we need to promote.
480386
let mut promoted_temps = IdxSetBuf::new_empty(self.temp_promotion_state.len());
481387

@@ -637,12 +543,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
637543
// with type parameters, take it into account.
638544
self.qualif.restrict(constant.ty, self.tcx, self.param_env);
639545
}
640-
641-
// Let `const fn` transitively have destructors,
642-
// but they do get stopped in `const` or `static`.
643-
if self.mode != Mode::ConstFn {
644-
self.deny_drop();
645-
}
646546
}
647547
}
648548
}
@@ -687,12 +587,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
687587
let allow = if self.mode == Mode::StaticMut {
688588
// Inside a `static mut`, &mut [...] is also allowed.
689589
match ty.sty {
690-
ty::TyArray(..) | ty::TySlice(_) => {
691-
// Mutating can expose drops, be conservative.
692-
self.add_type(ty);
693-
self.deny_drop();
694-
true
695-
}
590+
ty::TyArray(..) | ty::TySlice(_) => true,
696591
_ => false
697592
}
698593
} else if let ty::TyArray(_, 0) = ty.sty {
@@ -794,18 +689,12 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
794689
if let AggregateKind::Adt(def, ..) = **kind {
795690
if def.has_dtor(self.tcx) {
796691
self.add(Qualif::NEEDS_DROP);
797-
self.deny_drop();
798692
}
799693

800694
if Some(def.did) == self.tcx.lang_items().unsafe_cell_type() {
801695
let ty = rvalue.ty(self.mir, self.tcx);
802696
self.add_type(ty);
803697
assert!(self.qualif.intersects(Qualif::MUTABLE_INTERIOR));
804-
// Even if the value inside may not need dropping,
805-
// mutating it would change that.
806-
if !self.qualif.intersects(Qualif::NOT_CONST) {
807-
self.deny_drop();
808-
}
809698
}
810699
}
811700
}
@@ -915,12 +804,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
915804
let ty = dest.ty(self.mir, tcx).to_ty(tcx);
916805
self.qualif = Qualif::empty();
917806
self.add_type(ty);
918-
919-
// Let `const fn` transitively have destructors,
920-
// but they do get stopped in `const` or `static`.
921-
if self.mode != Mode::ConstFn {
922-
self.deny_drop();
923-
}
924807
}
925808
self.assign(dest, location);
926809
}
@@ -938,14 +821,15 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
938821
};
939822

940823
if let Some(span) = needs_drop {
824+
// Double-check the type being dropped, to minimize false positives.
941825
let ty = lvalue.ty(self.mir, self.tcx).to_ty(self.tcx);
942-
self.add_type(ty);
943-
944-
// Use the original assignment span to be more precise.
945-
let old_span = self.span;
946-
self.span = span;
947-
self.always_deny_drop();
948-
self.span = old_span;
826+
if ty.needs_drop(self.tcx, self.param_env) {
827+
struct_span_err!(self.tcx.sess, span, E0493,
828+
"destructors cannot be evaluated at compile-time")
829+
.span_label(span, format!("{}s cannot evaluate destructors",
830+
self.mode))
831+
.emit();
832+
}
949833
}
950834
}
951835
} else {

src/libsyntax/feature_gate.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,6 @@ declare_features! (
269269
// impl specialization (RFC 1210)
270270
(active, specialization, "1.7.0", Some(31844)),
271271

272-
// Allow Drop types in statics/const functions (RFC 1440)
273-
(active, drop_types_in_const, "1.9.0", Some(33156)),
274-
275272
// Allows cfg(target_has_atomic = "...").
276273
(active, cfg_target_has_atomic, "1.9.0", Some(32976)),
277274

@@ -466,6 +463,8 @@ declare_features! (
466463
(accepted, compile_error, "1.20.0", Some(40872)),
467464
// See rust-lang/rfcs#1414. Allows code like `let x: &'static u32 = &42` to work.
468465
(accepted, rvalue_static_promotion, "1.21.0", Some(38865)),
466+
// Allow Drop types in constants (RFC 1440)
467+
(accepted, drop_types_in_const, "1.22.0", Some(33156)),
469468
);
470469

471470
// If you change this, please modify src/doc/unstable-book as well. You must

src/test/compile-fail/check-static-values-constraints.rs

+1-26
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
// Verifies all possible restrictions for statics values.
1212

13-
// gate-test-drop_types_in_const
14-
1513
#![allow(warnings)]
1614
#![feature(box_syntax)]
1715

@@ -37,15 +35,8 @@ enum SafeEnum {
3735
// These should be ok
3836
static STATIC1: SafeEnum = SafeEnum::Variant1;
3937
static STATIC2: SafeEnum = SafeEnum::Variant2(0);
40-
41-
// This one should fail
4238
static STATIC3: SafeEnum = SafeEnum::Variant3(WithDtor);
43-
//~^ ERROR destructors in statics are an unstable feature
44-
4539

46-
// This enum will be used to test that variants
47-
// are considered unsafe if their enum type implements
48-
// a destructor.
4940
enum UnsafeEnum {
5041
Variant5,
5142
Variant6(isize)
@@ -57,9 +48,7 @@ impl Drop for UnsafeEnum {
5748

5849

5950
static STATIC4: UnsafeEnum = UnsafeEnum::Variant5;
60-
//~^ ERROR destructors in statics are an unstable feature
6151
static STATIC5: UnsafeEnum = UnsafeEnum::Variant6(0);
62-
//~^ ERROR destructors in statics are an unstable feature
6352

6453

6554
struct SafeStruct {
@@ -71,10 +60,8 @@ struct SafeStruct {
7160
// Struct fields are safe, hence this static should be safe
7261
static STATIC6: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, field2: SafeEnum::Variant2(0)};
7362

74-
// field2 has an unsafe value, hence this should fail
7563
static STATIC7: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
7664
field2: SafeEnum::Variant3(WithDtor)};
77-
//~^ ERROR destructors in statics are an unstable feature
7865

7966
// Test variadic constructor for structs. The base struct should be examined
8067
// as well as every field present in the constructor.
@@ -86,8 +73,7 @@ static STATIC8: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
8673
// This example should fail because field1 in the base struct is not safe
8774
static STATIC9: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
8875
..SafeStruct{field1: SafeEnum::Variant3(WithDtor),
89-
//~^ ERROR destructors in statics are an unstable feature
90-
//~| ERROR statics are not allowed to have destructors
76+
//~^ ERROR destructors cannot be evaluated at compile-time
9177
field2: SafeEnum::Variant1}};
9278

9379
struct UnsafeStruct;
@@ -96,29 +82,19 @@ impl Drop for UnsafeStruct {
9682
fn drop(&mut self) {}
9783
}
9884

99-
// Types with destructors are not allowed for statics
10085
static STATIC10: UnsafeStruct = UnsafeStruct;
101-
//~^ ERROR destructors in statics are an unstable feature
10286

10387
struct MyOwned;
10488

10589
static STATIC11: Box<MyOwned> = box MyOwned;
10690
//~^ ERROR allocations are not allowed in statics
10791

108-
// The following examples test that mutable structs are just forbidden
109-
// to have types with destructors
110-
// These should fail
11192
static mut STATIC12: UnsafeStruct = UnsafeStruct;
112-
//~^ ERROR destructors in statics are an unstable feature
113-
//~^^ ERROR destructors in statics are an unstable feature
11493

11594
static mut STATIC13: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
116-
//~^ ERROR destructors in statics are an unstable feature
11795
field2: SafeEnum::Variant3(WithDtor)};
118-
//~^ ERROR: destructors in statics are an unstable feature
11996

12097
static mut STATIC14: SafeStruct = SafeStruct {
121-
//~^ ERROR destructors in statics are an unstable feature
12298
field1: SafeEnum::Variant1,
12399
field2: SafeEnum::Variant4("str".to_string())
124100
//~^ ERROR calls in statics are limited to constant functions
@@ -135,7 +111,6 @@ static STATIC16: (&'static Box<MyOwned>, &'static Box<MyOwned>) = (
135111
);
136112

137113
static mut STATIC17: SafeEnum = SafeEnum::Variant1;
138-
//~^ ERROR destructors in statics are an unstable feature
139114

140115
static STATIC19: Box<isize> =
141116
box 3;

src/test/compile-fail/issue-43733-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(const_fn, drop_types_in_const)]
11+
#![feature(const_fn)]
1212
#![feature(cfg_target_thread_local, thread_local_internals)]
1313

1414
// On platforms *without* `#[thread_local]`, use

src/test/compile-fail/issue-43733.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(const_fn, drop_types_in_const)]
11+
#![feature(const_fn)]
1212
#![feature(cfg_target_thread_local, thread_local_internals)]
1313

1414
type Foo = std::cell::RefCell<String>;

0 commit comments

Comments
 (0)