Define known paths and group names
This commit is contained in:
parent
de9670fe45
commit
7ed3be3291
@ -7,8 +7,7 @@ use crate::{
|
||||
db::HirDatabase,
|
||||
diagnostics::{DiagnosticSink, MissingFields, MissingOkInTailExpr},
|
||||
expr::AstPtr,
|
||||
name,
|
||||
path::PathKind,
|
||||
path::known,
|
||||
ty::{ApplicationTy, InferenceResult, Ty, TypeCtor},
|
||||
Adt, Function, Name, Path,
|
||||
};
|
||||
@ -108,10 +107,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
|
||||
None => return,
|
||||
};
|
||||
|
||||
let std_result_path = Path::from_simple_segments(
|
||||
PathKind::Abs,
|
||||
vec![name::STD, name::RESULT_MOD, name::RESULT_TYPE],
|
||||
);
|
||||
let std_result_path = known::std_result_result();
|
||||
|
||||
let resolver = self.func.resolver(db);
|
||||
let std_result_enum = match resolver.resolve_known_enum(db, &std_result_path) {
|
||||
|
@ -85,6 +85,7 @@ impl AsName for ra_db::Dependency {
|
||||
}
|
||||
}
|
||||
|
||||
// Primitives
|
||||
pub(crate) const ISIZE: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"isize"));
|
||||
pub(crate) const I8: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"i8"));
|
||||
pub(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")
|
||||
pub(crate) const BOOL: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"bool"));
|
||||
pub(crate) const CHAR: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"char"));
|
||||
pub(crate) const STR: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"str"));
|
||||
|
||||
// Special names
|
||||
pub(crate) const SELF_PARAM: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"self"));
|
||||
pub(crate) const SELF_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Self"));
|
||||
pub(crate) const MACRO_RULES: Name = Name::new(SmolStr::new_inline_from_ascii(11, b"macro_rules"));
|
||||
|
||||
// Components of known path (value or mod name)
|
||||
pub(crate) const STD: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"std"));
|
||||
pub(crate) const ITER: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"iter"));
|
||||
pub(crate) const INTO_ITERATOR: Name =
|
||||
Name::new(SmolStr::new_inline_from_ascii(12, b"IntoIterator"));
|
||||
pub(crate) const ITEM: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Item"));
|
||||
pub(crate) const OPS: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"ops"));
|
||||
pub(crate) const TRY: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"Try"));
|
||||
pub(crate) const OK: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"Ok"));
|
||||
pub(crate) const FUTURE_MOD: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"future"));
|
||||
pub(crate) const FUTURE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"future"));
|
||||
pub(crate) const RESULT: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"result"));
|
||||
pub(crate) const BOXED: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"boxed"));
|
||||
|
||||
// Components of known path (type name)
|
||||
pub(crate) const INTO_ITERATOR_TYPE: Name =
|
||||
Name::new(SmolStr::new_inline_from_ascii(12, b"IntoIterator"));
|
||||
pub(crate) const ITEM_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Item"));
|
||||
pub(crate) const TRY_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"Try"));
|
||||
pub(crate) const OK_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"Ok"));
|
||||
pub(crate) const FUTURE_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Future"));
|
||||
pub(crate) const RESULT_MOD: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"result"));
|
||||
pub(crate) const RESULT_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Result"));
|
||||
pub(crate) const OUTPUT: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Output"));
|
||||
pub(crate) const TARGET: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Target"));
|
||||
pub(crate) const BOXED_MOD: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"boxed"));
|
||||
pub(crate) const OUTPUT_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Output"));
|
||||
pub(crate) const TARGET_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Target"));
|
||||
pub(crate) const BOX_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"Box"));
|
||||
|
||||
fn resolve_name(text: &SmolStr) -> SmolStr {
|
||||
|
@ -234,7 +234,7 @@ impl GenericArgs {
|
||||
}
|
||||
if let Some(ret_type) = ret_type {
|
||||
let type_ref = TypeRef::from_ast_opt(ret_type.type_ref());
|
||||
bindings.push((name::OUTPUT, type_ref))
|
||||
bindings.push((name::OUTPUT_TYPE, type_ref))
|
||||
}
|
||||
if args.is_empty() && bindings.is_empty() {
|
||||
None
|
||||
@ -338,3 +338,31 @@ fn convert_path(prefix: Option<Path>, path: ast::Path) -> Option<Path> {
|
||||
};
|
||||
Some(res)
|
||||
}
|
||||
|
||||
pub mod known {
|
||||
use super::{Path, PathKind};
|
||||
use crate::name;
|
||||
|
||||
pub fn std_iter_into_iterator() -> Path {
|
||||
Path::from_simple_segments(
|
||||
PathKind::Abs,
|
||||
vec![name::STD, name::ITER, name::INTO_ITERATOR_TYPE],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn std_ops_try() -> Path {
|
||||
Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::TRY_TYPE])
|
||||
}
|
||||
|
||||
pub fn std_result_result() -> Path {
|
||||
Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::RESULT, name::RESULT_TYPE])
|
||||
}
|
||||
|
||||
pub fn std_future_future() -> Path {
|
||||
Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::FUTURE, name::FUTURE_TYPE])
|
||||
}
|
||||
|
||||
pub fn std_boxed_box() -> Path {
|
||||
Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::BOXED, name::BOX_TYPE])
|
||||
}
|
||||
}
|
||||
|
@ -25,8 +25,7 @@ use crate::{
|
||||
BodySourceMap,
|
||||
},
|
||||
ids::LocationCtx,
|
||||
name,
|
||||
path::PathKind,
|
||||
path::known,
|
||||
resolve::{ScopeDef, TypeNs, ValueNs},
|
||||
ty::method_resolution::implements_trait,
|
||||
AsName, AstId, Const, Crate, DefWithBody, Either, Enum, Function, HasBody, HirFileId, MacroDef,
|
||||
@ -433,10 +432,7 @@ impl SourceAnalyzer {
|
||||
/// Checks that particular type `ty` implements `std::future::Future`.
|
||||
/// This function is used in `.await` syntax completion.
|
||||
pub fn impls_future(&self, db: &impl HirDatabase, ty: Ty) -> bool {
|
||||
let std_future_path = Path::from_simple_segments(
|
||||
PathKind::Abs,
|
||||
vec![name::STD, name::FUTURE_MOD, name::FUTURE_TYPE],
|
||||
);
|
||||
let std_future_path = known::std_future_future();
|
||||
|
||||
let std_future_trait = match self.resolver.resolve_known_trait(db, &std_future_path) {
|
||||
Some(it) => it,
|
||||
|
@ -42,7 +42,7 @@ fn deref_by_trait(
|
||||
crate::lang_item::LangItemTarget::Trait(t) => t,
|
||||
_ => return None,
|
||||
};
|
||||
let target = deref_trait.associated_type_by_name(db, &name::TARGET)?;
|
||||
let target = deref_trait.associated_type_by_name(db, &name::TARGET_TYPE)?;
|
||||
|
||||
if target.generic_params(db).count_params_including_parent() != 1 {
|
||||
// the Target type + Deref trait should only have one generic parameter,
|
||||
|
@ -44,7 +44,7 @@ use crate::{
|
||||
generics::{GenericParams, HasGenericParams},
|
||||
name,
|
||||
nameres::Namespace,
|
||||
path::{GenericArg, GenericArgs, PathKind},
|
||||
path::{known, GenericArg, GenericArgs},
|
||||
resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs},
|
||||
ty::infer::diagnostics::InferenceDiagnostic,
|
||||
type_ref::{Mutability, TypeRef},
|
||||
@ -1442,39 +1442,26 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||
}
|
||||
|
||||
fn resolve_into_iter_item(&self) -> Option<TypeAlias> {
|
||||
let into_iter_path = Path::from_simple_segments(
|
||||
PathKind::Abs,
|
||||
vec![name::STD, name::ITER, name::INTO_ITERATOR],
|
||||
);
|
||||
|
||||
let trait_ = self.resolver.resolve_known_trait(self.db, &into_iter_path)?;
|
||||
trait_.associated_type_by_name(self.db, &name::ITEM)
|
||||
let path = known::std_iter_into_iterator();
|
||||
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
|
||||
trait_.associated_type_by_name(self.db, &name::ITEM_TYPE)
|
||||
}
|
||||
|
||||
fn resolve_ops_try_ok(&self) -> Option<TypeAlias> {
|
||||
let ops_try_path =
|
||||
Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::TRY]);
|
||||
|
||||
let trait_ = self.resolver.resolve_known_trait(self.db, &ops_try_path)?;
|
||||
trait_.associated_type_by_name(self.db, &name::OK)
|
||||
let path = known::std_ops_try();
|
||||
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
|
||||
trait_.associated_type_by_name(self.db, &name::OK_TYPE)
|
||||
}
|
||||
|
||||
fn resolve_future_future_output(&self) -> Option<TypeAlias> {
|
||||
let future_future_path = Path::from_simple_segments(
|
||||
PathKind::Abs,
|
||||
vec![name::STD, name::FUTURE_MOD, name::FUTURE_TYPE],
|
||||
);
|
||||
|
||||
let trait_ = self.resolver.resolve_known_trait(self.db, &future_future_path)?;
|
||||
trait_.associated_type_by_name(self.db, &name::OUTPUT)
|
||||
let path = known::std_future_future();
|
||||
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
|
||||
trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE)
|
||||
}
|
||||
|
||||
fn resolve_boxed_box(&self) -> Option<Adt> {
|
||||
let boxed_box_path = Path::from_simple_segments(
|
||||
PathKind::Abs,
|
||||
vec![name::STD, name::BOXED_MOD, name::BOX_TYPE],
|
||||
);
|
||||
let struct_ = self.resolver.resolve_known_struct(self.db, &boxed_box_path)?;
|
||||
let path = known::std_boxed_box();
|
||||
let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
|
||||
Some(Adt::Struct(struct_))
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user