From 3818536ebf1670e8b0094cb794cbe9b1815c5513 Mon Sep 17 00:00:00 2001 From: sharnoff Date: Fri, 25 Nov 2022 13:21:35 -0800 Subject: [PATCH] Add tests for "mutably borrowing copies into a temporary" --- tests/ui/borrowck/return-borrow-mut-const.rs | 34 ++++++++++++++++++ .../borrowck/return-borrow-mut-const.stderr | 35 +++++++++++++++++++ tests/ui/borrowck/return-borrow-mut-fn.rs | 31 ++++++++++++++++ tests/ui/borrowck/return-borrow-mut-fn.stderr | 35 +++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 tests/ui/borrowck/return-borrow-mut-const.rs create mode 100644 tests/ui/borrowck/return-borrow-mut-const.stderr create mode 100644 tests/ui/borrowck/return-borrow-mut-fn.rs create mode 100644 tests/ui/borrowck/return-borrow-mut-fn.stderr diff --git a/tests/ui/borrowck/return-borrow-mut-const.rs b/tests/ui/borrowck/return-borrow-mut-const.rs new file mode 100644 index 0000000000000..497b40131918f --- /dev/null +++ b/tests/ui/borrowck/return-borrow-mut-const.rs @@ -0,0 +1,34 @@ +// See PR #104857 for details + +// don't want to tie this test to the lint, even though it's related +#![allow(const_item_mutation)] + +fn main() {} + +const X: i32 = 42; + +fn borrow_const_immut() -> &'static i32 { + &X +} + +fn borrow_const_immut_explicit_return() -> &'static i32 { + return &X; +} + +fn borrow_const_immut_into_temp() -> &'static i32 { + let x_ref = &X; + x_ref +} + +fn borrow_const_mut() -> &'static mut i32 { + return &mut X; //~ ERROR +} + +fn borrow_const_mut_explicit_return() -> &'static mut i32 { + return &mut X; //~ ERROR +} + +fn borrow_const_mut_into_temp() -> &'static mut i32 { + let x_ref = &mut X; + x_ref //~ ERROR +} diff --git a/tests/ui/borrowck/return-borrow-mut-const.stderr b/tests/ui/borrowck/return-borrow-mut-const.stderr new file mode 100644 index 0000000000000..e9aca721cc831 --- /dev/null +++ b/tests/ui/borrowck/return-borrow-mut-const.stderr @@ -0,0 +1,35 @@ +error[E0515]: cannot return reference to temporary value + --> $DIR/return-borrow-mut-const.rs:24:12 + | +LL | return &mut X; + | ^^^^^- + | | | + | | temporary value created here + | returns a reference to data owned by the current function + | + = note: mutably borrowing a constant copies the value to a temporary + +error[E0515]: cannot return reference to temporary value + --> $DIR/return-borrow-mut-const.rs:28:12 + | +LL | return &mut X; + | ^^^^^- + | | | + | | temporary value created here + | returns a reference to data owned by the current function + | + = note: mutably borrowing a constant copies the value to a temporary + +error[E0515]: cannot return value referencing temporary value + --> $DIR/return-borrow-mut-const.rs:33:5 + | +LL | let x_ref = &mut X; + | - temporary value created here +LL | x_ref + | ^^^^^ returns a value referencing data owned by the current function + | + = note: mutably borrowing a constant copies the value to a temporary + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0515`. diff --git a/tests/ui/borrowck/return-borrow-mut-fn.rs b/tests/ui/borrowck/return-borrow-mut-fn.rs new file mode 100644 index 0000000000000..54a9c8fd5526a --- /dev/null +++ b/tests/ui/borrowck/return-borrow-mut-fn.rs @@ -0,0 +1,31 @@ +// See PR #104857 for details + +fn main() {} + +fn do_nothing() {} + +fn borrow_fn_immut() -> &'static dyn Fn() { + &do_nothing +} + +fn borrow_fn_immut_explicit_return() -> &'static dyn Fn() { + &do_nothing +} + +fn borrow_fn_immut_into_temp() -> &'static dyn Fn() { + let f = &do_nothing; + f +} + +fn borrow_fn_mut() -> &'static mut dyn FnMut() { + &mut do_nothing //~ ERROR +} + +fn borrow_fn_mut_explicit_return() -> &'static mut dyn FnMut() { + &mut do_nothing //~ ERROR +} + +fn borrow_fn_mut_into_temp() -> &'static mut dyn FnMut() { + let f = &mut do_nothing; + f //~ ERROR +} diff --git a/tests/ui/borrowck/return-borrow-mut-fn.stderr b/tests/ui/borrowck/return-borrow-mut-fn.stderr new file mode 100644 index 0000000000000..9b3bb8717ed46 --- /dev/null +++ b/tests/ui/borrowck/return-borrow-mut-fn.stderr @@ -0,0 +1,35 @@ +error[E0515]: cannot return reference to temporary value + --> $DIR/return-borrow-mut-fn.rs:21:5 + | +LL | &mut do_nothing + | ^^^^^---------- + | | | + | | temporary value created here + | returns a reference to data owned by the current function + | + = note: mutably borrowing a function copies the function pointer to a temporary + +error[E0515]: cannot return reference to temporary value + --> $DIR/return-borrow-mut-fn.rs:25:5 + | +LL | &mut do_nothing + | ^^^^^---------- + | | | + | | temporary value created here + | returns a reference to data owned by the current function + | + = note: mutably borrowing a function copies the function pointer to a temporary + +error[E0515]: cannot return value referencing temporary value + --> $DIR/return-borrow-mut-fn.rs:30:5 + | +LL | let f = &mut do_nothing; + | ---------- temporary value created here +LL | f + | ^ returns a value referencing data owned by the current function + | + = note: mutably borrowing a function copies the function pointer to a temporary + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0515`.