rust-lang / rust-lang/rust

`-Zunpretty=expanded` no longer emitting `#[cfg(feature = xxx)]` guards?

Open
#139,715 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-pretty C-bug requires-nightly T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Hi there!

I'm trying to track down an issue we're having with nightly and cbindgen in https://github.com/rustls/rustls-ffi. In particular, we rely on cbindgen's functionality to map Cargo feature flags to C #define's and #if defined(xxx) guards. Since our codebase uses some macros, we also rely on cbindgen's parse.expand functionality, which in turn requires using nightly.

Sometime recently with a nightly update we observed the generated .h produced by cbindgen was no longer producing the required #if defined(XXX) guards.

I believe I've traced this down to something changing in the nightly toolchain such that -Zunpretty=expanded with --features enabled no longer produces expanded code decorated with the #[cfg(feature = "xxx")] annotations. I think this in turn means cbindgen doesn't associate the function with required features.

The reproduction I offer below doesn't use cbindgen at all, and so is hopefully easier to reason about.

Code

I tried cargo rustc --features=turbo-mode -- -Zunpretty=expanded with the following code using nightly:

pub fn add(left: u64, right: u64) -> u64 {
    left + right
}

#[cfg(feature = "turbo-mode")]
pub fn turbo_add(left: u64, right: u64) -> u64 {
    left + right
}

I expected to see this happen: I expected the produced output to show the turbo_add code annotated with the expected #[cfg(feature = "turbo-mode")] annotation:

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2024::*;
#[macro_use]
extern crate std;
pub fn add(left: u64, right: u64) -> u64 { left + right }

#[cfg(feature = "turbo-mode")]
pub fn turbo_add(left: u64, right: u64) -> u64 { left + right }

Instead, this happened: the output with the most recent nightly instead drops the annotation, producing:

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2024::*;
#[macro_use]
extern crate std;
pub fn add(left: u64, right: u64) -> u64 { left + right }

pub fn turbo_add(left: u64, right: u64) -> u64 { left + right }

I pushed a simple reproduction repo here: https://github.com/cpu/expand-test

Version it worked on

It most recently worked on: nightly-2025-03-25

Version with regression

The first failure I saw from this was on nightly-x86_64-unknown-linux-gnu - rustc 1.88.0-nightly (1799887bb 2025-03-29)

Backtrace

N/A

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 reproduction in https://github.com/cpu/expand-test and run cargo rustc --features=turbo-mode -- -Zunpretty=expanded. Compare nightly-2025-03-25 with rustc 1.88.0-nightly (1799887bb 2025-03-29), focusing on why the #[cfg(feature = "turbo-mode")] annotation is dropped. Done means expanded output preserves the feature guard when the feature is enabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.