unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body
@ 2024-02-14 11:16 Konstantin Kharlamov
  2024-02-14 15:23 ` Eli Zaretskii
  0 siblings, 1 reply; 11+ messages in thread
From: Konstantin Kharlamov @ 2024-02-14 11:16 UTC (permalink / raw)
  To: 69120

I have many functions that are only useful inside a specific mode. So I
group them with `use-package`, by defining them inside `:init` or
`:config` section, depending on the use.

It turns out, byte-compiler does not consider such functions to be
defined and prints a warning about it.

# Steps to reproduce (in terms of terminal commands)

    λ cat test.el
    ;;; -*- lexical-binding: t -*-
    (use-package prog-mode
      :init
      (defun hello()
        (print "hello"))
      (hello))
    λ emacs -batch -f batch-byte-compile test.el

    In end of data:
    test.el:6:4: Warning: the function ‘hello’ is not known to be defined.

## Expected

File gets compiled with no warnings

## Actual

There's a warning

    test.el:6:4: Warning: the function ‘hello’ is not known to be defined.

# Additional information

Emacs version: commit d4d5830f8a0 built two weeks ago from master.





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body
  2024-02-14 11:16 bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body Konstantin Kharlamov
@ 2024-02-14 15:23 ` Eli Zaretskii
  2024-02-14 15:28   ` Konstantin Kharlamov
  0 siblings, 1 reply; 11+ messages in thread
From: Eli Zaretskii @ 2024-02-14 15:23 UTC (permalink / raw)
  To: Konstantin Kharlamov; +Cc: 69120

> From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> Date: Wed, 14 Feb 2024 14:16:09 +0300
> 
> I have many functions that are only useful inside a specific mode. So I
> group them with `use-package`, by defining them inside `:init` or
> `:config` section, depending on the use.
> 
> It turns out, byte-compiler does not consider such functions to be
> defined and prints a warning about it.
> 
> # Steps to reproduce (in terms of terminal commands)
> 
>     λ cat test.el
>     ;;; -*- lexical-binding: t -*-
>     (use-package prog-mode
>       :init
>       (defun hello()
>         (print "hello"))
>       (hello))
>     λ emacs -batch -f batch-byte-compile test.el
> 
>     In end of data:
>     test.el:6:4: Warning: the function ‘hello’ is not known to be defined.
> 
> ## Expected
> 
> File gets compiled with no warnings
> 
> ## Actual
> 
> There's a warning
> 
>     test.el:6:4: Warning: the function ‘hello’ is not known to be defined.

What do you get if you macro-expand your code?





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body
  2024-02-14 15:23 ` Eli Zaretskii
@ 2024-02-14 15:28   ` Konstantin Kharlamov
  2024-02-14 16:07     ` Eli Zaretskii
  0 siblings, 1 reply; 11+ messages in thread
From: Konstantin Kharlamov @ 2024-02-14 15:28 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 69120

On Wed, 2024-02-14 at 17:23 +0200, Eli Zaretskii wrote:
> > From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> > Date: Wed, 14 Feb 2024 14:16:09 +0300
> > 
> > I have many functions that are only useful inside a specific mode.
> > So I
> > group them with `use-package`, by defining them inside `:init` or
> > `:config` section, depending on the use.
> > 
> > It turns out, byte-compiler does not consider such functions to be
> > defined and prints a warning about it.
> > 
> > # Steps to reproduce (in terms of terminal commands)
> > 
> >     λ cat test.el
> >     ;;; -*- lexical-binding: t -*-
> >     (use-package prog-mode
> >       :init
> >       (defun hello()
> >         (print "hello"))
> >       (hello))
> >     λ emacs -batch -f batch-byte-compile test.el
> > 
> >     In end of data:
> >     test.el:6:4: Warning: the function ‘hello’ is not known to be
> > defined.
> > 
> > ## Expected
> > 
> > File gets compiled with no warnings
> > 
> > ## Actual
> > 
> > There's a warning
> > 
> >     test.el:6:4: Warning: the function ‘hello’ is not known to be
> > defined.
> 
> What do you get if you macro-expand your code?

