diff options
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 88 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/source_analyzer.rs (renamed from crates/ra_hir/src/source_binder.rs) | 195 |
3 files changed, 153 insertions, 134 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index a177cebca..3b479356f 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -21,8 +21,8 @@ use hir_expand::{ | |||
21 | MacroDefId, | 21 | MacroDefId, |
22 | }; | 22 | }; |
23 | use hir_ty::{ | 23 | use hir_ty::{ |
24 | autoderef, display::HirFormatter, expr::ExprValidator, ApplicationTy, Canonical, InEnvironment, | 24 | autoderef, display::HirFormatter, expr::ExprValidator, method_resolution, ApplicationTy, |
25 | TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk, | 25 | Canonical, InEnvironment, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk, |
26 | }; | 26 | }; |
27 | use ra_db::{CrateId, Edition, FileId}; | 27 | use ra_db::{CrateId, Edition, FileId}; |
28 | use ra_prof::profile; | 28 | use ra_prof::profile; |
@@ -120,7 +120,8 @@ impl_froms!( | |||
120 | BuiltinType | 120 | BuiltinType |
121 | ); | 121 | ); |
122 | 122 | ||
123 | pub use hir_def::{attr::Attrs, visibility::Visibility}; | 123 | pub use hir_def::{attr::Attrs, visibility::Visibility, AssocItemId}; |
124 | use rustc_hash::FxHashSet; | ||
124 | 125 | ||
125 | impl Module { | 126 | impl Module { |
126 | pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { | 127 | pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { |
@@ -878,6 +879,28 @@ impl Type { | |||
878 | } | 879 | } |
879 | } | 880 | } |
880 | 881 | ||
882 | /// Checks that particular type `ty` implements `std::future::Future`. | ||
883 | /// This function is used in `.await` syntax completion. | ||
884 | pub fn impls_future(&self, db: &impl HirDatabase) -> bool { | ||
885 | let krate = self.krate; | ||
886 | |||
887 | let std_future_trait = | ||
888 | db.lang_item(krate, "future_trait".into()).and_then(|it| it.as_trait()); | ||
889 | let std_future_trait = match std_future_trait { | ||
890 | Some(it) => it, | ||
891 | None => return false, | ||
892 | }; | ||
893 | |||
894 | let canonical_ty = Canonical { value: self.ty.value.clone(), num_vars: 0 }; | ||
895 | method_resolution::implements_trait( | ||
896 | &canonical_ty, | ||
897 | db, | ||
898 | self.ty.environment.clone(), | ||
899 | krate, | ||
900 | std_future_trait, | ||
901 | ) | ||
902 | } | ||
903 | |||
881 | // FIXME: this method is broken, as it doesn't take closures into account. | 904 | // FIXME: this method is broken, as it doesn't take closures into account. |
882 | pub fn as_callable(&self) -> Option<CallableDef> { | 905 | pub fn as_callable(&self) -> Option<CallableDef> { |
883 | Some(self.ty.value.as_callable()?.0) | 906 | Some(self.ty.value.as_callable()?.0) |
@@ -986,6 +1009,65 @@ impl Type { | |||
986 | None | 1009 | None |
987 | } | 1010 | } |
988 | 1011 | ||
1012 | pub fn iterate_method_candidates<T>( | ||
1013 | &self, | ||
1014 | db: &impl HirDatabase, | ||
1015 | krate: Crate, | ||
1016 | traits_in_scope: &FxHashSet<TraitId>, | ||
1017 | name: Option<&Name>, | ||
1018 | mut callback: impl FnMut(&Ty, Function) -> Option<T>, | ||
1019 | ) -> Option<T> { | ||
1020 | // There should be no inference vars in types passed here | ||
1021 | // FIXME check that? | ||
1022 | // FIXME replace Unknown by bound vars here | ||
1023 | let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 }; | ||
1024 | |||
1025 | let env = self.ty.environment.clone(); | ||
1026 | let krate = krate.id; | ||
1027 | |||
1028 | method_resolution::iterate_method_candidates( | ||
1029 | &canonical, | ||
1030 | db, | ||
1031 | env, | ||
1032 | krate, | ||
1033 | traits_in_scope, | ||
1034 | name, | ||
1035 | method_resolution::LookupMode::MethodCall, | ||
1036 | |ty, it| match it { | ||
1037 | AssocItemId::FunctionId(f) => callback(ty, f.into()), | ||
1038 | _ => None, | ||
1039 | }, | ||
1040 | ) | ||
1041 | } | ||
1042 | |||
1043 | pub fn iterate_path_candidates<T>( | ||
1044 | &self, | ||
1045 | db: &impl HirDatabase, | ||
1046 | krate: Crate, | ||
1047 | traits_in_scope: &FxHashSet<TraitId>, | ||
1048 | name: Option<&Name>, | ||
1049 | mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>, | ||
1050 | ) -> Option<T> { | ||
1051 | // There should be no inference vars in types passed here | ||
1052 | // FIXME check that? | ||
1053 | // FIXME replace Unknown by bound vars here | ||
1054 | let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 }; | ||
1055 | |||
1056 | let env = self.ty.environment.clone(); | ||
1057 | let krate = krate.id; | ||
1058 | |||
1059 | method_resolution::iterate_method_candidates( | ||
1060 | &canonical, | ||
1061 | db, | ||
1062 | env, | ||
1063 | krate, | ||
1064 | traits_in_scope, | ||
1065 | name, | ||
1066 | method_resolution::LookupMode::Path, | ||
1067 | |ty, it| callback(ty, it.into()), | ||
1068 | ) | ||
1069 | } | ||
1070 | |||
989 | pub fn as_adt(&self) -> Option<Adt> { | 1071 | pub fn as_adt(&self) -> Option<Adt> { |
990 | let (adt, _subst) = self.ty.value.as_adt()?; | 1072 | let (adt, _subst) = self.ty.value.as_adt()?; |
991 | Some(adt.into()) | 1073 | Some(adt.into()) |
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 3d13978d4..a1cf89010 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -27,7 +27,7 @@ macro_rules! impl_froms { | |||
27 | } | 27 | } |
28 | 28 | ||
29 | pub mod db; | 29 | pub mod db; |
30 | pub mod source_binder; | 30 | pub mod source_analyzer; |
31 | 31 | ||
32 | pub mod diagnostics; | 32 | pub mod diagnostics; |
33 | 33 | ||
@@ -46,7 +46,7 @@ pub use crate::{ | |||
46 | }, | 46 | }, |
47 | from_source::FromSource, | 47 | from_source::FromSource, |
48 | has_source::HasSource, | 48 | has_source::HasSource, |
49 | source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, | 49 | source_analyzer::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, |
50 | }; | 50 | }; |
51 | 51 | ||
52 | pub use hir_def::{ | 52 | pub use hir_def::{ |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_analyzer.rs index a2a9d968c..76e0bff34 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_analyzer.rs | |||
@@ -15,17 +15,13 @@ use hir_def::{ | |||
15 | }, | 15 | }, |
16 | expr::{ExprId, PatId}, | 16 | expr::{ExprId, PatId}, |
17 | nameres::ModuleSource, | 17 | nameres::ModuleSource, |
18 | path::path, | ||
19 | resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, | 18 | resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, |
20 | AssocItemId, DefWithBodyId, | 19 | DefWithBodyId, TraitId, |
21 | }; | 20 | }; |
22 | use hir_expand::{ | 21 | use hir_expand::{ |
23 | hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind, | 22 | hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind, |
24 | }; | 23 | }; |
25 | use hir_ty::{ | 24 | use hir_ty::{InEnvironment, InferenceResult, TraitEnvironment}; |
26 | method_resolution::{self, implements_trait}, | ||
27 | Canonical, InEnvironment, InferenceResult, TraitEnvironment, Ty, | ||
28 | }; | ||
29 | use ra_prof::profile; | 25 | use ra_prof::profile; |
30 | use ra_syntax::{ | 26 | use ra_syntax::{ |
31 | ast::{self, AstNode}, | 27 | ast::{self, AstNode}, |
@@ -33,71 +29,13 @@ use ra_syntax::{ | |||
33 | SyntaxKind::*, | 29 | SyntaxKind::*, |
34 | SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextUnit, | 30 | SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextUnit, |
35 | }; | 31 | }; |
32 | use rustc_hash::FxHashSet; | ||
36 | 33 | ||
37 | use crate::{ | 34 | use crate::{ |
38 | db::HirDatabase, Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, | 35 | db::HirDatabase, Adt, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, ImplBlock, |
39 | ImplBlock, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, | 36 | Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, |
40 | TypeParam, | ||
41 | }; | 37 | }; |
42 | 38 | ||
43 | fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> { | ||
44 | match_ast! { | ||
45 | match (node.value) { | ||
46 | ast::Module(it) => { | ||
47 | let src = node.with_value(it); | ||
48 | Some(crate::Module::from_declaration(db, src)?.id.resolver(db)) | ||
49 | }, | ||
50 | ast::SourceFile(it) => { | ||
51 | let src = node.with_value(ModuleSource::SourceFile(it)); | ||
52 | Some(crate::Module::from_definition(db, src)?.id.resolver(db)) | ||
53 | }, | ||
54 | ast::StructDef(it) => { | ||
55 | let src = node.with_value(it); | ||
56 | Some(Struct::from_source(db, src)?.id.resolver(db)) | ||
57 | }, | ||
58 | ast::EnumDef(it) => { | ||
59 | let src = node.with_value(it); | ||
60 | Some(Enum::from_source(db, src)?.id.resolver(db)) | ||
61 | }, | ||
62 | ast::ImplBlock(it) => { | ||
63 | let src = node.with_value(it); | ||
64 | Some(ImplBlock::from_source(db, src)?.id.resolver(db)) | ||
65 | }, | ||
66 | ast::TraitDef(it) => { | ||
67 | let src = node.with_value(it); | ||
68 | Some(Trait::from_source(db, src)?.id.resolver(db)) | ||
69 | }, | ||
70 | _ => match node.value.kind() { | ||
71 | FN_DEF | CONST_DEF | STATIC_DEF => { | ||
72 | let def = def_with_body_from_child_node(db, node)?; | ||
73 | let def = DefWithBodyId::from(def); | ||
74 | Some(def.resolver(db)) | ||
75 | } | ||
76 | // FIXME add missing cases | ||
77 | _ => None | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | fn def_with_body_from_child_node( | ||
84 | db: &impl HirDatabase, | ||
85 | child: InFile<&SyntaxNode>, | ||
86 | ) -> Option<DefWithBody> { | ||
87 | let _p = profile("def_with_body_from_child_node"); | ||
88 | child.cloned().ancestors_with_macros(db).find_map(|node| { | ||
89 | let n = &node.value; | ||
90 | match_ast! { | ||
91 | match n { | ||
92 | ast::FnDef(def) => { return Function::from_source(db, node.with_value(def)).map(DefWithBody::from); }, | ||
93 | ast::ConstDef(def) => { return Const::from_source(db, node.with_value(def)).map(DefWithBody::from); }, | ||
94 | ast::StaticDef(def) => { return Static::from_source(db, node.with_value(def)).map(DefWithBody::from); }, | ||
95 | _ => { None }, | ||
96 | } | ||
97 | } | ||
98 | }) | ||
99 | } | ||
100 | |||
101 | /// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of | 39 | /// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of |
102 | /// original source files. It should not be used inside the HIR itself. | 40 | /// original source files. It should not be used inside the HIR itself. |
103 | #[derive(Debug)] | 41 | #[derive(Debug)] |
@@ -409,68 +347,9 @@ impl SourceAnalyzer { | |||
409 | .collect() | 347 | .collect() |
410 | } | 348 | } |
411 | 349 | ||
412 | pub fn iterate_method_candidates<T>( | 350 | /// Note: `FxHashSet<TraitId>` should be treated as an opaque type, passed into `Type |
413 | &self, | 351 | pub fn traits_in_scope(&self, db: &impl HirDatabase) -> FxHashSet<TraitId> { |
414 | db: &impl HirDatabase, | 352 | self.resolver.traits_in_scope(db) |
415 | ty: &Type, | ||
416 | name: Option<&Name>, | ||
417 | mut callback: impl FnMut(&Ty, Function) -> Option<T>, | ||
418 | ) -> Option<T> { | ||
419 | // There should be no inference vars in types passed here | ||
420 | // FIXME check that? | ||
421 | // FIXME replace Unknown by bound vars here | ||
422 | let canonical = Canonical { value: ty.ty.value.clone(), num_vars: 0 }; | ||
423 | method_resolution::iterate_method_candidates( | ||
424 | &canonical, | ||
425 | db, | ||
426 | &self.resolver, | ||
427 | name, | ||
428 | method_resolution::LookupMode::MethodCall, | ||
429 | |ty, it| match it { | ||
430 | AssocItemId::FunctionId(f) => callback(ty, f.into()), | ||
431 | _ => None, | ||
432 | }, | ||
433 | ) | ||
434 | } | ||
435 | |||
436 | pub fn iterate_path_candidates<T>( | ||
437 | &self, | ||
438 | db: &impl HirDatabase, | ||
439 | ty: &Type, | ||
440 | name: Option<&Name>, | ||
441 | mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>, | ||
442 | ) -> Option<T> { | ||
443 | // There should be no inference vars in types passed here | ||
444 | // FIXME check that? | ||
445 | // FIXME replace Unknown by bound vars here | ||
446 | let canonical = Canonical { value: ty.ty.value.clone(), num_vars: 0 }; | ||
447 | method_resolution::iterate_method_candidates( | ||
448 | &canonical, | ||
449 | db, | ||
450 | &self.resolver, | ||
451 | name, | ||
452 | method_resolution::LookupMode::Path, | ||
453 | |ty, it| callback(ty, it.into()), | ||
454 | ) | ||
455 | } | ||
456 | |||
457 | /// Checks that particular type `ty` implements `std::future::Future`. | ||
458 | /// This function is used in `.await` syntax completion. | ||
459 | pub fn impls_future(&self, db: &impl HirDatabase, ty: Type) -> bool { | ||
460 | let std_future_path = path![std::future::Future]; | ||
461 | |||
462 | let std_future_trait = match self.resolver.resolve_known_trait(db, &std_future_path) { | ||
463 | Some(it) => it.into(), | ||
464 | _ => return false, | ||
465 | }; | ||
466 | |||
467 | let krate = match self.resolver.krate() { | ||
468 | Some(krate) => krate, | ||
469 | _ => return false, | ||
470 | }; | ||
471 | |||
472 | let canonical_ty = Canonical { value: ty.ty.value, num_vars: 0 }; | ||
473 | implements_trait(&canonical_ty, db, &self.resolver, krate.into(), std_future_trait) | ||
474 | } | 353 | } |
475 | 354 | ||
476 | pub fn expand( | 355 | pub fn expand( |
@@ -487,6 +366,64 @@ impl SourceAnalyzer { | |||
487 | } | 366 | } |
488 | } | 367 | } |
489 | 368 | ||
369 | fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> { | ||
370 | match_ast! { | ||
371 | match (node.value) { | ||
372 | ast::Module(it) => { | ||
373 | let src = node.with_value(it); | ||
374 | Some(crate::Module::from_declaration(db, src)?.id.resolver(db)) | ||
375 | }, | ||
376 | ast::SourceFile(it) => { | ||
377 | let src = node.with_value(ModuleSource::SourceFile(it)); | ||
378 | Some(crate::Module::from_definition(db, src)?.id.resolver(db)) | ||
379 | }, | ||
380 | ast::StructDef(it) => { | ||
381 | let src = node.with_value(it); | ||
382 | Some(Struct::from_source(db, src)?.id.resolver(db)) | ||
383 | }, | ||
384 | ast::EnumDef(it) => { | ||
385 | let src = node.with_value(it); | ||
386 | Some(Enum::from_source(db, src)?.id.resolver(db)) | ||
387 | }, | ||
388 | ast::ImplBlock(it) => { | ||
389 | let src = node.with_value(it); | ||
390 | Some(ImplBlock::from_source(db, src)?.id.resolver(db)) | ||
391 | }, | ||
392 | ast::TraitDef(it) => { | ||
393 | let src = node.with_value(it); | ||
394 | Some(Trait::from_source(db, src)?.id.resolver(db)) | ||
395 | }, | ||
396 | _ => match node.value.kind() { | ||
397 | FN_DEF | CONST_DEF | STATIC_DEF => { | ||
398 | let def = def_with_body_from_child_node(db, node)?; | ||
399 | let def = DefWithBodyId::from(def); | ||
400 | Some(def.resolver(db)) | ||
401 | } | ||
402 | // FIXME add missing cases | ||
403 | _ => None | ||
404 | } | ||
405 | } | ||
406 | } | ||
407 | } | ||
408 | |||
409 | fn def_with_body_from_child_node( | ||
410 | db: &impl HirDatabase, | ||
411 | child: InFile<&SyntaxNode>, | ||
412 | ) -> Option<DefWithBody> { | ||
413 | let _p = profile("def_with_body_from_child_node"); | ||
414 | child.cloned().ancestors_with_macros(db).find_map(|node| { | ||
415 | let n = &node.value; | ||
416 | match_ast! { | ||
417 | match n { | ||
418 | ast::FnDef(def) => { return Function::from_source(db, node.with_value(def)).map(DefWithBody::from); }, | ||
419 | ast::ConstDef(def) => { return Const::from_source(db, node.with_value(def)).map(DefWithBody::from); }, | ||
420 | ast::StaticDef(def) => { return Static::from_source(db, node.with_value(def)).map(DefWithBody::from); }, | ||
421 | _ => { None }, | ||
422 | } | ||
423 | } | ||
424 | }) | ||
425 | } | ||
426 | |||
490 | fn scope_for( | 427 | fn scope_for( |
491 | scopes: &ExprScopes, | 428 | scopes: &ExprScopes, |
492 | source_map: &BodySourceMap, | 429 | source_map: &BodySourceMap, |