]> git.lizzy.rs Git - rust.git/blob - src/test/ui/span/issue-26656.rs
Rollup merge of #87922 - Manishearth:c-enum-target-spec, r=nagisa,eddyb
[rust.git] / src / test / ui / span / issue-26656.rs
1 // Issue #26656: Verify that trait objects cannot bypass dropck.
2
3 // Using this instead of Fn etc. to take HRTB out of the equation.
4 trait Trigger<B> { fn fire(&self, b: &mut B); }
5 impl<B: Button> Trigger<B> for () {
6     fn fire(&self, b: &mut B) {
7         b.push();
8     }
9 }
10
11 // Still unsound Zook
12 trait Button { fn push(&self); }
13 struct Zook<B> { button: B, trigger: Box<dyn Trigger<B>+'static> }
14
15 impl<B> Drop for Zook<B> {
16     fn drop(&mut self) {
17         self.trigger.fire(&mut self.button);
18     }
19 }
20
21 // AND
22 struct Bomb { usable: bool }
23 impl Drop for Bomb { fn drop(&mut self) { self.usable = false; } }
24 impl Bomb { fn activate(&self) { assert!(self.usable) } }
25
26 enum B<'a> { HarmlessButton, BigRedButton(&'a Bomb) }
27 impl<'a> Button for B<'a> {
28     fn push(&self) {
29         if let B::BigRedButton(borrowed) = *self {
30             borrowed.activate();
31         }
32     }
33 }
34
35 fn main() {
36     let (mut zook, ticking);
37     zook = Zook { button: B::HarmlessButton,
38                   trigger: Box::new(()) };
39     ticking = Bomb { usable: true };
40     zook.button = B::BigRedButton(&ticking);
41 }
42 //~^^ ERROR `ticking` does not live long enough