rust-lang / rust-lang/rust-analyzer

"Fill match arms" and similar auto completion doesn't respect cfg attributes on fields inside enum variants.

Open
#16,836 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-assists C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

rust-analyzer version: 0.3.1877-standalone

rustc version: rustc 1.78.0-nightly (3cbb93223 2024-03-13)

Problem: When using the "Fill match arms" feature on an enum where some fields are gated behind a feature flag, the generated code includes the gated fields, causing compilation errors. This does not occur with structs using the "Fill struct fields" feature.

I stumped upon this when using a library which hides some debugging data of an enum behind a feature flag, but I could see this pop up in other situations. Consider the snippet below

enum EnumWithSecret {
    Default,

    Other {
        #[cfg(feature = "gated")]
        secret: i32,
        public: i32
    },

    Another (
        #[cfg(feature = "gated")]
        i32, // secret
        i32, // public
    ),
}

Then using "Fill match arms" on some data of type EnumWithSecret yields

// Gives compilation error!
match data {
    EnumWithSecret::Default => todo!(),
    EnumWithSecret::Other { secret, public } => todo!(),
    EnumWithSecret::Another(_, _) => todo!(),
}

which gives compilation errors due to the hidden fields. I would expect the hidden fields to not be included by default.

A similar thing does not happen with just a struct, where we get the expected behavior.

struct WithSecret {
    #[cfg(feature = "gated")]
    gated: i32,
    public: i32,
}

and then using "Fill struct fields"

// This is OK
let data = WithSecret {
    public: todo!(),
};

Minimal code snippet to reproduce

enum EnumWithSecret {
    Default,

    Other {
        #[cfg(feature = "gated")]
        secret: i32,
    },
}

fn main() {
    let test = EnumWithSecret::Default;

    match test {
        // Run "Fill match arms"
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal Rust snippet and reproduce the problem using the "Fill match arms" completion. Trace the implementation and existing tests for this completion, comparing it with "Fill struct fields". Done means cfg-gated fields are omitted from generated enum match patterns and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.