Compiled @[computed_field] function silently takes wrong default branch after specialization (total code, default options)

Open
#16,925 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
compilers

Research direction

Start with the minimized Lean reproducer in the issue and confirm the mismatch between kernel evaluation and compiled execution. Then read src/Lean/Compiler/LCNF/Simp/InlineCandidate.lean, src/Lean/Compiler/LCNF/Specialize.lean, and src/Lean/Compiler/LCNF/Simp/JpCases.lean at the cited locations to trace specialization and constructor matching. Done means compiled evaluation agrees with the expected field value and the reproducer’s native_decide claim is rejected.

Written by the indexing model from the issue text.

Description

Prerequisites
  • Check that your issue is not already filed:
    https://github.com/leanprover/lean4/issues
  • Reduce the issue to a minimal, self-contained, reproducible test case.
    Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release, for example on
    https://live.lean-lang.org/#project=lean-nightly
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    No (this only affects compiled code and native_decide, not Lean's kernel).
Description

A total, terminating function using a @[computed_field] is silently miscompiled under default compiler options to take the wrong match branch and return 0 instead of the stored field value.

In the example below, Cell.wt returns 0 for .blank and .bar, and x for .letter x. Therefore total (.letter 5) 0 = 5 and isZero 5 = false (proved by decide with no axioms). Yet #eval, lean --run, and leanc -O0..-O3 all return total (.letter n) 0 = 0 and isZero n = true for every n (including run-time command-line arguments), and native_decide accepts isZero 5 = true.

Steps to Reproduce
class Weight (α : Type) where
  weight : α → Nat

instance natWeight : Weight Nat := ⟨fun n => n⟩

inductive Cell (α : Type) (w : Weight α) (z : Nat) where
  | blank
  | bar
  | letter (x : α)
with
  @[computed_field] wt : (α : Type) → (w : Weight α) → (z : Nat) → Cell α w z → Nat
    | _, _, z, .blank => z
    | _, _, z, .bar => z
    | _, w, _, .letter x => w.weight x

def weightOf (c : Cell Nat natWeight 0) : Nat :=
  Cell.wt Nat natWeight 0 c

@[noinline] def step (n : Nat) : Nat := n

def total (c : Cell Nat natWeight 0) (n : Nat) : Nat :=
  let d : Cell Nat natWeight 0 := match c with
    | .blank => .bar
    | .bar => .blank
    | .letter _ => c
  let p := step n
  Cell.wt Nat natWeight 0 d + p

def isZero (n : Nat) : Bool := total (.letter n) 0 == 0

theorem isZero_kernel : isZero 5 = false := by decide
theorem isZero_iff (n : Nat) : isZero n = true ↔ n = 0 := by
  simp [isZero, total, step, Cell.wt, Weight.weight]

#eval total (.letter 5) 0
#eval isZero 5
theorem bad_native : isZero 5 = true := by native_decide

def main (args : List String) : IO Unit := do
  IO.println s!"total (.letter 5) 0 = {total (.letter 5) 0}, isZero 5 = {isZero 5}"
  for a in args do
    let n := a.toNat!
    IO.println s!"n={n}: total={total (.letter n) 0} isZero={isZero n}"

Run lean -DwarningAsError=true -c JoinRepro.c JoinRepro.lean, lean --run JoinRepro.lean 5 42, or leanc -O3 -o join_bin JoinRepro.c && ./join_bin 5 42.

Expected behavior: total (.letter 5) 0 = 5, isZero 5 = false, n=5: total=5 isZero=false, n=42: total=42 isZero=false, and bad_native is rejected.

Actual behavior: #eval, lean --run, and leanc -O0..-O3 print total (.letter 5) 0 = 0, isZero 5 = true, n=5: total=0 isZero=true, n=42: total=0 isZero=true, and bad_native is accepted by native_decide.

Context / Cause
  1. In the LCNF base phase, user matches split on Cell while Cell.wt._override splits on Cell._impl.
  2. src/Lean/Compiler/LCNF/Simp/InlineCandidate.lean:90 (#9159 for #9156) blocks base-phase inlining only when the final name component is _override. src/Lean/Compiler/LCNF/Specialize.lean:315-354 creates ..._override._at_.weightOf.spec_0, whose final component is no longer _override; its size is 1 (LCNF/Basic.lean:859, within default compiler.small = 1), so it inlines into total in the base phase.
  3. src/Lean/Compiler/LCNF/Simp/JpCases.lean:87, 252-258, 286 then matches known constructor Cell.letter against Cell._impl alternatives by name only (missing Simp/Main.lean:191's cases.typeName == ctorVal.induct check from #12387) and routes it to the default arm (0).

Related: #9156, #9159, #6957, #12387, #14626.

(Disclosure: AI assistance was used during bug hunting and root-cause analysis; the reproducer was verified directly against stock Lean builds.)

Versions
  • Lean (version 4.38.0-pre, x86_64-unknown-linux-gnu, commit 3e00134f7966ba8eb89c8e50c88c3b2c4e5d44a7, Release)
  • Also reproduced on 4.38.0-nightly-2026-11-26, 4.37.0-rc1, 4.36.0, and 4.35.0.
  • OS: Linux x86-64 (glibc 2.39).
Dominant language
Lean
Stars
10.5k
Forks
1.2k
Avg merge
19h 36m
Merged PRs (30d)
323

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.