Subject: bug#50219: 28.0.50; Provide better errors when trying to specialize on optional args in generic methods

From: Eric Abrahamsen <[email-masked]>
Date: Thu, 26 Aug 2021 16:11:51 -0700

If you write a generic method and try to specialize on an &optional
argument, like so:

--8<---------------cut here---------------start------------->8---
(cl-defgeneric testy (arg1 &optional arg2))

(cl-defmethod testy ((arg1 string) &optional (arg2 integer))
  (message "Don't do this"))
--8<---------------cut here---------------end--------------->8---

It will behave weirdly when you try to call the function with a second
argument, but won't bark at you when you evaluate this form, and during
compilation will give you the error:

Warning: reference to free variable 'integer'

I was doing this with a specialization on an EIEIO class in EBDB, and
the warning was even weirder, but never mind that.

A few things could be done here:

- the manual section on "Generic Functions" could say explicitly that
  you can't specialize on optional arguments
- `eval'ling the `defmethod' form above could raise an error directly
- the compiler could say more explicitly what the problem is

I'd favor all three of these changes! Happy to implement what I can
(maybe not the compiler part).

Eric