clangir icon indicating copy to clipboard operation
clangir copied to clipboard

Assertion failure on switch statement with code outside a case

Open dkolsen-pgi opened this issue 11 months ago • 1 comments

ClangIR hits an assertion failure when a switch statement contains code that is not covered by a case label.

int f(int x) {
  switch (x) {
    return 0; // Dead code
  case 1:
    return -1;
  }
  return x;
}
clang++: .../clang/lib/CIR/CodeGen/CIRGenStmt.cpp:974: 
cir::CIRGenFunction::buildSwitchStmt(const clang::SwitchStmt&)::<lambda()>::<lambda(mlir::OpBuilder&, mlir::Location, mlir::OperationState&)>: 
Assertion `lastCaseBlock && "expects pre-existing case block"' failed.

While code like this should never appear in production and will only ever be found in test suites that try to break the compiler, it is legal code in both C and C++ and should not trigger an internal compiler error.

dkolsen-pgi avatar Mar 24 '24 17:03 dkolsen-pgi

Note that code outside a case isn't necessarily dead, because you can do things like https://godbolt.org/z/PqEGzKEzb to reach it.

smeenai avatar Sep 06 '24 21:09 smeenai