From unknown Fri Sep 12 02:09:23 2025 X-Loop: help-debbugs@gnu.org Subject: bug#79272: [PATCH] rust-ts-mode: handle invalid rust syntax without signaling Resent-From: Steven Allen Original-Sender: "Debbugs-submit" Resent-CC: dev@rjt.dev, juri@linkov.net, bug-gnu-emacs@gnu.org Resent-Date: Tue, 19 Aug 2025 18:34:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 79272 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch To: 79272@debbugs.gnu.org Cc: Randy Taylor , Juri Linkov X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: Randy Taylor , Juri Linkov Received: via spool by submit@debbugs.gnu.org id=B.175562839227267 (code B ref -1); Tue, 19 Aug 2025 18:34:01 +0000 Received: (at submit) by debbugs.gnu.org; 19 Aug 2025 18:33:12 +0000 Received: from localhost ([127.0.0.1]:54852 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uoR8x-00075j-Oc for submit@debbugs.gnu.org; Tue, 19 Aug 2025 14:33:12 -0400 Received: from lists.gnu.org ([2001:470:142::17]:52730) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uoR8s-000757-Ng for submit@debbugs.gnu.org; Tue, 19 Aug 2025 14:33:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uoR8l-0007vg-7P for bug-gnu-emacs@gnu.org; Tue, 19 Aug 2025 14:32:59 -0400 Received: from fhigh-b1-smtp.messagingengine.com ([202.12.124.152]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uoR8h-0005oL-1a for bug-gnu-emacs@gnu.org; Tue, 19 Aug 2025 14:32:58 -0400 Received: from phl-compute-03.internal (phl-compute-03.internal [10.202.2.43]) by mailfhigh.stl.internal (Postfix) with ESMTP id CF0217A02E1 for ; Tue, 19 Aug 2025 14:32:51 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-03.internal (MEProxy); Tue, 19 Aug 2025 14:32:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stebalien.com; h=cc:content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:subject:subject:to:to; s=fm2; t=1755628371; x=1755714771; bh=UoDLWiCD2pmlYAHXHJT7hLn+Pt0Ns0gA KoVJ5t05lBs=; b=PbwYz3jvl0LBBBSVm+tTO017zDtSBuKjE2PiQcbAh8otiY2Y vjsZCvbI31H+NPuJ8694bobi7/zCcN927fsXVK9ntFm6y+40FoA8UAOFBjaza1g/ OiZo+xuPj7n+ff1BlqJ7ax6UIjo1uXY9J+HgWjei6n2v96TZ1QIdlJsZV7Q+oYFW yFcH2FjwGTZ1CJEM9IuDw1dpy2umo6eyLvKrsLrhalftlDtnmDAJUUUMPe8sTagC ZW9L2NyqoGvAukqG5kTTAZbgmrz+1dgaiDRm+vGFB4Kh+i6j3DUH9hWkH11SDppl NZmuasMHirw3xjvRYPv+vHwa7Tq6N2WVm9VYhw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1755628371; x= 1755714771; bh=UoDLWiCD2pmlYAHXHJT7hLn+Pt0Ns0gAKoVJ5t05lBs=; b=m qyJkkhMDeUY86+uEqwzSDDPLv9HHXIna2lTYQnbyKya1HjXSGpQ9bUq3L1NbdVFW BUtI89P/vewX/4rsNStWcw+H70Hh4Xm0ZSkF9EofrYR7ujv5Go4Wpuxj6BA3lBQX vq/3qho5p9HFeIKNRCwR7zA3vFs/7/u9AcLGWkzVAf8Tmpf6pUl7qZZYQjj9ivwe Oekw8Oyy9mwYBshszyuCuG12GGTBpzpnEoRt+mdgLbFz0Y3HjDoc6JfqRCgIyh2d 3Pdlcz0HKVMKwIYLYMhQ+w+qsTrFMH1QvNmKLjqpHsrh6/e1q61sTofl5DA1aUqs fBM7fO/HnZY6MsYnwhQPQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgdduheeivddvucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkgggtsehmtderredttddtne cuhfhrohhmpefuthgvvhgvnhcutehllhgvnhcuoehsthgvvhgvnhesshhtvggsrghlihgv nhdrtghomheqnecuggftrfgrthhtvghrnhepkefffffgveethedtieffleffgeekvdeiie egheeftdetjeekfefhvedttdekfeffnecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomhepshhtvghvvghnsehsthgvsggrlhhivghnrdgtohhmpdhnsg gprhgtphhtthhopedupdhmohguvgepshhmthhpohhuthdprhgtphhtthhopegsuhhgqdhg nhhuqdgvmhgrtghssehgnhhurdhorhhg X-ME-Proxy: Feedback-ID: ie8a146a7:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Tue, 19 Aug 2025 14:32:51 -0400 (EDT) From: Steven Allen Date: Tue, 19 Aug 2025 11:32:49 -0700 Message-ID: <87tt236u26.fsf@stebalien.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=202.12.124.152; envelope-from=steven@stebalien.com; helo=fhigh-b1-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: 0.7 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.3 (/) --=-=-= Content-Type: text/plain Tags: patch Avoid signaling an error when encountering invalid rust syntax. The signaled error would never be propagated to the user (caught somewhere in the fontlocking logic, I think) but it would prevent a portion of the buffer from being fontified. Notes: 1. I encountered this error in `rust-ts-mode--fontify-pattern' (and reproduced it in the attached test). I applied the same fix to `rust-ts-mode--fontify-scope' to be safe, but I couldn't actually reproduce an issue there. 2. `treesit-node-parent' and `treesit-node-type' will blindly forward nil so there's no issue calling (treesit-node-type (treesit-node-parent node)) even if `node' has no parent. In GNU Emacs 31.0.50 (build 1, x86_64-pc-linux-gnu, cairo version 1.18.4) of 2025-08-16 built on Laptop Repository revision: 68657c30983afe745f9667a0c42bdf6dd7ed4890 Repository branch: merged Windowing system distributor 'The X.Org Foundation', version 11.0.12101018 System Description: Arch Linux Configured using: 'configure 'CPPFLAGS=-I/run/user/1000/build/emacs-git/src/mps-git/build/include ' 'LDFLAGS=-L/run/user/1000/build/emacs-git/src/mps-git/build/lib -Wl,-O1 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,-z,pack-relative-relocs -flto=auto' --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib --localstatedir=/var --mandir=/usr/share/man --with-gameuser=:games --with-modules --without-m17n-flt --without-selinux --without-pop --without-gconf --disable-gc-mark-trace --with-mps=yes --enable-link-time-optimization --with-native-compilation=yes --with-xinput2 --with-x-toolkit=no --without-toolkit-scroll-bars --without-xaw3d --without-gsettings --with-cairo-xcb --without-xft --with-sound=no --with-tree-sitter --without-gpm --without-compress-install '--program-transform-name=s/\([ec]tags\)/\1.emacs/' 'CFLAGS=-march=native -mtune=native -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=3 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -fomit-frame-pointer -fno-math-errno -fno-trapping-math -fno-math-errno -fno-trapping-math -flto=auto'' --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-rust-ts-mode-handle-invalid-rust-syntax-without-sign.patch >From bfb74edeaf8c94b2e5d6b2ce30e58a915348308b Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 19 Aug 2025 11:12:01 -0700 Subject: [PATCH] rust-ts-mode: handle invalid rust syntax without signaling Don't signal an error when encountering invalid rust syntax. Without this patch, invalid rust code would prevent a chunk of the buffer from being highlighted. * lisp/progmodes/rust-ts-mode.el (rust-ts-mode--fontify-scope): (rust-ts-mode--fontify-pattern): Avoid calling `string-match-p' on nil when a node is missing a parent. * test/lisp/progmodes/rust-ts-mode-resources/font-lock-no-parent.rs: Rust file that reproduces the issue. * test/lisp/progmodes/rust-ts-mode-tests.el: Test case to reproduce the issue. --- lisp/progmodes/rust-ts-mode.el | 9 +++++---- .../rust-ts-mode-resources/font-lock-no-parent.rs | 7 +++++++ test/lisp/progmodes/rust-ts-mode-tests.el | 7 +++++++ 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 test/lisp/progmodes/rust-ts-mode-resources/font-lock-no-parent.rs diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el index a5c217c0a4b..a98d621af65 100644 --- a/lisp/progmodes/rust-ts-mode.el +++ b/lisp/progmodes/rust-ts-mode.el @@ -366,7 +366,8 @@ rust-ts-mode--fontify-scope tail-p (string-match-p "\\`\\(?:use_list\\|call_expression\\|use_as_clause\\|use_declaration\\)\\'" - (treesit-node-type (treesit-node-parent (treesit-node-parent node))))) + (or (treesit-node-type (treesit-node-parent (treesit-node-parent node))) + "no_parent"))) nil) (t 'font-lock-constant-face)))) (when face @@ -387,9 +388,9 @@ 'rust-ts-mode--fontify-pattern ,(treesit-query-compile 'rust '((identifier) @id (shorthand_field_identifier) @id))))) (pcase-dolist (`(_name . ,id) captures) - (unless (string-match-p "\\`scoped_\\(?:type_\\)?identifier\\'" - (treesit-node-type - (treesit-node-parent id))) + (unless (string-match-p + "\\`scoped_\\(?:type_\\)?identifier\\'" + (or (treesit-node-type (treesit-node-parent id)) "no_parent")) (treesit-fontify-with-override (treesit-node-start id) (treesit-node-end id) 'font-lock-variable-name-face override start end))))))) diff --git a/test/lisp/progmodes/rust-ts-mode-resources/font-lock-no-parent.rs b/test/lisp/progmodes/rust-ts-mode-resources/font-lock-no-parent.rs new file mode 100644 index 00000000000..85d0ccc9bf3 --- /dev/null +++ b/test/lisp/progmodes/rust-ts-mode-resources/font-lock-no-parent.rs @@ -0,0 +1,7 @@ ++// intentionally invalid syntax ++const THING: [u8; 48] = []; + +// should recover here and highlight the text below +trait Foo() { +// ^ font-lock-keyword-face +} diff --git a/test/lisp/progmodes/rust-ts-mode-tests.el b/test/lisp/progmodes/rust-ts-mode-tests.el index d2e28dcfbd7..32d64260a87 100644 --- a/test/lisp/progmodes/rust-ts-mode-tests.el +++ b/test/lisp/progmodes/rust-ts-mode-tests.el @@ -39,6 +39,13 @@ rust-ts-test-font-lock-number (ert-font-lock-test-file (ert-resource-file "font-lock-number.rs") 'rust-ts-mode))) +(ert-deftest rust-ts-test-no-parent () + (skip-unless (treesit-ready-p 'rust)) + (let ((treesit-font-lock-level 4) + (rust-ts-mode-fontify-number-suffix-as-type t)) + (ert-font-lock-test-file (ert-resource-file "font-lock-no-parent.rs") + 'rust-ts-mode))) + (provide 'rust-ts-mode-tests) ;;; rust-ts-mode-tests.el ends here -- 2.50.1 --=-=-=-- From unknown Fri Sep 12 02:09:23 2025 X-Loop: help-debbugs@gnu.org Subject: bug#79272: [PATCH] rust-ts-mode: handle invalid rust syntax without signaling Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 21 Aug 2025 17:02:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 79272 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch To: Steven Allen Cc: Randy Taylor , 79272@debbugs.gnu.org Received: via spool by 79272-submit@debbugs.gnu.org id=B79272.175579571814085 (code B ref 79272); Thu, 21 Aug 2025 17:02:02 +0000 Received: (at 79272) by debbugs.gnu.org; 21 Aug 2025 17:01:58 +0000 Received: from localhost ([127.0.0.1]:32788 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1up8fm-0003f7-E3 for submit@debbugs.gnu.org; Thu, 21 Aug 2025 13:01:58 -0400 Received: from mout-p-202.mailbox.org ([80.241.56.172]:48034) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1up8fj-0003eh-RJ; Thu, 21 Aug 2025 13:01:56 -0400 Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4c78k20BC2z9tNm; Thu, 21 Aug 2025 19:01:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linkov.net; s=MBO0001; t=1755795706; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=+KBaVqnd+GVqTPw7L/VWu5hlhenONDOn4Az1Y7/iS9I=; b=jjHlL16Zd4hNUGHxDjlbtGVI3xNFw8b70hfo7sLzxGuEVQysuFrjyNOoL8TMjQh17cMe16 ftZhd9euLXl6Rxab+waWXzvjaf/6b3N4kkfdVmmTNqSOcOOS5HU/EXwaPzTB02Q2/ZkBml G1zwMdEafQB1+zaD40QoTVd5GOH7jGPri+XWzqn+fVRKkVHoF/YtTWt22yBhAPF1ZUhGb9 3SzhO7FvtUxPJ2Sxl4p2TV6dwLpcfK0yPTgLaBYKiLTceKvxe/OU8/o+vACZIhHweQS4+P XMEd4Mm8c269aV8SxYMCGAyC/PVxRAvrOLRleK95vfM/m2K44J1AkePnM1aA2Q== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of juri@linkov.net designates 2001:67c:2050:b231:465::202 as permitted sender) smtp.mailfrom=juri@linkov.net From: Juri Linkov In-Reply-To: <87tt236u26.fsf@stebalien.com> Organization: LINKOV.NET References: <87tt236u26.fsf@stebalien.com> Date: Thu, 21 Aug 2025 20:00:06 +0300 Message-ID: <87h5y08vah.fsf@mail.linkov.net> MIME-Version: 1.0 Content-Type: text/plain X-Rspamd-Queue-Id: 4c78k20BC2z9tNm X-Spam-Score: -0.7 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) close 79272 31.0.50 thanks > Avoid signaling an error when encountering invalid rust syntax. The > signaled error would never be propagated to the user (caught somewhere > in the fontlocking logic, I think) but it would prevent a portion of the > buffer from being fontified. > > Notes: > > 1. I encountered this error in `rust-ts-mode--fontify-pattern' (and > reproduced it in the attached test). I applied the same fix to > `rust-ts-mode--fontify-scope' to be safe, but I couldn't actually > reproduce an issue there. > 2. `treesit-node-parent' and `treesit-node-type' will blindly forward > nil so there's no issue calling (treesit-node-type (treesit-node-parent > node)) even if `node' has no parent. I couldn't even read your patch without signaling an error :-) But after applying it as a plain file, the test file opens correctly, so now pushed. Thanks.