forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#83004 - sunjay:field-never-read-issue-81658…
…, r=pnkfelix Improve diagnostic for when field is never read Related to (but does not close) rust-lang#81658 This completes the first step of `````@pnkfelix's````` [mentoring instructions](rust-lang#81658 (comment)) but does not actually improve the diagnostics (yet!). The two tests are heavily reduced versions of code from the original bug report. I've confirmed that the reduced `field-used-in-ffi` test [fails on nightly](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f0862c89ddca028c55c20a5ed05e679a) but [passes on stable](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f0862c89ddca028c55c20a5ed05e679a). This confirms that the regression is reproduced correctly. The `drop-only-field` test is a case that `````@pnkfelix````` mentioned in his mentoring instructions. It is not a regression, but will come in handy when we make the diagnostic smarter by looking at whether the field type implements `Drop`. Per the [rustc-dev-guide](https://rustc-dev-guide.rust-lang.org/tests/adding.html), each test includes a comment summarizing what it is about.
- Loading branch information
Showing
31 changed files
with
429 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,42 @@ | ||
//! The field `guard` is never used directly, but it is still useful for its side effect when | ||
//! dropped. Since rustc doesn't consider a `Drop` impl as a use, we want to make sure we at least | ||
//! produce a helpful diagnostic that points the user to what they can do if they indeed intended to | ||
//! have a field that is only used for its `Drop` side effect. | ||
//! | ||
//! Issue: https://github.com/rust-lang/rust/issues/81658 | ||
|
||
#![deny(dead_code)] | ||
|
||
use std::sync::{Mutex, MutexGuard}; | ||
|
||
/// Holds a locked value until it is dropped | ||
pub struct Locked<'a, T> { | ||
// Field is kept for its affect when dropped, but otherwise unused | ||
guard: MutexGuard<'a, T>, //~ ERROR field is never read | ||
} | ||
|
||
impl<'a, T> Locked<'a, T> { | ||
pub fn new(value: &'a Mutex<T>) -> Self { | ||
Self { | ||
guard: value.lock().unwrap(), | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let items = Mutex::new(vec![1, 2, 3]); | ||
|
||
// Hold a lock on items while doing something else | ||
let result = { | ||
// The lock will be released at the end of this scope | ||
let _lock = Locked::new(&items); | ||
|
||
do_something_else() | ||
}; | ||
|
||
println!("{}", result); | ||
} | ||
|
||
fn do_something_else() -> i32 { | ||
1 + 1 | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/ui/lint/dead-code/drop-only-field-issue-81658.stderr
This file contains 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,17 @@ | ||
error: field is never read: `guard` | ||
--> $DIR/drop-only-field-issue-81658.rs:15:5 | ||
| | ||
LL | guard: MutexGuard<'a, T>, | ||
| -----^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: if this is intentional, prefix it with an underscore: `_guard` | ||
| | ||
= note: the leading underscore signals that this field serves some other purpose even if it isn't used in a way that we can detect. (e.g. for its effect when dropped or in foreign code) | ||
note: the lint level is defined here | ||
--> $DIR/drop-only-field-issue-81658.rs:8:9 | ||
| | ||
LL | #![deny(dead_code)] | ||
| ^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
This file contains 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
50 changes: 50 additions & 0 deletions
50
src/test/ui/lint/dead-code/field-used-in-ffi-issue-81658.rs
This file contains 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,50 @@ | ||
//! The field `items` is being "used" by FFI (implicitly through pointers). However, since rustc | ||
//! doesn't know how to detect that, we produce a message that says the field is unused. This can | ||
//! cause some confusion and we want to make sure our diagnostics help as much as they can. | ||
//! | ||
//! Issue: https://github.com/rust-lang/rust/issues/81658 | ||
|
||
#![deny(dead_code)] | ||
|
||
/// A struct for holding on to data while it is being used in our FFI code | ||
pub struct FFIData<T> { | ||
/// These values cannot be dropped while the pointers to each item | ||
/// are still in use | ||
items: Option<Vec<T>>, //~ ERROR field is never read | ||
} | ||
|
||
impl<T> FFIData<T> { | ||
pub fn new() -> Self { | ||
Self {items: None} | ||
} | ||
|
||
/// Load items into this type and return pointers to each item that can | ||
/// be passed to FFI | ||
pub fn load(&mut self, items: Vec<T>) -> Vec<*const T> { | ||
let ptrs = items.iter().map(|item| item as *const _).collect(); | ||
|
||
self.items = Some(items); | ||
|
||
ptrs | ||
} | ||
} | ||
|
||
extern { | ||
/// The FFI code that uses items | ||
fn process_item(item: *const i32); | ||
} | ||
|
||
fn main() { | ||
// Data cannot be dropped until the end of this scope or else the items | ||
// will be dropped before they are processed | ||
let mut data = FFIData::new(); | ||
|
||
let ptrs = data.load(vec![1, 2, 3, 4, 5]); | ||
|
||
for ptr in ptrs { | ||
// Safety: This pointer is valid as long as the arena is in scope | ||
unsafe { process_item(ptr); } | ||
} | ||
|
||
// Items will be safely freed at the end of this scope | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/ui/lint/dead-code/field-used-in-ffi-issue-81658.stderr
This file contains 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,17 @@ | ||
error: field is never read: `items` | ||
--> $DIR/field-used-in-ffi-issue-81658.rs:13:5 | ||
| | ||
LL | items: Option<Vec<T>>, | ||
| -----^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: if this is intentional, prefix it with an underscore: `_items` | ||
| | ||
= note: the leading underscore signals that this field serves some other purpose even if it isn't used in a way that we can detect. (e.g. for its effect when dropped or in foreign code) | ||
note: the lint level is defined here | ||
--> $DIR/field-used-in-ffi-issue-81658.rs:7:9 | ||
| | ||
LL | #![deny(dead_code)] | ||
| ^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
This file contains 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.