Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid delaborating with field notation if object is a metavariable #6014

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Lean/PrettyPrinter/Delaborator/Builtins.lean
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,16 @@ def appFieldNotationCandidate? : DelabM (Option (Nat × Name)) := do
| return none
unless idx < e.getAppNumArgs do return none
/-
There are some kinds of expressions that cause issues with field notation,
so we prevent using it in these cases.
For example, `2.succ` is not parseable.
There are some kinds of expressions that cause issues with field notation, so we prevent using it in these cases.
-/
let obj := e.getArg! idx
-- `(2).fn` is unlikely to elaborate.
if obj.isRawNatLit || obj.isAppOfArity ``OfNat.ofNat 3 || obj.isAppOfArity ``OfScientific.ofScientific 5 then
return none
-- `(?m).fn` is unlikely to elaborate. https://github.com/leanprover/lean4/issues/5993
-- We also exclude metavariable applications (these are delayed assignments for example)
if obj.getAppFn.isMVar then
return none
return (idx, field)

/--
Expand Down
2 changes: 1 addition & 1 deletion tests/lean/474.lean.expected.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ y.add y
fun y_1 => y.add y_1
fun y => Nat.add FREE y
fun (y : Nat) => Nat.add y y
?m.add y
Nat.add ?m y
Nat.add (?m #0) #0
fun y => (y.add y).add y
2 changes: 1 addition & 1 deletion tests/lean/rewrite.lean.expected.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
as bs : List α
⊢ as ++ bs ++ bs = as ++ (bs ++ bs)
rewrite.lean:18:20-18:29: error: tactic 'rewrite' failed, did not find instance of the pattern in the target expression
?as.reverse.reverse
(List.reverse ?as).reverse
α : Type u_1
as bs : List α
⊢ as ++ [] ++ [] ++ bs ++ bs = as ++ (bs ++ bs)
Expand Down
32 changes: 32 additions & 0 deletions tests/lean/run/5993.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/-!
# Avoid delaborating with field notation if object is a metavariable application.

https://github.com/leanprover/lean4/issues/5993
-/

set_option pp.mvars false

/-!
No field notation notation here. Used to print `refine ?_.succ` and `refine ?_.snd`.
-/

/--
info: Try this: refine Nat.succ ?_
---
info: Try this: refine Prod.snd ?_
-/
#guard_msgs in
example : Nat := by
show_term refine Nat.succ ?_
show_term refine Prod.snd (α := Int) ?_
exact default

/-!
No field notation even under binders. (That is, be aware of delayed assignment metavariables.)
-/

/-- info: Try this: refine fun x => Nat.succ ?_ -/
#guard_msgs in
example : Nat → Nat := by
show_term refine fun _ => Nat.succ ?_
exact default
4 changes: 2 additions & 2 deletions tests/lean/run/meta2.lean
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,8 @@ withLocalDeclD `x nat $ fun x => do

/--
info: [Meta.debug] ----- tst30 -----
[Meta.debug] (?_ x).succ
[Meta.debug] ?_.succ
[Meta.debug] Nat.succ (?_ x)
[Meta.debug] Nat.succ ?_
[Meta.debug] fun x => ?_
-/
#guard_msgs in
Expand Down
10 changes: 5 additions & 5 deletions tests/lean/run/scopedunifhint.lean
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ argument
has type
Nat : Type
but is expected to have type
?_.α : Type _
Magma.α ?_ : Type _
-/
#guard_msgs in
#check mul x x -- Error: unification hint is not active
Expand All @@ -48,7 +48,7 @@ argument
has type
Nat × Nat : Type
but is expected to have type
?_.α : Type _
Magma.α ?_ : Type _
-/
#guard_msgs in
#check mul (x, x) (x, x) -- Error: no unification hint
Expand All @@ -63,7 +63,7 @@ argument
has type
Nat : Type
but is expected to have type
?_.α : Type _
Magma.α ?_ : Type _
-/
#guard_msgs in
#check x*x -- Error: unification hint is not active
Expand All @@ -81,7 +81,7 @@ argument
has type
Nat × Nat : Type
but is expected to have type
?_.α : Type _
Magma.α ?_ : Type _
-/
#guard_msgs in
#check mul (x, x) (x, x) -- still error
Expand Down Expand Up @@ -109,7 +109,7 @@ argument
has type
Nat × Nat : Type
but is expected to have type
?_.α : Type _
Magma.α ?_ : Type _
-/
#guard_msgs in
#check (x, x) * (x, x) -- error, local hint is not active after end of section anymore
Loading