unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#67883: 29.1.90; Native compiler hangs when compiling code with circular objects
@ 2023-12-18 17:22 hokomo via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-12-19 12:48 ` Andrea Corallo
  0 siblings, 1 reply; 7+ messages in thread
From: hokomo via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-12-18 17:22 UTC (permalink / raw)
  To: 67883

Hello,

The Emacs Lisp native compiler goes into an infinite loop when 
compiling code that contains circular objects.

For example, put the following into a file and use M-x 
emacs-lisp-native-compile; it should hang:

  (defun test1 ()
    '#1=(1 2 3 . #1#))

It seems like this only happens for certain top-level forms, such 
as `defun' (which may be expected behavior, due to the way the 
file compiler processes top-level forms). For example, this 
doesn't hang:

  (lambda ()
    '#1=(1 2 3 . #1#))

Here's a more interesting example, which is how I stumbled upon 
this issue in the first place:

  (defun cycle-pure (list)
    (declare (pure t) (side-effect-free t))
    (let ((newlist (append list ())))
      (nconc newlist newlist)))

  (defun test2 ()
    (cycle-pure '(1 2 3)))

(The definition of `cycle-pure' is copied from the `-cycle' 
function from the dash.el package.)

If `cycle-pure' is not yet defined when compiling, the compiler 
doesn't hang.

However, if `cycle-pure' is defined (e.g. via M-x eval-defun), the 
compiler hangs. This is weird because, unlike `test1' above, 
`test2' doesn't contain a circular list itself; it only builds it 
at run-time.

In contrast, this example doesn't hang, regardless of whether 
`cycle' is defined or not (note the removed declarations):

  (defun cycle (list)
    (let ((newlist (append list ())))
      (nconc newlist newlist)))

  (defun test3 ()
    (cycle '(1 2 3)))

My conjecture is that the `pure' and `side-effect-free' 
declarations within `cycle-pure' (which I assume are picked up 
only once the function definition is loaded) allow the compiler to 
do some constant folding when it sees the expression `(cycle-pure 
'(1 2 3))'. This results in the compiler manipulating a circular 
list at compile-time, just like in `test1', and leads to a hang.

Kind regards,
hokomo





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

* bug#67883: 29.1.90; Native compiler hangs when compiling code with circular objects
  2023-12-18 17:22 bug#67883: 29.1.90; Native compiler hangs when compiling code with circular objects hokomo via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-12-19 12:48 ` Andrea Corallo
  2023-12-19 13:29   ` Andrea Corallo
  0 siblings, 1 reply; 7+ messages in thread
From: Andrea Corallo @ 2023-12-19 12:48 UTC (permalink / raw)
  To: 67883; +Cc: hokomo

hokomo via "Bug reports for GNU Emacs, the Swiss army knife of text
editors" <bug-gnu-emacs@gnu.org> writes:

> Hello,
>
> The Emacs Lisp native compiler goes into an infinite loop when
> compiling code that contains circular objects.
>
> For example, put the following into a file and use M-x
> emacs-lisp-native-compile; it should hang:
>
>  (defun test1 ()
>    '#1=(1 2 3 . #1#))
>
> It seems like this only happens for certain top-level forms, such as
> `defun' (which may be expected behavior, due to the way the file
> compiler processes top-level forms). For example, this doesn't hang:
>
>  (lambda ()
>    '#1=(1 2 3 . #1#))
>
> Here's a more interesting example, which is how I stumbled upon this
> issue in the first place:
>
>  (defun cycle-pure (list)
>    (declare (pure t) (side-effect-free t))
>    (let ((newlist (append list ())))
>      (nconc newlist newlist)))
>
>  (defun test2 ()
>    (cycle-pure '(1 2 3)))
>
> (The definition of `cycle-pure' is copied from the `-cycle' function
> from the dash.el package.)
>
> If `cycle-pure' is not yet defined when compiling, the compiler
> doesn't hang.
>
> However, if `cycle-pure' is defined (e.g. via M-x eval-defun), the
> compiler hangs. This is weird because, unlike `test1' above, `test2'
> doesn't contain a circular list itself; it only builds it at run-time.
>
> In contrast, this example doesn't hang, regardless of whether `cycle'
> is defined or not (note the removed declarations):
>
>  (defun cycle (list)
>    (let ((newlist (append list ())))
>      (nconc newlist newlist)))
>
>  (defun test3 ()
>    (cycle '(1 2 3)))
>
> My conjecture is that the `pure' and `side-effect-free' declarations
> within `cycle-pure' (which I assume are picked up only once the
> function definition is loaded) allow the compiler to do some constant
> folding when it sees the expression `(cycle-pure '(1 2 3))'. This
> results in the compiler manipulating a circular list at compile-time,
> just like in `test1', and leads to a hang.
>
> Kind regards,
> hokomo

I'm pretty sure I've investigated this in the past.  IIRC this is due
some cl- function hanging on a circular list.  Can't find the bug number
ATM.

  Andrea





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

* bug#67883: 29.1.90; Native compiler hangs when compiling code with circular objects
  2023-12-19 12:48 ` Andrea Corallo
@ 2023-12-19 13:29   ` Andrea Corallo
  2023-12-24 19:41     ` Stefan Kangas
  0 siblings, 1 reply; 7+ messages in thread
From: Andrea Corallo @ 2023-12-19 13:29 UTC (permalink / raw)
  To: 67883; +Cc: hokomo

Andrea Corallo <acorallo@gnu.org> writes:

> hokomo via "Bug reports for GNU Emacs, the Swiss army knife of text
> editors" <bug-gnu-emacs@gnu.org> writes:
>
>> Hello,
>>
>> The Emacs Lisp native compiler goes into an infinite loop when
>> compiling code that contains circular objects.
>>
>> For example, put the following into a file and use M-x
>> emacs-lisp-native-compile; it should hang:
>>
>>  (defun test1 ()
>>    '#1=(1 2 3 . #1#))
>>
>> It seems like this only happens for certain top-level forms, such as
>> `defun' (which may be expected behavior, due to the way the file
>> compiler processes top-level forms). For example, this doesn't hang:
>>
>>  (lambda ()
>>    '#1=(1 2 3 . #1#))
>>
>> Here's a more interesting example, which is how I stumbled upon this
>> issue in the first place:
>>
>>  (defun cycle-pure (list)
>>    (declare (pure t) (side-effect-free t))
>>    (let ((newlist (append list ())))
>>      (nconc newlist newlist)))
>>
>>  (defun test2 ()
>>    (cycle-pure '(1 2 3)))
>>
>> (The definition of `cycle-pure' is copied from the `-cycle' function
>> from the dash.el package.)
>>
>> If `cycle-pure' is not yet defined when compiling, the compiler
>> doesn't hang.
>>
>> However, if `cycle-pure' is defined (e.g. via M-x eval-defun), the
>> compiler hangs. This is weird because, unlike `test1' above, `test2'
>> doesn't contain a circular list itself; it only builds it at run-time.
>>
>> In contrast, this example doesn't hang, regardless of whether `cycle'
>> is defined or not (note the removed declarations):
>>
>>  (defun cycle (list)
>>    (let ((newlist (append list ())))
>>      (nconc newlist newlist)))
>>
>>  (defun test3 ()
>>    (cycle '(1 2 3)))
>>
>> My conjecture is that the `pure' and `side-effect-free' declarations
>> within `cycle-pure' (which I assume are picked up only once the
>> function definition is loaded) allow the compiler to do some constant
>> folding when it sees the expression `(cycle-pure '(1 2 3))'. This
>> results in the compiler manipulating a circular list at compile-time,
>> just like in `test1', and leads to a hang.
>>
>> Kind regards,
>> hokomo
>
> I'm pretty sure I've investigated this in the past.  IIRC this is due
> some cl- function hanging on a circular list.  Can't find the bug number
> ATM.
>
>   Andrea

Okay I believe this most likely is a duplicate of bug#57957.





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

* bug#67883: 29.1.90; Native compiler hangs when compiling code with circular objects
  2023-12-19 13:29   ` Andrea Corallo
@ 2023-12-24 19:41     ` Stefan Kangas
  2023-12-26  8:23       ` Andrea Corallo
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Kangas @ 2023-12-24 19:41 UTC (permalink / raw)
  To: Andrea Corallo, 67883; +Cc: hokomo

Andrea Corallo <acorallo@gnu.org> writes:

> Okay I believe this most likely is a duplicate of bug#57957.

Should the bugs be merged?





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

* bug#67883: 29.1.90; Native compiler hangs when compiling code with circular objects
  2023-12-24 19:41     ` Stefan Kangas
@ 2023-12-26  8:23       ` Andrea Corallo
  2023-12-26 15:36         ` Stefan Kangas
  0 siblings, 1 reply; 7+ messages in thread
From: Andrea Corallo @ 2023-12-26  8:23 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: 67883, hokomo

Stefan Kangas <stefankangas@gmail.com> writes:

> Andrea Corallo <acorallo@gnu.org> writes:
>
>> Okay I believe this most likely is a duplicate of bug#57957.
>
> Should the bugs be merged?

Hi Stefan,

yes I think so.

  Andrea





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

* bug#67883: 29.1.90; Native compiler hangs when compiling code with circular objects
  2023-12-26  8:23       ` Andrea Corallo
@ 2023-12-26 15:36         ` Stefan Kangas
  2023-12-27 18:48           ` hokomo via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Kangas @ 2023-12-26 15:36 UTC (permalink / raw)
  To: Andrea Corallo; +Cc: 67883, hokomo

forcemerge 57957 67883
thanks

Andrea Corallo <acorallo@gnu.org> writes:

> Stefan Kangas <stefankangas@gmail.com> writes:
>
>> Andrea Corallo <acorallo@gnu.org> writes:
>>
>>> Okay I believe this most likely is a duplicate of bug#57957.
>>
>> Should the bugs be merged?
>
> Hi Stefan,
>
> yes I think so.
>
>   Andrea

Done.





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

* bug#67883: 29.1.90; Native compiler hangs when compiling code with circular objects
  2023-12-26 15:36         ` Stefan Kangas
@ 2023-12-27 18:48           ` hokomo via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 0 replies; 7+ messages in thread
From: hokomo via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-12-27 18:48 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: 67883, Andrea Corallo


Merging the two indeed seems appropriate.

FWIW, I've read the other thread [1] and agree with Stefan's view 
that circular structures should be handled correctly by the 
compiler. I believe the example with `cycle-pure' shows that this 
is both useful and necessary, since circular structures can appear 
even without being directly present in the source code as 
literals, e.g. after compiler optimizations (which seems to be the 
case here, although I haven't confirmed it 100%).

- [1] <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=57957>

Kind regards,
hokomo





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

end of thread, other threads:[~2023-12-27 18:48 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-18 17:22 bug#67883: 29.1.90; Native compiler hangs when compiling code with circular objects hokomo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-19 12:48 ` Andrea Corallo
2023-12-19 13:29   ` Andrea Corallo
2023-12-24 19:41     ` Stefan Kangas
2023-12-26  8:23       ` Andrea Corallo
2023-12-26 15:36         ` Stefan Kangas
2023-12-27 18:48           ` hokomo 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).