rust-lang / rust-lang/rust

Dead code pass no longer considers enum used in `pub extern "C" fn` as used

Open
#126,706 24 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lints C-bug L-dead_code S-has-mcve T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

A repository that I maintain recently received a bug report about our proc macro generating a warning after they upgraded to 1.79.0.

I haven't bisected for the exact commit that introduces the warning, but I have been able to reproduce it in 1.79.0.

Minimal Reproduction

fn main() {}

mod private_mod {
    #[repr(C)]
    pub enum MyEnum {
        Variant(u32)
    }
    
    #[no_mangle]
    pub extern "C" fn some_function() -> MyEnum {
        MyEnum::Variant(5)
    }
}
   Compiling playground v0.0.1 (/playground)
warning: field `0` is never read
 --> src/main.rs:6:17
  |
6 |         Variant(u32)
  |         ------- ^^^
  |         |
  |         field in this variant
  |
  = note: `#[warn(dead_code)]` on by default
help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
  |
6 |         Variant(())
  |                 ~~

warning: `playground` (bin "playground") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.85s
     Running `target/debug/playground`

In the playground -> https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3f5c81c0749130a6e46fbea14cbb055f

Potential Candidates

This comment links to some rustc commits that may have introduced the issue https://github.com/chinedufn/swift-bridge/pull/278#issuecomment-2179226690

As mentioned, I haven't taken the time to bisect since I know that it's possible that a core maintainer already has a good sense of when/how this regression could have been introduced.

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 by running the minimal Rust reproduction and comparing the dead-code behavior across versions, then inspect the rustc commits linked in the referenced swift-bridge comment. Trace the dead-code pass for a repr(C) enum returned by a pub extern "C" function; done means a regression test covers this case and no spurious field warning is emitted.

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
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.