lean4-htt/tests/lean/run/termParserAttr.lean
Kim Morrison 3a457e6ad6
chore: use #guard_msgs in run tests (#4175)
Many of our tests in `tests/lean/run/` produce output from `#eval` (or
`#check`) statements, that is then ignored.

This PR tries to capture all the useful output using `#guard_msgs`. I've
only done a cursory check that the output is still sane --- there is a
chance that some "unchecked" tests have already accumulated regressions
and this just cements them!

In the other direction, I did identify two rotten tests:
* a minor one in `setStructInstNotation.lean`, where a comment says `Set
Nat`, but `#check` actually prints `?_`. Weird?
* `CompilerProbe.lean` is generating empty output, apparently indicating
that something is broken, but I don't know the signficance of this file.

In any case, I'll ask about these elsewhere.

(This started by noticing that a recent `grind` test file had an
untested `trace_state`, and then got carried away.)
2024-05-16 00:38:31 +00:00

101 lines
2.4 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Lean
open Lean
open Lean.Elab
def runCore (input : String) (failIff : Bool := true) : CoreM Unit := do
let env ← getEnv;
let opts ← getOptions;
let (env, messages) ← process input env opts;
messages.toList.forM fun msg => do IO.println (← msg.toString)
if failIff && messages.hasErrors then throwError "errors have been found";
if !failIff && !messages.hasErrors then throwError "there are no errors";
pure ()
open Lean.Parser
@[term_parser] def tst := leading_parser "(|" >> termParser >> Parser.optional (symbol ", " >> termParser) >> "|)"
def tst2 : Parser := symbol "(||" >> termParser >> symbol "||)"
@[term_parser] def boo : ParserDescr :=
ParserDescr.node `boo 10
(ParserDescr.binary `andthen
(ParserDescr.symbol "[|")
(ParserDescr.binary `andthen
(ParserDescr.cat `term 0)
(ParserDescr.symbol "|]")))
@[term_parser] def boo2 : ParserDescr :=
ParserDescr.node `boo2 10 (ParserDescr.parser `tst2)
open Lean.Elab.Term
@[term_elab tst] def elabTst : TermElab :=
adaptExpander $ fun stx => match stx with
| `((| $e |)) => pure e
| _ => throwUnsupportedSyntax
@[term_elab boo] def elabBoo : TermElab :=
fun stx expected? =>
elabTerm (stx.getArg 1) expected?
@[term_elab boo2] def elabBool2 : TermElab :=
adaptExpander $ fun stx => match stx with
| `((|| $e ||)) => `($e + 1)
| _ => throwUnsupportedSyntax
/-- info: id : Nat → Nat -/
#guard_msgs in
#eval runCore "#check [| @id.{1} Nat |]"
/-- info: id 1 : Nat -/
#guard_msgs in
#eval runCore "#check (| id 1 |)"
/-- info: id 1 + 1 : Nat -/
#guard_msgs in
#eval runCore "#check (|| id 1 ||)"
-- #eval run "#check (| id 1, id 1 |)" -- it will fail
@[term_elab tst] def elabTst2 : TermElab :=
adaptExpander $ fun stx => match stx with
| `((| $e1, $e2 |)) => `(($e1, $e2))
| _ => throwUnsupportedSyntax
-- Now both work
/-- info: id 1 : Nat -/
#guard_msgs in
#eval runCore "#check (| id 1 |)"
/-- info: (id 1, id 2) : Nat × Nat -/
#guard_msgs in
#eval runCore "#check (| id 1, id 2 |)"
declare_syntax_cat foo
syntax "⟨|" term "|⟩" : foo
syntax term : foo
syntax term ">>>" term : foo
syntax (name := tst3) "FOO " foo : term
macro_rules
| `(FOO ⟨| $t |⟩) => `($t+1)
| `(FOO $t:term) => `($t)
/-- info: id 1 + 1 : Nat -/
#guard_msgs in
#check FOO ⟨| id 1 |⟩
/-- info: 1 : Nat -/
#guard_msgs in
#check FOO 1
/-- info: 1 >>> 2 : Nat -/
#guard_msgs in
#check FOO 1 >>> 2