From 29054e02fbc7a858d9d4a6efd61583ac45b3213b Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 15 Jun 2021 21:44:07 +0200 Subject: Highlight unsafe trait refs as unsafe only in impl blocks and definitions --- crates/ide/src/syntax_highlighting/highlight.rs | 50 ++++++++++++++-------- crates/ide/src/syntax_highlighting/html.rs | 1 + .../test_data/highlight_assoc_functions.html | 1 + .../test_data/highlight_doctest.html | 1 + .../test_data/highlight_extern_crate.html | 1 + .../test_data/highlight_injection.html | 1 + .../test_data/highlight_strings.html | 1 + .../test_data/highlight_unsafe.html | 6 +++ .../test_data/highlighting.html | 1 + .../syntax_highlighting/test_data/injection.html | 1 + .../test_data/rainbow_highlighting.html | 1 + crates/ide/src/syntax_highlighting/tests.rs | 5 +++ crates/syntax/src/ast/node_ext.rs | 9 ++++ 13 files changed, 60 insertions(+), 19 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 7a53268e8..6834fe11a 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -48,7 +48,13 @@ pub(super) fn element( match name_kind { Some(NameClass::ExternCrate(_)) => SymbolKind::Module.into(), Some(NameClass::Definition(def)) => { - highlight_def(db, krate, def) | HlMod::Definition + let mut h = highlight_def(db, krate, def) | HlMod::Definition; + if let Definition::ModuleDef(hir::ModuleDef::Trait(trait_)) = &def { + if trait_.is_unsafe(db) { + h |= HlMod::Unsafe; + } + } + h } Some(NameClass::ConstReference(def)) => highlight_def(db, krate, def), Some(NameClass::PatFieldShorthand { field_ref, .. }) => { @@ -87,20 +93,34 @@ pub(super) fn element( let mut h = highlight_def(db, krate, def); - if let Definition::Local(local) = &def { - if is_consumed_lvalue(name_ref.syntax().clone().into(), local, db) { + match def { + Definition::Local(local) + if is_consumed_lvalue( + name_ref.syntax().clone().into(), + &local, + db, + ) => + { h |= HlMod::Consuming; } - } - - if let Some(parent) = name_ref.syntax().parent() { - if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) { - if let Definition::Field(field) = def { - if let hir::VariantDef::Union(_) = field.parent_def(db) { - h |= HlMod::Unsafe; + Definition::ModuleDef(hir::ModuleDef::Trait(trait_)) + if trait_.is_unsafe(db) => + { + if ast::Impl::for_trait_name_ref(&name_ref).is_some() { + h |= HlMod::Unsafe; + } + } + Definition::Field(field) => { + if let Some(parent) = name_ref.syntax().parent() { + if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) { + if let hir::VariantDef::Union(_) = field.parent_def(db) + { + h |= HlMod::Unsafe; + } } } } + _ => (), } h @@ -354,15 +374,7 @@ fn highlight_def(db: &RootDatabase, krate: Option, def: Definition) h } - hir::ModuleDef::Trait(trait_) => { - let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Trait)); - - if trait_.is_unsafe(db) { - h |= HlMod::Unsafe; - } - - h - } + hir::ModuleDef::Trait(_) => Highlight::new(HlTag::Symbol(SymbolKind::Trait)), hir::ModuleDef::TypeAlias(type_) => { let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias)); diff --git a/crates/ide/src/syntax_highlighting/html.rs b/crates/ide/src/syntax_highlighting/html.rs index 478facfee..21376a7ae 100644 --- a/crates/ide/src/syntax_highlighting/html.rs +++ b/crates/ide/src/syntax_highlighting/html.rs @@ -67,6 +67,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .field { color: #94BFF3; } .function { color: #93E0E3; } .function.unsafe { color: #BC8383; } +.trait.unsafe { color: #BC8383; } .operator.unsafe { color: #BC8383; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html b/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html index a0ea1db34..4e85f7c0b 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html @@ -15,6 +15,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .field { color: #94BFF3; } .function { color: #93E0E3; } .function.unsafe { color: #BC8383; } +.trait.unsafe { color: #BC8383; } .operator.unsafe { color: #BC8383; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index 921a956e6..79a285107 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -15,6 +15,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .field { color: #94BFF3; } .function { color: #93E0E3; } .function.unsafe { color: #BC8383; } +.trait.unsafe { color: #BC8383; } .operator.unsafe { color: #BC8383; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html b/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html index ca9bb1e7d..13f589cc0 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html @@ -15,6 +15,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .field { color: #94BFF3; } .function { color: #93E0E3; } .function.unsafe { color: #BC8383; } +.trait.unsafe { color: #BC8383; } .operator.unsafe { color: #BC8383; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html b/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html index 6202a03ce..50df376ae 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html @@ -15,6 +15,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .field { color: #94BFF3; } .function { color: #93E0E3; } .function.unsafe { color: #BC8383; } +.trait.unsafe { color: #BC8383; } .operator.unsafe { color: #BC8383; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html index e860d713e..96cb09642 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html @@ -15,6 +15,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .field { color: #94BFF3; } .function { color: #93E0E3; } .function.unsafe { color: #BC8383; } +.trait.unsafe { color: #BC8383; } .operator.unsafe { color: #BC8383; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html index 68165bdbf..55453468b 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html @@ -15,6 +15,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .field { color: #94BFF3; } .function { color: #93E0E3; } .function.unsafe { color: #BC8383; } +.trait.unsafe { color: #BC8383; } .operator.unsafe { color: #BC8383; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } @@ -61,6 +62,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd a: u16, } +unsafe trait UnsafeTrait {} +unsafe impl UnsafeTrait for Packed {} + +fn require_unsafe_trait<T: UnsafeTrait>(_: T) {} + trait DoTheAutoref { fn calls_autoref(&self); } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index 59f1e8e4c..9232cf905 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -15,6 +15,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .field { color: #94BFF3; } .function { color: #93E0E3; } .function.unsafe { color: #BC8383; } +.trait.unsafe { color: #BC8383; } .operator.unsafe { color: #BC8383; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } diff --git a/crates/ide/src/syntax_highlighting/test_data/injection.html b/crates/ide/src/syntax_highlighting/test_data/injection.html index 9ab46d05c..082837328 100644 --- a/crates/ide/src/syntax_highlighting/test_data/injection.html +++ b/crates/ide/src/syntax_highlighting/test_data/injection.html @@ -15,6 +15,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .field { color: #94BFF3; } .function { color: #93E0E3; } .function.unsafe { color: #BC8383; } +.trait.unsafe { color: #BC8383; } .operator.unsafe { color: #BC8383; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } diff --git a/crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html b/crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html index 666b0b228..763917714 100644 --- a/crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/rainbow_highlighting.html @@ -15,6 +15,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .field { color: #94BFF3; } .function { color: #93E0E3; } .function.unsafe { color: #BC8383; } +.trait.unsafe { color: #BC8383; } .operator.unsafe { color: #BC8383; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index f7d8334a0..4f0b1ce85 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -527,6 +527,11 @@ struct Packed { a: u16, } +unsafe trait UnsafeTrait {} +unsafe impl UnsafeTrait for Packed {} + +fn require_unsafe_trait(_: T) {} + trait DoTheAutoref { fn calls_autoref(&self); } diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 3d27d2c1a..2bd9ad867 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -325,6 +325,15 @@ impl ast::Impl { let second = types.next(); (first, second) } + + pub fn for_trait_name_ref(name_ref: &ast::NameRef) -> Option { + let this = name_ref.syntax().ancestors().find_map(ast::Impl::cast)?; + if this.trait_()?.syntax().text_range().start() == name_ref.syntax().text_range().start() { + Some(this) + } else { + None + } + } } #[derive(Debug, Clone, PartialEq, Eq)] -- cgit v1.2.3