|
| 1 | +module |
| 2 | + |
| 3 | +public import Mathlib.Tactic.Linter.AuxLemma |
| 4 | +public import Mathlib.Init |
| 5 | +public import Init.Data.Iterators.Combinators.Monadic.FilterMap |
| 6 | + |
| 7 | +/-! |
| 8 | +# Tests for the `auxLemma` linter |
| 9 | +
|
| 10 | +The important tests here reference *genuinely* auto-generated declarations (`match_1`, |
| 11 | +`_proof_1`, `_sizeOf_1`), produced by the elaborator from the definitions below. If Lean's |
| 12 | +naming scheme for these auxiliary declarations ever changes, these tests break — which is the |
| 13 | +point: the linter would otherwise silently stop catching anything. |
| 14 | +-/ |
| 15 | + |
| 16 | +set_option linter.auxLemma true |
| 17 | + |
| 18 | +-- A `match`-based definition generates an auxiliary `foo.match_1`. |
| 19 | +def foo : Nat → Nat |
| 20 | + | 0 => 0 |
| 21 | + | n + 1 => n |
| 22 | + |
| 23 | +-- A structure with a proof field, plus an instance discharging it by tactic, generates |
| 24 | +-- `s._proof_1`; the structure itself generates `S._sizeOf_1`. |
| 25 | +structure S where |
| 26 | + n : Nat |
| 27 | + h : 0 < n |
| 28 | + |
| 29 | +def s : S where |
| 30 | + n := 1 |
| 31 | + h := by decide |
| 32 | + |
| 33 | +/-- |
| 34 | +warning: `foo.match_1` refers to an auto-generated auxiliary declaration. These are not stable across refactors; consider using a different approach. |
| 35 | +
|
| 36 | +Note: This linter can be disabled with `set_option linter.auxLemma false` |
| 37 | +-/ |
| 38 | +#guard_msgs in |
| 39 | +example := @foo.match_1 |
| 40 | + |
| 41 | +/-- |
| 42 | +warning: `s._proof_1` refers to an auto-generated auxiliary declaration. These are not stable across refactors; consider using a different approach. |
| 43 | +
|
| 44 | +Note: This linter can be disabled with `set_option linter.auxLemma false` |
| 45 | +-/ |
| 46 | +#guard_msgs in |
| 47 | +example := s._proof_1 |
| 48 | + |
| 49 | +-- `_sizeOf_1` is a compiler-internal that cannot be used as a term value, so we reference it |
| 50 | +-- with `#check` and only guard the linter warning (ignoring the `#check` info output). |
| 51 | +/-- |
| 52 | +warning: `S._sizeOf_1` refers to an auto-generated auxiliary declaration. These are not stable across refactors; consider using a different approach. |
| 53 | +
|
| 54 | +Note: This linter can be disabled with `set_option linter.auxLemma false` |
| 55 | +-/ |
| 56 | +#guard_msgs(drop info, warning) in |
| 57 | +#check @S._sizeOf_1 |
| 58 | + |
| 59 | +-- No warning on ordinary references, including names that merely resemble the auxiliary |
| 60 | +-- patterns but lack the trailing digits (e.g. `Nat.rec`, `foo`). |
| 61 | +#guard_msgs in |
| 62 | +example := @foo |
| 63 | + |
| 64 | +#guard_msgs in |
| 65 | +example := @Nat.rec |
| 66 | + |
| 67 | +-- The linter can be turned off. |
| 68 | +set_option linter.auxLemma false in |
| 69 | +#guard_msgs in |
| 70 | +example := @foo.match_1 |
| 71 | + |
| 72 | +-- copied from `Std.Iterators.Types.FilterMap.instIterator` at the time of writing this test |
| 73 | +-- `_aux_1` refers to the first field of the instance `fooAux` |
| 74 | +open Std Iterators in |
| 75 | +universe w w' w'' in |
| 76 | +instance fooAux {α β γ : Type w} {m : Type w → Type w'} {n : Type w → Type w''} [Monad n] |
| 77 | + [Iterator α m β] {lift : ⦃α : Type w⦄ → m α → n α} {f : β → PostconditionT n γ} : |
| 78 | + Iterator (Types.Map α m n lift f) n γ := |
| 79 | + inferInstanceAs <| Iterator (Types.FilterMap α m n lift _) n γ |
| 80 | + |
| 81 | +/-- |
| 82 | +warning: `fooAux._aux_1` refers to an auto-generated auxiliary declaration. These are not stable across refactors; consider using a different approach. |
| 83 | +
|
| 84 | +Note: This linter can be disabled with `set_option linter.auxLemma false` |
| 85 | +-/ |
| 86 | +#guard_msgs(drop info,warning) in |
| 87 | +#check fooAux._aux_1 |
| 88 | + |
| 89 | +-- minimised from `BitVec.lt_of_msb_false_of_msb_true` at the time of writing this test |
| 90 | +structure Foo where |
| 91 | + data : Nat |
| 92 | + |
| 93 | +def Foo.bar (_x : Foo) : Bool := false |
| 94 | + |
| 95 | +instance : LT (Foo) := ⟨fun x y ↦ x.data < y.data⟩ |
| 96 | + |
| 97 | +axiom silentSorry {α} : α |
| 98 | +@[simp] |
| 99 | +theorem fooBar {x y : Foo} (_hx : x.bar = false) (_hy : y.bar = true) : x < y := silentSorry |
| 100 | +/-- |
| 101 | +warning: `fooBar._simp_1` refers to an auto-generated auxiliary declaration. These are not stable across refactors; consider using a different approach. |
| 102 | +
|
| 103 | +Note: This linter can be disabled with `set_option linter.auxLemma false` |
| 104 | +-/ |
| 105 | +#guard_msgs in |
| 106 | +example {x y : Foo} := fooBar._simp_1 (x := x) (y := y) |
0 commit comments