Skip to content

Commit bf6847a

Browse files
committed
auto merge of #20631 : huonw/rust/no-drop-and-copy, r=nikomatsakis
Opt-in built-in traits allowed one to explicitly implement both `Drop` and `Copy` for a type. This can theoretically make some sense, but the current implementation means it is codegened totally incorrectly which can lead to memory unsafety, so this feature is disabled for now. Fixes #20126.
2 parents dfd557b + 0ea534f commit bf6847a

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

src/librustc/middle/ty.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6903,6 +6903,7 @@ pub enum CopyImplementationError {
69036903
FieldDoesNotImplementCopy(ast::Name),
69046904
VariantDoesNotImplementCopy(ast::Name),
69056905
TypeIsStructural,
6906+
TypeHasDestructor,
69066907
}
69076908

69086909
pub fn can_type_implement_copy<'a,'tcx>(param_env: &ParameterEnvironment<'a, 'tcx>,
@@ -6912,14 +6913,15 @@ pub fn can_type_implement_copy<'a,'tcx>(param_env: &ParameterEnvironment<'a, 'tc
69126913
{
69136914
let tcx = param_env.tcx;
69146915

6915-
match self_type.sty {
6916+
let did = match self_type.sty {
69166917
ty::ty_struct(struct_did, substs) => {
69176918
let fields = ty::struct_fields(tcx, struct_did, substs);
69186919
for field in fields.iter() {
69196920
if type_moves_by_default(param_env, span, field.mt.ty) {
69206921
return Err(FieldDoesNotImplementCopy(field.name))
69216922
}
69226923
}
6924+
struct_did
69236925
}
69246926
ty::ty_enum(enum_did, substs) => {
69256927
let enum_variants = ty::enum_variants(tcx, enum_did);
@@ -6932,8 +6934,13 @@ pub fn can_type_implement_copy<'a,'tcx>(param_env: &ParameterEnvironment<'a, 'tc
69326934
}
69336935
}
69346936
}
6937+
enum_did
69356938
}
69366939
_ => return Err(TypeIsStructural),
6940+
};
6941+
6942+
if ty::has_dtor(tcx, did) {
6943+
return Err(TypeHasDestructor)
69376944
}
69386945

69396946
Ok(())

src/librustc_typeck/coherence/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,11 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
507507
for this type; type is not a structure or \
508508
enumeration")
509509
}
510+
Err(ty::TypeHasDestructor) => {
511+
span_err!(tcx.sess, span, E0184,
512+
"the trait `Copy` may not be implement for this type; \
513+
the type has a destructor");
514+
}
510515
}
511516
}
512517
}

src/librustc_typeck/diagnostics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,6 @@ register_diagnostics! {
157157
E0180,
158158
E0181,
159159
E0182,
160-
E0183
160+
E0183,
161+
E0184
161162
}

src/test/compile-fail/drop-on-non-struct.rs

Lines changed: 1 addition & 1 deletion
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-
impl Drop for int {
11+
impl<'a> Drop for &'a mut int {
1212
//~^ ERROR the Drop trait may only be implemented on structures
1313
//~^^ ERROR E0117
1414
fn drop(&mut self) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(unsafe_destructor)]
12+
13+
// issue #20126
14+
15+
#[derive(Copy)] //~ ERROR the trait `Copy` may not be implemented
16+
struct Foo;
17+
18+
impl Drop for Foo {
19+
fn drop(&mut self) {}
20+
}
21+
22+
#[derive(Copy)] //~ ERROR the trait `Copy` may not be implemented
23+
struct Bar<T>;
24+
25+
#[unsafe_destructor]
26+
impl<T> Drop for Bar<T> {
27+
fn drop(&mut self) {}
28+
}
29+
30+
fn main() {}

0 commit comments

Comments
 (0)