Subject: Re: bug#61356: 29.0.60; wrong-type-argument in treesit-indent-region when indenting a C buffer

From: Daniel Martín <[email-masked]>
Date: Sat, 25 Feb 2023 03:24:54 +0100

Eli Zaretskii <[email-masked]> writes:

>> Date: Tue, 07 Feb 2023 23:20:09 +0100
>> From:  Daniel Martín via "Bug reports for GNU Emacs,
>>  the Swiss army knife of text editors" <[email-masked]>
>> 
>> 
>> Steps to reproduce the problem:
>> 
>> emacs -Q
>> C-x C-f emacs/src/xdisp.c RET
>> M-x c-ts-mode RET
>> C-x h
>> TAB
>> 
>> Expected result:
>> 
>> The buffer is reindented.
>> 
>> Actual results:
>> 
>> The operation fails with this error:
>> 
>> Debugger entered--Lisp error: (wrong-type-argument integer-or-marker-p nil)
>>   treesit-indent-region(1 1214226)
>>   indent-region(1 1214226)
>>   indent-for-tab-command(nil)
>>   funcall-interactively(indent-for-tab-command nil)
>>   call-interactively(indent-for-tab-command nil nil)
>>   command-execute(indent-for-tab-command)
>
> Thank you for your report.
>
> Yuan and Theo, could you please look into this?  I briefly stepped
> through the code involved in the problem, and it sounds like it makes
> some assumptions regarding the nodes which are not necessarily true in
> Real Life.

c-ts-mode from the tip of the emacs-29 branch can indent xdisp.c without
failures if I apply the patch from bug#61691 and the patch attached to
this email message.

It is very slow, though, compared to c-mode.  I have a very fast machine
and it took around 10 minutes to indent the whole file, while c-mode
indented the file in 30-40 seconds.  I guess the algorithm in
treesit-indent-region doesn't scale very well for very big files like
xdisp.c.

>From d082bfb92a17f3968910182009939300ed51a3ad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= <[email-masked]>
Date: Sat, 25 Feb 2023 03:07:55 +0100
Subject: [PATCH] Check the anchor along with the offset in
 treesit-indent-region

* lisp/treesit.el (treesit-indent-region): To compute a valid column,
having a non-nil offset is not enough, we need a non-nil anchor as
well.
---
 lisp/treesit.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 045fdf21cba..1decfc3d7cf 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -1533,7 +1533,7 @@ treesit-indent-region
         (dotimes (jdx idx)
           (let ((anchor (aref meta-vec (* jdx meta-len)))
                 (offset (aref meta-vec (+ 1 (* jdx meta-len)))))
-            (when offset
+            (when (and anchor offset)
               (let ((col (save-excursion
                            (goto-char anchor)
                            (+ offset (current-column)))))
-- 
2.34.1