Skip to content
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
3 changes: 3 additions & 0 deletions src/Init/Meta/Defs.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Elab/BuiltinDo/Match.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/Lean/Elab/Do/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/Lean/Elab/Extra.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Elab/MacroArgUtil.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/Lean/Elab/Match.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions src/Lean/Elab/Quotation/Precheck.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/Lean/Elab/Syntax.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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) -/
Expand Down
7 changes: 6 additions & 1 deletion src/Lean/Elab/Tactic/Grind/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
53 changes: 45 additions & 8 deletions src/Lean/Elab/Term/TermElabM.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Lean/Elab/Util.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

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
Expand Down
13 changes: 13 additions & 0 deletions tests/elab/deprecated_syntax.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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))
7 changes: 7 additions & 0 deletions tests/elab/deprecated_syntax.lean.out.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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`
75 changes: 75 additions & 0 deletions tests/elab/deprecated_syntax_grind.lean
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading