rust-lang / rust-lang/rust-analyzer

proc_macro attribue fails to expand when combined with `cfg_attr`

Open
#13,360 10 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I have found that mixing certain atribute style proc macros fails when combined with cfg_attr and feature flags.
Link to minimally reproducible example

// This one works
#[safer_ffi::derive_ReprC]
#[ReprC::opaque]
pub struct NotGated {
    bar: String,
}

// This one doesn't work
#[cfg_attr(feature = "c_api", safer_ffi::derive_ReprC, ReprC::opaque)]
pub struct Gated {
    bar: String,
}

rust-analyzer version: rust-analyzer version: 0.4.1227-standalone

rustc version: rustc 1.66.0-nightly (ce7f0f1aa 2022-09-28) (please note its been broken for months, its not a recent regression)

relevant settings: "rust-analyzer.cargo.features": [ "c_api" ], and c_api is a default feature.

Original post before I got a minimal example (outdated) Unfortunately I haven't been able to make a minimal reproducible example yet, but at least by making this issue I can bring some awareness.

I have a medium sized rust codebase, which uses macros quite a bit. There is one type here that never has autocomplete.

It is using proc-macros from safer-ffi and is cfg-gated. I haven't been able to figure out the root cause of the problem. I'll try to work a bit more on finding a minimal reproduction.

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 linked minimally reproducible example in src/lib.rs and reproduce the failure with rust-analyzer's c_api feature setting enabled. Compare the working separate attributes with the failing cfg_attr combination across the reported rustc and rust-analyzer versions. Done means the gated proc-macro attributes expand correctly and the resulting type is available to rust-analyzer.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.