Skip to content

Commit 2b94f07

Browse files
authored
Rollup merge of #161369 - KevinA-cpu:regression-test-79033, r=TaKO8Ki
Add regression test for confusing lifetime error message issue closes #79033
2 parents 2502d8f + d6e1d0e commit 2b94f07

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/79033/
2+
// rustc should be clear about the lifetime issue below where
3+
// self is borrowed for the duration of the returned iterator
4+
// but the lifetime of self will end when fn lines() returns.
5+
//@ edition: 2024
6+
7+
#[derive(Debug, Clone, Copy)]
8+
pub struct Location<'a> {
9+
pub filename: &'a str,
10+
pub start: LocationHalf,
11+
pub end: LocationHalf,
12+
}
13+
14+
#[derive(Debug, Clone, Copy)]
15+
pub struct LocationHalf {
16+
pub line: u32,
17+
pub column: u32,
18+
}
19+
20+
impl Location<'_> {
21+
/// Returns an iterator over the line numbers and lines of this location.
22+
pub fn lines<'a>(self, source: &'a str) -> impl Iterator<Item = (u32, &'a str)> {
23+
let lines = source.split('\n');
24+
lines.enumerate().filter_map(|(i, line)| {
25+
//~^ ERROR closure may outlive the current function, but it borrows `self.start.line`, which is owned by the current function
26+
//~| ERROR closure may outlive the current function, but it borrows `self.end.line`, which is owned by the current function
27+
if self.start.line as usize <= i && i <= self.end.line as usize {
28+
Some((i as u32 + 1, line))
29+
} else {
30+
None
31+
}
32+
})
33+
}
34+
}
35+
36+
fn main() {}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error[E0373]: closure may outlive the current function, but it borrows `self.start.line`, which is owned by the current function
2+
--> $DIR/confusing-lifetime-error-issue-79033.rs:24:38
3+
|
4+
LL | lines.enumerate().filter_map(|(i, line)| {
5+
| ^^^^^^^^^^^ may outlive borrowed value `self.start.line`
6+
...
7+
LL | if self.start.line as usize <= i && i <= self.end.line as usize {
8+
| --------------- `self.start.line` is borrowed here
9+
|
10+
note: function requires argument type to outlive `'static`
11+
--> $DIR/confusing-lifetime-error-issue-79033.rs:24:9
12+
|
13+
LL | / lines.enumerate().filter_map(|(i, line)| {
14+
LL | |
15+
LL | |
16+
LL | | if self.start.line as usize <= i && i <= self.end.line as usize {
17+
... |
18+
LL | | })
19+
| |__________^
20+
help: to force the closure to take ownership of `self.start.line` (and any other referenced variables), use the `move` keyword
21+
|
22+
LL | lines.enumerate().filter_map(move |(i, line)| {
23+
| ++++
24+
25+
error[E0373]: closure may outlive the current function, but it borrows `self.end.line`, which is owned by the current function
26+
--> $DIR/confusing-lifetime-error-issue-79033.rs:24:38
27+
|
28+
LL | lines.enumerate().filter_map(|(i, line)| {
29+
| ^^^^^^^^^^^ may outlive borrowed value `self.end.line`
30+
...
31+
LL | if self.start.line as usize <= i && i <= self.end.line as usize {
32+
| ------------- `self.end.line` is borrowed here
33+
|
34+
note: function requires argument type to outlive `'static`
35+
--> $DIR/confusing-lifetime-error-issue-79033.rs:24:9
36+
|
37+
LL | / lines.enumerate().filter_map(|(i, line)| {
38+
LL | |
39+
LL | |
40+
LL | | if self.start.line as usize <= i && i <= self.end.line as usize {
41+
... |
42+
LL | | })
43+
| |__________^
44+
help: to force the closure to take ownership of `self.end.line` (and any other referenced variables), use the `move` keyword
45+
|
46+
LL | lines.enumerate().filter_map(move |(i, line)| {
47+
| ++++
48+
49+
error: aborting due to 2 previous errors
50+
51+
For more information about this error, try `rustc --explain E0373`.

0 commit comments

Comments
 (0)