lean4 icon indicating copy to clipboard operation
lean4 copied to clipboard

Compiler PANIC in `ExplicitBoxing` when `deriving Repr`/`BEq` over an `unsafe instance`

Open keithadler opened this issue 2 months ago • 1 comments

Prerequisites

  • [x] Check that your issue is not already filed
  • [x] Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
  • [x] Test your test case against the latest nightly release

Description

Deriving Repr or BEq for a structure whose field type only has an unsafe instance panics the compiler. The kernel correctly rejects the derived instance, but compilation proceeds anyway and hits unreachable! in the ExplicitBoxing LCNF pass, emitting five chained PANIC messages.

Writing the same instance by hand produces a clean kernel error and no panic, so the panic is specific to the deriving path.

Context

Noticed while checking whether #3498 (deriving does not handle unsafe or noncomputable) still reproduces. The underlying "cannot derive over unsafe" behaviour is #3498; this issue is only about the panic.

This looks like the unsafe counterpart of a panic already fixed for noncomputable: same pass and same unreachable! as #13371 and #13921, whose deriving-related flavour was fixed in #14125. That fix was in Deriving/Inhabited.lean's aux-function path; this case goes through processDefDeriving/mkDefinitionValInferringUnsafe in Deriving/Basic.lean, so it is not covered by it.

Steps to Reproduce

structure Foo

unsafe instance : Repr Foo where
  reprPrec f n := "foo"

structure Bar where
  foo : Foo
deriving Repr

Expected behavior: an error explaining that the instance cannot be derived because it depends on an unsafe declaration — as happens when the instance is written by hand.

Actual behavior:

error: (kernel) invalid declaration, it uses unsafe declaration 'instReprFoo'
error: failed to compile definition, compiler IR check failed at `instReprBar._closed_0`.
       Error: unknown join point 'block_0'
PANIC at ...ExplicitBoxing.tryCorrectLetDeclType Lean.Compiler.LCNF.ExplicitBoxing:301:45: unreachable code has been reached
PANIC at ...ExplicitBoxing.visitLet Lean.Compiler.LCNF.ExplicitBoxing:336:45: unreachable code has been reached
PANIC at Std.DTreeMap.Internal.Impl.Const.get! Std.Data.DTreeMap.Internal.Queries:227:13: Key is not present in map
PANIC at Option.get! Init.Data.Option.BasicAux:22:14: value is none
PANIC at Std.DHashMap.Internal.AssocList.get! Std.Data.DHashMap.Internal.AssocList.Basic:163:11: key is not present in hash table

Versions

Reproduces on both:

  • Lean 4.35.0-nightly-2026-09-04, arm64-apple-darwin24.6.0 macOS
  • Lean 4.33.1 (stable), arm64-apple-darwin24.6.0 macOS

macOS 26.5.2, arm64.

Additional Information

deriving BEq panics identically (instBEqBar._closed_0).

The panics cascade across three passes: ExplicitBoxing.tryCorrectLetDeclType → visitLet, then LCNF.Decl.explicitRc, then Lean.IR.ToIR.getJoinPointValue inside lowerCode — the last of which is where the unknown join point 'block_0' IR check error originates.

Deriving over an unsafe inductive does not panic — it gives a clean (kernel) invalid declaration, it uses unsafe declaration 'Foo':

unsafe inductive Foo where
  | default : Foo
  | io : IO Foo → Foo
  deriving Inhabited

The same instance written by hand errors cleanly, with no panic:

structure Foo
unsafe instance : Repr Foo where
  reprPrec f n := "foo"
structure Bar where
  foo : Foo
instance : Repr Bar where
  reprPrec b n := reprPrec b.foo n

Investigation of this issue was assisted by Claude; the repros were run and checked manually.

Impact

Add :+1: to issues you consider important. If others are impacted by this issue, please ask them to add :+1: to it.

keithadler avatar Sep 04 '26 19:09 keithadler

Follow-up: this is not specific to deriving, and not specific to instances. The trigger is any definition that refers to a kernel-rejected 0-ary constant. Three lines, no #eval:

unsafe def bad : Nat := 0
def risky : Nat := bad        -- (kernel) invalid declaration, as expected
def risky2 : Nat := risky + 1 -- compiler IR check failed … then 4 chained PANICs

Same five panics as above (ExplicitBoxing.tryCorrectLetDeclType → visitLet → DTreeMap … get! → Option.get! → AssocList.get!), on both 4.35.0-nightly-2026-09-04 and stable 4.33.1.

Two observations that may narrow it down:

  • The arity of the rejected constant matters. If risky is a function (def risky (n : Nat) : Nat := bad n, used as risky n), there is no panic — you get a clean Failed to find LCNF signature for risky. It is only the 0-ary case (risky used as a bare value) that gets past the signature check and reaches ExplicitBoxing. set_option compiler.extract_closed false makes no difference, so it isn't closed-term extraction as such.
  • v4.20.0 handles the same three lines cleanly — failed to compile definition, consider marking it as 'noncomputable' because it depends on 'risky' — so this looks like a regression in the new LCNF pipeline rather than a long-standing issue.

deriving Repr over an unsafe instance is just one way to produce such a definition. I've also reproduced the identical panic via deriving Hashable, deriving Ord, deriving Repr on an inductive, and inside a noncomputable section. The noncomputable counterpart (noncomputable def nc … ; def r := nc ; def r2 := r + 1) is handled cleanly at every step, so it's specifically the unsafe-rejection path.

Happy to retitle the issue if that helps.

Triage assisted by Claude; repros run and checked manually.

keithadler avatar Sep 04 '26 21:09 keithadler