Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
felipeamp opened this issue May 25, 2021 · 2 comments
Labels
A-FFI Area: Foreign function interface (FFI) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-dead_code Lint: dead_code T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@felipeamp
Copy link

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

@felipeamp felipeamp added the C-bug Category: This is a bug. label May 25, 2021
@jonas-schievink jonas-schievink added A-FFI Area: Foreign function interface (FFI) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels May 25, 2021
@felipeamp felipeamp 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 May 25, 2021
@JohnTitor JohnTitor added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 15, 2021
@WaffleLapkin
Copy link
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)

@jieyouxu jieyouxu added the L-dead_code Lint: dead_code label May 13, 2024
@klensy
Copy link
Contributor

klensy commented Jul 9, 2024

Found that in #127274 with TypeKind enum.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-FFI Area: Foreign function interface (FFI) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-dead_code Lint: dead_code T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants