lean4-htt/tests/elab/nonClassInstance.lean
Wojciech Różowski 3fc99eef10
feat: add instance validation checks in addInstance (#13389)
This PR adds two validation checks to `addInstance` that provide early
feedback for common mistakes in instance declarations:

1. **Non-class instance check**: errors when an instance target type is
not a type class. This catches the common mistake of writing `instance`
for a plain structure. Previously handled by the `nonClassInstance`
linter in Batteries (`Batteries.Tactic.Lint.TypeClass`), this is now
checked directly at declaration time.

2. **Impossible argument check**: errors when an instance has arguments
that cannot be inferred by instance synthesis. Specifically, it flags
arguments that are not instance-implicit and do not appear in any
subsequent instance-implicit argument or in the return type. Previously
such instances would be silently accepted but could never be
synthesised.

Supersedes #13237 and #13333.
2026-04-16 17:48:16 +00:00

37 lines
884 B
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.

/-! Registering an instance whose target type is not a class should warn. -/
structure Foo where
x : Nat
class Bar where
x : Nat
/-- error: instance `instFoo` target `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: instance `instFoo2` target `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: instance `instQux` target `Qux Nat` is not a type class. -/
#guard_msgs in
instance instQux : Qux Nat := ⟨0⟩