Skip to content

Commit

Permalink
Add known bug test.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jul 2, 2022
1 parent 9a6fa4f commit d5354eb
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/test/ui/lifetimes/bare-trait-object-borrowck.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![allow(bare_trait_objects)]

pub struct FormatWith<'a, I, F> {
sep: &'a str,
/// FormatWith uses interior mutability because Display::fmt takes &self.
inner: RefCell<Option<(I, F)>>,
}

use std::cell::RefCell;
use std::fmt;

struct Layout;

pub fn new_format<'a, I, F>(iter: I, separator: &'a str, f: F) -> FormatWith<'a, I, F>
where
I: Iterator,
F: FnMut(I::Item, &mut FnMut(&fmt::Display) -> fmt::Result) -> fmt::Result,
{
FormatWith { sep: separator, inner: RefCell::new(Some((iter, f))) }
}

fn main() {
let _ = new_format(0..32, " | ", |i, f| f(&format_args!("0x{:x}", i)));
//~^ ERROR temporary value dropped while borrowed
//~| ERROR temporary value dropped while borrowed
//~| ERROR `i` does not live long enough
}
43 changes: 43 additions & 0 deletions src/test/ui/lifetimes/bare-trait-object-borrowck.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/bare-trait-object-borrowck.rs:23:48
|
LL | let _ = new_format(0..32, " | ", |i, f| f(&format_args!("0x{:x}", i)));
| - ---^^^^^^^^^^^^^^^^^^^^^^^^^-
| | | | |
| | | | temporary value is freed at the end of this statement
| | | creates a temporary which is freed while still in use
| | argument requires that borrow lasts for `'1`
| has type `&mut dyn FnMut(&'1 (dyn std::fmt::Display + '1)) -> Result<(), std::fmt::Error>`
|
= note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0716]: temporary value dropped while borrowed
--> $DIR/bare-trait-object-borrowck.rs:23:48
|
LL | let _ = new_format(0..32, " | ", |i, f| f(&format_args!("0x{:x}", i)));
| - ---^^^^^^^^^^^^^^^^^^^^^^^^^-
| | | | |
| | | | temporary value is freed at the end of this statement
| | | creates a temporary which is freed while still in use
| | argument requires that borrow lasts for `'1`
| has type `&mut dyn FnMut(&'1 (dyn std::fmt::Display + '1)) -> Result<(), std::fmt::Error>`
|
= note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0597]: `i` does not live long enough
--> $DIR/bare-trait-object-borrowck.rs:23:71
|
LL | let _ = new_format(0..32, " | ", |i, f| f(&format_args!("0x{:x}", i)));
| - --------------------------^--
| | | | |
| | | | `i` dropped here while still borrowed
| | | borrowed value does not live long enough
| | argument requires that `i` is borrowed for `'1`
| has type `&mut dyn FnMut(&'1 (dyn std::fmt::Display + '1)) -> Result<(), std::fmt::Error>`
|
= note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0597, E0716.
For more information about an error, try `rustc --explain E0597`.
27 changes: 27 additions & 0 deletions src/test/ui/lifetimes/bare-trait-object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Verify that lifetime resolution correctly accounts for `Fn` bare trait objects.

#![allow(bare_trait_objects)]

// This should work as: fn next_u32(fill_buf: &mut dyn FnMut(&mut [u8]))
fn next_u32(fill_buf: &mut FnMut(&mut [u8])) {
let mut buf: [u8; 4] = [0; 4];
fill_buf(&mut buf);
}

fn explicit(fill_buf: &mut dyn FnMut(&mut [u8])) {
let mut buf: [u8; 4] = [0; 4];
fill_buf(&mut buf);
}

fn main() {
let _: fn(&mut FnMut(&mut [u8])) = next_u32;
//~^ ERROR mismatched types
let _: &dyn Fn(&mut FnMut(&mut [u8])) = &next_u32;
let _: fn(&mut FnMut(&mut [u8])) = explicit;
//~^ ERROR mismatched types
let _: &dyn Fn(&mut FnMut(&mut [u8])) = &explicit;
let _: fn(&mut dyn FnMut(&mut [u8])) = next_u32;
let _: &dyn Fn(&mut dyn FnMut(&mut [u8])) = &next_u32;
let _: fn(&mut dyn FnMut(&mut [u8])) = explicit;
let _: &dyn Fn(&mut dyn FnMut(&mut [u8])) = &explicit;
}
21 changes: 21 additions & 0 deletions src/test/ui/lifetimes/bare-trait-object.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0308]: mismatched types
--> $DIR/bare-trait-object.rs:17:40
|
LL | let _: fn(&mut FnMut(&mut [u8])) = next_u32;
| ^^^^^^^^ one type is more general than the other
|
= note: expected fn pointer `for<'r, 's> fn(&'r mut (dyn for<'s> FnMut(&'s mut [u8]) + 'r))`
found fn item `for<'r> fn(&'r mut (dyn for<'r> FnMut(&'r mut [u8]) + 'r)) {next_u32}`

error[E0308]: mismatched types
--> $DIR/bare-trait-object.rs:20:40
|
LL | let _: fn(&mut FnMut(&mut [u8])) = explicit;
| ^^^^^^^^ one type is more general than the other
|
= note: expected fn pointer `for<'r, 's> fn(&'r mut (dyn for<'s> FnMut(&'s mut [u8]) + 'r))`
found fn item `for<'r> fn(&'r mut (dyn for<'r> FnMut(&'r mut [u8]) + 'r)) {explicit}`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

0 comments on commit d5354eb

Please sign in to comment.