-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: don't panic when linting in empty environments (#494)
The "unreachable tactic" linter would previously assume that various parser categories were in the environment, and panic if they weren't. This caused problems when elaborating declarations in a fresh, empty environment. This also adds a regression test that makes sure linters work in a fresh environment and configures testing to fail if Lean panics.
- Loading branch information
1 parent
03a0f24
commit 6ed0fa6
Showing
3 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Std | ||
import Lean.Elab | ||
|
||
set_option linter.all true | ||
|
||
/-- Some syntax to elaborate in a fresh env -/ | ||
def natDef : String := | ||
"prelude | ||
inductive Nat where | ||
| zero | ||
| succ (n : Nat) : Nat | ||
def id (x : α) := x | ||
" | ||
|
||
open Lean Parser Elab in | ||
/-- | ||
Test that the linters imported from Std all work when run on a declaration in an empty environment. | ||
This runs the linters because there's a global IO.Ref that contains them, rather than having them | ||
be in a field of the environment itself, precisely so they can run in situations like this. However, | ||
a linter that assumes it's run in an environment with the Lean prelude available may use of things | ||
like `find!` and `get!` in linters, with the result that the linter panics. | ||
This test elaborates the definition of the natural numbers and the identity function in a fresh | ||
environment with all linters enabled. Running the tests in an environment with `LEAN_ABORT_ON_PANIC` | ||
set ensures that the linters can at least run. | ||
-/ | ||
elab "#testInEmptyEnv" : command => do | ||
let context := mkInputContext natDef "test" | ||
let (header, state, msgs) ← parseHeader context | ||
let opts := Options.empty |>.setBool `linter.all true | ||
let (env, msgs) ← processHeader header opts msgs context | ||
if msgs.hasErrors then | ||
for msg in msgs.toList do logMessage msg | ||
liftM (m := IO) <| throw <| IO.userError "Errors in header" | ||
let cmdState := Command.mkState env msgs | ||
let s ← IO.processCommands context state cmdState | ||
for t in s.commandState.infoState.trees do | ||
pushInfoTree t | ||
for msg in s.commandState.messages.toList do | ||
logMessage msg | ||
|
||
#testInEmptyEnv |