|
| 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 | +// Issue #26656: Verify that trait objects cannot bypass dropck. |
| 12 | + |
| 13 | +// Using this instead of Fn etc. to take HRTB out of the equation. |
| 14 | +trait Trigger<B> { fn fire(&self, b: &mut B); } |
| 15 | +impl<B: Button> Trigger<B> for () { |
| 16 | + fn fire(&self, b: &mut B) { |
| 17 | + b.push(); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// Still unsound Zook |
| 22 | +trait Button { fn push(&self); } |
| 23 | +struct Zook<B> { button: B, trigger: Box<Trigger<B>+'static> } |
| 24 | + |
| 25 | +impl<B> Drop for Zook<B> { |
| 26 | + fn drop(&mut self) { |
| 27 | + self.trigger.fire(&mut self.button); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// AND |
| 32 | +struct Bomb { usable: bool } |
| 33 | +impl Drop for Bomb { fn drop(&mut self) { self.usable = false; } } |
| 34 | +impl Bomb { fn activate(&self) { assert!(self.usable) } } |
| 35 | + |
| 36 | +enum B<'a> { HarmlessButton, BigRedButton(&'a Bomb) } |
| 37 | +impl<'a> Button for B<'a> { |
| 38 | + fn push(&self) { |
| 39 | + if let B::BigRedButton(borrowed) = *self { |
| 40 | + borrowed.activate(); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +fn main() { |
| 46 | + let (mut zook, ticking); |
| 47 | + zook = Zook { button: B::HarmlessButton, |
| 48 | + trigger: Box::new(()) }; |
| 49 | + ticking = Bomb { usable: true }; |
| 50 | + zook.button = B::BigRedButton(&ticking); |
| 51 | + //~^ ERROR `ticking` does not live long enough |
| 52 | +} |
0 commit comments