This PR makes cdot function expansion take hygiene information into account, fixing "parenthesis capturing" errors that can make erroneous cdots trigger cdot expansion in conjunction with macros. For example, given ```lean macro "baz% " t:term : term => `(1 + ($t)) ``` it used to be that `baz% ·` would expand to `1 + fun x => x`, but now the parentheses in `($t)` do not capture the cdot. We also fix an oversight where cdot function expansion ignored the fact that type ascriptions and tuples were supposed to delimit expansion, and also now the quotation prechecker ignores the identifier in `hygieneInfo`. (#9491 added the hygiene information to the parenthesis and cdot syntaxes.) This fixes a bug discovered by [Google DeepMind](https://storage.googleapis.com/deepmind-media/DeepMind.com/Blog/imo-2024-solutions/P1/index.html), which made use of `useλy . x=>y.rec λS p=>?_`. The `use` tactic from Mathlib wrapped the provided term in a type ascription, and so this was equivalent to `use fun x => λy x x=>y.rec λS p=>?_`. (Note that cdot function expansion is not able to take into account *where* the cdots are located, and it is syntactically valid to insert an identifier into the binder list like this. If we ever want to address this in the future, we could have cdots expand into a special term that wraps an identifier that evaluates to a local, but which would cause errors in other contexts.) Design note: we put the `hygieneInfo` on the open parenthesis rather than at the end, since that way the hygiene information is available even when there are parsing errors. This is important since we rely on being able to elaborate partial syntax to get elab info (e.g. in `(a.` to get completion info). Note that syntax matchers check that the `hygieneInfo` is actually present, so such partial syntax would not be matched.
67 lines
2.6 KiB
Text
67 lines
2.6 KiB
Text
import Lean.Parser
|
||
/-! Reprint file after removing all parentheses and then passing it through the parenthesizer -/
|
||
|
||
open Lean
|
||
open Std.Format open Std
|
||
|
||
def unparenAux (parens body : Syntax) : Syntax :=
|
||
match parens.getHeadInfo, body.getHeadInfo, body.getTailInfo, parens.getTailInfo with
|
||
| SourceInfo.original lead _ _ _, SourceInfo.original _ pos trail pos',
|
||
SourceInfo.original endLead endPos _ endPos', SourceInfo.original _ _ endTrail _ =>
|
||
body.setHeadInfo (SourceInfo.original lead pos trail pos') |>.setTailInfo (SourceInfo.original endLead endPos endTrail endPos')
|
||
| _, _, _, _ => body
|
||
|
||
partial def unparen : Syntax → Syntax
|
||
-- don't remove parentheses in syntax quotations, they might be semantically significant
|
||
| stx => if stx.isOfKind `Lean.Parser.Term.stxQuot then stx
|
||
else match stx with
|
||
| `(($stx')) => unparenAux stx $ unparen stx'
|
||
| `(level|($stx')) => unparenAux stx $ unparen stx'
|
||
| _ => stx.modifyArgs $ Array.map unparen
|
||
|
||
def clearHygieneInfo (stx : Syntax) : Syntax :=
|
||
Id.run <| stx.replaceM fun s => do
|
||
if s.isOfKind hygieneInfoKind then
|
||
return some <| s.setArg 0 (mkIdent .anonymous)
|
||
else
|
||
return none
|
||
|
||
unsafe def main (args : List String) : IO Unit := do
|
||
let (debug, f) : Bool × String := match args with
|
||
| [f, "-d"] => (true, f)
|
||
| [f] => (false, f)
|
||
| _ => panic! "usage: file [-d]";
|
||
let env ← mkEmptyEnvironment;
|
||
let stx ← Lean.Parser.testParseFile env args.head!;
|
||
let header := stx.raw.getArg 0;
|
||
let some s ← pure header.reprint | throw $ IO.userError "header reprint failed";
|
||
IO.print s;
|
||
let cmds := (stx.raw.getArg 1).getArgs;
|
||
cmds.forM $ fun cmd => do
|
||
let cmd := unparen cmd;
|
||
let (cmd, _) ← (tryFinally (PrettyPrinter.parenthesizeCommand cmd) printTraces).toIO { options := Options.empty.setBool `trace.PrettyPrinter.parenthesize debug, fileName := "", fileMap := default } { env := env };
|
||
let some s ← pure cmd.reprint | throw $ IO.userError "cmd reprint failed";
|
||
IO.print s
|
||
|
||
#eval main ["../../../src/Init/Prelude.lean"]
|
||
|
||
def check (stx : Syntax) : CoreM Unit := do
|
||
let stx' := unparen stx;
|
||
let stx' ← clearHygieneInfo <$> PrettyPrinter.parenthesizeTerm stx';
|
||
let f ← PrettyPrinter.formatTerm stx';
|
||
IO.println f;
|
||
if (clearHygieneInfo stx != stx') then
|
||
throwError "reparenthesization failed"
|
||
|
||
open Lean
|
||
|
||
syntax:80 term " ^~ " term:80 : term
|
||
syntax:70 term " *~ " term:71 : term
|
||
|
||
/-- info: ((1 + 2) *~ 3) ^~ 4 -/
|
||
#guard_msgs in
|
||
#eval check $ Unhygienic.run `(((1 + 2) *~ 3) ^~ 4)
|
||
|
||
/-- info: opaque foo.1 (a.1 := (by exact 1)) : True.1 -/
|
||
#guard_msgs in
|
||
#eval check $ Unhygienic.run `(opaque foo (a := (by exact 1)) : True)
|