Skip to content

Commit 3818536

Browse files
committed
Add tests for "mutably borrowing copies into a temporary"
1 parent da49674 commit 3818536

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// See PR #104857 for details
2+
3+
// don't want to tie this test to the lint, even though it's related
4+
#![allow(const_item_mutation)]
5+
6+
fn main() {}
7+
8+
const X: i32 = 42;
9+
10+
fn borrow_const_immut() -> &'static i32 {
11+
&X
12+
}
13+
14+
fn borrow_const_immut_explicit_return() -> &'static i32 {
15+
return &X;
16+
}
17+
18+
fn borrow_const_immut_into_temp() -> &'static i32 {
19+
let x_ref = &X;
20+
x_ref
21+
}
22+
23+
fn borrow_const_mut() -> &'static mut i32 {
24+
return &mut X; //~ ERROR
25+
}
26+
27+
fn borrow_const_mut_explicit_return() -> &'static mut i32 {
28+
return &mut X; //~ ERROR
29+
}
30+
31+
fn borrow_const_mut_into_temp() -> &'static mut i32 {
32+
let x_ref = &mut X;
33+
x_ref //~ ERROR
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0515]: cannot return reference to temporary value
2+
--> $DIR/return-borrow-mut-const.rs:24:12
3+
|
4+
LL | return &mut X;
5+
| ^^^^^-
6+
| | |
7+
| | temporary value created here
8+
| returns a reference to data owned by the current function
9+
|
10+
= note: mutably borrowing a constant copies the value to a temporary
11+
12+
error[E0515]: cannot return reference to temporary value
13+
--> $DIR/return-borrow-mut-const.rs:28:12
14+
|
15+
LL | return &mut X;
16+
| ^^^^^-
17+
| | |
18+
| | temporary value created here
19+
| returns a reference to data owned by the current function
20+
|
21+
= note: mutably borrowing a constant copies the value to a temporary
22+
23+
error[E0515]: cannot return value referencing temporary value
24+
--> $DIR/return-borrow-mut-const.rs:33:5
25+
|
26+
LL | let x_ref = &mut X;
27+
| - temporary value created here
28+
LL | x_ref
29+
| ^^^^^ returns a value referencing data owned by the current function
30+
|
31+
= note: mutably borrowing a constant copies the value to a temporary
32+
33+
error: aborting due to 3 previous errors
34+
35+
For more information about this error, try `rustc --explain E0515`.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// See PR #104857 for details
2+
3+
fn main() {}
4+
5+
fn do_nothing() {}
6+
7+
fn borrow_fn_immut() -> &'static dyn Fn() {
8+
&do_nothing
9+
}
10+
11+
fn borrow_fn_immut_explicit_return() -> &'static dyn Fn() {
12+
&do_nothing
13+
}
14+
15+
fn borrow_fn_immut_into_temp() -> &'static dyn Fn() {
16+
let f = &do_nothing;
17+
f
18+
}
19+
20+
fn borrow_fn_mut() -> &'static mut dyn FnMut() {
21+
&mut do_nothing //~ ERROR
22+
}
23+
24+
fn borrow_fn_mut_explicit_return() -> &'static mut dyn FnMut() {
25+
&mut do_nothing //~ ERROR
26+
}
27+
28+
fn borrow_fn_mut_into_temp() -> &'static mut dyn FnMut() {
29+
let f = &mut do_nothing;
30+
f //~ ERROR
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0515]: cannot return reference to temporary value
2+
--> $DIR/return-borrow-mut-fn.rs:21:5
3+
|
4+
LL | &mut do_nothing
5+
| ^^^^^----------
6+
| | |
7+
| | temporary value created here
8+
| returns a reference to data owned by the current function
9+
|
10+
= note: mutably borrowing a function copies the function pointer to a temporary
11+
12+
error[E0515]: cannot return reference to temporary value
13+
--> $DIR/return-borrow-mut-fn.rs:25:5
14+
|
15+
LL | &mut do_nothing
16+
| ^^^^^----------
17+
| | |
18+
| | temporary value created here
19+
| returns a reference to data owned by the current function
20+
|
21+
= note: mutably borrowing a function copies the function pointer to a temporary
22+
23+
error[E0515]: cannot return value referencing temporary value
24+
--> $DIR/return-borrow-mut-fn.rs:30:5
25+
|
26+
LL | let f = &mut do_nothing;
27+
| ---------- temporary value created here
28+
LL | f
29+
| ^ returns a value referencing data owned by the current function
30+
|
31+
= note: mutably borrowing a function copies the function pointer to a temporary
32+
33+
error: aborting due to 3 previous errors
34+
35+
For more information about this error, try `rustc --explain E0515`.

0 commit comments

Comments
 (0)