Skip to content

Commit b573250

Browse files
committed
add tests
1 parent cfd0623 commit b573250

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// edition:2021
2+
3+
// regression test for #112056
4+
5+
fn extend_lifetime<'a, 'b>(x: &mut (&'a str,), y: &'b str) {
6+
let mut closure = |input| x.0 = input;
7+
//~^ ERROR: lifetime may not live long enough
8+
closure(y);
9+
}
10+
11+
fn main() {
12+
let mut tuple = ("static",);
13+
{
14+
let x = String::from("temporary");
15+
extend_lifetime(&mut tuple, &x);
16+
}
17+
println!("{}", tuple.0);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/unique-borrows-are-invariant-1.rs:6:31
3+
|
4+
LL | fn extend_lifetime<'a, 'b>(x: &mut (&'a str,), y: &'b str) {
5+
| -- -- lifetime `'b` defined here
6+
| |
7+
| lifetime `'a` defined here
8+
LL | let mut closure = |input| x.0 = input;
9+
| ^^^^^^^^^^^ assignment requires that `'b` must outlive `'a`
10+
|
11+
= help: consider adding the following bound: `'b: 'a`
12+
13+
error: aborting due to previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// edition:2021
2+
3+
// regression test for #112056
4+
5+
struct Spooky<'b> {
6+
owned: Option<&'static u32>,
7+
borrowed: &'b &'static u32,
8+
}
9+
10+
impl<'b> Spooky<'b> {
11+
fn create_self_reference<'a>(&'a mut self) {
12+
let mut closure = || {
13+
if let Some(owned) = &self.owned {
14+
let borrow: &'a &'static u32 = owned;
15+
self.borrowed = borrow;
16+
//~^ ERROR: lifetime may not live long enough
17+
}
18+
};
19+
closure();
20+
}
21+
}
22+
23+
fn main() {
24+
let mut spooky: Spooky<'static> = Spooky {
25+
owned: Some(&1),
26+
borrowed: &&1,
27+
};
28+
spooky.create_self_reference();
29+
spooky.owned = None;
30+
println!("{}", **spooky.borrowed);
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/unique-borrows-are-invariant-2.rs:15:17
3+
|
4+
LL | impl<'b> Spooky<'b> {
5+
| -- lifetime `'b` defined here
6+
LL | fn create_self_reference<'a>(&'a mut self) {
7+
| -- lifetime `'a` defined here
8+
...
9+
LL | self.borrowed = borrow;
10+
| ^^^^^^^^^^^^^^^^^^^^^^ assignment requires that `'a` must outlive `'b`
11+
|
12+
= help: consider adding the following bound: `'a: 'b`
13+
14+
error: aborting due to previous error
15+

0 commit comments

Comments
 (0)