fix: improve error for join point assignment failure in do elaborator (#13397)

This PR improves error reporting when the `do` elaborator produces an
ill-formed expression that fails `checkedAssign` in
`withDuplicableCont`. Previously the failure was silently discarded,
making it hard to diagnose bugs in the `do` elaborator. Now a
descriptive error is thrown showing the join point RHS and the
metavariable it failed to assign to.

Closes #12826
This commit is contained in:
Sebastian Graf 2026-04-13 21:32:43 +02:00 committed by GitHub
parent cbda692e7e
commit a0f2a8bf60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 1 deletions

View file

@ -545,7 +545,10 @@ def DoElemCont.withDuplicableCont (nondupDec : DoElemCont) (callerInfo : Control
withDeadCode (if callerInfo.numRegularExits > 0 then .alive else .deadSemantically) do
let e ← nondupDec.k
mkLambdaFVars (#[r] ++ muts) e
discard <| joinRhsMVar.mvarId!.checkedAssign joinRhs
unless ← joinRhsMVar.mvarId!.checkedAssign joinRhs do
joinRhsMVar.mvarId!.withContext do
throwError "Bug in a `do` elaborator. Failed to assign join point RHS{indentExpr joinRhs}\n\
to metavariable\n{joinRhsMVar.mvarId!}"
let body ← body?.getDM do
-- Here we unconditionally add a pending MVar.

View file

@ -0,0 +1,27 @@
import Lean
syntax (name := testElem) "test" : doElem
open Lean Elab Do
@[doElem_elab testElem]
def elabTestElem : DoElab := fun _stx _dec => do
return .fvar ⟨`bad_fvar⟩
@[doElem_control_info testElem]
def controlInfoTestElem : ControlInfoHandler := fun _stx => do
return .pure
set_option backward.do.legacy false
/--
error: Bug in a `do` elaborator. Failed to assign join point RHS
fun __r => bad_fvar
to metavariable
⊢ Unit → IO Unit
-/
#guard_msgs in
def testFn : IO Unit := do
if true then pure () else pure () -- uses `withDuplicableCont`
test -- produces a term that `checkedAssign` will reject
set_option pp.mvars false