Compare commits

...

1 Commits

Author SHA1 Message Date
Kim Morrison
2b2e0bf8bb fix: include local variable dot notation params in grind? suggestions
This PR fixes a bug where `grind?` suggestions would not include parameters
using local variable dot notation (e.g., `cs.getD_rightInvSeq` where `cs` is
a local variable). These parameters were incorrectly filtered out because
the code assumed all ident params resolve to global declarations. In fact,
local variable dot notation produces anchors that need the original term to
be loaded during replay, so they must be preserved in the suggestion.

Closes #12185

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 11:05:01 +11:00
2 changed files with 52 additions and 7 deletions

View File

@@ -348,15 +348,28 @@ def evalGrindTraceCore (stx : Syntax) (trace := true) (verbose := true) (useSorr
let paramStxs := if let some params := params? then params.getElems else #[]
-- Extract term parameters (non-ident params) to include in the suggestion.
-- These are not tracked via E-matching, so we conservatively include them all.
-- Ident params resolve to global declarations and are tracked via E-matching.
-- Plain ident params that resolve to global declarations are tracked via E-matching.
-- But idents with local variable dot notation (e.g., `cs.getD_rightInvSeq` where `cs`
-- is a local variable) must be preserved because they produce anchors that need
-- the original term to be loaded during replay.
-- Non-ident terms (like `show P by tac`) need to be preserved explicitly.
let termParamStxs : Array Grind.TParam := paramStxs.filter fun p =>
let termParamStxs : Array Grind.TParam paramStxs.filterM fun p => do
match p with
| `(Parser.Tactic.grindParam| $[$_:grindMod]? $_:ident) => false
| `(Parser.Tactic.grindParam| ! $[$_:grindMod]? $_:ident) => false
| `(Parser.Tactic.grindParam| - $_:ident) => false
| `(Parser.Tactic.grindParam| #$_:hexnum) => false
| _ => true
| `(Parser.Tactic.grindParam| $[$_:grindMod]? $id:ident) =>
-- Check if this ident resolves to local variable dot notation
-- If so, keep it because it's not a simple global declaration
if let some (_, _ :: _) := ( resolveLocalName id.getId) then
return true
else
return false
| `(Parser.Tactic.grindParam| ! $[$_:grindMod]? $id:ident) =>
if let some (_, _ :: _) := ( resolveLocalName id.getId) then
return true
else
return false
| `(Parser.Tactic.grindParam| - $_:ident) => return false
| `(Parser.Tactic.grindParam| #$_:hexnum) => return false
| _ => return true
let mvarId getMainGoal
let params mkGrindParams config only paramStxs mvarId
Grind.withProtectedMCtx config mvarId fun mvarId' => do

View File

@@ -0,0 +1,32 @@
/-!
# Test that `grind?` includes local variable dot notation params in its suggestion
When a user provides a parameter like `s.myThm` where `s` is a local variable
and `myThm` is a field access, it should be included in the suggestion.
These params are processed as term params (not global decls) and produce anchors.
Without including the original term in the suggestion, the anchor can't match on replay.
Regression test for: grind? suggestions not working with local variable dot notation
-/
set_option linter.unusedVariables false
structure MyStruct where
val : Nat
-- A theorem with an indexable pattern
theorem MyStruct.add_comm (s : MyStruct) (a b : Nat) : a + b = b + a := Nat.add_comm a b
-- Test: Local variable dot notation should be included in the suggestion
-- `s.add_comm` is a local variable dot notation (not a global decl),
-- so it should appear in the grind only suggestion
/--
info: Try this:
[apply] grind only [= s.add_comm]
-/
#guard_msgs in
example (s : MyStruct) : (1 : Nat) + 2 = 2 + 1 := by
grind? [= s.add_comm]
-- Verify the suggestion works by using it
example (s : MyStruct) : (1 : Nat) + 2 = 2 + 1 := by
grind only [= s.add_comm]