Miscompiled `ctorElim` for inductives with `@[computed_field]` enables arbitrary type coercion in safe Lean

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

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

bug
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:

  1. A zero-import, 100%-pure-safe arbitrary type coercion α → β (no unsafe, sorry, @[extern], @[implemented_by], or native_decide).
  2. Miscompilation of stock Lean.Expr.ctorElim itself (since Lean.Expr has a @[computed_field]).
Context

Discovered while auditing pure-safe Lean 4 compiler soundness on nightly-2026-12-02 (5e756ba2).

Steps to Reproduce
  1. Zero-import pure-safe arbitrary type coercion — paste directly into https://live.lean-lang.org/#project=lean-nightly (or run lean --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
  1. Stock Lean.Expr.ctorElim miscompilation — paste into https://live.lean-lang.org/#project=lean-nightly (or run lean --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

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.