-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Improve diagnostics for buffer reuse with borrowed references #147974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JohnTitor
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
JohnTitor:diag-detect-buf-reuse-pattern
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+185
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
fn process_data(_: &[&[u8]]) {} | ||
|
||
fn test_buffer_cleared_after_use() { | ||
let sources = vec![vec![1u8, 2, 3, 4, 5], vec![6, 7, 8, 9]]; | ||
let mut buffer: Vec<&[u8]> = vec![]; | ||
//~^ NOTE variable `buffer` declared here | ||
|
||
for source in sources { | ||
let data: Vec<u8> = source; | ||
//~^ NOTE binding `data` declared here | ||
buffer.extend(data.split(|x| *x == 3)); | ||
//~^ ERROR `data` does not live long enough | ||
//~| NOTE borrowed value does not live long enough | ||
//~| NOTE borrow later used here | ||
//~| NOTE `buffer` is a collection that stores borrowed references, but `data` does not live long enough to be stored in it | ||
//~| HELP buffer reuse with borrowed references requires unsafe code or restructuring | ||
process_data(&buffer); | ||
buffer.clear(); | ||
} //~ NOTE `data` dropped here while still borrowed | ||
} | ||
|
||
fn test_buffer_cleared_at_start() { | ||
let sources = vec![vec![1u8, 2, 3, 4, 5], vec![6, 7, 8, 9]]; | ||
let mut buffer: Vec<&[u8]> = vec![]; | ||
//~^ NOTE variable `buffer` declared here | ||
|
||
for source in sources { | ||
buffer.clear(); | ||
//~^ NOTE borrow later used here | ||
let data: Vec<u8> = source; | ||
//~^ NOTE binding `data` declared here | ||
buffer.extend(data.split(|x| *x == 3)); | ||
//~^ ERROR `data` does not live long enough | ||
//~| NOTE borrowed value does not live long enough | ||
//~| NOTE `buffer` is a collection that stores borrowed references, but `data` does not live long enough to be stored in it | ||
//~| HELP buffer reuse with borrowed references requires unsafe code or restructuring | ||
process_data(&buffer); | ||
} //~ NOTE `data` dropped here while still borrowed | ||
} | ||
|
||
fn test_no_explicit_clear() { | ||
let sources = vec![vec![1u8, 2, 3, 4, 5], vec![6, 7, 8, 9]]; | ||
let mut buffer: Vec<&[u8]> = vec![]; | ||
//~^ NOTE variable `buffer` declared here | ||
|
||
for source in sources { | ||
let data: Vec<u8> = source; | ||
//~^ NOTE binding `data` declared here | ||
buffer.extend(data.split(|x| *x == 3)); | ||
//~^ ERROR `data` does not live long enough | ||
//~| NOTE borrowed value does not live long enough | ||
//~| NOTE borrow later used here | ||
//~| NOTE `buffer` is a collection that stores borrowed references, but `data` does not live long enough to be stored in it | ||
//~| HELP buffer reuse with borrowed references requires unsafe code or restructuring | ||
process_data(&buffer); | ||
} //~ NOTE `data` dropped here while still borrowed | ||
} | ||
|
||
fn main() { | ||
test_buffer_cleared_after_use(); | ||
test_buffer_cleared_at_start(); | ||
test_no_explicit_clear(); | ||
} |
64 changes: 64 additions & 0 deletions
64
tests/ui/borrowck/buffer-reuse-pattern-issue-147694.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
error[E0597]: `data` does not live long enough | ||
--> $DIR/buffer-reuse-pattern-issue-147694.rs:11:23 | ||
| | ||
LL | let mut buffer: Vec<&[u8]> = vec![]; | ||
| ---------- variable `buffer` declared here | ||
... | ||
LL | let data: Vec<u8> = source; | ||
| ---- binding `data` declared here | ||
LL | | ||
LL | buffer.extend(data.split(|x| *x == 3)); | ||
| ------ ^^^^ borrowed value does not live long enough | ||
| | | ||
| borrow later used here | ||
... | ||
LL | } | ||
| - `data` dropped here while still borrowed | ||
| | ||
= note: `buffer` is a collection that stores borrowed references, but `data` does not live long enough to be stored in it | ||
= help: buffer reuse with borrowed references requires unsafe code or restructuring | ||
|
||
error[E0597]: `data` does not live long enough | ||
--> $DIR/buffer-reuse-pattern-issue-147694.rs:32:23 | ||
| | ||
LL | let mut buffer: Vec<&[u8]> = vec![]; | ||
| ---------- variable `buffer` declared here | ||
... | ||
LL | buffer.clear(); | ||
| ------ borrow later used here | ||
LL | | ||
LL | let data: Vec<u8> = source; | ||
| ---- binding `data` declared here | ||
LL | | ||
LL | buffer.extend(data.split(|x| *x == 3)); | ||
| ^^^^ borrowed value does not live long enough | ||
... | ||
LL | } | ||
| - `data` dropped here while still borrowed | ||
| | ||
= note: `buffer` is a collection that stores borrowed references, but `data` does not live long enough to be stored in it | ||
= help: buffer reuse with borrowed references requires unsafe code or restructuring | ||
|
||
error[E0597]: `data` does not live long enough | ||
--> $DIR/buffer-reuse-pattern-issue-147694.rs:49:23 | ||
| | ||
LL | let mut buffer: Vec<&[u8]> = vec![]; | ||
| ---------- variable `buffer` declared here | ||
... | ||
LL | let data: Vec<u8> = source; | ||
| ---- binding `data` declared here | ||
LL | | ||
LL | buffer.extend(data.split(|x| *x == 3)); | ||
| ------ ^^^^ borrowed value does not live long enough | ||
| | | ||
| borrow later used here | ||
... | ||
LL | } | ||
| - `data` dropped here while still borrowed | ||
| | ||
= note: `buffer` is a collection that stores borrowed references, but `data` does not live long enough to be stored in it | ||
= help: buffer reuse with borrowed references requires unsafe code or restructuring | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0597`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't mention
recycle_vec
as I'm not sure if using that crate is the most common solution here.