Miscompiled `ctorElim` for inductives with `@[computed_field]` enables arbitrary type coercion in safe Lean
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 20/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- compilers
Research direction
Start with the ordering of mkAuxConstructions, applyComputedFields, and elabInductiveViewsFinalize in src/Lean/Elab/MutualInductive.lean, then compare ctorElim handling with constructor and casesOn overrides in src/Lean/Elab/ComputedFields.lean. Run both reproductions with lean --trust=0; done means evaluation agrees with the kernel proofs and neither example permits the reported coercion.
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
(You can also use the settings there to switch to “Lean nightly”) - When reporting a perceived soundness bug: Does it affect
lake check --paranoid?
If in doubt, please reach out on [email protected] for responsible disclosure.
Description
In src/Lean/Elab/MutualInductive.lean, elabInductiveViews runs mkAuxConstructions (lines 1554–1560, which constructs and compiles <Ind>.ctorElim) before elabInductiveViewsFinalize runs applyComputedFields views (line 1669, which carries the comment -- NOTE: any generated code before this line is invalid).
ComputedFields.setComputedFields (src/Lean/Elab/ComputedFields.lean) installs @[implemented_by] overrides for constructors, casesOn, and computed fields to account for the prepended computed-field slots in the runtime layout, but never overrides or recompiles <Ind>.ctorElim. As a result, compiled <Ind>.ctorElim projects oproj[0] (the prepended computed field) as user field 0, yielding:
- A zero-import, 100%-pure-safe arbitrary type coercion
α → β(nounsafe,sorry,@[extern],@[implemented_by], ornative_decide). - Miscompilation of stock
Lean.Expr.ctorElimitself (sinceLean.Exprhas a@[computed_field]).
Context
Discovered while auditing pure-safe Lean 4 compiler soundness on nightly-2026-12-02 (5e756ba2).
Steps to Reproduce
- Zero-import pure-safe arbitrary type coercion — paste directly into
https://live.lean-lang.org/#project=lean-nightly(or runlean --trust=0 Repro.lean):
inductive CastBox (α β : Type) where
| empty
| mk (x : Option β) (a : α)
with
@[computed_field] cf : (α β : Type) → CastBox α β → Option α
| _, _, .empty => none
| _, _, .mk _ a => some a
@[noinline]
def callElim {α β : Type} (i : Nat) (b : CastBox α β) (h : i = b.ctorIdx)
(k : @CastBox.ctorElimType α β (fun _ => Option β) i) : Option β :=
CastBox.ctorElim (motive := fun _ => Option β) i b h k
def coerce! {α β : Type} [Inhabited β] (a : α) : β :=
(callElim 1 (CastBox.mk (α := α) (β := β) none a) rfl (PULift.up (fun x _ => x))).getD default
def forgedBool : Bool := coerce! (β := Bool) (1 : Nat)
theorem kernel_eq : forgedBool = false := rfl
#eval forgedBool
- Stock
Lean.Expr.ctorElimmiscompilation — paste intohttps://live.lean-lang.org/#project=lean-nightly(or runlean --trust=0 ReproExpr.lean):
import Lean.Expr
open Lean
@[noinline]
def callExprElim (i : Nat) (e : Expr) (h : i = e.ctorIdx)
(k : @Expr.ctorElimType (fun _ => Bool) i) : Bool :=
Expr.ctorElim (motive := fun _ => Bool) i e h k
def runtimeVal : Bool :=
callExprElim 8 (.letE `x (.sort .zero) (.sort .zero) (.bvar 49) false) rfl
(PULift.up (fun _ _ _ _ nondep => nondep))
theorem kernel_eq : runtimeVal = false := rfl
#eval runtimeVal
Expected behavior: #eval forgedBool prints false (matching kernel_eq : forgedBool = false := rfl), and #eval runtimeVal prints false (matching kernel_eq : runtimeVal = false := rfl).
Actual behavior: #eval forgedBool prints true (coercing 1 : Nat into Bool), and #eval runtimeVal prints true.
Versions
"4.38.0-nightly-2026-12-02"
Lean (version 4.38.0-nightly-2026-12-02, x86_64-unknown-linux-gnu, commit 5e756ba2526bd88b18fa8b01588964fe848e0f91, Release)
Ubuntu 24.04.4 LTS (Linux 6.17.0-1022-azure x86_64)
Suggested fix
Either defer mkCtorElim (and any other compiled auxiliary constructions whose projections depend on constructor field offsets) until after applyComputedFields in elabInductiveViewsFinalize, or override/recompile <Ind>.ctorElim inside ComputedFields.setComputedFields (as already done for casesOn and constructors).
- 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