Compiled @[computed_field] function silently takes wrong default branch after specialization (total code, default options)
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 andnative_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
- In the LCNF base phase, user matches split on
CellwhileCell.wt._overridesplits onCell._impl. 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-354creates..._override._at_.weightOf.spec_0, whose final component is no longer_override; its size is 1 (LCNF/Basic.lean:859, within defaultcompiler.small = 1), so it inlines intototalin the base phase.src/Lean/Compiler/LCNF/Simp/JpCases.lean:87, 252-258, 286then matches known constructorCell.letteragainstCell._implalternatives by name only (missingSimp/Main.lean:191'scases.typeName == ctorVal.inductcheck 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, and4.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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from leanprover/lean4
-
P-medium
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
leanprover/lean4#16958 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
leanprover/lean4#16897 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16892 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16886 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 78/100
leanprover/lean4#16820 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
area-dart-model gardening
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 87/100
Maintainers usually reply within 1 day
-
`shift(x, y)` with a uint256 shift amount >= 2**255 shifts in the wrong directionPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 71/100
facebookexperimental/semcode#195 ·
Maintainers usually reply within 2 days
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
stjude-rust-labs/sprocket#1476 · 3 comments ·
Maintainers usually reply within 1 day