rust-lang/rust · error

LLVM does not have support for cleanupret

Error message

LLVM does not have support for cleanupret

What it means

This panic is raised when `Builder::cleanup_ret` calls the LLVM-C `LLVMBuildCleanupRet` intrinsic (which emits a `cleanupret` instruction to exit a funclet cleanup pad during structured exception handling) and LLVM returns an error/null instead of a value. The `.expect("LLVM does not have support for cleanupret")` guard converts the LLVM failure into an immediate compiler panic, since a missing cleanupret leaves the funclet IR malformed. It is the companion to cleanuppad for MSVC-style unwinding.

Source

Thrown at compiler/rustc_codegen_llvm/src/builder.rs:1322

    }

    fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
        let ret = unsafe {
            llvm::LLVMBuildCleanupPad(
                self.llbuilder,
                parent,
                args.as_ptr(),
                args.len() as c_uint,
                c"cleanuppad".as_ptr(),
            )
        };
        Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
    }

    fn cleanup_ret(&mut self, funclet: &Funclet<'ll>, unwind: Option<&'ll BasicBlock>) {
        unsafe {
            llvm::LLVMBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind)
                .expect("LLVM does not have support for cleanupret");
        }
    }

    fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
        let ret = unsafe {
            llvm::LLVMBuildCatchPad(
                self.llbuilder,
                parent,
                args.as_ptr(),
                args.len() as c_uint,
                c"catchpad".as_ptr(),
            )
        };
        Funclet::new(ret.expect("LLVM does not have support for catchpad"))
    }

    fn catch_switch(
        &mut self,

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Use the rustc toolchain's bundled LLVM instead of a system LLVM.
  2. Rebuild LLVM with `-DLLVM_ENABLE_EH=ON` and `-DLLVM_ENABLE_MSVC`-compatible target support.
  3. Upgrade or downgrade rustc to a release whose LLVM version matches the expected funclet EH support.
  4. For non-shipping builds, compile with `-Cpanic=abort` to avoid emitting cleanupret.

Example fix

// before
RUSTFLAGS="-Cllvm-args=..." rustc --target x86_64-pc-windows-msvc main.rs
// after
rustc +stable --target x86_64-pc-windows-msvc main.rs   # bundled LLVM
Defensive patterns

Strategy: validation

Validate before calling

# cleanupret is the unwind edge emitted right after a cleanuppad, so the same
# WinEH probe applies.
rustc -vV >/dev/null 2>&1 || { echo 'no working rustc'; exit 1; }
if [ -n "$RUST_TARGET" ] && case "$RUST_TARGET" in *windows-*) false;; *) true;; esac; then
  echo "target $RUST_TARGET will not emit funclet instructions" 
else
  rustup show >/dev/null 2>&1 || echo 'WARNING: non-rustup toolchain; verify LLVM WinEH support'
fi

Try / catch

out="$(cargo build 2>&1)"; rc=$?
if [ $rc -ne 0 ]; then
  case "$out" in
    *"LLVM does not have support for cleanupret"*)
      echo "LLVM cannot lower SEH cleanupret for this toolchain/target" >&2
      echo "fix: use rustup toolchain or switch to *-windows-msvc" >&2 ;;
    *) echo "$out" >&2 ;;
  esac
  exit $rc
fi

Prevention

When it happens

Trigger: Compulating any function that unwinds through a cleanup pad on an MSVC target (`*-pc-windows-msvc`) and the linked LLVM build does not support the `cleanupret` instruction — i.e. `LLVMBuildCleanupRet` at builder.rs:1321 returns Err. This typically happens only when the LLVM C API wrapper returns an error code for an unsupported EH opcode.

Common situations: Same root cause as the other funclet EH panics: a rustc built against an LLVM that lacks MSVC exception-handling support, a hand-built LLVM with `-DLLVM_ENABLE_EH=OFF`, or a toolchain mismatch where `rustc_codegen_llvm` is newer/older than its LLVM.

Related errors


AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03). Data as JSON: /data/errors/9c9c876d44c84072.json. Report an issue: GitHub.