aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authoruHOOCCOOHu <[email protected]>2019-09-15 13:14:33 +0100
committeruHOOCCOOHu <[email protected]>2019-09-15 13:14:33 +0100
commit7ed3be32916facf3b709d5277381408cd3ec134a (patch)
tree7ac9169b41465315ca1c5541c096ea61ac789357 /crates/ra_hir/src
parentde9670fe456d89f97e8044d4e0919d2c16d1087f (diff)
Define known paths and group names
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/expr/validation.rs8
-rw-r--r--crates/ra_hir/src/name.rs27
-rw-r--r--crates/ra_hir/src/path.rs30
-rw-r--r--crates/ra_hir/src/source_binder.rs8
-rw-r--r--crates/ra_hir/src/ty/autoderef.rs2
-rw-r--r--crates/ra_hir/src/ty/infer.rs37
6 files changed, 63 insertions, 49 deletions
diff --git a/crates/ra_hir/src/expr/validation.rs b/crates/ra_hir/src/expr/validation.rs
index c675bf8de..f06e5ec07 100644
--- a/crates/ra_hir/src/expr/validation.rs
+++ b/crates/ra_hir/src/expr/validation.rs
@@ -7,8 +7,7 @@ use crate::{
7 db::HirDatabase, 7 db::HirDatabase,
8 diagnostics::{DiagnosticSink, MissingFields, MissingOkInTailExpr}, 8 diagnostics::{DiagnosticSink, MissingFields, MissingOkInTailExpr},
9 expr::AstPtr, 9 expr::AstPtr,
10 name, 10 path::known,
11 path::PathKind,
12 ty::{ApplicationTy, InferenceResult, Ty, TypeCtor}, 11 ty::{ApplicationTy, InferenceResult, Ty, TypeCtor},
13 Adt, Function, Name, Path, 12 Adt, Function, Name, Path,
14}; 13};
@@ -108,10 +107,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
108 None => return, 107 None => return,
109 }; 108 };
110 109
111 let std_result_path = Path::from_simple_segments( 110 let std_result_path = known::std_result_result();
112 PathKind::Abs,
113 vec![name::STD, name::RESULT_MOD, name::RESULT_TYPE],
114 );
115 111
116 let resolver = self.func.resolver(db); 112 let resolver = self.func.resolver(db);
117 let std_result_enum = match resolver.resolve_known_enum(db, &std_result_path) { 113 let std_result_enum = match resolver.resolve_known_enum(db, &std_result_path) {
diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs
index abdfec296..1bf993ffb 100644
--- a/crates/ra_hir/src/name.rs
+++ b/crates/ra_hir/src/name.rs
@@ -85,6 +85,7 @@ impl AsName for ra_db::Dependency {
85 } 85 }
86} 86}
87 87
88// Primitives
88pub(crate) const ISIZE: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"isize")); 89pub(crate) const ISIZE: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"isize"));
89pub(crate) const I8: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"i8")); 90pub(crate) const I8: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"i8"));
90pub(crate) const I16: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i16")); 91pub(crate) const I16: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i16"));
@@ -102,24 +103,30 @@ pub(crate) const F64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"f64")
102pub(crate) const BOOL: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"bool")); 103pub(crate) const BOOL: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"bool"));
103pub(crate) const CHAR: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"char")); 104pub(crate) const CHAR: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"char"));
104pub(crate) const STR: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"str")); 105pub(crate) const STR: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"str"));
106
107// Special names
105pub(crate) const SELF_PARAM: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"self")); 108pub(crate) const SELF_PARAM: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"self"));
106pub(crate) const SELF_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Self")); 109pub(crate) const SELF_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Self"));
107pub(crate) const MACRO_RULES: Name = Name::new(SmolStr::new_inline_from_ascii(11, b"macro_rules")); 110pub(crate) const MACRO_RULES: Name = Name::new(SmolStr::new_inline_from_ascii(11, b"macro_rules"));
111
112// Components of known path (value or mod name)
108pub(crate) const STD: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"std")); 113pub(crate) const STD: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"std"));
109pub(crate) const ITER: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"iter")); 114pub(crate) const ITER: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"iter"));
110pub(crate) const INTO_ITERATOR: Name =
111 Name::new(SmolStr::new_inline_from_ascii(12, b"IntoIterator"));
112pub(crate) const ITEM: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Item"));
113pub(crate) const OPS: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"ops")); 115pub(crate) const OPS: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"ops"));
114pub(crate) const TRY: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"Try")); 116pub(crate) const FUTURE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"future"));
115pub(crate) const OK: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"Ok")); 117pub(crate) const RESULT: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"result"));
116pub(crate) const FUTURE_MOD: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"future")); 118pub(crate) const BOXED: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"boxed"));
119
120// Components of known path (type name)
121pub(crate) const INTO_ITERATOR_TYPE: Name =
122 Name::new(SmolStr::new_inline_from_ascii(12, b"IntoIterator"));
123pub(crate) const ITEM_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Item"));
124pub(crate) const TRY_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"Try"));
125pub(crate) const OK_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"Ok"));
117pub(crate) const FUTURE_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Future")); 126pub(crate) const FUTURE_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Future"));
118pub(crate) const RESULT_MOD: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"result"));
119pub(crate) const RESULT_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Result")); 127pub(crate) const RESULT_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Result"));
120pub(crate) const OUTPUT: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Output")); 128pub(crate) const OUTPUT_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Output"));
121pub(crate) const TARGET: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Target")); 129pub(crate) const TARGET_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Target"));
122pub(crate) const BOXED_MOD: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"boxed"));
123pub(crate) const BOX_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"Box")); 130pub(crate) const BOX_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"Box"));
124 131
125fn resolve_name(text: &SmolStr) -> SmolStr { 132fn resolve_name(text: &SmolStr) -> SmolStr {
diff --git a/crates/ra_hir/src/path.rs b/crates/ra_hir/src/path.rs
index 9e449f6cc..a61161b63 100644
--- a/crates/ra_hir/src/path.rs
+++ b/crates/ra_hir/src/path.rs
@@ -234,7 +234,7 @@ impl GenericArgs {
234 } 234 }
235 if let Some(ret_type) = ret_type { 235 if let Some(ret_type) = ret_type {
236 let type_ref = TypeRef::from_ast_opt(ret_type.type_ref()); 236 let type_ref = TypeRef::from_ast_opt(ret_type.type_ref());
237 bindings.push((name::OUTPUT, type_ref)) 237 bindings.push((name::OUTPUT_TYPE, type_ref))
238 } 238 }
239 if args.is_empty() && bindings.is_empty() { 239 if args.is_empty() && bindings.is_empty() {
240 None 240 None
@@ -338,3 +338,31 @@ fn convert_path(prefix: Option<Path>, path: ast::Path) -> Option<Path> {
338 }; 338 };
339 Some(res) 339 Some(res)
340} 340}
341
342pub mod known {
343 use super::{Path, PathKind};
344 use crate::name;
345
346 pub fn std_iter_into_iterator() -> Path {
347 Path::from_simple_segments(
348 PathKind::Abs,
349 vec![name::STD, name::ITER, name::INTO_ITERATOR_TYPE],
350 )
351 }
352
353 pub fn std_ops_try() -> Path {
354 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::TRY_TYPE])
355 }
356
357 pub fn std_result_result() -> Path {
358 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::RESULT, name::RESULT_TYPE])
359 }
360
361 pub fn std_future_future() -> Path {
362 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::FUTURE, name::FUTURE_TYPE])
363 }
364
365 pub fn std_boxed_box() -> Path {
366 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::BOXED, name::BOX_TYPE])
367 }
368}
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 59053cda3..2a907c9f1 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -25,8 +25,7 @@ use crate::{
25 BodySourceMap, 25 BodySourceMap,
26 }, 26 },
27 ids::LocationCtx, 27 ids::LocationCtx,
28 name, 28 path::known,
29 path::PathKind,
30 resolve::{ScopeDef, TypeNs, ValueNs}, 29 resolve::{ScopeDef, TypeNs, ValueNs},
31 ty::method_resolution::implements_trait, 30 ty::method_resolution::implements_trait,
32 AsName, AstId, Const, Crate, DefWithBody, Either, Enum, Function, HasBody, HirFileId, MacroDef, 31 AsName, AstId, Const, Crate, DefWithBody, Either, Enum, Function, HasBody, HirFileId, MacroDef,
@@ -433,10 +432,7 @@ impl SourceAnalyzer {
433 /// Checks that particular type `ty` implements `std::future::Future`. 432 /// Checks that particular type `ty` implements `std::future::Future`.
434 /// This function is used in `.await` syntax completion. 433 /// This function is used in `.await` syntax completion.
435 pub fn impls_future(&self, db: &impl HirDatabase, ty: Ty) -> bool { 434 pub fn impls_future(&self, db: &impl HirDatabase, ty: Ty) -> bool {
436 let std_future_path = Path::from_simple_segments( 435 let std_future_path = known::std_future_future();
437 PathKind::Abs,
438 vec![name::STD, name::FUTURE_MOD, name::FUTURE_TYPE],
439 );
440 436
441 let std_future_trait = match self.resolver.resolve_known_trait(db, &std_future_path) { 437 let std_future_trait = match self.resolver.resolve_known_trait(db, &std_future_path) {
442 Some(it) => it, 438 Some(it) => it,
diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs
index 5ba7cf2e0..94f8ecdc9 100644
--- a/crates/ra_hir/src/ty/autoderef.rs
+++ b/crates/ra_hir/src/ty/autoderef.rs
@@ -42,7 +42,7 @@ fn deref_by_trait(
42 crate::lang_item::LangItemTarget::Trait(t) => t, 42 crate::lang_item::LangItemTarget::Trait(t) => t,
43 _ => return None, 43 _ => return None,
44 }; 44 };
45 let target = deref_trait.associated_type_by_name(db, &name::TARGET)?; 45 let target = deref_trait.associated_type_by_name(db, &name::TARGET_TYPE)?;
46 46
47 if target.generic_params(db).count_params_including_parent() != 1 { 47 if target.generic_params(db).count_params_including_parent() != 1 {
48 // the Target type + Deref trait should only have one generic parameter, 48 // the Target type + Deref trait should only have one generic parameter,
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index 3981de829..785499f77 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -44,7 +44,7 @@ use crate::{
44 generics::{GenericParams, HasGenericParams}, 44 generics::{GenericParams, HasGenericParams},
45 name, 45 name,
46 nameres::Namespace, 46 nameres::Namespace,
47 path::{GenericArg, GenericArgs, PathKind}, 47 path::{known, GenericArg, GenericArgs},
48 resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, 48 resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs},
49 ty::infer::diagnostics::InferenceDiagnostic, 49 ty::infer::diagnostics::InferenceDiagnostic,
50 type_ref::{Mutability, TypeRef}, 50 type_ref::{Mutability, TypeRef},
@@ -1442,39 +1442,26 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1442 } 1442 }
1443 1443
1444 fn resolve_into_iter_item(&self) -> Option<TypeAlias> { 1444 fn resolve_into_iter_item(&self) -> Option<TypeAlias> {
1445 let into_iter_path = Path::from_simple_segments( 1445 let path = known::std_iter_into_iterator();
1446 PathKind::Abs, 1446 let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
1447 vec![name::STD, name::ITER, name::INTO_ITERATOR], 1447 trait_.associated_type_by_name(self.db, &name::ITEM_TYPE)
1448 );
1449
1450 let trait_ = self.resolver.resolve_known_trait(self.db, &into_iter_path)?;
1451 trait_.associated_type_by_name(self.db, &name::ITEM)
1452 } 1448 }
1453 1449
1454 fn resolve_ops_try_ok(&self) -> Option<TypeAlias> { 1450 fn resolve_ops_try_ok(&self) -> Option<TypeAlias> {
1455 let ops_try_path = 1451 let path = known::std_ops_try();
1456 Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::TRY]); 1452 let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
1457 1453 trait_.associated_type_by_name(self.db, &name::OK_TYPE)
1458 let trait_ = self.resolver.resolve_known_trait(self.db, &ops_try_path)?;
1459 trait_.associated_type_by_name(self.db, &name::OK)
1460 } 1454 }
1461 1455
1462 fn resolve_future_future_output(&self) -> Option<TypeAlias> { 1456 fn resolve_future_future_output(&self) -> Option<TypeAlias> {
1463 let future_future_path = Path::from_simple_segments( 1457 let path = known::std_future_future();
1464 PathKind::Abs, 1458 let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
1465 vec![name::STD, name::FUTURE_MOD, name::FUTURE_TYPE], 1459 trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE)
1466 );
1467
1468 let trait_ = self.resolver.resolve_known_trait(self.db, &future_future_path)?;
1469 trait_.associated_type_by_name(self.db, &name::OUTPUT)
1470 } 1460 }
1471 1461
1472 fn resolve_boxed_box(&self) -> Option<Adt> { 1462 fn resolve_boxed_box(&self) -> Option<Adt> {
1473 let boxed_box_path = Path::from_simple_segments( 1463 let path = known::std_boxed_box();
1474 PathKind::Abs, 1464 let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
1475 vec![name::STD, name::BOXED_MOD, name::BOX_TYPE],
1476 );
1477 let struct_ = self.resolver.resolve_known_struct(self.db, &boxed_box_path)?;
1478 Some(Adt::Struct(struct_)) 1465 Some(Adt::Struct(struct_))
1479 } 1466 }
1480} 1467}