Skip to content

Commit 3f9fc1f

Browse files
WaffleLapkinBoxyUwU
authored andcommitted
Add old borrowck examples
1 parent ef4f220 commit 3f9fc1f

File tree

16 files changed

+290
-0
lines changed

16 files changed

+290
-0
lines changed

code/examples/borrowck_1.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn func(a: &'static u32) {
2+
dbg!(a);
3+
}
4+
5+
fn accepts_func(f: fn(&u32), data: &u32) {
6+
f(data);
7+
}
8+
9+
fn main() {
10+
// &'static u32 can be shortened to any
11+
// lifetime borrow so func should be fine
12+
accepts_func(func, &274);
13+
}

code/examples/borrowck_2.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
consume_fn(identity);
3+
}
4+
5+
fn identity(x: &u32) -> &u32 {
6+
x
7+
}
8+
9+
fn consume_fn<T>(_: impl FnOnce(&u32) -> T) {}

code/examples/borrowck_3.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn foo(a: &mut u32) -> u32 {
2+
let mut b = || &mut *a;
3+
4+
*b() = 12;
5+
*b() = 73;
6+
*a
7+
}
8+
9+
fn main() {
10+
foo(&mut 292);
11+
}

code/examples/borrowck_4.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[derive(Debug)]
2+
struct Foo<'a>(&'a mut u32);
3+
4+
fn main() {
5+
let mut data = 10;
6+
let mut foo = Foo(&mut data);
7+
mutate(&mut foo);
8+
dbg!(foo);
9+
}
10+
11+
fn mutate<'a>(f: &'a mut Foo<'a>) {
12+
*f.0 += 1;
13+
}

code/examples/borrowck_5.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::collections::HashMap;
2+
3+
fn get_or_insert_1234(map: &mut HashMap<u32, u64>) -> &mut u64 {
4+
if let Some(v) = map.get_mut(&0) {
5+
return v;
6+
}
7+
8+
map.insert(0, 1234);
9+
map.get_mut(&0).unwrap()
10+
}
11+
12+
fn main() {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0308]: mismatched types
2+
--> examples/borrowck_1.rs:12:18
3+
|
4+
12 | accepts_func(func, &274);
5+
| ------------ ^^^^ one type is more general than the other
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: expected fn pointer `for<'a> fn(&'a _)`
10+
found fn item `fn(&'static _) {func}`
11+
note: function defined here
12+
--> examples/borrowck_1.rs:5:4
13+
|
14+
5 | fn accepts_func(f: fn(&u32), data: &u32) {
15+
| ^^^^^^^^^^^^ -----------
16+
17+
For more information about this error, try `rustc --explain E0308`.
18+
error: could not compile `code` (example "borrowck_1") due to 1 previous error
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0308]: mismatched types
2+
--> examples/borrowck_2.rs:2:5
3+
|
4+
2 | consume_fn(identity);
5+
| ^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
6+
|
7+
= note: expected reference `&_`
8+
found reference `&_`
9+
note: the lifetime requirement is introduced here
10+
--> examples/borrowck_2.rs:9:42
11+
|
12+
9 | fn consume_fn<T>(_: impl FnOnce(&u32) -> T) {}
13+
| ^
14+
15+
For more information about this error, try `rustc --explain E0308`.
16+
error: could not compile `code` (example "borrowck_2") due to 1 previous error
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: captured variable cannot escape `FnMut` closure body
2+
--> examples/borrowck_3.rs:2:20
3+
|
4+
1 | fn foo(a: &mut u32) -> u32 {
5+
| - variable defined here
6+
2 | let mut b = || &mut *a;
7+
| - ^^^^^^-
8+
| | | |
9+
| | | variable captured here
10+
| | returns a reference to a captured variable which escapes the closure body
11+
| inferred to be a `FnMut` closure
12+
|
13+
= note: `FnMut` closures only have access to their captured variables while they are executing...
14+
= note: ...therefore, they cannot allow references to captured variables to escape
15+
16+
error: could not compile `code` (example "borrowck_3") due to 1 previous error
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0505]: cannot move out of `foo` because it is borrowed
2+
--> examples/borrowck_4.rs:8:5
3+
|
4+
6 | let mut foo = Foo(&mut data);
5+
| ------- binding `foo` declared here
6+
7 | mutate(&mut foo);
7+
| -------- borrow of `foo` occurs here
8+
8 | dbg!(foo);
9+
| ^^^^^^^^^
10+
| |
11+
| move out of `foo` occurs here
12+
| borrow later used here
13+
|
14+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
15+
help: consider cloning the value if the performance cost is acceptable
16+
|
17+
7 | mutate(&mut foo).clone();
18+
| ++++++++
19+
20+
For more information about this error, try `rustc --explain E0505`.
21+
error: could not compile `code` (example "borrowck_4") due to 1 previous error
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0499]: cannot borrow `*map` as mutable more than once at a time
2+
--> examples/borrowck_5.rs:8:5
3+
|
4+
3 | fn get_or_insert_1234(map: &mut HashMap<u32, u64>) -> &mut u64 {
5+
| - let's call the lifetime of this reference `'1`
6+
4 | if let Some(v) = map.get_mut(&0) {
7+
| --- first mutable borrow occurs here
8+
5 | return v;
9+
| - returning this value requires that `*map` is borrowed for `'1`
10+
...
11+
8 | map.insert(0, 1234);
12+
| ^^^ second mutable borrow occurs here
13+
14+
error[E0499]: cannot borrow `*map` as mutable more than once at a time
15+
--> examples/borrowck_5.rs:9:5
16+
|
17+
3 | fn get_or_insert_1234(map: &mut HashMap<u32, u64>) -> &mut u64 {
18+
| - let's call the lifetime of this reference `'1`
19+
4 | if let Some(v) = map.get_mut(&0) {
20+
| --- first mutable borrow occurs here
21+
5 | return v;
22+
| - returning this value requires that `*map` is borrowed for `'1`
23+
...
24+
9 | map.get_mut(&0).unwrap()
25+
| ^^^ second mutable borrow occurs here
26+
27+
For more information about this error, try `rustc --explain E0499`.
28+
error: could not compile `code` (example "borrowck_5") due to 2 previous errors

0 commit comments

Comments
 (0)