From b892264ea4f048feb5f380d3e659d82ba463f5b7 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 28 Apr 2015 17:47:16 +0200 Subject: [PATCH 1/2] 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); impl Drop for D { fn drop(&mut self) { } } trait UserT { fn c(&self) { } } impl UserT for T { } struct E(T); impl Drop for E { 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(); } ``` --- src/librustc_typeck/check/dropck.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/dropck.rs b/src/librustc_typeck/check/dropck.rs index 2f7e0073e1751..008ba1c6bf83e 100644 --- a/src/librustc_typeck/check/dropck.rs +++ b/src/librustc_typeck/check/dropck.rs @@ -464,9 +464,9 @@ fn iterate_over_potentially_unsafe_regions_in_type<'a, 'tcx>( ty::Predicate::Trait(ty::Binder(ref t_pred)) => { let def_id = t_pred.trait_ref.def_id; match rcx.tcx().lang_items.to_builtin_kind(def_id) { + // Issue 24895: deliberately do not include `BoundCopy` here. Some(ty::BoundSend) | Some(ty::BoundSized) | - Some(ty::BoundCopy) | Some(ty::BoundSync) => false, _ => true, } From 1f793482930ab98c3ecb2da7507cd4d55ace023c Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 28 Apr 2015 17:51:08 +0200 Subject: [PATCH 2/2] regression test for Issue 24895. --- .../issue-24895-copy-clone-dropck.rs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/test/compile-fail/issue-24895-copy-clone-dropck.rs diff --git a/src/test/compile-fail/issue-24895-copy-clone-dropck.rs b/src/test/compile-fail/issue-24895-copy-clone-dropck.rs new file mode 100644 index 0000000000000..2883511736920 --- /dev/null +++ b/src/test/compile-fail/issue-24895-copy-clone-dropck.rs @@ -0,0 +1,38 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Check that one cannot subvert Drop Check rule via a user-defined +// Clone implementation. + +#![allow(unused_variables, unused_assignments)] + +struct D(T, &'static str); + +#[derive(Copy)] +struct S<'a>(&'a D, &'static str); +impl<'a> Clone for S<'a> { + fn clone(&self) -> S<'a> { + println!("cloning `S(_, {})` and thus accessing: {}", self.1, (self.0).0); + S(self.0, self.1) + } +} + +impl Drop for D { + fn drop(&mut self) { + println!("calling Drop for {}", self.1); + let _call = self.0.clone(); + } +} + +fn main() { + let (d2, d1); + d1 = D(34, "d1"); + d2 = D(S(&d1, "inner"), "d2"); //~ ERROR `d1` does not live long enough +}