1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -08:00

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 (bug#79272).

* 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.
This commit is contained in:
Steven Allen 2025-08-19 11:12:01 -07:00 committed by Juri Linkov
parent 1a549762ed
commit f0b987c32c
3 changed files with 19 additions and 4 deletions

View file

@ -366,7 +366,8 @@ See https://doc.rust-lang.org/reference/tokens.html#suffixes.")
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 @@ See https://doc.rust-lang.org/reference/tokens.html#suffixes.")
,(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)))))))

View file

@ -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
}

View file

@ -39,6 +39,13 @@
(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