Skip to content

Commit d5354eb

Browse files
committed
Add known bug test.
1 parent 9a6fa4f commit d5354eb

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

Diff for: src/test/ui/lifetimes/bare-trait-object-borrowck.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![allow(bare_trait_objects)]
2+
3+
pub struct FormatWith<'a, I, F> {
4+
sep: &'a str,
5+
/// FormatWith uses interior mutability because Display::fmt takes &self.
6+
inner: RefCell<Option<(I, F)>>,
7+
}
8+
9+
use std::cell::RefCell;
10+
use std::fmt;
11+
12+
struct Layout;
13+
14+
pub fn new_format<'a, I, F>(iter: I, separator: &'a str, f: F) -> FormatWith<'a, I, F>
15+
where
16+
I: Iterator,
17+
F: FnMut(I::Item, &mut FnMut(&fmt::Display) -> fmt::Result) -> fmt::Result,
18+
{
19+
FormatWith { sep: separator, inner: RefCell::new(Some((iter, f))) }
20+
}
21+
22+
fn main() {
23+
let _ = new_format(0..32, " | ", |i, f| f(&format_args!("0x{:x}", i)));
24+
//~^ ERROR temporary value dropped while borrowed
25+
//~| ERROR temporary value dropped while borrowed
26+
//~| ERROR `i` does not live long enough
27+
}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/bare-trait-object-borrowck.rs:23:48
3+
|
4+
LL | let _ = new_format(0..32, " | ", |i, f| f(&format_args!("0x{:x}", i)));
5+
| - ---^^^^^^^^^^^^^^^^^^^^^^^^^-
6+
| | | | |
7+
| | | | temporary value is freed at the end of this statement
8+
| | | creates a temporary which is freed while still in use
9+
| | argument requires that borrow lasts for `'1`
10+
| has type `&mut dyn FnMut(&'1 (dyn std::fmt::Display + '1)) -> Result<(), std::fmt::Error>`
11+
|
12+
= note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
14+
error[E0716]: temporary value dropped while borrowed
15+
--> $DIR/bare-trait-object-borrowck.rs:23:48
16+
|
17+
LL | let _ = new_format(0..32, " | ", |i, f| f(&format_args!("0x{:x}", i)));
18+
| - ---^^^^^^^^^^^^^^^^^^^^^^^^^-
19+
| | | | |
20+
| | | | temporary value is freed at the end of this statement
21+
| | | creates a temporary which is freed while still in use
22+
| | argument requires that borrow lasts for `'1`
23+
| has type `&mut dyn FnMut(&'1 (dyn std::fmt::Display + '1)) -> Result<(), std::fmt::Error>`
24+
|
25+
= note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
26+
27+
error[E0597]: `i` does not live long enough
28+
--> $DIR/bare-trait-object-borrowck.rs:23:71
29+
|
30+
LL | let _ = new_format(0..32, " | ", |i, f| f(&format_args!("0x{:x}", i)));
31+
| - --------------------------^--
32+
| | | | |
33+
| | | | `i` dropped here while still borrowed
34+
| | | borrowed value does not live long enough
35+
| | argument requires that `i` is borrowed for `'1`
36+
| has type `&mut dyn FnMut(&'1 (dyn std::fmt::Display + '1)) -> Result<(), std::fmt::Error>`
37+
|
38+
= note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
39+
40+
error: aborting due to 3 previous errors
41+
42+
Some errors have detailed explanations: E0597, E0716.
43+
For more information about an error, try `rustc --explain E0597`.

Diff for: src/test/ui/lifetimes/bare-trait-object.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Verify that lifetime resolution correctly accounts for `Fn` bare trait objects.
2+
3+
#![allow(bare_trait_objects)]
4+
5+
// This should work as: fn next_u32(fill_buf: &mut dyn FnMut(&mut [u8]))
6+
fn next_u32(fill_buf: &mut FnMut(&mut [u8])) {
7+
let mut buf: [u8; 4] = [0; 4];
8+
fill_buf(&mut buf);
9+
}
10+
11+
fn explicit(fill_buf: &mut dyn FnMut(&mut [u8])) {
12+
let mut buf: [u8; 4] = [0; 4];
13+
fill_buf(&mut buf);
14+
}
15+
16+
fn main() {
17+
let _: fn(&mut FnMut(&mut [u8])) = next_u32;
18+
//~^ ERROR mismatched types
19+
let _: &dyn Fn(&mut FnMut(&mut [u8])) = &next_u32;
20+
let _: fn(&mut FnMut(&mut [u8])) = explicit;
21+
//~^ ERROR mismatched types
22+
let _: &dyn Fn(&mut FnMut(&mut [u8])) = &explicit;
23+
let _: fn(&mut dyn FnMut(&mut [u8])) = next_u32;
24+
let _: &dyn Fn(&mut dyn FnMut(&mut [u8])) = &next_u32;
25+
let _: fn(&mut dyn FnMut(&mut [u8])) = explicit;
26+
let _: &dyn Fn(&mut dyn FnMut(&mut [u8])) = &explicit;
27+
}

Diff for: src/test/ui/lifetimes/bare-trait-object.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/bare-trait-object.rs:17:40
3+
|
4+
LL | let _: fn(&mut FnMut(&mut [u8])) = next_u32;
5+
| ^^^^^^^^ one type is more general than the other
6+
|
7+
= note: expected fn pointer `for<'r, 's> fn(&'r mut (dyn for<'s> FnMut(&'s mut [u8]) + 'r))`
8+
found fn item `for<'r> fn(&'r mut (dyn for<'r> FnMut(&'r mut [u8]) + 'r)) {next_u32}`
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/bare-trait-object.rs:20:40
12+
|
13+
LL | let _: fn(&mut FnMut(&mut [u8])) = explicit;
14+
| ^^^^^^^^ one type is more general than the other
15+
|
16+
= note: expected fn pointer `for<'r, 's> fn(&'r mut (dyn for<'s> FnMut(&'s mut [u8]) + 'r))`
17+
found fn item `for<'r> fn(&'r mut (dyn for<'r> FnMut(&'r mut [u8]) + 'r)) {explicit}`
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)