lean4-htt/tests/elab/variable.lean
Michael Rothgang fe3ba4dc4c
fix: make the omit, unusedSectionVars and loopingSimpArgs linter respect linter.all (#12563)
This PR makes the `omit`, `unusedSectionVars` and `loopingSimpArgs`
linters respect the `linter.all` option:
when `linter.all` is set to false (and the respective linter option is
unset), the linter should not report errors.

Similarly to #12559, these linters should honour the linter.all flag
being set to false. These are all remaining occurrences of this pattern.

This fixes an issue analogous to #12559.
This PR and #12560 fix all occurrences of this pattern. (The only
question is around `RCases.linter.unusedRCasesPattern`: should this also
respect this? I have left this alone for now.)

Co-authored-by: fiforeach <249703130+fiforeach@users.noreply.github.com>
2026-03-09 11:58:02 +00:00

193 lines
5.5 KiB
Text
Raw Permalink 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.

/-! # Basic section variable tests -/
set_option linter.unusedSectionVars true
/-! Directly referenced variables should be included. -/
variable {n : Nat} in
theorem t1 : n = n := by induction n <;> rfl
/-! Variables mentioned only in the body should not be included. -/
variable {n : Nat} in
/-- error: Unknown identifier `n` -/
#guard_msgs in
theorem t2 : ∃ (n : Nat), n = n := by exists n
/-! Variables transitively mentioned should be included. -/
variable {n : Nat} (h : n = n) in
theorem t3 : h = h := rfl
/-! Instance variables mentioning only included variables should be included. -/
variable {α : Type} [ToString α] in
theorem t4 (a : α) : a = a := let _ := toString a; rfl
/-! Instance variables not mentioning only included variables should not be included. -/
variable {α β : Type} [Coe α β] in
/--
error: don't know how to synthesize placeholder
context:
α : Type
a : α
⊢ a = a
-/
#guard_msgs in
theorem t5 (a : α) : a = a := _
/-! Accidentally included variables should be warned for. -/
variable {α : Type} [ToString α] in
/--
warning: automatically included section variable(s) unused in theorem `t6`:
[ToString α]
consider restructuring your `variable` declarations so that the variables are not in scope or explicitly omit them:
omit [ToString α] in theorem ...
Note: This linter can be disabled with `set_option linter.unusedSectionVars false`
-/
#guard_msgs in
theorem t6 (a : α) : a = a := rfl
/-! `include` should always include. -/
variable {n : Nat} in
include n in
theorem t7 : ∃ (n : Nat), n = n := by exists n
/-! traversal order bug broke instance inclusion -/
variable {M N : Type} (r : N → N → Prop)
class IsTrans (N : Type) (r : N → N → Prop) : Prop
variable [IsTrans N r] {a b c d : N}
/--
warning: automatically included section variable(s) unused in theorem `act_rel_of_rel_of_act_rel`:
[IsTrans N r]
consider restructuring your `variable` declarations so that the variables are not in scope or explicitly omit them:
omit [IsTrans N r] in theorem ...
Note: This linter can be disabled with `set_option linter.unusedSectionVars false`
-/
#guard_msgs in
theorem act_rel_of_rel_of_act_rel (ab : r a b) : r a b := ab
/-! More complex include case, instance should be included via `f`. -/
class EquivLike (F : Type) (α β : Type) : Type
variable {F : Type} [EquivLike F α β] (f : F) in
include f in
theorem MulEquiv.decompositionMonoid (_b : β) : α = α :=
let _ : EquivLike F α β := inferInstance; let _ := f; rfl
/-- info: MulEquiv.decompositionMonoid {α β F : Type} [EquivLike F α β] (f : F) (_b : β) : α = α -/
#guard_msgs in
#check MulEquiv.decompositionMonoid
section
/-! `omit` -/
variable [ToString α] [ToString β]
/--
error: failed to synthesize instance of type class
ToString α
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
-/
#guard_msgs in
omit [ToString α] in
theorem t8 (a : α) (b : β) : True :=
let _ := toString a; let _ := toString b; trivial
/--
error: failed to synthesize instance of type class
ToString β
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
-/
#guard_msgs in
omit [ToString β] in
theorem t9 (a : α) (b : β) : True :=
let _ := toString a; let _ := toString b; trivial
/--
error: failed to synthesize instance of type class
ToString α
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
---
error: failed to synthesize instance of type class
ToString β
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
-/
#guard_msgs in
omit [ToString _] in
theorem t10 (a : α) (b : β) : True :=
let _ := toString a; let _ := toString b; trivial
end
/-! illegal `omit`s -/
/-- error: invalid 'omit', `α` has not been declared in the current scope -/
#guard_msgs in
variable (a : α) in
omit α in
theorem t11 (a : α) : True := trivial
/-- error: cannot omit referenced section variable `α` -/
#guard_msgs in
variable (α : Type) in
omit α in
theorem t12 (a : α) : True := trivial
/-- error: cannot omit referenced section variable `inst✝` -/
#guard_msgs in
variable [ToString α] in
omit [ToString α] in
theorem t13 (a : α) : toString a = toString a := rfl
set_option pp.mvars false in
/--
error: Application type mismatch: The argument
True
has type
Prop
of sort `Type` but is expected to have type
Type _
of sort `Type (_ + 1)` in the application
ToString True
-/
#guard_msgs in
omit [ToString True]
/-- error: `[ToString Nat]` did not match any variables in the current scope -/
#guard_msgs in
omit [ToString Nat]
/-! `omit` can also be used to revert an `include` -/
variable (α : Type) in
include α in
omit α in
theorem t14 : True := trivial
/--
warning: automatically included section variable(s) unused in theorem `t15`:
α
consider restructuring your `variable` declarations so that the variables are not in scope or explicitly omit them:
omit α in theorem ...
Note: This linter can be disabled with `set_option linter.unusedSectionVars false`
-/
#guard_msgs in
variable (α : Type) in
include α in
omit α in
include α in
theorem t15 : True := trivial
/-! But you probably shouldn't use it -/
set_option linter.omit true in
/--
warning: `omit` should be avoided in favor of restructuring your `variable` declarations
Note: This linter can be disabled with `set_option linter.omit false`
-/
#guard_msgs in
variable (α : Type) in
include α in
omit α in
theorem t16 : True := trivial