-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
dead_code suggestion for unused fields does not account for changes to auto trait implementations #119645
Comments
For the record, we also warn on named fields, though I feel like the suggestion shouldn't be given if the field has a |
FWIW, the lint does actually check for and allow |
Right: use std::rc::Rc;
struct Pending {
a: Rc<()>,
}
fn pending() -> Pending {
Pending { a: Rc::new(()) }
}
fn main() {
let _ = pending();
}
The new code is consistent with the current behavior, so I think that it's a net step forward and I'd argue about it being a regression. The question becomes what do we want to do next? It seems like the longer prose about "PhantomData is not able to propagate all auto-traits" shouldn't go into the lint message because it feels too wordy and niche, but should certainly go into the lint docs. Or maybe it should go in the diagnostic? Maybe we get some outside diagnostics opinion? It does seem like the named and tuple field lint text should be more similar though. |
Yeah, I agree.
What do you mean by this? rust/compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs Lines 76 to 77 in 595bc6f
|
Sorry, I thought PhantomData always implemented Unpin, but it did not (there was a PR for it but not merged: #62786). At least at this time, it seems to propagate all (stable?) auto-traits. |
@rustbot labels +T-lang +I-lang-nominated Nominating so we can discuss the proposal to mention |
We talked about this in today's @rust-lang/lang meeting. Some of us were generally skeptical about making this suggestion in the general case. At most, we felt that it might be worth adding a note to the default suggestion if the removal would actually affect the auto traits of the struct (if we can tell that). If it wouldn't affect the auto traits we should preserve the suggestion. If it would, we could add a note like "Note that this removal will make the struct |
@rustbot labels -I-lang-nominated In the end, there seemed general consensus to suggest Let's go ahead and unnominate the issue. If a PR is filed to add the suggestion to the lint in this manner, please nominate that for T-lang. |
I don't believe it's possible in general to detect whether or not the auto traits of a struct are going to change in the presence of generics, normalizable associated types, and the fact that the list of auto-traits is not hard-coded into the compiler 😓 |
Generics are another valid use case for PhantomData, making them used when they would otherwise be unused. Otherwise most uses of PhantomData to affect auto traits don't depend on generics in my experience. My proposal would be: If we had a list of auto traits, we could ask whether the type implements each of those auto traits.
|
I see that implementations of struct Foo(&'static ());
impl Foo {
fn new() -> Self {
Self(&())
}
}
impl Drop for Foo {
fn drop(&mut self) {
println!("Foo dropped: {:?}", self.0);
}
}
struct Bar(Foo);
impl Bar {
fn new() -> Self {
Self(Foo::new())
}
}
fn main() {
let _ = Bar::new();
} In this case, removing the field causes the |
Note this is now a stable-to-beta regression. |
You can simply |
Thanks, it's good to know! If this makes it into stable, I guess that's what I'll end up doing, although I still feel like the suggestion is a bid misleading (the warning text mentions that the field is "never read", which is true, but suggesting to remove the field or change its type actually changes the behavior). |
…ywiser Document how removing a type's field can be bad and what to do instead Related to rust-lang#119645
…ywiser Document how removing a type's field can be bad and what to do instead Related to rust-lang#119645
…ywiser Document how removing a type's field can be bad and what to do instead Related to rust-lang#119645
…ywiser Document how removing a type's field can be bad and what to do instead Related to rust-lang#119645
Rollup merge of rust-lang#121899 - shepmaster:dead-code-docs, r=wesleywiser Document how removing a type's field can be bad and what to do instead Related to rust-lang#119645
I tried this code:
(The original code: https://github.com/rust-lang/futures-rs/blob/72e7e397cdfe7574e9b5d8845cafefc6e5dda70a/futures-executor/tests/local_pool.rs#L13)
I expected to see this happen: no warning or a warning suggests
Pending(PhantomData<Rc<()>>)
.Instead, this happened:
A warning suggests
Pending(())
.Changing
Rc<()>
to()
changes auto trait implementations ofPending
, so dead_code should not suggest such a code or should suggest both code (PhantomData<Rc<()>>
and()
) with explanations.Also, if it suggests PhantomData, it should also be noted that PhantomData is not able to propagate all auto-traits (e.g., Unpin). (although this is not a problem in the above case)EDIT: see #119645 (comment)https://github.com/rust-lang/futures-rs/actions/runs/7428509435/job/20215931270
Meta
rustc --version --verbose
:This has been shown in the latest nightly (nightly-2024-01-06) since #118297 (cc @shepmaster). IMO, it should not be changed to be warned by default with such a lint with a wrong suggestion.
The text was updated successfully, but these errors were encountered: