From 3131397aace34b73f2288f8af3ecd9c4db62c6f0 Mon Sep 17 00:00:00 2001 From: Julia Markus Himmel <2065352+TwoFX@users.noreply.github.com> Date: Tue, 20 Oct 2026 12:32:21 +0000 Subject: [PATCH 1/3] fix: show deprecated syntax warning in more places --- src/Lean/Elab/BuiltinDo/Match.lean | 2 +- src/Lean/Elab/Do/Basic.lean | 6 +- src/Lean/Elab/Extra.lean | 5 +- src/Lean/Elab/MacroArgUtil.lean | 2 +- src/Lean/Elab/Match.lean | 7 +- src/Lean/Elab/Quotation/Precheck.lean | 16 +- src/Lean/Elab/Syntax.lean | 5 +- src/Lean/Elab/Term/TermElabM.lean | 53 ++++- src/Lean/Elab/Util.lean | 3 + tests/elab/deprecated_syntax.lean | 13 ++ .../elab/deprecated_syntax.lean.out.expected | 7 + .../deprecated_syntax_macro_expansion.lean | 211 ++++++++++++++++++ 12 files changed, 301 insertions(+), 29 deletions(-) create mode 100644 tests/elab/deprecated_syntax_macro_expansion.lean diff --git a/src/Lean/Elab/BuiltinDo/Match.lean b/src/Lean/Elab/BuiltinDo/Match.lean index 249e89b55de8..3700c344974c 100644 --- a/src/Lean/Elab/BuiltinDo/Match.lean +++ b/src/Lean/Elab/BuiltinDo/Match.lean @@ -182,7 +182,7 @@ private def elabMatchAlt (discrs : Array Term.Discr) (matchType : Expr) return (altLHS, rhs) private def elabMatchAlts (discrs : Array Term.Discr) (alts : Array DoMatchAltView) (dec : DoElemCont) : DoElabM (Expr × Array AltLHS × Array Expr) := do - let alts ← liftMacroM <| Term.expandMacrosInPatterns alts + let alts ← Term.expandMacrosInPatterns alts let matchType ← mkDepMatchMotive discrs (← mkMonadApp (← read).doBlockResultType) let (lhss, rhss) ← Array.unzip <$> alts.mapM (elabMatchAlt discrs matchType · dec) return (matchType, lhss, rhss) diff --git a/src/Lean/Elab/Do/Basic.lean b/src/Lean/Elab/Do/Basic.lean index ae2bf2d971a0..55aafcc2ee1b 100644 --- a/src/Lean/Elab/Do/Basic.lean +++ b/src/Lean/Elab/Do/Basic.lean @@ -1031,9 +1031,7 @@ partial def elabDoElem (stx : DoElem) (cont : DoElemCont) (catchExPostpone : Boo if (← read).deadCode matches .deadSemantically then logWarningAt stx "This `do` element and its control-flow region are dead code. Consider refactoring your code to remove it." withDeadCode .alive do -- we have warned for this doElem. No need to warn for every element of the block - let env ← getEnv - if let some (decl, stxNew?) ← liftMacroM (expandMacroImpl? env stx) then - let stxNew ← liftMacroM <| liftExcept stxNew? + if let some (decl, stxNew) ← Term.expandMacroReportingDeprecated? stx then return ← withTermInfoContext' decl stx mγ <| Term.withMacroExpansion stx stxNew <| withRef stxNew <| elabDoElem ⟨stxNew⟩ cont @@ -1042,6 +1040,8 @@ partial def elabDoElem (stx : DoElem) (cont : DoElemCont) (catchExPostpone : Boo if !doElems.isEmpty then return ← elabDoElems1 (doElems.push stx) cont + -- After `expandNestedActions`, as syntax with nested actions is elaborated again without them + Term.reportDeprecatedSyntax stx match doElemElabAttribute.getEntries (← getEnv) k with | [] => throwError "elaboration function for `{k}` has not been implemented{indentD stx}" | elabFns => elabDoElemFns stx cont elabFns catchExPostpone diff --git a/src/Lean/Elab/Extra.lean b/src/Lean/Elab/Extra.lean index 375749812ea7..885ad10e584b 100644 --- a/src/Lean/Elab/Extra.lean +++ b/src/Lean/Elab/Extra.lean @@ -205,9 +205,8 @@ where go e | _ => withRef s do - match (← liftMacroM <| expandMacroImpl? (← getEnv) s) with - | some (macroName, s?) => - let s' ← liftMacroM <| liftExcept s? + match (← expandMacroReportingDeprecated? s) with + | some (macroName, s') => withPushMacroExpansionStack s s' do return .macroExpansion macroName s s' (← go s') | none => processLeaf s diff --git a/src/Lean/Elab/MacroArgUtil.lean b/src/Lean/Elab/MacroArgUtil.lean index 197062db439c..52add79837d0 100644 --- a/src/Lean/Elab/MacroArgUtil.lean +++ b/src/Lean/Elab/MacroArgUtil.lean @@ -17,7 +17,7 @@ open Lean.Parser.Command /-- Convert `macro` arg into a `syntax` command item and a pattern element -/ partial def expandMacroArg (stx : TSyntax ``macroArg) : CommandElabM (TSyntax `stx × Term) := do - let (id?, id, stx) ← match (← liftMacroM <| expandMacros stx) with + let (id?, id, stx) ← match (← liftTermElabM <| Term.expandMacrosReportingDeprecated stx) with | `(macroArg| $id:ident:$stx) => pure (some id, (id : Term), stx) | `(macroArg| $stx:stx) => pure (none, (← `(x)), stx) | _ => throwUnsupportedSyntax diff --git a/src/Lean/Elab/Match.lean b/src/Lean/Elab/Match.lean index f944820b68fe..894e9bbcb850 100644 --- a/src/Lean/Elab/Match.lean +++ b/src/Lean/Elab/Match.lean @@ -151,9 +151,10 @@ where else return { discrs, alts := matchAltViews, isDep := false, matchType := expectedType } -def expandMacrosInPatterns (matchAlts : Array (MatchAltView k)) : MacroM (Array (MatchAltView k)) := do +def expandMacrosInPatterns (matchAlts : Array (MatchAltView k)) : + TermElabM (Array (MatchAltView k)) := do matchAlts.mapM fun matchAlt => do - let patterns ← matchAlt.patterns.mapM expandMacros + let patterns ← matchAlt.patterns.mapM (expandMacrosReportingDeprecated ·) pure { matchAlt with patterns := patterns } private def getMatchGeneralizing? : Syntax → Option Bool @@ -1123,7 +1124,7 @@ private def elabMatchAux (generalizing? : Option Bool) (discrStxs : Array Syntax generalizing? := some false let (discrs, matchType, altLHSS, isDep, rhss) ← commitIfDidNotPostpone do let ⟨discrs, matchType, isDep, altViews⟩ ← elabMatchTypeAndDiscrs discrStxs matchOptMotive altViews expectedType - let matchAlts ← liftMacroM <| expandMacrosInPatterns altViews + let matchAlts ← expandMacrosInPatterns altViews trace[Elab.match] "matchType: {matchType}" let (discrs, matchType, alts, refined) ← elabMatchAltViews generalizing? discrs matchType matchAlts let isDep := isDep || refined diff --git a/src/Lean/Elab/Quotation/Precheck.lean b/src/Lean/Elab/Quotation/Precheck.lean index 0ae20abd959e..3ae430496ab5 100644 --- a/src/Lean/Elab/Quotation/Precheck.lean +++ b/src/Lean/Elab/Quotation/Precheck.lean @@ -57,14 +57,16 @@ unsafe builtin_initialize precheckAttribute : KeyedDeclsAttribute Precheck ← } partial def precheck : Precheck := fun stx => do - -- Check for deprecated syntax kinds in quotations + -- Check for deprecated syntax kinds in quotations. Syntax produced by expanding macros in the + -- quotation is not part of it; elaborating the result of the quotation reports it. if let some entry := (deprecatedSyntaxExt.getState (← getEnv)).find? stx.getKind then - let extraMsg := match entry.text? with - | some text => m!": {text}" - | none => m!"" - withRef stx do - Linter.logLintIf Linter.linter.deprecated.syntax stx - m!"quotation uses deprecated syntax '{stx.getKind}'{extraMsg}" + unless stx.getHeadInfo matches .synthetic .. do + let extraMsg := match entry.text? with + | some text => m!": {text}" + | none => m!"" + withRef stx do + Linter.logLintIf Linter.linter.deprecated.syntax stx + m!"quotation uses deprecated syntax '{stx.getKind}'{extraMsg}" if let p::_ := precheckAttribute.getValues (← getEnv) stx.getKind then if ← catchInternalId unsupportedSyntaxExceptionId (do withRef stx <| p stx; pure true) (fun _ => pure false) then return diff --git a/src/Lean/Elab/Syntax.lean b/src/Lean/Elab/Syntax.lean index 87640dc1dd5b..f608e1242035 100644 --- a/src/Lean/Elab/Syntax.lean +++ b/src/Lean/Elab/Syntax.lean @@ -147,9 +147,8 @@ where else if kind == ``Lean.Parser.Syntax.unicodeAtom then processUnicode stx else - let stxNew? ← liftM (liftMacroM (expandMacro? stx) : TermElabM _) - match stxNew? with - | some stxNew => process stxNew + match (← liftM (Term.expandMacroReportingDeprecated? stx : TermElabM _)) with + | some (_, stxNew) => Term.withPushMacroExpansionStack stx stxNew <| process stxNew | none => throwErrorAt stx "unexpected syntax kind of category `syntax`: {kind}" /-- Sequence (aka NullNode) -/ diff --git a/src/Lean/Elab/Term/TermElabM.lean b/src/Lean/Elab/Term/TermElabM.lean index 138692e9d0fb..87da4bd0966a 100644 --- a/src/Lean/Elab/Term/TermElabM.lean +++ b/src/Lean/Elab/Term/TermElabM.lean @@ -825,8 +825,10 @@ def elabLevel (stx : Syntax) : TermElabM Level := liftLevelM <| Level.elabLevel stx /-- Elaborate `x` with `stx` on the macro stack -/ -def withPushMacroExpansionStack (beforeStx afterStx : Syntax) (x : TermElabM α) : TermElabM α := - withReader (fun ctx => { ctx with macroStack := { before := beforeStx, after := afterStx } :: ctx.macroStack }) x +def withPushMacroExpansionStack [MonadWithReaderOf Context m] (beforeStx afterStx : Syntax) + (x : m α) : m α := + withTheReader Context (fun ctx => + { ctx with macroStack := { before := beforeStx, after := afterStx } :: ctx.macroStack }) x /-- Elaborate `x` with `stx` on the macro stack and produce macro expansion info -/ @[specialize] @@ -1698,6 +1700,44 @@ private def elabUsingElabFns (stx : Syntax) (expectedType? : Option Expr) (catch -- Shortcut instance; otherwise every use searches for a lift down to `CoreM`. instance : MonadMacroAdapter TermElabM := inferInstance +/-- +Reports `stx` if its syntax kind is deprecated by `deprecated_syntax`, unless deprecation checks are +disabled by `withoutCheckDeprecated`. + +The elaborator of a syntax category calls this before running the elaborators of `stx`, as +`elabTerm` does for terms. Macros are instead reported when expanding them with +`expandMacroReportingDeprecated?`. +-/ +def reportDeprecatedSyntax (stx : Syntax) : TermElabM Unit := do + if (← read).checkDeprecated then + checkDeprecatedSyntax stx (← read).macroStack + +/-- +Expands the macro `stx`, if any, returning the name of the macro declaration and the expansion. +Unlike `expandMacroImpl?`, this reports `stx` if its syntax kind is deprecated. Elaborators that +expand macros themselves instead of elaborating them with `elabTerm` should use this function, +while analyses that do not elaborate the expansion should use `expandMacroImpl?`, so that +deprecated syntax is reported once. +-/ +def expandMacroReportingDeprecated? (stx : Syntax) : TermElabM (Option (Name × Syntax)) := do + let some (declName, stxNew?) ← liftMacroM <| expandMacroImpl? (← getEnv) stx | return none + reportDeprecatedSyntax stx + return some (declName, ← liftMacroM <| liftExcept stxNew?) + +/-- +Expands all macros in `stx` like `expandMacros`, except in nodes whose kind does not satisfy `p`, +and reports deprecated syntax like `expandMacroReportingDeprecated?`. +-/ +partial def expandMacrosReportingDeprecated (stx : Syntax) + (p : SyntaxNodeKind → Bool := (· != ``Parser.Term.byTactic)) : TermElabM Syntax := + withRef stx <| withIncRecDepth do + let .node info k args := stx | return stx + unless p k do return stx + if let some (_, stxNew) ← expandMacroReportingDeprecated? stx then + withPushMacroExpansionStack stx stxNew <| expandMacrosReportingDeprecated stxNew p + else + return .node info k (← args.mapM (expandMacrosReportingDeprecated · p)) + private def isExplicit (stx : Syntax) : Bool := match stx with | `(@$_) => true @@ -1853,17 +1893,14 @@ private partial def elabTermAux (expectedType? : Option Expr) (catchExPostpone : withTraceNode `Elab.step (fun _ => return m!"expected type: {expectedType?}, term\n{stx}") (tag := stx.getKind.toString) do checkSystem "elaborator" - if (← read).checkDeprecated then - checkDeprecatedSyntax stx (← read).macroStack - let env ← getEnv - let result ← match (← liftMacroM (expandMacroImpl? env stx)) with - | some (decl, stxNew?) => - let stxNew ← liftMacroM <| liftExcept stxNew? + let result ← match (← expandMacroReportingDeprecated? stx) with + | some (decl, stxNew) => withTermInfoContext' decl stx (expectedType? := expectedType?) <| withMacroExpansion stx stxNew <| withRef stxNew <| elabTermAux expectedType? catchExPostpone implicitLambda stxNew | _ => + reportDeprecatedSyntax stx let useImplicitResult ← if implicitLambda && (← read).implicitLambda then useImplicitLambda stx expectedType? else pure .no match useImplicitResult with | .yes expectedType => elabImplicitLambda stx catchExPostpone expectedType diff --git a/src/Lean/Elab/Util.lean b/src/Lean/Elab/Util.lean index d845cbd2a28a..c65cb6aa0e9b 100644 --- a/src/Lean/Elab/Util.lean +++ b/src/Lean/Elab/Util.lean @@ -153,6 +153,9 @@ builtin_initialize macroAttribute : KeyedDeclsAttribute Macro ← mkMacroAttribu /-- Try to expand macro at syntax tree root and return macro declaration name and new syntax if successful. Return none if all macros threw `Macro.Exception.unsupportedSyntax`. + +Elaborators should use `Lean.Elab.Term.expandMacroReportingDeprecated?` instead, which also reports +deprecated syntax. -/ def expandMacroImpl? (env : Environment) : Syntax → MacroM (Option (Name × Except Macro.Exception Syntax)) := fun stx => do for e in macroAttribute.getEntries env stx.getKind do diff --git a/tests/elab/deprecated_syntax.lean b/tests/elab/deprecated_syntax.lean index 2447e1a243aa..44b31b0e475a 100644 --- a/tests/elab/deprecated_syntax.lean +++ b/tests/elab/deprecated_syntax.lean @@ -105,3 +105,16 @@ example : True := by depRec 2 macro "wrapsDepRec" : tactic => `(tactic| depRec 1) example : True := by wrapsDepRec + +-- Test 12: deprecated syntax produced by a macro in a checked quotation is not reported at the +-- quotation, which does not contain it, but where the quotation's result is elaborated +syntax "wrapsOld " ident : term +macro_rules | `(wrapsOld $_) => `(oldThing) +def someIdent := 0 +macro "usesWrapsOld" : term => ``(wrapsOld someIdent) + +#check usesWrapsOld + +-- Test 12b: deprecated syntax written in a macro argument in a checked quotation is reported +macro "idTerm " x:term:max : term => `($x) +macro "usesIdTerm" : term => ``(idTerm (oldThing + someIdent)) diff --git a/tests/elab/deprecated_syntax.lean.out.expected b/tests/elab/deprecated_syntax.lean.out.expected index 3e4f5b63570c..b9c709f7114c 100644 --- a/tests/elab/deprecated_syntax.lean.out.expected +++ b/tests/elab/deprecated_syntax.lean.out.expected @@ -38,3 +38,10 @@ Note: This linter can be disabled with `set_option linter.deprecated.syntax fals deprecated_syntax.lean:107:21-107:32: warning: syntax 'depRec' has been deprecated: use `trivial` instead Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +deprecated_syntax.lean:116:7-116:19: warning: macro 'termWrapsOld_' (expanded from 'termUsesWrapsOld') produces deprecated syntax 'termOldThing': use `42` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +42 : Nat +deprecated_syntax.lean:120:40-120:48: warning: quotation uses deprecated syntax 'termOldThing': use `42` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` diff --git a/tests/elab/deprecated_syntax_macro_expansion.lean b/tests/elab/deprecated_syntax_macro_expansion.lean new file mode 100644 index 000000000000..2af74e8adaa0 --- /dev/null +++ b/tests/elab/deprecated_syntax_macro_expansion.lean @@ -0,0 +1,211 @@ +/-! +Tests that deprecated syntax is reported where elaborators expand macros themselves instead of +elaborating the syntax with `elabTerm`: operands of `binop%` and related elaborators, `do` +elements, patterns and syntax definitions. +-/ + +set_option linter.deprecated.syntax true + +syntax (name := oldAdd) "old_add " term:max term:max : term +macro_rules | `(old_add $a $b) => `($a + $b) + +deprecated_syntax oldAdd "use `+` instead" (since := "2026-10-19") + +syntax:max (name := oldSucc) "old_succ " term:max : term +macro_rules | `(old_succ $e) => `(Nat.succ $e) + +deprecated_syntax oldSucc "use `Nat.succ` instead" (since := "2026-10-19") + +syntax:max (name := oldNeg) "old_neg " term:max : term +macro_rules | `(old_neg $e) => `(-$e) + +deprecated_syntax oldNeg "use `-` instead" (since := "2026-10-19") + +syntax (name := oldZero) "old_zero" : term +macro_rules | `(old_zero) => `(Nat.zero) + +deprecated_syntax oldZero "use `Nat.zero` instead" (since := "2026-10-19") + +/-! ## Operands of `binop%`, `unop%` and `binrel%` -/ + +/-- +warning: syntax 'oldAdd' has been deprecated: use `+` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +--- +info: (1 + 2) * 3 : Nat +-/ +#guard_msgs in +#check old_add 1 2 * 3 + +/-- +warning: syntax 'oldNeg' has been deprecated: use `-` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +--- +info: - -1 : Int +-/ +#guard_msgs in +#check -old_neg (1 : Int) + +/-- +warning: syntax 'oldSucc' has been deprecated: use `Nat.succ` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +--- +info: Nat.succ 1 = 2 : Prop +-/ +#guard_msgs in +#check old_succ 1 = 2 + +syntax:65 (name := oldPlus) term:65 " +' " term:66 : term +macro_rules | `($a +' $b) => `($a + $b) + +deprecated_syntax oldPlus "use `+` instead" (since := "2026-10-19") + +syntax:50 (name := oldLe) term:51 " ≤' " term:51 : term +macro_rules | `($a ≤' $b) => `($a ≤ $b) + +deprecated_syntax oldLe "use `≤` instead" (since := "2026-10-19") + +-- Deprecated syntax nested in deprecated syntax is reported as well. +/-- +warning: syntax 'oldLe' has been deprecated: use `≤` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +--- +warning: syntax 'oldPlus' has been deprecated: use `+` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +--- +info: (1 + 2 ≤ 3) = True : Prop +-/ +#guard_msgs in +#check (1 +' 2 ≤' 3) = True + +macro "wrapped_add" : term => `(old_add 1 2) + +/-- +warning: macro 'termWrapped_add' (expanded from '«term_+_»') produces deprecated syntax 'oldAdd': use `+` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +--- +info: 1 + 2 + 3 : Nat +-/ +#guard_msgs in +#check wrapped_add + 3 + +-- Deprecated syntax is not reported in deprecated declarations. +#guard_msgs in +@[deprecated "use `+` instead" (since := "2026-10-19")] +def deprecatedAdd : Nat := old_add 1 2 * 3 + +/-! ## `do` elements -/ + +syntax (name := oldReturn) "old_return " term : doElem +macro_rules | `(doElem| old_return $e) => `(doElem| return $e) + +deprecated_syntax oldReturn "use `return` instead" (since := "2026-10-19") + +/-- +warning: syntax 'oldReturn' has been deprecated: use `return` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +def doReturnTest : Id Nat := do + old_return 1 + +/-! ## Patterns -/ + +/-- +warning: syntax 'oldSucc' has been deprecated: use `Nat.succ` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +def patternTest : Nat → Nat + | old_succ n => n + | _ => 0 + +/-- +warning: syntax 'oldZero' has been deprecated: use `Nat.zero` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +def doMatchTest (n : Nat) : Id Nat := do + match n with + | old_zero => return 1 + | _ => return 0 + +macro "zero_pat" : term => `(old_zero) + +/-- +warning: macro 'termZero_pat' produces deprecated syntax 'oldZero': use `Nat.zero` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +def macroPatternTest : Nat → Nat + | zero_pat => 1 + | _ => 0 + +-- Elaboration of the `match` is postponed until the type of `n` is known. +/-- +warning: syntax 'oldSucc' has been deprecated: use `Nat.succ` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +example : Nat → Nat := (fun n => match n with | old_succ k => k | _ => 0 : _ → Nat) + +/-! ## Syntax definitions -/ + +syntax:arg (name := oldMany) stx:max "*'" : stx +macro_rules | `(stx| $p*') => `(stx| $p*) + +deprecated_syntax oldMany "use `*` instead" (since := "2026-10-19") + +/-- +warning: syntax 'oldMany' has been deprecated: use `*` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +syntax "nums " num*' : term + +/-- +warning: syntax 'oldMany' has been deprecated: use `*` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +macro "nums' " num*' : term => `(0) + +/-! ## `do` elements implemented by an elaborator -/ + +deprecated_syntax Lean.Parser.Term.doUnless "use `if` instead" (since := "2026-10-19") + +/-- +warning: syntax 'Lean.Parser.Term.doUnless' has been deprecated: use `if` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +def doUnlessTest (b : Bool) : Id Nat := do + unless b do + return 1 + return 2 + +-- Nested actions are lifted out of the syntax before it is elaborated, and it is reported once. +/-- +warning: syntax 'Lean.Parser.Term.doUnless' has been deprecated: use `if` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +def doUnlessNestedTest (b : Id Bool) : Id Nat := do + unless (← b) do + return 1 + return 2 From 496a10cd671e52de47955b3b4801edd5892252e0 Mon Sep 17 00:00:00 2001 From: Julia Markus Himmel <2065352+TwoFX@users.noreply.github.com> Date: Tue, 20 Oct 2026 12:56:07 +0000 Subject: [PATCH 2/3] Docstrings --- src/Init/Meta/Defs.lean | 3 +++ src/Lean/Elab/Util.lean | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Init/Meta/Defs.lean b/src/Init/Meta/Defs.lean index b6c576f14a15..0e49a062349c 100644 --- a/src/Init/Meta/Defs.lean +++ b/src/Init/Meta/Defs.lean @@ -675,6 +675,9 @@ end Syntax Expand macros in the given syntax. A node with kind `k` is visited only if `p k` is true. + To elaborate the expansion in `TermElabM`, use `Lean.Elab.Term.expandMacrosReportingDeprecated` + instead, which also reports deprecated syntax. + Note that the default value for `p` returns false for `by ...` nodes. This is a "hack". The tactic framework abuses the macro system to implement extensible tactics. For example, one can define diff --git a/src/Lean/Elab/Util.lean b/src/Lean/Elab/Util.lean index c65cb6aa0e9b..aeb1e310d05b 100644 --- a/src/Lean/Elab/Util.lean +++ b/src/Lean/Elab/Util.lean @@ -154,8 +154,8 @@ builtin_initialize macroAttribute : KeyedDeclsAttribute Macro ← mkMacroAttribu Try to expand macro at syntax tree root and return macro declaration name and new syntax if successful. Return none if all macros threw `Macro.Exception.unsupportedSyntax`. -Elaborators should use `Lean.Elab.Term.expandMacroReportingDeprecated?` instead, which also reports -deprecated syntax. +To elaborate the expansion in `TermElabM`, use `Lean.Elab.Term.expandMacroReportingDeprecated?` +instead, which also reports deprecated syntax. -/ def expandMacroImpl? (env : Environment) : Syntax → MacroM (Option (Name × Except Macro.Exception Syntax)) := fun stx => do for e in macroAttribute.getEntries env stx.getKind do From e9c68ba8e55f409248fe30c1db8c1356a197c7da Mon Sep 17 00:00:00 2001 From: Julia Markus Himmel <2065352+TwoFX@users.noreply.github.com> Date: Tue, 20 Oct 2026 12:59:18 +0000 Subject: [PATCH 3/3] Deprecated syntax check for grind tactics --- src/Lean/Elab/Tactic/Grind/Basic.lean | 7 ++- tests/elab/deprecated_syntax_grind.lean | 75 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 tests/elab/deprecated_syntax_grind.lean diff --git a/src/Lean/Elab/Tactic/Grind/Basic.lean b/src/Lean/Elab/Tactic/Grind/Basic.lean index 313a8a52a98f..8d7baaa1b13a 100644 --- a/src/Lean/Elab/Tactic/Grind/Basic.lean +++ b/src/Lean/Elab/Tactic/Grind/Basic.lean @@ -8,6 +8,7 @@ prelude public import Lean.Elab.Tactic.Basic public import Lean.Meta.Tactic.Grind.Main import Lean.Meta.Tactic.Grind.Intro +import Lean.Elab.DeprecatedSyntax public import Lean.Meta.Sym.Apply public import Lean.Meta.Sym.Util public import Lean.Meta.Sym.Simp.SimpM @@ -149,12 +150,16 @@ partial def evalGrindTactic (stx : Syntax) : GrindTacticM Unit := do Term.withoutTacticIncrementality true <| withTacticInfoContext stx do stx.getArgs.forM evalGrindTactic else withTraceNode `Elab.step (fun _ => return stx) (tag := stx.getKind.toString) do + Term.reportDeprecatedSyntax stx let evalFns := grindTacElabAttribute.getEntries (← getEnv) stx.getKind let macros := macroAttribute.getEntries (← getEnv) stx.getKind if evalFns.isEmpty && macros.isEmpty then throwErrorAt stx "Grind tactic `{stx.getKind}` has not been implemented" let s ← Grind.saveState - expandEval s macros evalFns #[] + if isDeprecatedSyntax (← getEnv) stx.getKind then + Term.withoutCheckDeprecated <| expandEval s macros evalFns #[] + else + expandEval s macros evalFns #[] | .missing => pure () | _ => throwError m!"Unexpected grind tactic{indentD stx}" where diff --git a/tests/elab/deprecated_syntax_grind.lean b/tests/elab/deprecated_syntax_grind.lean new file mode 100644 index 000000000000..245722e64f94 --- /dev/null +++ b/tests/elab/deprecated_syntax_grind.lean @@ -0,0 +1,75 @@ +/-! +Tests that deprecated syntax is reported in `grind` interactive mode, like in regular tactic blocks. +-/ + +set_option linter.deprecated.syntax true + +variable {α : Type} + +syntax (name := oldFinish) "old_finish" : grind +macro_rules | `(grind| old_finish) => `(grind| finish) + +deprecated_syntax oldFinish "use `finish` instead" (since := "2026-10-20") + +/-- +warning: syntax 'oldFinish' has been deprecated: use `finish` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +example (op : α → α → α) [Std.Associative op] (a b c d : α) : + d = op b c → op a d = op (op a b) c := by + grind => old_finish + +-- A deprecated grind tactic warns once, although its expansion uses itself and `old_finish`. +syntax (name := oldRec) "old_rec " num : grind +macro_rules + | `(grind| old_rec 0) => `(grind| old_finish) + | `(grind| old_rec $n) => `(grind| old_rec $(Lean.quote (n.getNat - 1))) + +deprecated_syntax oldRec "use `finish` instead" (since := "2026-10-20") + +/-- +warning: syntax 'oldRec' has been deprecated: use `finish` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +example (op : α → α → α) [Std.Associative op] (a b c d : α) : + d = op b c → op a d = op (op a b) c := by + grind => old_rec 2 + +-- A macro expanding to deprecated syntax warns at its call site. +macro "wraps_old_finish" : grind => `(grind| old_finish) + +/-- +warning: syntax 'oldFinish' has been deprecated: use `finish` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +example (op : α → α → α) [Std.Associative op] (a b c d : α) : + d = op b c → op a d = op (op a b) c := by + grind => wraps_old_finish + +-- Grind tactics implemented by an elaborator are reported as well. +deprecated_syntax Lean.Parser.Tactic.Grind.done "use `finish` instead" (since := "2026-10-20") + +/-- +warning: syntax 'Lean.Parser.Tactic.Grind.done' has been deprecated: use `finish` instead + +Note: This linter can be disabled with `set_option linter.deprecated.syntax false` +-/ +#guard_msgs in +example (op : α → α → α) [Std.Associative op] (a b c d : α) : + d = op b c → op a d = op (op a b) c := by + grind => + finish + done + +-- Deprecated syntax is not reported in deprecated declarations. +#guard_msgs in +@[deprecated "use something else" (since := "2026-10-20")] +theorem deprecatedUsesOldFinish (op : α → α → α) [Std.Associative op] (a b c d : α) : + d = op b c → op a d = op (op a b) c := by + grind => old_finish