Wrapping the above code to a `(macroexpand …)` gives a:

   "hello"
   nil





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body
  2024-02-14 15:28   ` Konstantin Kharlamov
@ 2024-02-14 16:07     ` Eli Zaretskii
  2024-02-14 16:10       ` Konstantin Kharlamov
  0 siblings, 1 reply; 11+ messages in thread
From: Eli Zaretskii @ 2024-02-14 16:07 UTC (permalink / raw)
  To: Konstantin Kharlamov; +Cc: 69120

> From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> Cc: 69120@debbugs.gnu.org
> Date: Wed, 14 Feb 2024 18:28:15 +0300
> 
> > >     test.el:6:4: Warning: the function ‘hello’ is not known to be
> > > defined.
> > 
> > What do you get if you macro-expand your code?
> 
> Wrapping the above code to a `(macroexpand …)` gives a:
> 
>    "hello"
>    nil

You need to quote the form you pass to macroexpand, to get the
expansion, I think.





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body
  2024-02-14 16:07     ` Eli Zaretskii
@ 2024-02-14 16:10       ` Konstantin Kharlamov
  2024-02-14 16:38         ` Eli Zaretskii
  0 siblings, 1 reply; 11+ messages in thread
From: Konstantin Kharlamov @ 2024-02-14 16:10 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 69120

On Wed, 2024-02-14 at 18:07 +0200, Eli Zaretskii wrote:
> > From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> > Cc: 69120@debbugs.gnu.org
> > Date: Wed, 14 Feb 2024 18:28:15 +0300
> >
> > > >     test.el:6:4: Warning: the function ‘hello’ is not known to
> > > > be
> > > > defined.
> > >
> > > What do you get if you macro-expand your code?
> >
> > Wrapping the above code to a `(macroexpand …)` gives a:
> >
> >    "hello"
> >    nil
>
> You need to quote the form you pass to macroexpand, to get the
> expansion, I think.

Oh indeed you're right!

It gives me the following:

