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.
37 lines
884 B
Text
37 lines
884 B
Text
/-! 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⟩
|