Skip to content

Commit 537bb27

Browse files
committed
add code to handle new-style rustc errors
These errors are available on nightly builds (or will be soon), but only (at the moment) when enabled via environment variable. They will become the default at some point in the future. In this commit we match on the `-->`, but after that we have to scroll the compilation window to make the error visible. One shortcoming is that if you enter the window and click on the filename/line-number, then the "next-error-hook" doesn't seem to run. (If you click at the start of the line, it does.) It may be possible to tweak the "hyperlink" here to make that work more smoothly, or perhaps add a hook somewhere else.
1 parent b23efef commit 537bb27

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

rust-mode.el

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,16 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
13811381
"Specifications for matching errors in rustc invocations.
13821382
See `compilation-error-regexp-alist' for help on their format.")
13831383

1384+
(defvar rustc-new-compilation-regexps
1385+
(let ((file "\\([^\n]+\\)")
1386+
(start-line "\\([0-9]+\\)")
1387+
(start-col "\\([0-9]+\\)"))
1388+
(let ((re (concat "^ *--> " file ":" start-line ":" start-col ; --> 1:2:3
1389+
)))
1390+
(cons re '(1 2 3))))
1391+
"Specifications for matching errors in rustc invocations (new style).
1392+
See `compilation-error-regexp-alist' for help on their format.")
1393+
13841394
;; Match test run failures and panics during compilation as
13851395
;; compilation warnings
13861396
(defvar cargo-compilation-regexps
@@ -1390,13 +1400,38 @@ See `compilation-error-regexp-alist' for help on their format.")
13901400

13911401
(eval-after-load 'compile
13921402
'(progn
1403+
(add-to-list 'compilation-error-regexp-alist-alist
1404+
(cons 'rustc-new rustc-new-compilation-regexps))
1405+
(add-to-list 'compilation-error-regexp-alist 'rustc-new)
13931406
(add-to-list 'compilation-error-regexp-alist-alist
13941407
(cons 'rustc rustc-compilation-regexps))
13951408
(add-to-list 'compilation-error-regexp-alist 'rustc)
13961409
(add-to-list 'compilation-error-regexp-alist-alist
13971410
(cons 'cargo cargo-compilation-regexps))
13981411
(add-to-list 'compilation-error-regexp-alist 'cargo)))
13991412

1413+
(defun rustc-scroll-down-after-next-error ()
1414+
"In the new style error messages, the regular expression
1415+
matches on the file name (which appears after `-->`), but the
1416+
start of the error appears a few lines earlier. This hook runs
1417+
after `M-x next-error`; it simply scrolls down a few lines in
1418+
the compilation window until the top of the error is visible."
1419+
(save-selected-window
1420+
(when (eq major-mode 'rust-mode)
1421+
(select-window (get-buffer-window next-error-last-buffer))
1422+
(when (save-excursion
1423+
(beginning-of-line)
1424+
(looking-at " *-->"))
1425+
(let ((start-of-error
1426+
(save-excursion
1427+
(beginning-of-line)
1428+
(while (not (looking-at "^[a-z]+:"))
1429+
(forward-line -1))
1430+
(point))))
1431+
(set-window-start (selected-window) start-of-error))))))
1432+
1433+
(add-hook 'next-error-hook 'rustc-scroll-down-after-next-error)
1434+
14001435
;;; Functions to submit (parts of) buffers to the rust playpen, for
14011436
;;; sharing.
14021437
(defun rust-playpen-region (begin end)

0 commit comments

Comments
 (0)