fix: have runTermElabM reset local context when types of autobound implicits contain metavariables (#7952)

This PR makes two improvements to the local context when there are
autobound implicits in `variable`s. First, the local context no longer
has two copies of every variable (the local context is rebuilt if the
types of autobound implicits have metavariables). Second, these
metavariables get names using the same algorithm used by binders that
appear in declarations (with `mkForallFVars'` instead of
`mkForallFVars`).

This removes the last use of `Term.addAutoBoundImplicits'`, which
inherently has this variable duplication issue.
This commit is contained in:
Kyle Miller 2025-04-23 20:29:10 -07:00 committed by GitHub
parent 3d31b1f608
commit 42ab5dfab0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 92 additions and 2 deletions

View file

@ -9,6 +9,7 @@ import Lean.Elab.Binders
import Lean.Elab.SyntheticMVars
import Lean.Elab.SetOption
import Lean.Language.Basic
import Lean.Meta.ForEachExpr
namespace Lean.Elab.Command
@ -719,9 +720,16 @@ def runTermElabM (elabFn : Array Expr → TermElabM α) : CommandElabM α := do
-- We don't want to store messages produced when elaborating `(getVarDecls s)` because they have already been saved when we elaborated the `variable`(s) command.
-- So, we use `Core.resetMessageLog`.
Core.resetMessageLog
let someType := mkSort levelZero
Term.addAutoBoundImplicits' xs someType fun xs _ =>
let xs ← Term.addAutoBoundImplicits xs none
if xs.all (·.isFVar) then
Term.withoutAutoBoundImplicit <| elabFn xs
else
-- Abstract any mvars that appear in `xs` using `mkForallFVars` (the type `mkSort levelZero` is an arbitrary placeholder)
-- and then rebuild the local context from scratch.
-- Resetting prevents the local context from including the original fvars from `xs`.
let ctxType ← Meta.mkForallFVars' xs (mkSort levelZero)
Meta.withLCtx {} {} <| Meta.forallBoundedTelescope ctxType xs.size fun xs _ =>
Term.withoutAutoBoundImplicit <| elabFn xs
private def liftAttrM {α} (x : AttrM α) : CommandElabM α := do
liftCoreM x

View file

@ -1,10 +1,92 @@
/-!
# Regression tests for auto-bound implicits
-/
set_option pp.mvars false
/-!
Auto-bound implicit appears in dot notation in the type, for a variable that appears later.
-/
example : n.succ = 1 → n = 0 := by
intros h; injection h
/-!
Auto-bound implicit appears in dot notation in a binder, for a variable that appears later.
-/
example (h : n.succ = 1) : n = 0 := by
injection h
/-!
Auto-bound implicit appears as argument to notation that is postponed.
The type of `σ` is specialized to `T` later.
-/
opaque T : Type
opaque T.Pred : T → T → Prop
example {ρ} (hρ : ρ.Pred σ) : T.Pred ρ ρ := sorry
namespace TestRunTermElab
/-!
In the following tests, there is an auto-bound implicit whose type is a metavariable,
which gets turned into an additional variable.
-/
def constUnit (a : A) : Type := Unit
/-!
The auto-bound implicit creates a new variable `A✝`, which comes from the argument name in `constUnit`.
(This has been the case well before the creation of this test.)
-/
/--
info: A✝ : Sort u_1
a : A✝
_x : constUnit a
⊢ True
-/
#guard_msgs in
example (_x : constUnit a) : True := by
trace_state
trivial
variable (x : constUnit a)
/-!
The local context here used to be
```
a✝ : ?_
x✝¹ : constUnit a✝
x✝ : Sort ?_
a : x✝
x : constUnit a
⊢ True
```
Note the duplication and `x✝` for the name of the metavariable-as-a-variable.
The duplication was because `runTermElabM` wasn't resetting the local context.
The poor variable name was due to using `mkForallFVars` instead of `mkForallFVars'`.
-/
/--
info: A✝ : Sort _
a : A✝
x : constUnit a
⊢ True
-/
#guard_msgs in
example : True := by
trace_state
trivial
/-!
Checking that `#check` also has the improvement.
-/
/--
info: 1 : Nat
---
info: A✝ : Sort _
a : A✝
x : constUnit a
⊢ ?_
-/
#guard_msgs in #check (by trace_state; exact 1)
end TestRunTermElab