diff --git a/src/test/ui/lifetimes/bare-trait-object-borrowck.rs b/src/test/ui/lifetimes/bare-trait-object-borrowck.rs new file mode 100644 index 0000000000000..ee2c61468cb60 --- /dev/null +++ b/src/test/ui/lifetimes/bare-trait-object-borrowck.rs @@ -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>, +} + +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 +} diff --git a/src/test/ui/lifetimes/bare-trait-object-borrowck.stderr b/src/test/ui/lifetimes/bare-trait-object-borrowck.stderr new file mode 100644 index 0000000000000..d7b059019eb3b --- /dev/null +++ b/src/test/ui/lifetimes/bare-trait-object-borrowck.stderr @@ -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`. diff --git a/src/test/ui/lifetimes/bare-trait-object.rs b/src/test/ui/lifetimes/bare-trait-object.rs new file mode 100644 index 0000000000000..dc8b611c03978 --- /dev/null +++ b/src/test/ui/lifetimes/bare-trait-object.rs @@ -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; +} diff --git a/src/test/ui/lifetimes/bare-trait-object.stderr b/src/test/ui/lifetimes/bare-trait-object.stderr new file mode 100644 index 0000000000000..8f06410152e62 --- /dev/null +++ b/src/test/ui/lifetimes/bare-trait-object.stderr @@ -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`.