|
| 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