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

ppSpace in elab/macro is ignored before constructor dot notation #4561

Closed
3 tasks done
eric-wieser opened this issue Jun 25, 2024 · 1 comment · Fixed by #4631
Closed
3 tasks done

ppSpace in elab/macro is ignored before constructor dot notation #4561

eric-wieser opened this issue Jun 25, 2024 · 1 comment · Fixed by #4631
Labels
bug Something isn't working

Comments

@eric-wieser
Copy link
Contributor

eric-wieser commented Jun 25, 2024

Prerequisites

Please put an X between the brackets as you perform the following steps:

Description

Using ppSpace in a parser does not always result in a space. As a result, syntax does not roundtrip.

Context

Zulip

Steps to Reproduce

  1. Run the following:
import Lean.Elab.Command

macro (name := useSyntax) "use" ppSpace arg:term : tactic => `(tactic| sorry)

run_cmd do
  Lean.logInfo <| ← `(tactic| use .succ 0)

Expected behavior: Outputs use .succ✝ 0

Actual behavior: Outputs use.succ✝ 0, which is invalid syntax due to the missing space

Versions

4.10.0-nightly-2024-06-25

Additional Information

[Additional information, configuration or data that might be necessary to reproduce the issue]

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

@eric-wieser eric-wieser added the bug Something isn't working label Jun 25, 2024
@eric-wieser eric-wieser changed the title ppSpace is ignored before constructor dot notation ppSpace in elab is ignored before constructor dot notation Jun 25, 2024
@eric-wieser eric-wieser changed the title ppSpace in elab is ignored before constructor dot notation ppSpace in elab/macro is ignored before constructor dot notation Jun 25, 2024
@kmill
Copy link
Collaborator

kmill commented Jun 25, 2024

It looks like the issue is that when ppSpace is used as a macroArg without a variable name, it gets wrapped in a group, which inhibits inserting the space.

Is this a bug in whitespace insertion? Or should Lean.Elab.Command.expandMacroArg special case ppSpace?

import Lean

open Lean Lean.Parser.Command Lean.Elab.Command

elab "test_macroArg " args:macroArg* : command => do
    let (stxParts, patArgs) := (← args.mapM expandMacroArg).unzip
    logInfo m!"{stxParts}"

test_macroArg ppSpace term,+
-- [group✝(ppSpace), sepBy1(term, ",", ", ")]
test_macroArg x:ppSpace term,+
-- [ppSpace, sepBy1(term, ",", ", ")]

elab "use1" ppSpace term,+ : tactic => failure
elab "use2" x:ppSpace term,+ : tactic => failure

run_cmd Lean.Elab.Command.liftTermElabM do
  Lean.logInfo <| ← `(tactic| use1 .succ 0) -- use1.succ✝ 0
  Lean.logInfo <| ← `(tactic| use2 .succ 0) -- use2 .succ✝ 0

kmill added a commit to kmill/lean4 that referenced this issue Jul 2, 2024
This fixes an issue where if `ppSpace` appears in a `macro`/`elab` then it does not format with a space due to the fact that it gets wrapped as `group(ppSpace)` in the macro argument processing.

Closes leanprover#4561
kmill added a commit to kmill/lean4 that referenced this issue Jul 2, 2024
This fixes an issue where if `ppSpace` appears in a `macro`/`elab` then it does not format with a space due to the fact that it gets wrapped as `group(ppSpace)` in the macro argument processing.

Closes leanprover#4561
github-merge-queue bot pushed a commit that referenced this issue Jul 3, 2024
Now syntax nodes have their formatters run even if the parsers they wrap
are all arity zero. This fixes an issue where if `ppSpace` appears in a
`macro`/`elab` then it does not format with a space due to the fact that
macro argument processing wraps this as `group(ppSpace)`, and `ppSpace`
has arity zero.

Implementation note: the fix is to make the `visitArgs` formatter
combinator always visit the last child, even if it does not exist (in
which case the visited node will be `Syntax.missing`). To compensate,
parser combinators like many and optional need to be sure to keep track
of whether there any children. Only optional's needed to be modified.

Closes #4561
@Kha Kha closed this as completed in #4631 Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants