Skip to content

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

Open
@felipeamp

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

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