|
| 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 item-less traits do not cause dropck to inject extra |
| 12 | +// region constraints. |
| 13 | + |
| 14 | +#![allow(non_camel_case_types)] |
| 15 | + |
| 16 | +trait UserDefined { } |
| 17 | + |
| 18 | +impl UserDefined for i32 { } |
| 19 | +impl<'a, T> UserDefined for &'a T { } |
| 20 | + |
| 21 | +// e.g. `impl_drop!(Send, D_Send)` expands to: |
| 22 | +// ```rust |
| 23 | +// struct D_Send<T:Send>(T); |
| 24 | +// impl<T:Send> Drop for D_Send<T> { fn drop(&mut self) { } } |
| 25 | +// ``` |
| 26 | +macro_rules! impl_drop { |
| 27 | + ($Bound:ident, $Id:ident) => { |
| 28 | + struct $Id<T:$Bound>(T); |
| 29 | + impl <T:$Bound> Drop for $Id<T> { fn drop(&mut self) { } } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl_drop!{Send, D_Send} |
| 34 | +impl_drop!{Sized, D_Sized} |
| 35 | + |
| 36 | +// See note below regarding Issue 24895 |
| 37 | +// impl_drop!{Copy, D_Copy} |
| 38 | + |
| 39 | +impl_drop!{Sync, D_Sync} |
| 40 | +impl_drop!{UserDefined, D_UserDefined} |
| 41 | + |
| 42 | +macro_rules! body { |
| 43 | + ($id:ident) => { { |
| 44 | + // `_d` and `d1` are assigned the *same* lifetime by region inference ... |
| 45 | + let (_d, d1); |
| 46 | + |
| 47 | + d1 = $id(1); |
| 48 | + // ... we store a reference to `d1` within `_d` ... |
| 49 | + _d = $id(&d1); |
| 50 | + |
| 51 | + // ... a *conservative* dropck will thus complain, because it |
| 52 | + // thinks Drop of _d could access the already dropped `d1`. |
| 53 | + } } |
| 54 | +} |
| 55 | + |
| 56 | +fn f_send() { body!(D_Send) } |
| 57 | +fn f_sized() { body!(D_Sized) } |
| 58 | +fn f_sync() { body!(D_Sync) } |
| 59 | + |
| 60 | +// Issue 24895: Copy: Clone implies `impl<T:Copy> Drop for ...` can |
| 61 | +// access a user-defined clone() method, which causes this test case |
| 62 | +// to fail. |
| 63 | +// |
| 64 | +// If 24895 is resolved by removing the `Copy: Clone` relationship, |
| 65 | +// then this definition and the call below should be uncommented. If |
| 66 | +// it is resolved by deciding to keep the `Copy: Clone` relationship, |
| 67 | +// then this comment and the associated bits of code can all be |
| 68 | +// removed. |
| 69 | + |
| 70 | +// fn f_copy() { body!(D_Copy) } |
| 71 | + |
| 72 | +fn f_userdefined() { body!(D_UserDefined) } |
| 73 | + |
| 74 | +fn main() { |
| 75 | + f_send(); |
| 76 | + f_sized(); |
| 77 | + // See note above regarding Issue 24895. |
| 78 | + // f_copy(); |
| 79 | + f_sync(); |
| 80 | + f_userdefined(); |
| 81 | +} |
0 commit comments