lean4-htt/tests/elab/nonClassInstance.lean
MoritzBeroRoos 48729abf4a
feat: better error messages and more complete logic for checkImpossibleInstance and checkNonClassInstance (#13550)
This PR improves the logic and performance of the
`checkImpossibleInstance` function to detect more arguments that are
impossible to
infer for typeclass synthesis. It also improves the formatting of the
error messages for `checkImpossibleInstance` and `checkNonClassInstance`
to be more readable.

This integrates [independent (temporarily merged)
work](https://github.com/leanprover-community/batteries/pull/1717) on
these former Batteries linters which has been reverted in Batteries
because of the unprecedented upstreaming of these linters.

We also disable these checks (if the declaration contains any `sorry`s
so that partially completing instances is still possible without being
terrorized by obvious errors. We also swap the order of the checks, such
that now `checkNonClassInstance` is called first - it would be wrong to
warn for synthesis impossibility if the return type isn't even
synthesizable because it is not class-valued.

Co-authored by: @thorimur

---------

Co-authored-by: Wojciech Rozowski <wojciech@lean-fro.org>
2026-05-14 09:40:03 +00:00

47 lines
1.1 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.

/-! Registering an instance whose target type is not a class should warn. -/
structure Foo where
x : Nat
class Bar where
x : Nat
/--
error: The declaration `instFoo` should not be an instance as its return type `Foo` is not a type class.
-/
#guard_msgs in
instance instFoo : Foo := ⟨0⟩
-- Applying @[instance] to a non-class def should also warn
def instFoo2 : Foo := ⟨1⟩
/--
error: The declaration `instFoo2` should not be an instance as its return type `Foo` is not a type class.
-/
#guard_msgs in
attribute [instance] instFoo2
-- No warning for a proper class instance
#guard_msgs in
instance : Bar := ⟨0⟩
-- No warning for a class instance with parameters
class Baz (α : Type) where
x : α
#guard_msgs in
instance : Baz Nat := ⟨0⟩
-- Warning for non-class with parameters
structure Qux (α : Type) where
x : α
/--
error: The declaration `instQux` should not be an instance as its return type `Qux Nat` is not a type class.
-/
#guard_msgs in
instance instQux : Qux Nat := ⟨0⟩
/-- warning: declaration uses `sorry` -/
#guard_msgs in
instance bad : ∀ n : Nat, sorry := by sorry