Skip to content

Commit 06cf088

Browse files
authored
Add raw borrow operator as a contextual keyword (#586)
* Revert "Add keyword `raw` (#585)" This reverts commit 6482f56. The raw borrow operator is a weak keyword, as such, it must only be highlighted in the right context. Weak keywords: https://doc.rust-lang.org/reference/keywords.html#weak-keywords * Add `raw` borrow operator as a contextual keyword The `raw` borrow operator is a contextual keyword. Highlight it only in the context of borrows. ```rust let mut x = 5; // highlight "raw" here let a = &raw const x; let b = &raw mut x; // do not highlight "raw" here let raw = &x as *const _; let raw_mut = &x as *mut _; ``` Raw borrow operator: https://doc.rust-lang.org/reference/expressions/operator-expr.html#raw-borrow-operators Rust weak keywords: https://doc.rust-lang.org/reference/keywords.html#weak-keywords
1 parent 6482f56 commit 06cf088

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

rust-mode-tests.el

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,28 @@ this_is_not_a_string();)"
19791979
"union" font-lock-variable-name-face
19801980
"bar" font-lock-type-face)))
19811981

1982+
(ert-deftest rust-test-raw-context-sensitive ()
1983+
(rust-test-font-lock
1984+
"let raw = 7; let foo = &raw const raw; let bar = &raw mut raw;"
1985+
'("let" font-lock-keyword-face
1986+
;; The first raw is a variable name.
1987+
"raw" font-lock-variable-name-face
1988+
"let" font-lock-keyword-face
1989+
"foo" font-lock-variable-name-face
1990+
"&" rust-ampersand-face
1991+
;; The second raw is a contextual keyword.
1992+
"raw" font-lock-keyword-face
1993+
"const" font-lock-keyword-face
1994+
;; The third raw is a value.
1995+
"let" font-lock-keyword-face
1996+
"bar" font-lock-variable-name-face
1997+
"&" rust-ampersand-face
1998+
;; The fourth raw is a contextual keyword.
1999+
"raw" font-lock-keyword-face
2000+
"mut" font-lock-keyword-face
2001+
;; The fifth raw is a value.
2002+
)))
2003+
19822004
(ert-deftest indent-method-chains-no-align ()
19832005
(let ((rust-indent-method-chain nil)) (test-indent
19842006
"

rust-prog-mode.el

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ to the function arguments. When nil, `->' will be indented one level."
112112
(or space line-start)
113113
(group symbol-start "union" symbol-end)
114114
(+ space) (regexp ,rust-re-ident))))
115+
(defconst rust-re-raw
116+
(rx-to-string
117+
`(seq
118+
"&"
119+
(zero-or-more space)
120+
(group "raw")
121+
(one-or-more space)
122+
(or "const" "mut"))))
115123

116124
(defun rust-re-item-def (itype)
117125
(concat (rust-re-word itype)
@@ -188,7 +196,7 @@ See `prettify-symbols-compose-predicate'."
188196
"let" "loop"
189197
"match" "mod" "move" "mut"
190198
"priv" "pub"
191-
"raw" "ref" "return"
199+
"ref" "return"
192200
"self" "static" "struct" "super"
193201
"true" "trait" "type" "try"
194202
"use"
@@ -269,6 +277,7 @@ Does not match type annotations of the form \"foo::<\"."
269277
;; Contextual keywords
270278
("\\_<\\(default\\)[[:space:]]+fn\\_>" 1 font-lock-keyword-face)
271279
(,rust-re-union 1 font-lock-keyword-face)
280+
(,rust-re-raw 1 font-lock-keyword-face)
272281

273282
;; Special types
274283
(,(regexp-opt rust-special-types 'symbols) . font-lock-type-face)

0 commit comments

Comments
 (0)