Give ‘unsafe’ semantic token modifier to unsafe traits

This commit is contained in:
Aramis Razzaghipour 2021-05-23 21:43:23 +10:00
parent 57eedd9066
commit 4d4dbcfead
No known key found for this signature in database
GPG key ID: F788F7E990136003
5 changed files with 20 additions and 3 deletions

View file

@ -1085,6 +1085,10 @@ impl Trait {
pub fn is_auto(self, db: &dyn HirDatabase) -> bool { pub fn is_auto(self, db: &dyn HirDatabase) -> bool {
db.trait_data(self.id).is_auto 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 { impl HasVisibility for Trait {

View file

@ -338,7 +338,14 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
return h; 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_) => { hir::ModuleDef::TypeAlias(type_) => {
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias)); let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias));
if let Some(item) = type_.as_assoc_item(db) { if let Some(item) = type_.as_assoc_item(db) {

View file

@ -68,7 +68,7 @@ pub enum HlMod {
/// Used with keywords like `async` and `await`. /// Used with keywords like `async` and `await`.
Async, Async,
// Keep this last! // 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, Unsafe,
} }

View 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">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> <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> 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> <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>

View file

@ -219,6 +219,9 @@ async fn async_main() {
let f2 = dance(); let f2 = dance();
futures::join!(f1, f2); futures::join!(f1, f2);
} }
unsafe trait Dangerous {}
impl Dangerous for () {}
"# "#
.trim(), .trim(),
expect_file!["./test_data/highlighting.html"], expect_file!["./test_data/highlighting.html"],