Skip to content

Commit 441b3f0

Browse files
committed
Auto merge of #24906 - pnkfelix:fsk-fix-24895, r=alexcrichton
dropck: Remove `Copy` from special-cased traits Fix #24895. [breaking-change] What does this break? Basically, code that implements `Drop` and is using `T:Copy` for one of its type parameters and is relying on the Drop Check rule not applying to it. Here is an example: ```rust #![allow(dead_code,unused_variables,unused_assignments)] struct D<T:Copy>(T); impl<T:Copy> Drop for D<T> { fn drop(&mut self) { } } trait UserT { fn c(&self) { } } impl<T:Copy> UserT for T { } struct E<T:UserT>(T); impl<T:UserT> Drop for E<T> { fn drop(&mut self) { } } // This one will start breaking. fn foo() { let (d2, d1); d1 = D(34); d2 = D(&d1); } #[cfg(this_one_does_and_should_always_break)] fn bar() { let (e2, e1); e1 = E(34); e2 = E(&e1); } fn main() { foo(); } ```
2 parents d8b64c7 + 1f79348 commit 441b3f0

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/librustc_typeck/check/dropck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,9 @@ fn iterate_over_potentially_unsafe_regions_in_type<'a, 'tcx>(
464464
ty::Predicate::Trait(ty::Binder(ref t_pred)) => {
465465
let def_id = t_pred.trait_ref.def_id;
466466
match rcx.tcx().lang_items.to_builtin_kind(def_id) {
467+
// Issue 24895: deliberately do not include `BoundCopy` here.
467468
Some(ty::BoundSend) |
468469
Some(ty::BoundSized) |
469-
Some(ty::BoundCopy) |
470470
Some(ty::BoundSync) => false,
471471
_ => true,
472472
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
// Check that one cannot subvert Drop Check rule via a user-defined
12+
// Clone implementation.
13+
14+
#![allow(unused_variables, unused_assignments)]
15+
16+
struct D<T:Copy>(T, &'static str);
17+
18+
#[derive(Copy)]
19+
struct S<'a>(&'a D<i32>, &'static str);
20+
impl<'a> Clone for S<'a> {
21+
fn clone(&self) -> S<'a> {
22+
println!("cloning `S(_, {})` and thus accessing: {}", self.1, (self.0).0);
23+
S(self.0, self.1)
24+
}
25+
}
26+
27+
impl<T:Copy> Drop for D<T> {
28+
fn drop(&mut self) {
29+
println!("calling Drop for {}", self.1);
30+
let _call = self.0.clone();
31+
}
32+
}
33+
34+
fn main() {
35+
let (d2, d1);
36+
d1 = D(34, "d1");
37+
d2 = D(S(&d1, "inner"), "d2"); //~ ERROR `d1` does not live long enough
38+
}

0 commit comments

Comments
 (0)