Skip to content

Commit 31bbfaf

Browse files
committed
fix(ClickSuggestions): instantiate all metavariables in the local context (#41001)
This PR fixes the panic in `#click_suggestions` that was reported at https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/.23click_suggestions.20doesn.27t.20find.20lemma/near/606231763 There are two separate problems that this PR fixes: 1. The implementation of `#click_suggestion` assumes all metavariables have already been instantiated. This is valid because they are all instantiated at the very beginning. However, this was not done properly, causing the types of free variables to still be able to contain metavariables. 2. The function `viewKAbstractSubExpr` was able to reach its `unreachable!` block. The reason is that `kabstractPositions` calls `instantiateMVars` on the expression `e` but not on the pattern `p`, causing a discrepancy. This is fixed by removing the `instantiateMVars` call. This is fine, because in both of the use cases (`rw??` and `#click_suggestions`), the expressions already have instantiated metavariables at this point.
1 parent 179c414 commit 31bbfaf

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

Mathlib/Lean/Meta/KAbstractPositions.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ namespace Lean.Meta
3333
/-- Return the positions that `kabstract` would abstract for pattern `p` in expression `e`.
3434
i.e. the positions that unify with `p`. -/
3535
def kabstractPositions (p e : Expr) : MetaM (Array SubExpr.Pos) := do
36-
let e ← instantiateMVars e
3736
let mctx ← getMCtx
3837
let pHeadIdx := p.toHeadIndex
3938
let pNumArgs := p.headNumArgs

Mathlib/Tactic/ClickSuggestions.lean

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ def viewKAbstractSubExpr' {m α}
7272
/-- Compute the suggestions. Use `token` for the output. -/
7373
public def generateSuggestions (loc : SubExpr.GoalsLocation) (parentDecl? : Option Name)
7474
(token : RefreshToken) : ClickSuggestionsM Unit := withReducible do
75+
-- Instantiate all metavariables, so that we won't need to worry about this later on.
76+
instantiateMVarDeclMVars loc.mvarId
77+
loc.mvarId.withContext do
7578
-- TODO: instead of just putting `✝` after inaccessible names,
7679
-- we should figure out how to use `rename_i` to actually refer to shadowed local variables.
7780
let lctx := (← getLCtx).sanitizeNames.run' { options := (← getOptions) }
7881
Meta.withLCtx' lctx do
79-
-- Instantiate all metavariables, so that we will not need to do this later on.
80-
instantiateMVarDeclMVars loc.mvarId
8182
trackingComputation "click_suggestions" do
8283
let (fvarId?, pos) ← match loc.loc with
8384
| .hypType fvarId pos => pure (some fvarId, pos)

MathlibTest/ClickSuggestions/Test.lean

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,10 @@ example (a b c : Nat) : a + b + c = a + b := by
184184
click_test "/1" => "nth_rw 2 [Nat.add_comm a b]"
185185
click_test "/0/1/0/1" => "nth_rw 1 [Nat.add_comm a b]"
186186
exact test_sorry
187+
188+
-- This example used to panic
189+
example : True := by
190+
by_cases h : False
191+
· click_test h "" => "rw [← true_eq_false_of_false h] at h"
192+
trivial
193+
· trivial

0 commit comments

Comments
 (0)