Skip to content

Commit 8f4785c

Browse files
committed
feat: warn on deprecated declarations in scripts/check-yaml (leanprover-community#38313)
This PR updates `scripts/check-yaml.lean` to warn on deprecated declarations. It also cleans up some code: `initSearchPath` does exactly what the removed code plus `CoreM.withImportModules` does (and as far as I can see we no longer need `CoreM`). Also modulizes and removes the dependence on Mathlib. Co-authored-by: thorimur <thomasmurrills@gmail.com>
1 parent 8c46287 commit 8f4785c

1 file changed

Lines changed: 32 additions & 26 deletions

File tree

scripts/check-yaml.lean

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
/-
22
Copyright (c) 2023 Eric Wieser. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
4-
Authors: Eric Wieser
4+
Authors: Eric Wieser, Thomas R. Murrills
55
-/
6-
import Mathlib.Lean.CoreM
7-
import Mathlib.Tactic.ToExpr
6+
module
7+
8+
import Lean.Data.Json
9+
import Lean.Linter.Deprecated
810

911
/-! # Script to check `undergrad.yaml`, `overview.yaml`, `100.yaml` and `1000.yaml`
1012
1113
This assumes `yaml_check.py` has first translated these to `json` files.
1214
13-
It verifies that the referenced declarations exist, and prints an error otherwise.
15+
It verifies that the referenced declarations exist and are not deprecated, and prints an error
16+
otherwise.
1417
-/
1518

16-
open IO.FS Lean Lean.Elab
17-
open Lean Core Elab Command
19+
open Lean
1820

1921
abbrev DBFile := Array (String × Name)
2022

@@ -25,25 +27,29 @@ def readJsonFile (α) [FromJson α] (path : System.FilePath) : IO α := do
2527
def databases : List String :=
2628
["undergrad", "overview", "100", "1000"]
2729

28-
def processDb (decls : ConstMap) : String → IO Bool
29-
| file => do
30+
def processDb (env : Environment) (file : String) : IO Bool := do
3031
let lines ← readJsonFile DBFile s!"{file}.json"
31-
let missing := lines.filter (fun l ↦ !(decls.contains l.2))
32-
if 0 < missing.size then
33-
IO.println s!"Entries in `docs/{file}.yaml` refer to {missing.size} declaration(s) that don't exist. \
34-
Please correct the following:"
35-
for p in missing do
36-
IO.println s!" {p.1}: {p.2}"
37-
IO.println ""
38-
return true
39-
else
40-
return false
41-
42-
unsafe def main : IO Unit := do
43-
let searchPath ← addSearchPathFromEnv (← getBuiltinSearchPath (← findSysroot))
44-
CoreM.withImportModules #[`Mathlib, `Archive]
45-
(searchPath := searchPath) (trustLevel := 1024) do
46-
let decls := (← getEnv).constants
47-
let results ← databases.mapM (fun p ↦ processDb decls p)
48-
if results.any id then
32+
let mut (missing, deprecated) := (#[], #[])
33+
for entry@(_, decl) in lines do
34+
if !env.contains decl then
35+
missing := missing.push entry
36+
else if let some newName := Linter.getDeprecatedNewName env decl then
37+
deprecated := deprecated.push (entry, newName)
38+
unless missing.isEmpty do
39+
IO.println s!"Entries in `docs/{file}.yaml` refer to {missing.size} declaration(s) that don't \
40+
exist. Please correct the following:"
41+
for (key, decl) in missing do
42+
IO.println s!" {key}: '{decl}'"
43+
unless deprecated.isEmpty do
44+
IO.println s!"Entries in `docs/{file}.yaml` refer to {deprecated.size} declaration(s) that are \
45+
deprecated. Please update the document to refer to the following declaration(s):"
46+
for ((key, decl), newName) in deprecated do
47+
IO.println s!" {key}: '{newName}' (previously '{decl}')"
48+
return missing.isEmpty && deprecated.isEmpty
49+
50+
public unsafe def main : IO Unit := do
51+
initSearchPath (← findSysroot)
52+
withImportModules #[`Mathlib, `Archive] ∅ (trustLevel := 1024) fun env => do
53+
let results ← databases.mapM (fun p ↦ processDb env p)
54+
unless results.all id do
4955
IO.Process.exit 1

0 commit comments

Comments
 (0)