Use path macro

This commit is contained in:
Florian Diebold 2019-12-13 22:32:44 +01:00
parent 6911bc89a7
commit f02fcc1644
4 changed files with 49 additions and 80 deletions

View File

@ -15,7 +15,7 @@
},
expr::{ExprId, PatId},
nameres::ModuleSource,
path::known,
path::path,
resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
AssocItemId, DefWithBodyId,
};
@ -418,7 +418,7 @@ pub fn iterate_path_candidates<T>(
/// 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: Type) -> bool {
let std_future_path = known::std_future_future();
let std_future_path = path![std::future::Future];
let std_future_trait = match self.resolver.resolve_known_trait(db, &std_future_path) {
Some(it) => it.into(),

View File

@ -76,10 +76,7 @@ pub(crate) fn expand_use_item(
}
}
pub(crate) fn from_simple_segments(
kind: PathKind,
segments: impl IntoIterator<Item = Name>,
) -> Path {
pub fn from_simple_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> Path {
Path {
kind,
segments: segments
@ -296,64 +293,36 @@ fn from(name: Name) -> Path {
}
}
pub mod known {
use hir_expand::name::name;
pub use hir_expand::name as __name;
use super::{Path, PathKind};
macro_rules! P {
($start:ident $(:: $seg:ident)*) => { Path::from_simple_segments(PathKind::Abs, vec![name![$start], $(name![$seg],)*]) };
}
pub fn std_iter_into_iterator() -> Path {
P![std::iter::IntoIterator]
}
pub fn std_ops_try() -> Path {
P![std::ops::Try]
}
pub fn std_ops_range() -> Path {
P![std::ops::Range]
}
pub fn std_ops_range_from() -> Path {
P![std::ops::RangeFrom]
}
pub fn std_ops_range_full() -> Path {
P![std::ops::RangeFull]
}
pub fn std_ops_range_inclusive() -> Path {
P![std::ops::RangeInclusive]
}
pub fn std_ops_range_to() -> Path {
P![std::ops::RangeTo]
}
pub fn std_ops_range_to_inclusive() -> Path {
P![std::ops::RangeToInclusive]
}
pub fn std_ops_neg() -> Path {
P![std::ops::Neg]
}
pub fn std_ops_not() -> Path {
P![std::ops::Not]
}
pub fn std_result_result() -> Path {
P![std::result::Result]
}
pub fn std_future_future() -> Path {
P![std::future::Future]
}
pub fn std_boxed_box() -> Path {
P![std::boxed::Box]
}
#[macro_export]
macro_rules! __known_path {
(std::iter::IntoIterator) => {};
(std::result::Result) => {};
(std::ops::Range) => {};
(std::ops::RangeFrom) => {};
(std::ops::RangeFull) => {};
(std::ops::RangeTo) => {};
(std::ops::RangeToInclusive) => {};
(std::ops::RangeInclusive) => {};
(std::boxed::Box) => {};
(std::future::Future) => {};
(std::ops::Try) => {};
(std::ops::Neg) => {};
(std::ops::Not) => {};
($path:path) => {
compile_error!("Please register your known path in the path module")
};
}
#[macro_export]
macro_rules! __path {
($start:ident $(:: $seg:ident)*) => ({
$crate::__known_path!($start $(:: $seg)*);
$crate::path::Path::from_simple_segments($crate::path::PathKind::Abs, vec![
$crate::path::__name![$start], $($crate::path::__name![$seg],)*
])
});
}
pub use crate::__path as path;

View File

@ -3,7 +3,7 @@
use std::sync::Arc;
use hir_def::{
path::{known, Path},
path::{path, Path},
resolver::HasResolver,
AdtId, FunctionId,
};
@ -124,7 +124,7 @@ fn validate_results_in_tail_expr(
None => return,
};
let std_result_path = known::std_result_result();
let std_result_path = path![std::result::Result];
let resolver = self.func.resolver(db);
let std_result_enum = match resolver.resolve_known_enum(db, &std_result_path) {

View File

@ -24,7 +24,7 @@
body::Body,
data::{ConstData, FunctionData},
expr::{BindingAnnotation, ExprId, PatId},
path::{known, Path},
path::{path, Path},
resolver::{HasResolver, Resolver, TypeNs},
type_ref::{Mutability, TypeRef},
AdtId, AssocItemId, DefWithBodyId, FunctionId, StructFieldId, TypeAliasId, VariantId,
@ -422,73 +422,73 @@ fn infer_body(&mut self) {
}
fn resolve_into_iter_item(&self) -> Option<TypeAliasId> {
let path = known::std_iter_into_iterator();
let path = path![std::iter::IntoIterator];
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
self.db.trait_data(trait_).associated_type_by_name(&name![Item])
}
fn resolve_ops_try_ok(&self) -> Option<TypeAliasId> {
let path = known::std_ops_try();
let path = path![std::ops::Try];
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
self.db.trait_data(trait_).associated_type_by_name(&name![Ok])
}
fn resolve_ops_neg_output(&self) -> Option<TypeAliasId> {
let path = known::std_ops_neg();
let path = path![std::ops::Neg];
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
self.db.trait_data(trait_).associated_type_by_name(&name![Output])
}
fn resolve_ops_not_output(&self) -> Option<TypeAliasId> {
let path = known::std_ops_not();
let path = path![std::ops::Not];
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
self.db.trait_data(trait_).associated_type_by_name(&name![Output])
}
fn resolve_future_future_output(&self) -> Option<TypeAliasId> {
let path = known::std_future_future();
let path = path![std::future::Future];
let trait_ = self.resolver.resolve_known_trait(self.db, &path)?;
self.db.trait_data(trait_).associated_type_by_name(&name![Output])
}
fn resolve_boxed_box(&self) -> Option<AdtId> {
let path = known::std_boxed_box();
let path = path![std::boxed::Box];
let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
Some(struct_.into())
}
fn resolve_range_full(&self) -> Option<AdtId> {
let path = known::std_ops_range_full();
let path = path![std::ops::RangeFull];
let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
Some(struct_.into())
}
fn resolve_range(&self) -> Option<AdtId> {
let path = known::std_ops_range();
let path = path![std::ops::Range];
let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
Some(struct_.into())
}
fn resolve_range_inclusive(&self) -> Option<AdtId> {
let path = known::std_ops_range_inclusive();
let path = path![std::ops::RangeInclusive];
let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
Some(struct_.into())
}
fn resolve_range_from(&self) -> Option<AdtId> {
let path = known::std_ops_range_from();
let path = path![std::ops::RangeFrom];
let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
Some(struct_.into())
}
fn resolve_range_to(&self) -> Option<AdtId> {
let path = known::std_ops_range_to();
let path = path![std::ops::RangeTo];
let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
Some(struct_.into())
}
fn resolve_range_to_inclusive(&self) -> Option<AdtId> {
let path = known::std_ops_range_to_inclusive();
let path = path![std::ops::RangeToInclusive];
let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
Some(struct_.into())
}