Skip to content

[AUTOMATED] Print a byte converted to bool as a comparison and Boolean constants as false/true in Rust output - #1967

Merged
mahaloz merged 5 commits into
mainfrom
fix/rust-bool-cast
Nov 25, 2026
Merged

mahaloz merged 5 commits into
mainfrom
fix/rust-bool-cast

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

With --language rust, a function returning bool can print a call's byte result as call() as bool and its constant returns as integer literals. Neither compiles: Rust has no integer-to-bool cast, and 0 is not a bool.

$ kuna decompile decompiler/crates/kuna-analysis/tests/fixtures/scalar_call_results_x86_64 narrow_bool \
    --language rust --mode reliable --assert "prototype narrow_bool bool narrow_bool(bool skip,uint8 value)"
unsafe fn narrow_bool(mut skip: bool, mut value: u64) -> bool // return-dupe
{
  if skip {
    return 0;
  }
  return wide_value(value) as bool;
}

rustc: error[E0308]: mismatched types (the return 0) and error[E0054]: cannot cast u64 as bool. With this change:

  if skip {
    return false;
  }
  return wide_value(value) as u8 != 0;
  • The Boolean conversion handled a call only when the call's prototype stated a one-byte result. A call with no stated result, or a void one, now uses the callee's recovered return type. A bool result keeps the cast, which is valid. A one-byte integer prints call() != 0. Any other result prints call() as u8 != 0, which tests the low byte the caller reads, whatever the callee's printed return type.
  • Directly under another comparison, that != uses Rust's own operator token, so (f() != 0) != g gets parentheses instead of the chained f() != 0 != g, which does not parse. Elsewhere the existing parentheses are kept.
  • A constant of type bool prints as false/true (as Go output already does) when what consumes it is also Boolean: the return of a function that returns bool, a declared bool variable, or the other side of ==/!=.

C and Go output do not change. Dumping the datatest and stage corpora before and after, the only differences are two Rust lines in kuna-outlang-rust (v1 = 0; becomes v1 = false;). In Rust output of 40 decbench binaries, one per project, 3,544 of 3,549 integer literals given to a bool variable or return now print as false/true. An integer as bool is rarer: none in those 40 binaries, and two functions among sshd, bash, ls (-O2 and -O0) and a Cortex-M firmware, sub_b9df0(a0) as bool != (...) and sub_15168(p) as bool. They now print (sub_b9df0(a0) != 0) != (...) and sub_15168(p) != 0. Casts of values that are already Boolean do not change.

Tests: kuna-cli/tests/rust_boolean_conversions.rs gains a case that compiles the emitted narrow_bool with rustc next to a reference wide_value and checks it on 16 inputs. Before this change the emitted function does not compile.

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

@mahaloz
mahaloz merged commit d85062b into main Nov 25, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/rust-bool-cast branch November 25, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant