`joinTypes?` erases a join point parameter that carries data when another branch returns a type

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

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
compilers

Research direction

The bug is in joinTypes? in Lean/Compiler/LCNF/Types.lean, where the join point parameter becomes erased when one branch's type is erased. Start by reproducing with the minimal h/get1 example in a file run through lean, then read the comment on compatibleTypes that the issue references. Done means the example prints 8, the decide and #eval results agree, and the existing test suite shows no new failures.

Written by the indexing model from the issue text.

Description

code-generator
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.
  • Test your test case against the latest nightly release.
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    No, the kernel gets it right. But native_decide can prove get1 3 = 0 next to decide proving get1 3 = 8.
Description

If a match that isn't in tail position returns a type in one branch and a value in the other, the compiled code loses the value:

def T (b : Bool) : Type 1 := cond b Type (ULift.{1} Nat)

def h (b : Bool) (n : Nat) : T b × List Nat := (match b with
  | true => (Nat : Type)
  | false => (⟨n + 5⟩ : ULift.{1} Nat), [n, n+1, n*2, n*n, n+5, n+6, n+7, n+8, n*3, n*4, n*5, n*6, n*7, n*8, n*9, n+10, n+11, n+12, n+13])

def get1 (n : Nat) : Nat := (h false n).1.down

example : get1 3 = 8 := by decide
#eval get1 3

#eval get1 3 prints 0, while decide proves it's 8. h needs the long list, otherwise it gets inlined and the bug goes away.

joinTypes? in Lean/Compiler/LCNF/Types.lean returns ◾ as soon as one side is erased. Here one branch has type Type and the other ULift Nat, so the join point parameter ends up erased and the value from the false branch is dropped.

Steps to Reproduce

lean jointypes.lean

Expected behavior: prints 8

Actual behavior: prints 0

Possible fix

Return ◾ only if both sides are erased:

-  if a.isErased || b.isErased then
-    return erasedExpr -- See comment at `compatibleTypes`.
+  if a.isErased && b.isErased then
+    return erasedExpr
+  else if a.isErased || b.isErased then
+    return mkConst ``lcAny

With this the example prints 8, and make test has no new failures.

Versions

Lean (version 4.37.0-nightly-2026-11-11, x86_64-unknown-linux-gnu, commit 80799ea6aeb2f73f1cf60cabe9db80e409cb0c3a, Release). Also nightly-2026-10-30. Linux.

Additional Information

Assisted-by: Elenchus(claude-code):opus-5.5

Dominant language
Lean
Stars
10.3k
Forks
1.1k
Avg merge
19h 8m
Merged PRs (30d)
421

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.