Skip to content

Commit bace69d

Browse files
committed
Improve error messages in sorry extraction
1 parent a18223f commit bace69d

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

LeanUtils/ExtractSorry.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def ppGoalIfNoMVar (mvar : MVarId) : MetaM (Option Format) := do
1414
catch _ =>
1515
return none
1616

17-
1817
/-- Traverses an info tree and applies `x` on the type of each sorry,
1918
while iteratively reconstructing the MetaM context.
2019
@@ -37,9 +36,10 @@ where
3736
def extractSorries (T : InfoTree) : IO (List <| SorryData Format) :=
3837
traverseInfoTree ppGoalIfNoMVar T
3938

40-
4139
/-- `parseFile myLeanFile` extracts the sorries contained in the Lean file `myLeanFile`. -/
4240
def parseFile (path : System.FilePath) : IO (List ParsedSorry) := do
41+
-- Throw an error if the oleans of the file can't be found...
42+
path.checkOLeans
4343
let (fileMap, trees) ← extractInfoTrees path
4444
-- TODO(Paul-Lez): here ideally we should filter `trees` so we only run
4545
-- `extractSorries` on infotrees that arise from theorems/lemmas/definitions/...

LeanUtils/Utils.lean

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ def SorryData.toParsedSorry {Out} [ToString Out] (fileMap : FileMap) : SorryData
5656
instance : ToString ParsedSorry where
5757
toString a := ToString.toString <| ToJson.toJson a
5858

59-
#check Frontend.State
59+
def Lean.Message.printIfError (m : Message) : IO Unit := do
60+
if m.severity == .error then IO.eprintln <| ← m.toString
61+
62+
def Lean.MessageLog.printErrors (m : MessageLog) : IO Unit := do
63+
for message in m.reported ++ m.unreported do message.printIfError
6064

6165
/-- `extractInfoTree myLeanFile` takes as input the path to a Lean file and outputs
6266
the infotrees of the file, together with the `FileMap`. -/
@@ -65,15 +69,18 @@ def extractInfoTrees (fileName : System.FilePath) : IO (FileMap × List InfoTree
6569
let inputCtx := Parser.mkInputContext input fileName.toString
6670
let (header, parserState, messages) ← Parser.parseHeader inputCtx
6771
if Lean.MessageLog.hasErrors messages then
68-
IO.println s!"Ran into errors while parsing the header of {fileName}"
72+
IO.eprintln s!"Ran into errors while parsing the header of {fileName}"
73+
MessageLog.printErrors messages
6974
-- TODO: do we need to specify the main module here?
7075
let (env, messages) ← processHeader header {} messages inputCtx
7176
if Lean.MessageLog.hasErrors messages then
72-
IO.println s!"Ran into errors whist processing the header of {fileName}"
77+
IO.eprintln s!"Ran into errors whist processing the header of {fileName}"
78+
MessageLog.printErrors messages
7379
let commandState := Command.mkState env messages
7480
let frontendState ← IO.processCommands inputCtx parserState commandState
7581
if Lean.MessageLog.hasErrors frontendState.commandState.messages then
76-
IO.println s!"Ran into errors whist processing the commands in {fileName}"
82+
IO.eprintln s!"Ran into errors whist processing the commands in {fileName}"
83+
MessageLog.printErrors messages
7784
let fileMap := FileMap.ofString input
7885
return (fileMap, frontendState.commandState.infoState.trees.toList)
7986

@@ -117,3 +124,6 @@ def getProjectSearchPath (path : System.FilePath) : IO (System.SearchPath) := do
117124
let paths ← getAllLakePaths rootDir
118125
let originalSearchPath ← getBuiltinSearchPath (← findSysroot)
119126
return originalSearchPath.append paths.toList
127+
128+
def System.FilePath.checkOLeans (path : System.FilePath) : IO Unit := do
129+
discard <| Lean.findOLean (← moduleNameOfFileName path none)

bins/ExtractSorry.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ only have to run them once per project, rather than once per Lean file.
1313
-/
1414

1515
def main (args : List String) : IO Unit := do
16+
Lean.initSearchPath (← Lean.findSysroot)
1617
logRun
1718
if let some path := args[0]? then
1819
IO.println s!"Running sorry extraction on file {path}."

0 commit comments

Comments
 (0)