]> git.lizzy.rs Git - rust.git/blob - src/test/ui/nll/issue-54556-niconii.rs
Auto merge of #105121 - oli-obk:simpler-cheaper-dump_mir, r=nnethercote
[rust.git] / src / test / ui / nll / issue-54556-niconii.rs
1 // This is a reduction of a concrete test illustrating a case that was
2 // annoying to Rust developer niconii (see comment thread on #21114).
3 //
4 // With resolving issue #54556, pnkfelix hopes that the new diagnostic
5 // output produced by NLL helps to *explain* the semantic significance
6 // of temp drop order, and thus why inserting a semi-colon after the
7 // `if let` expression in `main` works.
8
9 struct Mutex;
10 struct MutexGuard<'a>(&'a Mutex);
11
12 impl Drop for Mutex { fn drop(&mut self) { println!("Mutex::drop"); } }
13 impl<'a> Drop for MutexGuard<'a> { fn drop(&mut self) { println!("MutexGuard::drop");  } }
14
15 impl Mutex {
16     fn lock(&self) -> Result<MutexGuard, ()> { Ok(MutexGuard(self)) }
17 }
18
19 fn main() {
20     let counter = Mutex;
21
22     if let Ok(_) = counter.lock() { } //~ ERROR does not live long enough
23
24     // With this code as written, the dynamic semantics here implies
25     // that `Mutex::drop` for `counter` runs *before*
26     // `MutexGuard::drop`, which would be unsound since `MutexGuard`
27     // still has a reference to `counter`.
28     //
29     // The goal of #54556 is to explain that within a compiler
30     // diagnostic.
31 }