]> git.lizzy.rs Git - rust.git/commitdiff
Give ‘unsafe’ semantic token modifier to unsafe traits
authorAramis Razzaghipour <aramisnoah@gmail.com>
Sun, 23 May 2021 11:43:23 +0000 (21:43 +1000)
committerAramis Razzaghipour <aramisnoah@gmail.com>
Sun, 23 May 2021 11:45:10 +0000 (21:45 +1000)
crates/hir/src/lib.rs
crates/ide/src/syntax_highlighting/highlight.rs
crates/ide/src/syntax_highlighting/tags.rs
crates/ide/src/syntax_highlighting/test_data/highlighting.html
crates/ide/src/syntax_highlighting/tests.rs

index 800101c91968f879d6b331f242bfe5ea7c3bcfb7..a7c42ca1e5fe78a8cd600cc18fa74454f24cb756 100644 (file)
@@ -1085,6 +1085,10 @@ pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
     pub fn is_auto(self, db: &dyn HirDatabase) -> bool {
         db.trait_data(self.id).is_auto
     }
+
+    pub fn is_unsafe(&self, db: &dyn HirDatabase) -> bool {
+        db.trait_data(self.id).is_unsafe
+    }
 }
 
 impl HasVisibility for Trait {
index baed8e2170fb1e4a6eae0e698636f1274b1d9267..058e37ff06dac9340d6df8e3f7d319a3f3922ebe 100644 (file)
@@ -338,7 +338,14 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
 
                 return h;
             }
-            hir::ModuleDef::Trait(_) => HlTag::Symbol(SymbolKind::Trait),
+            hir::ModuleDef::Trait(trait_) => {
+                let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Trait));
+
+                if trait_.is_unsafe(db) {
+                    h |= HlMod::Unsafe;
+                }
+                return h;
+            }
             hir::ModuleDef::TypeAlias(type_) => {
                 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias));
                 if let Some(item) = type_.as_assoc_item(db) {
index f4a2e7506a6df255c315cfceee589e2cb90caa10..27473a2f96df80e37c59d5020f3b4dc4e18ec2b9 100644 (file)
@@ -68,7 +68,7 @@ pub enum HlMod {
     /// Used with keywords like `async` and `await`.
     Async,
     // Keep this last!
-    /// Used for unsafe functions, mutable statics, union accesses and unsafe operations.
+    /// Used for unsafe functions, unsafe traits, mutable statics, union accesses and unsafe operations.
     Unsafe,
 }
 
index 0d325f3f3d8841c84ee471175181585b3755985f..878431b56a081dce08deb2b8519e03ae57c5b132 100644 (file)
@@ -245,4 +245,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     <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>
     <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>
     futures::<span class="macro">join!</span><span class="parenthesis">(</span>f1<span class="comma">,</span> f2<span class="parenthesis">)</span><span class="semicolon">;</span>
-<span class="brace">}</span></code></pre>
\ No newline at end of file
+<span class="brace">}</span>
+
+<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>
+<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>
\ No newline at end of file
index 8c8878d3679824388d843c68363610fc5c20a789..9ce26e930eb7becf3ea7a716030ea216335690a7 100644 (file)
@@ -219,6 +219,9 @@ async fn async_main() {
     let f2 = dance();
     futures::join!(f1, f2);
 }
+
+unsafe trait Dangerous {}
+impl Dangerous for () {}
 "#
         .trim(),
         expect_file!["./test_data/highlighting.html"],