Skip to content
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

False positive "field is never read" warnings #81626

Closed
emilio opened this issue Feb 1, 2021 · 10 comments · Fixed by #82297
Closed

False positive "field is never read" warnings #81626

emilio opened this issue Feb 1, 2021 · 10 comments · Fixed by #82297
Assignees
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Milestone

Comments

@emilio
Copy link
Contributor

emilio commented Feb 1, 2021

Here's a reduced test-case from Firefox source code:

struct Data {
    pub field: bool,
}

struct Foo<'a> {
    data: &'a mut Data,
}

impl<'a> Foo<'a> {
    fn parse_data(&mut self) {
        macro_rules! parse {
            () => {
                self.data.field = true;
            }
        }
        parse!()
    }
}

fn main() {
    let mut data = Data { field: false };
    {
        let mut foo = Foo { data: &mut data };
        foo.parse_data();
    }
    println!("{}", data.field);
}

Compiling that program warns with:

warning: field is never read: `data`
 --> t.rs:6:5
  |
6 |     data: &'a mut Data,
  |     ^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

Which is obviously not true.

Meta

rustc --version --verbose:

rustc 1.51.0-nightly (04caa632d 2021-01-30)
binary: rustc
commit-hash: 04caa632dd10c2bf64b69524c7f9c4c30a436877
commit-date: 2021-01-30
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
LLVM version: 11.0.1
@emilio emilio added the C-bug Category: This is a bug. label Feb 1, 2021
@jonas-schievink jonas-schievink added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Feb 1, 2021
@emilio
Copy link
Contributor Author

emilio commented Feb 1, 2021

Seems like this is a regression from d3c69a4.

@emilio
Copy link
Contributor Author

emilio commented Feb 1, 2021

cc @sanxiyn

@sanxiyn
Copy link
Member

sanxiyn commented Feb 2, 2021

Oops, sorry! Technically, Foo.data field is, in fact, never read. What went wrong is Foo.data is written and read through other ways.

I think restricting the lint to non-reference fields should work, and will submit a PR to do so quickly. Please double check whether my thinking is correct.

@emilio
Copy link
Contributor Author

emilio commented Feb 2, 2021

Ah, yeah, I somehow thought that the macro was needed to trigger the bug but it is not. That sounds good. Writes to mutable references have side effects so doing that seems ok.

@djc
Copy link
Contributor

djc commented Feb 8, 2021

Would that also cover this case here, in the idna crate? (The Mapper::errors field is a &mut reference to an Errors type, which is in fact returned to the caller of processing().)

https://github.com/servo/rust-url/pull/674/checks?check_run_id=1855869353

@emilio
Copy link
Contributor Author

emilio commented Feb 8, 2021

Yeah, I think it's the same.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 20, 2021
Consider auto derefs before warning about write only fields

Changes from rust-lang#81473 extended the dead code lint with an ability to detect
fields that are written to but never read from. The implementation skips
over fields on the left hand side of an assignment, without marking them
as live.

A field access might involve an automatic dereference and de-facto read
the field. Conservatively mark expressions with deref adjustments as
live to avoid generating false positive warnings.

Closes rust-lang#81626.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 22, 2021
Consider auto derefs before warning about write only fields

Changes from rust-lang#81473 extended the dead code lint with an ability to detect
fields that are written to but never read from. The implementation skips
over fields on the left hand side of an assignment, without marking them
as live.

A field access might involve an automatic dereference and de-facto read
the field. Conservatively mark expressions with deref adjustments as
live to avoid generating false positive warnings.

Closes rust-lang#81626.
@bors bors closed this as completed in 9d378b3 Feb 23, 2021
@pnkfelix
Copy link
Member

Ah, this was closed, but the bug leaked into beta (as mentioned in #81658) while the fix is only in nightly.

Reopening and tagging as stable-to-beta regression

@pnkfelix pnkfelix reopened this Mar 10, 2021
@pnkfelix pnkfelix added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. regression-from-stable-to-beta Performance or correctness regression from stable to beta. labels Mar 10, 2021
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Mar 10, 2021
@JohnTitor
Copy link
Member

Assigning P-medium as discussed as part of the Prioritization Working Group procedure and removing I-prioritize.

@JohnTitor JohnTitor added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Mar 14, 2021
@pnkfelix pnkfelix self-assigned this Mar 15, 2021
@pnkfelix
Copy link
Member

As noted on #81473 (comment) , we're going to resolve the beta issue by reverting PR #81473 on beta (which I have assigned myself to take care of).

bors added a commit to rust-lang-ci/rust that referenced this issue Mar 18, 2021
…-fields, r=Mark-Simulacrum

Revert PR 81473 to resolve (on beta) issues 81626 and 81658.

Revert PR rust-lang#81473 to resolve (on beta) issues rust-lang#81626 and rust-lang#81658.

Revert "Add missing brace"

This reverts commit 85ad773.

Revert "Simplify base_expr"

This reverts commit 899aae4.

Revert "Warn write-only fields"

This reverts commit d3c69a4.
@pietroalbini pietroalbini modified the milestone: 1.52.0 Apr 30, 2021
@pietroalbini pietroalbini added this to the 1.51.0 milestone Apr 30, 2021
@pietroalbini
Copy link
Member

This was a regression affecting beta 1.51, closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants