-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for "mutably borrowing copies into a temporary"
- Loading branch information
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |