diff options
author | GrayJack <[email protected]> | 2020-10-16 11:52:18 +0100 |
---|---|---|
committer | GrayJack <[email protected]> | 2020-10-16 11:52:18 +0100 |
commit | a483b5545dad2d36336c1e9a4f5dc991d2c8460b (patch) | |
tree | 4a9a2a515fa205a316f662cfa12a9cd220055875 | |
parent | 83d6bc7113080c9bf3fd70bed1b89c6b4795d826 (diff) |
Add Callable modifier for variables that implements Fnonce
-rw-r--r-- | crates/hir/src/code_model.rs | 25 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting.rs | 2 |
2 files changed, 24 insertions, 3 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index b65be4fe1..8ecf72bd9 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs | |||
@@ -31,8 +31,7 @@ use hir_ty::{ | |||
31 | autoderef, | 31 | autoderef, |
32 | display::{HirDisplayError, HirFormatter}, | 32 | display::{HirDisplayError, HirFormatter}, |
33 | method_resolution, | 33 | method_resolution, |
34 | traits::Solution, | 34 | traits::{FnTrait, Solution, SolutionVariables}, |
35 | traits::SolutionVariables, | ||
36 | ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, | 35 | ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, |
37 | InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, Ty, | 36 | InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, Ty, |
38 | TyDefId, TyKind, TypeCtor, | 37 | TyDefId, TyKind, TypeCtor, |
@@ -1385,6 +1384,28 @@ impl Type { | |||
1385 | ) | 1384 | ) |
1386 | } | 1385 | } |
1387 | 1386 | ||
1387 | /// Checks that particular type `ty` implements `std::ops::FnOnce`. | ||
1388 | /// | ||
1389 | /// This function can be used to check if a particular type is callable, since FnOnce is a | ||
1390 | /// supertrait of Fn and FnMut, so all callable types implements at least FnOnce. | ||
1391 | pub fn impls_fnonce(&self, db: &dyn HirDatabase) -> bool { | ||
1392 | let krate = self.krate; | ||
1393 | |||
1394 | let fnonce_trait = match FnTrait::FnOnce.get_id(db, krate) { | ||
1395 | Some(it) => it, | ||
1396 | None => return false, | ||
1397 | }; | ||
1398 | |||
1399 | let canonical_ty = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; | ||
1400 | method_resolution::implements_trait( | ||
1401 | &canonical_ty, | ||
1402 | db, | ||
1403 | self.ty.environment.clone(), | ||
1404 | krate, | ||
1405 | fnonce_trait, | ||
1406 | ) | ||
1407 | } | ||
1408 | |||
1388 | pub fn impls_trait(&self, db: &dyn HirDatabase, trait_: Trait, args: &[Type]) -> bool { | 1409 | pub fn impls_trait(&self, db: &dyn HirDatabase, trait_: Trait, args: &[Type]) -> bool { |
1389 | let trait_ref = hir_ty::TraitRef { | 1410 | let trait_ref = hir_ty::TraitRef { |
1390 | trait_: trait_.id, | 1411 | trait_: trait_.id, |
diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 3982838d5..750848467 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs | |||
@@ -763,7 +763,7 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { | |||
763 | if local.is_mut(db) || local.ty(db).is_mutable_reference() { | 763 | if local.is_mut(db) || local.ty(db).is_mutable_reference() { |
764 | h |= HighlightModifier::Mutable; | 764 | h |= HighlightModifier::Mutable; |
765 | } | 765 | } |
766 | if local.ty(db).as_callable(db).is_some() { | 766 | if local.ty(db).as_callable(db).is_some() || local.ty(db).impls_fnonce(db) { |
767 | h |= HighlightModifier::Callable; | 767 | h |= HighlightModifier::Callable; |
768 | } | 768 | } |
769 | return h; | 769 | return h; |