(progn (defvar use-package--warning196 #'(lambda (keyword err) (let ((msg (format "%s/%s: %s" 'prog-mode keyword (error-message-string err)))) (display-warning 'use-package msg :error)))) (condition-case-unless-debug err (progn (condition-case-unless-debug err (progn (defun hello nil (print "hello")) (hello)) (error (funcall use-package--warning196 :init err))) (if (not (require 'prog-mode nil t)) (display-warning 'use-package (format "Cannot load %s" 'prog-mode) :error))) (error (funcall use-package--warning196 :catch err))))





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body
  2024-02-14 16:10       ` Konstantin Kharlamov
@ 2024-02-14 16:38         ` Eli Zaretskii
  2024-02-14 16:58           ` Konstantin Kharlamov
  0 siblings, 1 reply; 11+ messages in thread
From: Eli Zaretskii @ 2024-02-14 16:38 UTC (permalink / raw)
  To: Konstantin Kharlamov; +Cc: 69120

> From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> Cc: 69120@debbugs.gnu.org
> Date: Wed, 14 Feb 2024 19:10:54 +0300
> 
> On Wed, 2024-02-14 at 18:07 +0200, Eli Zaretskii wrote:
> > > From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> > > Cc: 69120@debbugs.gnu.org
> > > Date: Wed, 14 Feb 2024 18:28:15 +0300
> > >
> > > > >     test.el:6:4: Warning: the function ‘hello’ is not known to
> > > > > be
> > > > > defined.
> > > >
> > > > What do you get if you macro-expand your code?
> > >
> > > Wrapping the above code to a `(macroexpand …)` gives a:
> > >
> > >    "hello"
> > >    nil
> >
> > You need to quote the form you pass to macroexpand, to get the
> > expansion, I think.
> 
> Oh indeed you're right!
> 
> It gives me the following:
> 
> (progn (defvar use-package--warning196 #'(lambda (keyword err) (let ((msg (format "%s/%s: %s" 'prog-mode keyword (error-message-string err)))) (display-warning 'use-package msg :error)))) (condition-case-unless-debug err (progn (condition-case-unless-debug err (progn (defun hello nil (print "hello")) (hello)) (error (funcall use-package--warning196 :init err))) (if (not (require 'prog-mode nil t)) (display-warning 'use-package (format "Cannot load %s" 'prog-mode) :error))) (error (funcall use-package--warning196 :catch err))))

And that doesn't explain the warning?





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body
  2024-02-14 16:38         ` Eli Zaretskii
@ 2024-02-14 16:58           ` Konstantin Kharlamov
  2024-02-15  3:57             ` Konstantin Kharlamov
  0 siblings, 1 reply; 11+ messages in thread
From: Konstantin Kharlamov @ 2024-02-14 16:58 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 69120

On Wed, 2024-02-14 at 18:38 +0200, Eli Zaretskii wrote:
> > From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> > Cc: 69120@debbugs.gnu.org
> > Date: Wed, 14 Feb 2024 19:10:54 +0300
> >
> > On Wed, 2024-02-14 at 18:07 +0200, Eli Zaretskii wrote:
> > > > From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> > > > Cc: 69120@debbugs.gnu.org
> > > > Date: Wed, 14 Feb 2024 18:28:15 +0300
> > > >
> > > > > >     test.el:6:4: Warning: the function ‘hello’ is not known
> > > > > > to
> > > > > > be
> > > > > > defined.
> > > > >
> > > > > What do you get if you macro-expand your code?
> > > >
> > > > Wrapping the above code to a `(macroexpand …)` gives a:
> > > >
> > > >    "hello"
> > > >    nil
> > >
> > > You need to quote the form you pass to macroexpand, to get the
> > > expansion, I think.
> >
> > Oh indeed you're right!
> >
> > It gives me the following:
> >
> > (progn (defvar use-package--warning196 #'(lambda (keyword err) (let
> > ((msg (format "%s/%s: %s" 'prog-mode keyword (error-message-string
> > err)))) (display-warning 'use-package msg :error)))) (condition-
> > case-unless-debug err (progn (condition-case-unless-debug err
> > (progn (defun hello nil (print "hello")) (hello)) (error (funcall
> > use-package--warning196 :init err))) (if (not (require 'prog-mode
> > nil t)) (display-warning 'use-package (format "Cannot load %s"
> > 'prog-mode) :error))) (error (funcall use-package--warning196
> > :catch err))))
>
> And that doesn't explain the warning?

Mhm… Well, that does reduce the testcase to this code:

    ;;; -*- lexical-binding: t -*-
    (progn
      (defun hello()
        (print "hello"))
      (hello))

So the function is defined in the same visibility scope as where it's used, right
before its use, but byte-compiler apparently doesn't see that.





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body
  2024-02-14 16:58           ` Konstantin Kharlamov
@ 2024-02-15  3:57             ` Konstantin Kharlamov
  2024-02-15  4:00               ` Konstantin Kharlamov
  0 siblings, 1 reply; 11+ messages in thread
From: Konstantin Kharlamov @ 2024-02-15  3:57 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 69120

On Wed, 2024-02-14 at 19:58 +0300, Konstantin Kharlamov wrote:
> Mhm… Well, that does reduce the testcase to this code:
> 
>     ;;; -*- lexical-binding: t -*-
>     (progn
>       (defun hello()
>         (print "hello"))
>       (hello))
> 
> So the function is defined in the same visibility scope as where it's
> used, right
> before its use, but byte-compiler apparently doesn't see that.

Turns out this false-positive is even more common than I thought.

As upstream Emacs deprecated `defadvice`, I'm porting `lsp-mode` plugin
to an `advice-add`. They use it as a debugging facility, and there's an
"advicing" call inside a `(defun …)`, and the function is defined
inside the same `(defun …)` as well. And it also triggers the same
warning. In terms of minimal testcase:

   ;;; -*- lexical-binding: t -*-
   (defun foo ()
     (defun hello()
       (print "hello"))
     (hello))
   





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body
  2024-02-15  3:57             ` Konstantin Kharlamov
@ 2024-02-15  4:00               ` Konstantin Kharlamov
  2024-02-15  6:51                 ` Eli Zaretskii
  0 siblings, 1 reply; 11+ messages in thread
From: Konstantin Kharlamov @ 2024-02-15  4:00 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 69120

On Thu, 2024-02-15 at 06:57 +0300, Konstantin Kharlamov wrote:
> On Wed, 2024-02-14 at 19:58 +0300, Konstantin Kharlamov wrote:
> > Mhm… Well, that does reduce the testcase to this code:
> > 
> >     ;;; -*- lexical-binding: t -*-
> >     (progn
> >       (defun hello()
> >         (print "hello"))
> >       (hello))
> > 
> > So the function is defined in the same visibility scope as where
> > it's
> > used, right
> > before its use, but byte-compiler apparently doesn't see that.
> 
> Turns out this false-positive is even more common than I thought.
> 
> As upstream Emacs deprecated `defadvice`, I'm porting `lsp-mode`
> plugin
> to an `advice-add`. They use it as a debugging facility, and there's
> an
> "advicing" call inside a `(defun …)`, and the function is defined
> inside the same `(defun …)` as well. And it also triggers the same
> warning. In terms of minimal testcase:
> 
>    ;;; -*- lexical-binding: t -*-
>    (defun foo ()
>      (defun hello()
>        (print "hello"))
>      (hello))

An interesting fact: inserting a `(declare-function hello nil)` after a
`defun` suppresses the warning. So I guess `defun` should work somehow
similarly to `declare-function`.





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body
  2024-02-15  4:00               ` Konstantin Kharlamov
@ 2024-02-15  6:51                 ` Eli Zaretskii
  2024-02-15 15:21                   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 11+ messages in thread
From: Eli Zaretskii @ 2024-02-15  6:51 UTC (permalink / raw)
  To: Konstantin Kharlamov, Stefan Monnier; +Cc: 69120

> From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> Cc: 69120@debbugs.gnu.org
> Date: Thu, 15 Feb 2024 07:00:40 +0300
> 
> On Thu, 2024-02-15 at 06:57 +0300, Konstantin Kharlamov wrote:
> > On Wed, 2024-02-14 at 19:58 +0300, Konstantin Kharlamov wrote:
> > > Mhm… Well, that does reduce the testcase to this code:
> > > 
> > >     ;;; -*- lexical-binding: t -*-
> > >     (progn
> > >       (defun hello()
> > >         (print "hello"))
> > >       (hello))
> > > 
> > > So the function is defined in the same visibility scope as where
> > > it's
> > > used, right
> > > before its use, but byte-compiler apparently doesn't see that.
> > 
> > Turns out this false-positive is even more common than I thought.
> > 
> > As upstream Emacs deprecated `defadvice`, I'm porting `lsp-mode`
> > plugin
> > to an `advice-add`. They use it as a debugging facility, and there's
> > an
> > "advicing" call inside a `(defun …)`, and the function is defined
> > inside the same `(defun …)` as well. And it also triggers the same
> > warning. In terms of minimal testcase:
> > 
> >    ;;; -*- lexical-binding: t -*-
> >    (defun foo ()
> >      (defun hello()
> >        (print "hello"))
> >      (hello))
> 
> An interesting fact: inserting a `(declare-function hello nil)` after a
> `defun` suppresses the warning. So I guess `defun` should work somehow
> similarly to `declare-function`.

Perhaps Stefan (CC'ed) will have some comments or advice.





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body
  2024-02-15  6:51                 ` Eli Zaretskii
@ 2024-02-15 15:21                   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 0 replies; 11+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-02-15 15:21 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 69120, Konstantin Kharlamov

>> >    ;;; -*- lexical-binding: t -*-
>> >    (defun foo ()
>> >      (defun hello()
>> >        (print "hello"))
>> >      (hello))
>> 
>> An interesting fact: inserting a `(declare-function hello nil)` after a
>> `defun` suppresses the warning. So I guess `defun` should work somehow
>> similarly to `declare-function`.
>
> Perhaps Stefan (CC'ed) will have some comments or advice.

Both `defun` and `declare-function` work in unsatisfactory ways in this
respect.

Any `declare-function` anywhere in the file will silence all warnings
about this function in the whole file, which is too lax.

`defun` in contrast only silences the warnings if it's at the top-level,
which is too restrictive.

We should make them both work a bit more like `defvar`.


        Stefan






^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-02-15 15:21 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-14 11:16 bug#69120: Spurious "function is not known to be defined" if defined in `use-package` body Konstantin Kharlamov
2024-02-14 15:23 ` Eli Zaretskii
2024-02-14 15:28   ` Konstantin Kharlamov
2024-02-14 16:07     ` Eli Zaretskii
2024-02-14 16:10       ` Konstantin Kharlamov
2024-02-14 16:38         ` Eli Zaretskii
2024-02-14 16:58           ` Konstantin Kharlamov
2024-02-15  3:57             ` Konstantin Kharlamov
2024-02-15  4:00               ` Konstantin Kharlamov
2024-02-15  6:51                 ` Eli Zaretskii
2024-02-15 15:21                   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).