Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/ui/impl-trait/confusing-lifetime-error-issue-79033.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Regression test for https://github.com/rust-lang/rust/issues/79033/
// rustc should be clear about the lifetime issue below where
// self is borrowed for the duration of the returned iterator
// but the lifetime of self will end when fn lines() returns.
//@ edition: 2024

#[derive(Debug, Clone, Copy)]
pub struct Location<'a> {
pub filename: &'a str,
pub start: LocationHalf,
pub end: LocationHalf,
}

#[derive(Debug, Clone, Copy)]
pub struct LocationHalf {
pub line: u32,
pub column: u32,
}

impl Location<'_> {
/// Returns an iterator over the line numbers and lines of this location.
pub fn lines<'a>(self, source: &'a str) -> impl Iterator<Item = (u32, &'a str)> {
let lines = source.split('\n');
lines.enumerate().filter_map(|(i, line)| {
//~^ ERROR closure may outlive the current function, but it borrows `self.start.line`, which is owned by the current function
//~| ERROR closure may outlive the current function, but it borrows `self.end.line`, which is owned by the current function
if self.start.line as usize <= i && i <= self.end.line as usize {
Some((i as u32 + 1, line))
} else {
None
}
})
}
}

fn main() {}
51 changes: 51 additions & 0 deletions tests/ui/impl-trait/confusing-lifetime-error-issue-79033.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
error[E0373]: closure may outlive the current function, but it borrows `self.start.line`, which is owned by the current function
--> $DIR/confusing-lifetime-error-issue-79033.rs:24:38
|
LL | lines.enumerate().filter_map(|(i, line)| {
| ^^^^^^^^^^^ may outlive borrowed value `self.start.line`
...
LL | if self.start.line as usize <= i && i <= self.end.line as usize {
| --------------- `self.start.line` is borrowed here
|
note: function requires argument type to outlive `'static`
--> $DIR/confusing-lifetime-error-issue-79033.rs:24:9
|
LL | / lines.enumerate().filter_map(|(i, line)| {
LL | |
LL | |
LL | | if self.start.line as usize <= i && i <= self.end.line as usize {
... |
LL | | })
| |__________^
help: to force the closure to take ownership of `self.start.line` (and any other referenced variables), use the `move` keyword
|
LL | lines.enumerate().filter_map(move |(i, line)| {
| ++++

error[E0373]: closure may outlive the current function, but it borrows `self.end.line`, which is owned by the current function
--> $DIR/confusing-lifetime-error-issue-79033.rs:24:38
|
LL | lines.enumerate().filter_map(|(i, line)| {
| ^^^^^^^^^^^ may outlive borrowed value `self.end.line`
...
LL | if self.start.line as usize <= i && i <= self.end.line as usize {
| ------------- `self.end.line` is borrowed here
|
note: function requires argument type to outlive `'static`
--> $DIR/confusing-lifetime-error-issue-79033.rs:24:9
|
LL | / lines.enumerate().filter_map(|(i, line)| {
LL | |
LL | |
LL | | if self.start.line as usize <= i && i <= self.end.line as usize {
... |
LL | | })
| |__________^
help: to force the closure to take ownership of `self.end.line` (and any other referenced variables), use the `move` keyword
|
LL | lines.enumerate().filter_map(move |(i, line)| {
| ++++

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0373`.
Loading