-
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 false positive introduced in rustc 1.78.0-nightly (8ace7ea1f 2024-02-07) #120770
Comments
Some of these are legit, others are false positives. I've filed rust-lang/rust#120770 for the latter.
WG-prioritization assigning priority (Zulip discussion). @rustbot label -I-prioritize +P-high |
searched nightlies: from nightly-2024-02-01 to nightly-2024-02-08 bisected with cargo-bisect-rustc v0.6.7Host triple: x86_64-unknown-linux-gnu cargo bisect-rustc --start=2024-02-01 --regress error --script script.sh |
Some of these are legit, others are false positives. I've filed rust-lang/rust#120770 for the latter.
How would you expect the compiler to figure out that it is constructed? Just because there's a value of that type somewhere doesn't mean it is, that would make the lint basically useless. |
By the way, the following code does not compile on stable (play): #![deny(warnings)]
pub struct Bar(u32);
#[repr(transparent)]
struct Wrapper(Bar);
fn try_read<T>(p: &u32) -> T {
unsafe { std::ptr::read_unaligned(p as *const u32 as *const T) }
}
pub fn foo(p: &u32) -> Bar {
let Wrapper(bar) = try_read(p);
bar
} |
Yeah, this isn't a regression, but a long standing issue (which seems hard to impossible to solve) that's now being exposed a bit more. |
I don't see a way to solve this without looking through generic function calls (recursively, by looking at the function bodies). That also means it can only be done after monomorphization. And even then it won't catch e.g. a transmute that produces a None. Instead that would be treated as producing a value of the Some type hypothetically. Such deeper analyses maybe better suited to external tools that even do partial function instantiation, cross crate analysis... |
At risk of mentioning the obvious, the example provided is not entirely minimal. It will work for any "constructor" of the form I'm seeing this in some ffi-heavy code where it didn't trigger previously. In my case the "constructor" looks like this: unsafe fn sysctl<T>(mib: &mut [c_int]) -> Option<T> {
todo!()
} |
Is that not what is shown in #120770 (comment) ? |
@rustbot claim I suddenly realized that this probably didn't need to be done after monomorphization. We won't meet this warning if specific the type: #![deny(warnings)]
pub struct Bar(u32);
#[repr(transparent)]
struct Wrapper(Bar);
fn try_read<T>(p: &u32) -> T {
unsafe { std::ptr::read_unaligned(p as *const u32 as *const T) }
}
pub fn foo(p: &u32) -> Bar {
let Wrapper(bar) = try_read::<Wrapper>(p);
bar
} So I think maybe we can just mark the type live when visiting the corresponding pattern. I will try it. |
…=pnkfelix Improve dead code analysis Fixes rust-lang#120770 1. check impl items later if self ty is private although the trait method is public, cause we must use the ty firstly if it's private 2. mark the adt live if it appears in pattern, like generic argument, this implies the use of the adt 3. based on the above, we can handle the case that private adts impl Default, so that we don't need adding rustc_trivial_field_reads on Default, and the logic in should_ignore_item r? `@pnkfelix`
Rollup merge of rust-lang#127107 - mu001999-contrib:dead/enhance-2, r=pnkfelix Improve dead code analysis Fixes rust-lang#120770 1. check impl items later if self ty is private although the trait method is public, cause we must use the ty firstly if it's private 2. mark the adt live if it appears in pattern, like generic argument, this implies the use of the adt 3. based on the above, we can handle the case that private adts impl Default, so that we don't need adding rustc_trivial_field_reads on Default, and the logic in should_ignore_item r? ``@pnkfelix``
I believe this is uncovered again since we reverted recent changes to dead code analysis |
It can also trigger when the constructor is used in reachable code:
Perhaps without monomorphization we could reason that:
|
This code compiles on stable (and nightlies before 2024-02-07) but now produces:
NOTE: playground's nightly is not new enough
playground
The text was updated successfully, but these errors were encountered: