|
1 |
| -// check-fail |
| 1 | +// check-pass |
| 2 | +#![crate_type = "lib"] |
2 | 3 |
|
3 | 4 | use std::fmt::{self, Display};
|
4 | 5 |
|
5 | 6 | struct Mutex;
|
6 | 7 |
|
7 | 8 | impl Mutex {
|
8 |
| - fn lock(&self) -> MutexGuard { |
9 |
| - MutexGuard(self) |
| 9 | + /// Dependent item with (potential) drop glue to disable NLL. |
| 10 | + fn lock(&self) -> impl '_ + Display { |
| 11 | + 42 |
10 | 12 | }
|
11 | 13 | }
|
12 | 14 |
|
13 |
| -struct MutexGuard<'a>(&'a Mutex); |
| 15 | +struct Stderr(); |
14 | 16 |
|
15 |
| -impl<'a> Drop for MutexGuard<'a> { |
16 |
| - fn drop(&mut self) { |
17 |
| - // Empty but this is a necessary part of the repro. Otherwise borrow |
18 |
| - // checker is fine with 'a dangling at the time that MutexGuard goes out |
19 |
| - // of scope. |
20 |
| - } |
21 |
| -} |
22 |
| - |
23 |
| -struct Out; |
24 |
| - |
25 |
| -impl Out { |
26 |
| - fn write_fmt(&self, _args: fmt::Arguments) {} |
27 |
| -} |
28 |
| - |
29 |
| -impl<'a> Display for MutexGuard<'a> { |
30 |
| - fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result { |
31 |
| - Ok(()) |
32 |
| - } |
| 17 | +impl Stderr { |
| 18 | + /// A "lending" `write_fmt` method. See: |
| 19 | + /// https://docs.rs/async-std/1.12.0/async_std/io/prelude/trait.WriteExt.html#method.write_fmt |
| 20 | + fn write_fmt(&mut self, _args: fmt::Arguments) -> &() { &() } |
33 | 21 | }
|
34 | 22 |
|
35 |
| -fn main() { |
36 |
| - // FIXME(dtolnay): We actually want both of these to work. I think it's |
37 |
| - // sadly unimplementable today though. |
| 23 | +fn early_drop_for_format_args_temporaries() { |
| 24 | + let mut out = Stderr(); |
38 | 25 |
|
39 | 26 | let _write = {
|
40 | 27 | let mutex = Mutex;
|
41 |
| - write!(Out, "{}", mutex.lock()) /* no semicolon */ |
42 |
| - //~^ ERROR `mutex` does not live long enough |
| 28 | + write!(out, "{}", mutex.lock()) /* no semicolon */ |
43 | 29 | };
|
44 | 30 |
|
45 | 31 | let _writeln = {
|
46 | 32 | let mutex = Mutex;
|
47 |
| - writeln!(Out, "{}", mutex.lock()) /* no semicolon */ |
48 |
| - //~^ ERROR `mutex` does not live long enough |
| 33 | + writeln!(out, "{}", mutex.lock()) /* no semicolon */ |
49 | 34 | };
|
50 | 35 | }
|
| 36 | + |
| 37 | +fn late_drop_for_receiver() { |
| 38 | + let mutex = Mutex; |
| 39 | + drop(write!(&mut Stderr(), "{}", mutex.lock())); |
| 40 | + drop(writeln!(&mut Stderr(), "{}", mutex.lock())); |
| 41 | +} |
| 42 | + |
| 43 | +fn two_phased_borrows_retrocompat(w: (&mut Stderr, i32)) { |
| 44 | + write!(w.0, "{}", w.1); |
| 45 | + writeln!(w.0, "{}", w.1); |
| 46 | + struct Struct<W> { |
| 47 | + w: W, |
| 48 | + len: i32 |
| 49 | + } |
| 50 | + let s = (Struct { w: (w.0, ), len: w.1 }, ); |
| 51 | + write!(s.0.w.0, "{}", s.0.len); |
| 52 | + writeln!(s.0.w.0, "{}", s.0.len); |
| 53 | +} |
0 commit comments