feat: reject notations with repeated variables or antiquotations This PR makes `notation` reject a left-hand side that repeats a variable and an expansion that contains antiquotations, instead of elaborating such notations via generated `macro_rules` and unexpander definitions. With a repeated variable, all but one of the arguments were silently dropped; an antiquotation in the expansion ran its term when the notation was expanded. `notation` thus never generates definitions beyond the parser declaration anymore, and `mkUnexpander` is removed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/src/Lean/Elab/Notation.lean b/src/Lean/Elab/Notation.lean index 9e3d0fc..b4556be 100644 --- a/src/Lean/Elab/Notation.lean +++ b/src/Lean/Elab/Notation.lean
@@ -127,19 +127,6 @@ guard !hasDuplicateAntiquot args return (c, args) -/-- Try to derive an unexpander from a notation, see `getUnexpanderArgs?`. -/ -def mkUnexpander (attrKind : TSyntax ``attrKind) (pat qrhs : Term) : OptionT MacroM Syntax := do - let (c, args) ← getUnexpanderArgs? qrhs - -- replace head constant with antiquotation so we're not dependent on the exact pretty printing of the head - -- The reference is attached to the syntactic representation of the called function itself, not the entire function application - let lhs ← `($$f:ident) - let lhs := Syntax.mkApp lhs (.mk args) - let vis := Parser.Command.visibility.ofAttrKind attrKind - `(@[$attrKind app_unexpander $(mkIdent c)] $vis:visibility - aux_def unexpand $(mkIdent c) : Lean.PrettyPrinter.Unexpander := fun - | `($lhs) => withRef f `($pat) - | _ => throw ()) - /-- Converts `stx` into a template whose holes are the antiquotations of `vars`. If `quote` is set, identifiers are resolved like in a quotation term. @@ -173,11 +160,21 @@ | _ => s /-- -Returns whether the expansion `rhs` of a notation with variables `vars` can be turned into a -template. +Rejects notations that cannot be stored as a `NotationRule`: those with a repeated variable and those +with antiquotations in the expansion `rhs`. -/ -private def canUseTemplate (vars : Array Name) (rhs : Syntax) : Bool := - vars.allDiff && (rhs.find? fun stx => stx.isAnyAntiquot || stx.isMissing).isNone +private def checkNotation (vars : Array Syntax) (rhs : Syntax) : CommandElabM Unit := do + let mut seen : NameSet := {} + for var in vars do + if seen.contains var.getId then + throwErrorAt var "Invalid notation: The variable `{var.getId}` occurs more than once" + seen := seen.insert var.getId + if let some stx := rhs.find? (·.isAnyAntiquot) then + throwErrorAt stx "Invalid notation: Antiquotations are not supported in the expansion of a \ + notation; use `macro` or `macro_rules` instead" + -- The parse error was already reported + if rhs.hasMissing then + throwAbortCommand /-- Adds info about the variables of a notation at their binders `varStxs` and at their occurrences in @@ -228,6 +225,9 @@ @[builtin_command_elab Lean.Parser.Command.notation] def elabNotation : CommandElab | ref@`($[$doc?:docComment]? $[@[$attrs?,*]]? $attrKind:attrKind notation $[: $prec?]? $[(name := $name?)]? $[(priority := $prio?)]? $items* => $rhs) => do + let vars := items.filter fun item => item.raw.getKind == ``identPrec + let vars := vars.map fun var => var.raw[0] + checkNotation vars rhs let attrs? := addInheritDocDefault rhs attrs? let prio ← liftMacroM <| evalOptPrio prio? -- build parser @@ -235,29 +235,23 @@ let cat := mkIdentFrom ref `term let kind ← elabSyntax (← `($[$doc?:docComment]? $[@[$attrs?,*]]? $attrKind:attrKind syntax $[: $prec?]? $[(name := $name?)]? (priority := $(quote prio)) $[$syntaxParts]* : $cat)) - -- build macro rules - let vars := items.filter fun item => item.raw.getKind == ``identPrec - let vars := vars.map fun var => var.raw[0] + unless (← getEnv).contains kind do + -- The parser declaration failed to elaborate + if (← MonadLog.hasErrors) then + throwAbortCommand + throwError "Failed to declare notation: The parser declaration `{kind}` was not found" + -- build notation rule let qrhs := ⟨antiquote vars rhs⟩ let patArgs ← liftMacroM <| items.mapM expandNotationItemIntoPattern let pat : Term := ⟨mkNode kind patArgs⟩ - let varNames := vars.map (·.getId) - -- Make sure the quotation pre-checker takes section variables into account for local notation. - let withPrecheckScope (x : CommandElabM Unit) : CommandElabM Unit := do - if isLocalAttrKind attrKind then - let opts ← getOptions - let opts := Term.Quotation.quotPrecheck.allowSectionVars.set opts true - withScope (fun sc => { sc with opts }) x - else - x - if canUseTemplate varNames rhs && (← getEnv).contains kind then - withPrecheckScope <| addNotationRule attrKind kind vars pat qrhs + if isLocalAttrKind attrKind then + -- Make sure the quotation pre-checker takes section variables into account for local notation. + let opts ← getOptions + let opts := Term.Quotation.quotPrecheck.allowSectionVars.set opts true + withScope (fun sc => { sc with opts }) do + addNotationRule attrKind kind vars pat qrhs else - let macroDecl ← `($attrKind:attrKind macro_rules | `($pat) => ``($qrhs)) - withPrecheckScope <| elabCommand macroDecl - if let some delabDecl := (← liftMacroM <| mkUnexpander attrKind pat qrhs |>.run) then - -- Deprecated constants on the right-hand side were reported by the quotation precheck. - withoutCheckDeprecated <| elabCommand delabDecl + addNotationRule attrKind kind vars pat qrhs | _ => throwUnsupportedSyntax end Lean.Elab.Command
diff --git a/tests/elab/notationRule.lean b/tests/elab/notationRule.lean index b7af81c..e58a067 100644 --- a/tests/elab/notationRule.lean +++ b/tests/elab/notationRule.lean
@@ -178,18 +178,23 @@ #guard_msgs in #eval answer -/-! Notations with repeated variables fall back to generated definitions. -/ +/-! Notations with a repeated variable or with antiquotations are rejected. -/ -set_option linter.unusedVariables false in +/-- error: Invalid notation: The variable `x` occurs more than once -/ +#guard_msgs in notation "dup " x ", " x => myAdd x 1 -/-- info: 2 -/ +/-- +error: Invalid notation: Antiquotations are not supported in the expansion of a notation; use `macro` or `macro_rules` instead +-/ #guard_msgs in -#eval dup 1, 2 +notation "quoted" => (`($(mkIdent `x)) : MacroM Term) -/-- info: false -/ +/-- +error: Invalid notation: Antiquotations are not supported in the expansion of a notation; use `macro` or `macro_rules` instead +-/ #guard_msgs in -#eval show CoreM Bool from return (notationRuleExt.find? (← getEnv) `«termDup_,_»).isSome +notation "escaped" => (`($$x) : MacroM Term) /-! Syntax defined by `syntax` is pretty-printed without evaluating its parser declaration. -/