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: missing structure fields, on structure fields behind #cfg #16947

Closed
tyrypyrking opened this issue Mar 25, 2024 · 1 comment · Fixed by #17006
Closed

False positive: missing structure fields, on structure fields behind #cfg #16947

tyrypyrking opened this issue Mar 25, 2024 · 1 comment · Fixed by #17006
Assignees
Labels
A-nameres name, path and module resolution A-proc-macro proc macro C-bug Category: bug

Comments

@tyrypyrking
Copy link

tyrypyrking commented Mar 25, 2024

minimal code snippet to reproduce:

//Cargo.toml
[package]
name = "minissue"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0.197", features = ["derive"] }

[features]
default = ["hello", "bye"]
hello = []
bye = []

//main.rs
use serde::Deserialize;

//Both lines display as two errors each.
#[derive(Deserialize)]
struct SomeStruct { //Error: missing structure field:  - hello
    #[cfg(feature = "hello")]
    pub hello: String,
    #[cfg(not(feature = "bye"))]
    pub bye: String, //Error: no such field
}

fn main() {
    #[cfg(feature = "hello")]
    println!(
        "{}",
        SomeStruct {
            hello: "hello".to_owned(),
        }
        .hello
    );
}

Only requires #[derive(Deserialize)], other derives such as Serialize, Debug, etc, are working fine.
cargo check doesn't show any errors.
We can avoid the rust-analyzer false flags by adding redundant code. Something like the following:

#[cfg(not(feature = "hello"))]
#[allow(dead_code)]
#[serde(skip_deserializing)]
pub hello: (),

Previously we were tracking this behavior with #11657 as it was the same error we had in this snippet. But as of recently the issue was marked resolved.

rust-analyzer version: 2024-03-25

rustc version:
rustc 1.74.0 (79e9716c9 2023-11-13)

@tyrypyrking tyrypyrking added the C-bug Category: bug label Mar 25, 2024
@Veykril Veykril added A-nameres name, path and module resolution A-proc-macro proc macro labels Mar 25, 2024
@TinyQAQ
Copy link

TinyQAQ commented Apr 3, 2024

I meet this error today, with RA extension version v0.3.1906 in vscode.

@Veykril Veykril self-assigned this Apr 3, 2024
bors added a commit that referenced this issue Apr 3, 2024
fix: Fix some cfg censoring bugs

Fixes #16947
@bors bors closed this as completed in c0eb78e Apr 3, 2024
lnicola pushed a commit to lnicola/rust that referenced this issue Apr 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-nameres name, path and module resolution A-proc-macro proc macro C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants