Skip to content

Commit 064ff63

Browse files
Merge #8940
8940: Give ‘unsafe’ semantic token modifier to unsafe traits r=Veykril a=arzg Hi! This is my first pull request that touches rust-analyzer itself beyond a search-and-replace, so please tell me if I should change anything or do anything differently. :) Co-authored-by: Aramis Razzaghipour <aramisnoah@gmail.com>
2 parents 57eedd9 + 4d4dbcf commit 064ff63

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

crates/hir/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,10 @@ impl Trait {
10851085
pub fn is_auto(self, db: &dyn HirDatabase) -> bool {
10861086
db.trait_data(self.id).is_auto
10871087
}
1088+
1089+
pub fn is_unsafe(&self, db: &dyn HirDatabase) -> bool {
1090+
db.trait_data(self.id).is_unsafe
1091+
}
10881092
}
10891093

10901094
impl HasVisibility for Trait {

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,14 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
338338

339339
return h;
340340
}
341-
hir::ModuleDef::Trait(_) => HlTag::Symbol(SymbolKind::Trait),
341+
hir::ModuleDef::Trait(trait_) => {
342+
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Trait));
343+
344+
if trait_.is_unsafe(db) {
345+
h |= HlMod::Unsafe;
346+
}
347+
return h;
348+
}
342349
hir::ModuleDef::TypeAlias(type_) => {
343350
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias));
344351
if let Some(item) = type_.as_assoc_item(db) {

crates/ide/src/syntax_highlighting/tags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub enum HlMod {
6868
/// Used with keywords like `async` and `await`.
6969
Async,
7070
// Keep this last!
71-
/// Used for unsafe functions, mutable statics, union accesses and unsafe operations.
71+
/// Used for unsafe functions, unsafe traits, mutable statics, union accesses and unsafe operations.
7272
Unsafe,
7373
}
7474

crates/ide/src/syntax_highlighting/test_data/highlighting.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,7 @@
245245
<span class="keyword">let</span> <span class="variable declaration">f1</span> <span class="operator">=</span> <span class="function async">learn_and_sing</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
246246
<span class="keyword">let</span> <span class="variable declaration">f2</span> <span class="operator">=</span> <span class="unresolved_reference">dance</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
247247
futures::<span class="macro">join!</span><span class="parenthesis">(</span>f1<span class="comma">,</span> f2<span class="parenthesis">)</span><span class="semicolon">;</span>
248-
<span class="brace">}</span></code></pre>
248+
<span class="brace">}</span>
249+
250+
<span class="keyword unsafe">unsafe</span> <span class="keyword">trait</span> <span class="trait declaration unsafe">Dangerous</span> <span class="brace">{</span><span class="brace">}</span>
251+
<span class="keyword">impl</span> <span class="trait unsafe">Dangerous</span> <span class="keyword">for</span> <span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span></code></pre>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ async fn async_main() {
219219
let f2 = dance();
220220
futures::join!(f1, f2);
221221
}
222+
223+
unsafe trait Dangerous {}
224+
impl Dangerous for () {}
222225
"#
223226
.trim(),
224227
expect_file!["./test_data/highlighting.html"],

0 commit comments

Comments
 (0)