Skip to content

Commit d69a8ef

Browse files
authored
fix: deduplicate elaboration of constant argument to rw (#8232)
This PR fixes elaboration of constants in the `rewrite` tactic. previously, `rw [eq_self]` would elaborate `eq_self` twice, and add it to the infotree twice. This would lead to the "Expected type" being delaborated with an unknown universe metavariable. I added a test to show this error during delaboration of the "Expected type". This was reported on Zulip as a panic message during delaboration: [#mathlib4 > Crash in `sup`/`inf` / `max`/`min` delaborators](https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Crash.20in.20.60sup.60.2F.60inf.60.20.2F.20.60max.60.2F.60min.60.20delaborators/with/515946714)
1 parent 8154aaa commit d69a8ef

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/Lean/Elab/Tactic/Rewrite.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def withRWRulesSeq (token : Syntax) (rwRulesSeqStx : Syntax) (x : (symm : Bool)
5151
let term := rule[1]
5252
let processId (id : Syntax) : TacticM Unit := do
5353
-- See if we can interpret `id` as a hypothesis first.
54-
if (← optional <| getFVarId id).isSome then
54+
if (← withMainContext <| Term.isLocalIdent? id).isSome then
5555
x symm term
5656
else
5757
-- Try to get equation theorems for `id`.
@@ -62,8 +62,8 @@ def withRWRulesSeq (token : Syntax) (rwRulesSeqStx : Syntax) (x : (symm : Bool)
6262
let rec go : List Name → TacticM Unit
6363
| [] => throwError "failed to rewrite using equation theorems for '{declName}'.{hint}"
6464
| eqThm::eqThms => (x symm (mkCIdentFrom id eqThm)) <|> go eqThms
65-
go eqThms.toList
6665
discard <| Term.addTermInfo id (← mkConstWithFreshMVarLevels declName) (lctx? := ← getLCtx)
66+
go eqThms.toList
6767
match term with
6868
| `($id:ident) => processId id
6969
| `(@$id:ident) => processId id
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Lean
2+
3+
/-!
4+
Previously, `rw [my_lemma]` would elaborate `my_lemma` twice, both times generating new universe metavariables.
5+
This caused the "Expected type" to contain a universe metavariable that wasn't in the metavariable context.
6+
7+
This test verifies that the generated universe level is in the metavariable context.
8+
-/
9+
10+
open Lean PrettyPrinter Delaborator SubExpr
11+
/-- No-op delaborator that checks that the universe level is in the metavariable context -/
12+
@[delab app.Eq]
13+
def checkUniv : Delab := do
14+
let .const _ [.mvar u] := (← getExpr).getAppFn | failure
15+
_ ← u.getLevel -- if `u` isn't in the metavariable context, this throws an error during elaboration
16+
failure
17+
18+
example : True := by
19+
try rw [eq_self]
20+
--^ $/lean/plainTermGoal
21+
trivial
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{"textDocument": {"uri": "file:///rwElabConst.lean"},
2+
"position": {"line": 18, "character": 12}}
3+
{"range":
4+
{"start": {"line": 18, "character": 10}, "end": {"line": 18, "character": 17}},
5+
"goal": "⊢ ∀ {α : Sort ?u} (a : α), (a = a) = True"}

0 commit comments

Comments
 (0)