Skip to content

Commit

Permalink
Add tests for "mutably borrowing copies into a temporary"
Browse files Browse the repository at this point in the history
  • Loading branch information
sharnoff committed Jan 13, 2023
1 parent da49674 commit 3818536
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/ui/borrowck/return-borrow-mut-const.rs
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions tests/ui/borrowck/return-borrow-mut-const.stderr
Original file line number Diff line number Diff line change
@@ -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`.
31 changes: 31 additions & 0 deletions tests/ui/borrowck/return-borrow-mut-fn.rs
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions tests/ui/borrowck/return-borrow-mut-fn.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 3818536

Please sign in to comment.