Skip to content

Compiler warning about dead code when enum is used in a match against return value of FFI call #85677

Open

Description

@felipeamp

I tried this code:

#[repr(C)]
#[derive(Debug, PartialEq)]
enum ResultEnumFromC {
    Success = 0,
    FailureOfTypeOne = 1,
    FailureOfTypeTwo = 2,
}

pub enum ErrEnumInRust {
    FailureOfTypeOne = 0,
    FailureOfTypeTwo = 1,
}

extern "C" {
    fn FunctionFromC() -> ResultEnumFromC;
}

pub fn wrapper_around_function_from_c(
) -> Result<(), ErrEnumInRust> {
    match unsafe { FunctionFromC() } {
        ResultEnumFromC::Success => Ok(()),
        ResultEnumFromC::FailureOfTypeOne => Err(ErrEnumInRust::FailureOfTypeOne),
        ResultEnumFromC::FailureOfTypeTwo => Err(ErrEnumInRust::FailureOfTypeTwo),
    }
}

link to example in Rust playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c3d9e0b917bfabeebeb6285a4aa73a6d

I expected to see this happen: no warning about dead code, since ResultEnumFromC is created on the C side and used in a match on the Rust side.

Instead, this happened: Compiler complained about ResultEnumFromC never being constructed (which is true on the Rust side, but I wouldn't expect this since it's the result of an FFI call).

warning: variant is never constructed: `Success`
 --> src/main.rs:4:5
  |
4 |     Success = 0,
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: variant is never constructed: `FailureOfTypeOne`
 --> src/main.rs:5:5
  |
5 |     FailureOfTypeOne = 1,
  |     ^^^^^^^^^^^^^^^^^^^^

warning: variant is never constructed: `FailureOfTypeTwo`
 --> src/main.rs:6:5
  |
6 |     FailureOfTypeTwo = 2,
  |     ^^^^^^^^^^^^^^^^^^^^

warning: 3 warnings emitted

Meta

Happens on Rust playground on stable, beta and nightly.

rustc --version --verbose:

1.52.1
Backtrace

warning: variant is never constructed: `Success`
 --> src/main.rs:4:5
  |
4 |     Success = 0,
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: variant is never constructed: `FailureOfTypeOne`
 --> src/main.rs:5:5
  |
5 |     FailureOfTypeOne = 1,
  |     ^^^^^^^^^^^^^^^^^^^^

warning: variant is never constructed: `FailureOfTypeTwo`
 --> src/main.rs:6:5
  |
6 |     FailureOfTypeTwo = 2,
  |     ^^^^^^^^^^^^^^^^^^^^

warning: 3 warnings emitted

Activity

  1. added
    A-FFIArea: Foreign function interface (FFI)
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    on May 25, 2021
  2. changed the title [-]Compiler complaining about dead code when enum is used in a match against return value of FFI call[/-] [+]Compiler warning about dead code when enum is used in a match against return value of FFI call[/+] on May 25, 2021
  3. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 15, 2021
  4. WaffleLapkin commented on Dec 21, 2023

    @WaffleLapkin
    Member

    MRE:

    extern {
        fn f() -> E;
    }
    
    #[repr(C)]
    enum E {
        V
    }
    
    pub fn g() {
        let e = unsafe { f() };
        match e {
            E::V => ()
        }
    }
    
    warning: variant `V` is never constructed
     --> src/lib.rs:7:5
      |
    6 | enum E {
      |      - variant in this enum
    7 |     V
      |     ^
      |
      = note: `#[warn(dead_code)]` on by default
    

    (playground)

  5. klensy commented on Jul 9, 2024

    @klensy
    Contributor

    Found that in #127274 with TypeKind enum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-dead_codeLint: dead_codeT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions