mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-26 14:43:24 +00:00
Merge #2554
2554: Add macros for known names and paths r=matklad a=flodiebold Co-authored-by: Florian Diebold <flodiebold@gmail.com>
This commit is contained in:
commit
77db617765
@ -17,7 +17,7 @@ use hir_def::{
|
||||
};
|
||||
use hir_expand::{
|
||||
diagnostics::DiagnosticSink,
|
||||
name::{self, AsName},
|
||||
name::{name, AsName},
|
||||
MacroDefId,
|
||||
};
|
||||
use hir_ty::{
|
||||
@ -723,7 +723,7 @@ impl Local {
|
||||
}
|
||||
|
||||
pub fn is_self(self, db: &impl HirDatabase) -> bool {
|
||||
self.name(db) == Some(name::SELF_PARAM)
|
||||
self.name(db) == Some(name![self])
|
||||
}
|
||||
|
||||
pub fn is_mut(self, db: &impl HirDatabase) -> bool {
|
||||
|
@ -15,7 +15,7 @@ use hir_def::{
|
||||
},
|
||||
expr::{ExprId, PatId},
|
||||
nameres::ModuleSource,
|
||||
path::known,
|
||||
path::path,
|
||||
resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
|
||||
AssocItemId, DefWithBodyId,
|
||||
};
|
||||
@ -418,7 +418,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: 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(),
|
||||
|
@ -2,7 +2,7 @@
|
||||
//! representation.
|
||||
|
||||
use either::Either;
|
||||
use hir_expand::name::{self, AsName, Name};
|
||||
use hir_expand::name::{name, AsName, Name};
|
||||
use ra_arena::Arena;
|
||||
use ra_syntax::{
|
||||
ast::{
|
||||
@ -68,7 +68,7 @@ where
|
||||
let ptr = AstPtr::new(&self_param);
|
||||
let param_pat = self.alloc_pat(
|
||||
Pat::Bind {
|
||||
name: name::SELF_PARAM,
|
||||
name: name![self],
|
||||
mode: BindingAnnotation::Unannotated,
|
||||
subpat: None,
|
||||
},
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use hir_expand::name::{self, Name};
|
||||
use hir_expand::name::{name, Name};
|
||||
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
||||
pub enum Signedness {
|
||||
@ -52,26 +52,26 @@ pub enum BuiltinType {
|
||||
impl BuiltinType {
|
||||
#[rustfmt::skip]
|
||||
pub const ALL: &'static [(Name, BuiltinType)] = &[
|
||||
(name::CHAR, BuiltinType::Char),
|
||||
(name::BOOL, BuiltinType::Bool),
|
||||
(name::STR, BuiltinType::Str ),
|
||||
(name![char], BuiltinType::Char),
|
||||
(name![bool], BuiltinType::Bool),
|
||||
(name![str], BuiltinType::Str),
|
||||
|
||||
(name::ISIZE, BuiltinType::Int(BuiltinInt::ISIZE)),
|
||||
(name::I8, BuiltinType::Int(BuiltinInt::I8)),
|
||||
(name::I16, BuiltinType::Int(BuiltinInt::I16)),
|
||||
(name::I32, BuiltinType::Int(BuiltinInt::I32)),
|
||||
(name::I64, BuiltinType::Int(BuiltinInt::I64)),
|
||||
(name::I128, BuiltinType::Int(BuiltinInt::I128)),
|
||||
(name![isize], BuiltinType::Int(BuiltinInt::ISIZE)),
|
||||
(name![i8], BuiltinType::Int(BuiltinInt::I8)),
|
||||
(name![i16], BuiltinType::Int(BuiltinInt::I16)),
|
||||
(name![i32], BuiltinType::Int(BuiltinInt::I32)),
|
||||
(name![i64], BuiltinType::Int(BuiltinInt::I64)),
|
||||
(name![i128], BuiltinType::Int(BuiltinInt::I128)),
|
||||
|
||||
(name::USIZE, BuiltinType::Int(BuiltinInt::USIZE)),
|
||||
(name::U8, BuiltinType::Int(BuiltinInt::U8)),
|
||||
(name::U16, BuiltinType::Int(BuiltinInt::U16)),
|
||||
(name::U32, BuiltinType::Int(BuiltinInt::U32)),
|
||||
(name::U64, BuiltinType::Int(BuiltinInt::U64)),
|
||||
(name::U128, BuiltinType::Int(BuiltinInt::U128)),
|
||||
(name![usize], BuiltinType::Int(BuiltinInt::USIZE)),
|
||||
(name![u8], BuiltinType::Int(BuiltinInt::U8)),
|
||||
(name![u16], BuiltinType::Int(BuiltinInt::U16)),
|
||||
(name![u32], BuiltinType::Int(BuiltinInt::U32)),
|
||||
(name![u64], BuiltinType::Int(BuiltinInt::U64)),
|
||||
(name![u128], BuiltinType::Int(BuiltinInt::U128)),
|
||||
|
||||
(name::F32, BuiltinType::Float(BuiltinFloat::F32)),
|
||||
(name::F64, BuiltinType::Float(BuiltinFloat::F64)),
|
||||
(name![f32], BuiltinType::Float(BuiltinFloat::F32)),
|
||||
(name![f64], BuiltinType::Float(BuiltinFloat::F64)),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use hir_expand::{
|
||||
name::{self, AsName, Name},
|
||||
name::{name, AsName, Name},
|
||||
AstId,
|
||||
};
|
||||
use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
|
||||
@ -37,7 +37,7 @@ impl FunctionData {
|
||||
let self_type = if let Some(type_ref) = self_param.ascribed_type() {
|
||||
TypeRef::from_ast(type_ref)
|
||||
} else {
|
||||
let self_type = TypeRef::Path(name::SELF_TYPE.into());
|
||||
let self_type = TypeRef::Path(name![Self].into());
|
||||
match self_param.kind() {
|
||||
ast::SelfParamKind::Owned => self_type,
|
||||
ast::SelfParamKind::Ref => {
|
||||
|
@ -6,7 +6,7 @@ use std::sync::Arc;
|
||||
|
||||
use either::Either;
|
||||
use hir_expand::{
|
||||
name::{self, AsName, Name},
|
||||
name::{name, AsName, Name},
|
||||
InFile,
|
||||
};
|
||||
use ra_arena::{map::ArenaMap, Arena};
|
||||
@ -90,11 +90,11 @@ impl GenericParams {
|
||||
|
||||
// traits get the Self type as an implicit first type parameter
|
||||
let self_param_id =
|
||||
generics.types.alloc(TypeParamData { name: name::SELF_TYPE, default: None });
|
||||
generics.types.alloc(TypeParamData { name: name![Self], default: None });
|
||||
sm.insert(self_param_id, Either::Left(src.value.clone()));
|
||||
// add super traits as bounds on Self
|
||||
// i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
|
||||
let self_param = TypeRef::Path(name::SELF_TYPE.into());
|
||||
let self_param = TypeRef::Path(name![Self].into());
|
||||
generics.fill_bounds(&src.value, self_param);
|
||||
|
||||
generics.fill(&mut sm, &src.value);
|
||||
|
@ -6,7 +6,7 @@
|
||||
use hir_expand::{
|
||||
builtin_derive::find_builtin_derive,
|
||||
builtin_macro::find_builtin_macro,
|
||||
name::{self, AsName, Name},
|
||||
name::{name, AsName, Name},
|
||||
HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
|
||||
};
|
||||
use ra_cfg::CfgOptions;
|
||||
@ -918,7 +918,7 @@ where
|
||||
}
|
||||
|
||||
fn is_macro_rules(path: &Path) -> bool {
|
||||
path.as_ident() == Some(&name::MACRO_RULES)
|
||||
path.as_ident() == Some(&name![macro_rules])
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -6,7 +6,7 @@ use std::{iter, sync::Arc};
|
||||
use either::Either;
|
||||
use hir_expand::{
|
||||
hygiene::Hygiene,
|
||||
name::{self, AsName, Name},
|
||||
name::{name, AsName, Name},
|
||||
};
|
||||
use ra_db::CrateId;
|
||||
use ra_syntax::{
|
||||
@ -76,10 +76,7 @@ impl Path {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
@ -276,7 +273,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, type_ref))
|
||||
bindings.push((name![Output], type_ref))
|
||||
}
|
||||
if args.is_empty() && bindings.is_empty() {
|
||||
None
|
||||
@ -296,69 +293,36 @@ impl From<Name> for Path {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod known {
|
||||
use hir_expand::name;
|
||||
pub use hir_expand::name as __name;
|
||||
|
||||
use super::{Path, PathKind};
|
||||
|
||||
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_ops_range() -> Path {
|
||||
Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::RANGE_TYPE])
|
||||
}
|
||||
|
||||
pub fn std_ops_range_from() -> Path {
|
||||
Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::RANGE_FROM_TYPE])
|
||||
}
|
||||
|
||||
pub fn std_ops_range_full() -> Path {
|
||||
Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::RANGE_FULL_TYPE])
|
||||
}
|
||||
|
||||
pub fn std_ops_range_inclusive() -> Path {
|
||||
Path::from_simple_segments(
|
||||
PathKind::Abs,
|
||||
vec![name::STD, name::OPS, name::RANGE_INCLUSIVE_TYPE],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn std_ops_range_to() -> Path {
|
||||
Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::RANGE_TO_TYPE])
|
||||
}
|
||||
|
||||
pub fn std_ops_range_to_inclusive() -> Path {
|
||||
Path::from_simple_segments(
|
||||
PathKind::Abs,
|
||||
vec![name::STD, name::OPS, name::RANGE_TO_INCLUSIVE_TYPE],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn std_ops_neg() -> Path {
|
||||
Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::NEG_TYPE])
|
||||
}
|
||||
|
||||
pub fn std_ops_not() -> Path {
|
||||
Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::NOT_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])
|
||||
}
|
||||
#[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;
|
||||
|
@ -2,7 +2,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use hir_expand::{
|
||||
name::{self, Name},
|
||||
name::{name, Name},
|
||||
MacroDefId,
|
||||
};
|
||||
use ra_db::CrateId;
|
||||
@ -163,13 +163,13 @@ impl Resolver {
|
||||
}
|
||||
}
|
||||
Scope::ImplBlockScope(impl_) => {
|
||||
if first_name == &name::SELF_TYPE {
|
||||
if first_name == &name![Self] {
|
||||
let idx = if path.segments.len() == 1 { None } else { Some(1) };
|
||||
return Some((TypeNs::SelfType(*impl_), idx));
|
||||
}
|
||||
}
|
||||
Scope::AdtScope(adt) => {
|
||||
if first_name == &name::SELF_TYPE {
|
||||
if first_name == &name![Self] {
|
||||
let idx = if path.segments.len() == 1 { None } else { Some(1) };
|
||||
return Some((TypeNs::AdtSelfType(*adt), idx));
|
||||
}
|
||||
@ -223,7 +223,7 @@ impl Resolver {
|
||||
return None;
|
||||
}
|
||||
let n_segments = path.segments.len();
|
||||
let tmp = name::SELF_PARAM;
|
||||
let tmp = name![self];
|
||||
let first_name = if path.is_self() { &tmp } else { &path.segments.first()?.name };
|
||||
let skip_to_mod = path.kind != PathKind::Plain && !path.is_self();
|
||||
for scope in self.scopes.iter().rev() {
|
||||
@ -259,13 +259,13 @@ impl Resolver {
|
||||
Scope::GenericParams { .. } => continue,
|
||||
|
||||
Scope::ImplBlockScope(impl_) if n_segments > 1 => {
|
||||
if first_name == &name::SELF_TYPE {
|
||||
if first_name == &name![Self] {
|
||||
let ty = TypeNs::SelfType(*impl_);
|
||||
return Some(ResolveValueResult::Partial(ty, 1));
|
||||
}
|
||||
}
|
||||
Scope::AdtScope(adt) if n_segments > 1 => {
|
||||
if first_name == &name::SELF_TYPE {
|
||||
if first_name == &name![Self] {
|
||||
let ty = TypeNs::AdtSelfType(*adt);
|
||||
return Some(ResolveValueResult::Partial(ty, 1));
|
||||
}
|
||||
@ -439,10 +439,10 @@ impl Scope {
|
||||
}
|
||||
}
|
||||
Scope::ImplBlockScope(i) => {
|
||||
f(name::SELF_TYPE, ScopeDef::ImplSelfType((*i).into()));
|
||||
f(name![Self], ScopeDef::ImplSelfType((*i).into()));
|
||||
}
|
||||
Scope::AdtScope(i) => {
|
||||
f(name::SELF_TYPE, ScopeDef::AdtSelfType((*i).into()));
|
||||
f(name![Self], ScopeDef::AdtSelfType((*i).into()));
|
||||
}
|
||||
Scope::ExprScope(scope) => {
|
||||
scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| {
|
||||
|
@ -12,10 +12,10 @@ use crate::db::AstDatabase;
|
||||
use crate::{name, quote, MacroCallId, MacroDefId, MacroDefKind};
|
||||
|
||||
macro_rules! register_builtin {
|
||||
( $(($name:ident, $kind: ident) => $expand:ident),* ) => {
|
||||
( $($trait:ident => $expand:ident),* ) => {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum BuiltinDeriveExpander {
|
||||
$($kind),*
|
||||
$($trait),*
|
||||
}
|
||||
|
||||
impl BuiltinDeriveExpander {
|
||||
@ -26,7 +26,7 @@ macro_rules! register_builtin {
|
||||
tt: &tt::Subtree,
|
||||
) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||
let expander = match *self {
|
||||
$( BuiltinDeriveExpander::$kind => $expand, )*
|
||||
$( BuiltinDeriveExpander::$trait => $expand, )*
|
||||
};
|
||||
expander(db, id, tt)
|
||||
}
|
||||
@ -34,7 +34,7 @@ macro_rules! register_builtin {
|
||||
|
||||
pub fn find_builtin_derive(ident: &name::Name) -> Option<MacroDefId> {
|
||||
let kind = match ident {
|
||||
$( id if id == &name::$name => BuiltinDeriveExpander::$kind, )*
|
||||
$( id if id == &name::name![$trait] => BuiltinDeriveExpander::$trait, )*
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
@ -44,15 +44,15 @@ macro_rules! register_builtin {
|
||||
}
|
||||
|
||||
register_builtin! {
|
||||
(COPY_TRAIT, Copy) => copy_expand,
|
||||
(CLONE_TRAIT, Clone) => clone_expand,
|
||||
(DEFAULT_TRAIT, Default) => default_expand,
|
||||
(DEBUG_TRAIT, Debug) => debug_expand,
|
||||
(HASH_TRAIT, Hash) => hash_expand,
|
||||
(ORD_TRAIT, Ord) => ord_expand,
|
||||
(PARTIAL_ORD_TRAIT, PartialOrd) => partial_ord_expand,
|
||||
(EQ_TRAIT, Eq) => eq_expand,
|
||||
(PARTIAL_EQ_TRAIT, PartialEq) => partial_eq_expand
|
||||
Copy => copy_expand,
|
||||
Clone => clone_expand,
|
||||
Default => default_expand,
|
||||
Debug => debug_expand,
|
||||
Hash => hash_expand,
|
||||
Ord => ord_expand,
|
||||
PartialOrd => partial_ord_expand,
|
||||
Eq => eq_expand,
|
||||
PartialEq => partial_eq_expand
|
||||
}
|
||||
|
||||
struct BasicAdtInfo {
|
||||
|
@ -34,7 +34,7 @@ macro_rules! register_builtin {
|
||||
ast_id: AstId<ast::MacroCall>,
|
||||
) -> Option<MacroDefId> {
|
||||
let kind = match ident {
|
||||
$( id if id == &name::$name => BuiltinFnLikeExpander::$kind, )*
|
||||
$( id if id == &name::name![$name] => BuiltinFnLikeExpander::$kind, )*
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
@ -44,15 +44,15 @@ macro_rules! register_builtin {
|
||||
}
|
||||
|
||||
register_builtin! {
|
||||
(COLUMN_MACRO, Column) => column_expand,
|
||||
(COMPILE_ERROR_MACRO, CompileError) => compile_error_expand,
|
||||
(FILE_MACRO, File) => file_expand,
|
||||
(LINE_MACRO, Line) => line_expand,
|
||||
(STRINGIFY_MACRO, Stringify) => stringify_expand,
|
||||
(FORMAT_ARGS_MACRO, FormatArgs) => format_args_expand,
|
||||
(column, Column) => column_expand,
|
||||
(compile_error, CompileError) => compile_error_expand,
|
||||
(file, File) => file_expand,
|
||||
(line, Line) => line_expand,
|
||||
(stringify, Stringify) => stringify_expand,
|
||||
(format_args, FormatArgs) => format_args_expand,
|
||||
// format_args_nl only differs in that it adds a newline in the end,
|
||||
// so we use the same stub expansion for now
|
||||
(FORMAT_ARGS_NL_MACRO, FormatArgsNl) => format_args_expand
|
||||
(format_args_nl, FormatArgsNl) => format_args_expand
|
||||
}
|
||||
|
||||
fn to_line_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize {
|
||||
|
@ -104,73 +104,99 @@ impl AsName for ra_db::Dependency {
|
||||
}
|
||||
}
|
||||
|
||||
// Primitives
|
||||
pub const ISIZE: Name = Name::new_inline_ascii(b"isize");
|
||||
pub const I8: Name = Name::new_inline_ascii(b"i8");
|
||||
pub const I16: Name = Name::new_inline_ascii(b"i16");
|
||||
pub const I32: Name = Name::new_inline_ascii(b"i32");
|
||||
pub const I64: Name = Name::new_inline_ascii(b"i64");
|
||||
pub const I128: Name = Name::new_inline_ascii(b"i128");
|
||||
pub const USIZE: Name = Name::new_inline_ascii(b"usize");
|
||||
pub const U8: Name = Name::new_inline_ascii(b"u8");
|
||||
pub const U16: Name = Name::new_inline_ascii(b"u16");
|
||||
pub const U32: Name = Name::new_inline_ascii(b"u32");
|
||||
pub const U64: Name = Name::new_inline_ascii(b"u64");
|
||||
pub const U128: Name = Name::new_inline_ascii(b"u128");
|
||||
pub const F32: Name = Name::new_inline_ascii(b"f32");
|
||||
pub const F64: Name = Name::new_inline_ascii(b"f64");
|
||||
pub const BOOL: Name = Name::new_inline_ascii(b"bool");
|
||||
pub const CHAR: Name = Name::new_inline_ascii(b"char");
|
||||
pub const STR: Name = Name::new_inline_ascii(b"str");
|
||||
pub mod known {
|
||||
macro_rules! known_names {
|
||||
($($ident:ident),* $(,)?) => {
|
||||
$(
|
||||
#[allow(bad_style)]
|
||||
pub const $ident: super::Name =
|
||||
super::Name::new_inline_ascii(stringify!($ident).as_bytes());
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
// Special names
|
||||
pub const SELF_PARAM: Name = Name::new_inline_ascii(b"self");
|
||||
pub const SELF_TYPE: Name = Name::new_inline_ascii(b"Self");
|
||||
pub const MACRO_RULES: Name = Name::new_inline_ascii(b"macro_rules");
|
||||
known_names!(
|
||||
// Primitives
|
||||
isize,
|
||||
i8,
|
||||
i16,
|
||||
i32,
|
||||
i64,
|
||||
i128,
|
||||
usize,
|
||||
u8,
|
||||
u16,
|
||||
u32,
|
||||
u64,
|
||||
u128,
|
||||
f32,
|
||||
f64,
|
||||
bool,
|
||||
char,
|
||||
str,
|
||||
// Special names
|
||||
macro_rules,
|
||||
// Components of known path (value or mod name)
|
||||
std,
|
||||
iter,
|
||||
ops,
|
||||
future,
|
||||
result,
|
||||
boxed,
|
||||
// Components of known path (type name)
|
||||
IntoIterator,
|
||||
Item,
|
||||
Try,
|
||||
Ok,
|
||||
Future,
|
||||
Result,
|
||||
Output,
|
||||
Target,
|
||||
Box,
|
||||
RangeFrom,
|
||||
RangeFull,
|
||||
RangeInclusive,
|
||||
RangeToInclusive,
|
||||
RangeTo,
|
||||
Range,
|
||||
Neg,
|
||||
Not,
|
||||
// Builtin macros
|
||||
file,
|
||||
column,
|
||||
compile_error,
|
||||
line,
|
||||
stringify,
|
||||
format_args,
|
||||
format_args_nl,
|
||||
// Builtin derives
|
||||
Copy,
|
||||
Clone,
|
||||
Default,
|
||||
Debug,
|
||||
Hash,
|
||||
Ord,
|
||||
PartialOrd,
|
||||
Eq,
|
||||
PartialEq,
|
||||
);
|
||||
|
||||
// Components of known path (value or mod name)
|
||||
pub const STD: Name = Name::new_inline_ascii(b"std");
|
||||
pub const ITER: Name = Name::new_inline_ascii(b"iter");
|
||||
pub const OPS: Name = Name::new_inline_ascii(b"ops");
|
||||
pub const FUTURE: Name = Name::new_inline_ascii(b"future");
|
||||
pub const RESULT: Name = Name::new_inline_ascii(b"result");
|
||||
pub const BOXED: Name = Name::new_inline_ascii(b"boxed");
|
||||
// self/Self cannot be used as an identifier
|
||||
pub const SELF_PARAM: super::Name = super::Name::new_inline_ascii(b"self");
|
||||
pub const SELF_TYPE: super::Name = super::Name::new_inline_ascii(b"Self");
|
||||
|
||||
// Components of known path (type name)
|
||||
pub const INTO_ITERATOR_TYPE: Name = Name::new_inline_ascii(b"IntoIterator");
|
||||
pub const ITEM_TYPE: Name = Name::new_inline_ascii(b"Item");
|
||||
pub const TRY_TYPE: Name = Name::new_inline_ascii(b"Try");
|
||||
pub const OK_TYPE: Name = Name::new_inline_ascii(b"Ok");
|
||||
pub const FUTURE_TYPE: Name = Name::new_inline_ascii(b"Future");
|
||||
pub const RESULT_TYPE: Name = Name::new_inline_ascii(b"Result");
|
||||
pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(b"Output");
|
||||
pub const TARGET_TYPE: Name = Name::new_inline_ascii(b"Target");
|
||||
pub const BOX_TYPE: Name = Name::new_inline_ascii(b"Box");
|
||||
pub const RANGE_FROM_TYPE: Name = Name::new_inline_ascii(b"RangeFrom");
|
||||
pub const RANGE_FULL_TYPE: Name = Name::new_inline_ascii(b"RangeFull");
|
||||
pub const RANGE_INCLUSIVE_TYPE: Name = Name::new_inline_ascii(b"RangeInclusive");
|
||||
pub const RANGE_TO_INCLUSIVE_TYPE: Name = Name::new_inline_ascii(b"RangeToInclusive");
|
||||
pub const RANGE_TO_TYPE: Name = Name::new_inline_ascii(b"RangeTo");
|
||||
pub const RANGE_TYPE: Name = Name::new_inline_ascii(b"Range");
|
||||
pub const NEG_TYPE: Name = Name::new_inline_ascii(b"Neg");
|
||||
pub const NOT_TYPE: Name = Name::new_inline_ascii(b"Not");
|
||||
#[macro_export]
|
||||
macro_rules! name {
|
||||
(self) => {
|
||||
$crate::name::known::SELF_PARAM
|
||||
};
|
||||
(Self) => {
|
||||
$crate::name::known::SELF_TYPE
|
||||
};
|
||||
($ident:ident) => {
|
||||
$crate::name::known::$ident
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Builtin Macros
|
||||
pub const FILE_MACRO: Name = Name::new_inline_ascii(b"file");
|
||||
pub const COLUMN_MACRO: Name = Name::new_inline_ascii(b"column");
|
||||
pub const COMPILE_ERROR_MACRO: Name = Name::new_inline_ascii(b"compile_error");
|
||||
pub const LINE_MACRO: Name = Name::new_inline_ascii(b"line");
|
||||
pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(b"stringify");
|
||||
pub const FORMAT_ARGS_MACRO: Name = Name::new_inline_ascii(b"format_args");
|
||||
pub const FORMAT_ARGS_NL_MACRO: Name = Name::new_inline_ascii(b"format_args_nl");
|
||||
|
||||
// Builtin derives
|
||||
pub const COPY_TRAIT: Name = Name::new_inline_ascii(b"Copy");
|
||||
pub const CLONE_TRAIT: Name = Name::new_inline_ascii(b"Clone");
|
||||
pub const DEFAULT_TRAIT: Name = Name::new_inline_ascii(b"Default");
|
||||
pub const DEBUG_TRAIT: Name = Name::new_inline_ascii(b"Debug");
|
||||
pub const HASH_TRAIT: Name = Name::new_inline_ascii(b"Hash");
|
||||
pub const ORD_TRAIT: Name = Name::new_inline_ascii(b"Ord");
|
||||
pub const PARTIAL_ORD_TRAIT: Name = Name::new_inline_ascii(b"PartialOrd");
|
||||
pub const EQ_TRAIT: Name = Name::new_inline_ascii(b"Eq");
|
||||
pub const PARTIAL_EQ_TRAIT: Name = Name::new_inline_ascii(b"PartialEq");
|
||||
pub use crate::name;
|
||||
|
@ -6,7 +6,7 @@
|
||||
use std::iter::successors;
|
||||
|
||||
use hir_def::lang_item::LangItemTarget;
|
||||
use hir_expand::name;
|
||||
use hir_expand::name::name;
|
||||
use log::{info, warn};
|
||||
use ra_db::CrateId;
|
||||
|
||||
@ -52,7 +52,7 @@ fn deref_by_trait(
|
||||
LangItemTarget::TraitId(it) => it,
|
||||
_ => return None,
|
||||
};
|
||||
let target = db.trait_data(deref_trait).associated_type_by_name(&name::TARGET_TYPE)?;
|
||||
let target = db.trait_data(deref_trait).associated_type_by_name(&name![Target])?;
|
||||
|
||||
let generic_params = generics(db, target.into());
|
||||
if generic_params.len() != 1 {
|
||||
|
@ -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 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
|
||||
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) {
|
||||
|
@ -24,12 +24,12 @@ use hir_def::{
|
||||
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,
|
||||
};
|
||||
use hir_expand::{diagnostics::DiagnosticSink, name};
|
||||
use hir_expand::{diagnostics::DiagnosticSink, name::name};
|
||||
use ra_arena::map::ArenaMap;
|
||||
use ra_prof::profile;
|
||||
|
||||
@ -422,73 +422,73 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||
}
|
||||
|
||||
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_TYPE)
|
||||
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_TYPE)
|
||||
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_TYPE)
|
||||
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_TYPE)
|
||||
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_TYPE)
|
||||
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())
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ use hir_def::{
|
||||
resolver::resolver_for_expr,
|
||||
AdtId, ContainerId, Lookup, StructFieldId,
|
||||
};
|
||||
use hir_expand::name::{self, Name};
|
||||
use hir_expand::name::{name, Name};
|
||||
use ra_syntax::ast::RangeOp;
|
||||
|
||||
use crate::{
|
||||
@ -631,7 +631,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||
// Parent arguments are unknown, except for the receiver type
|
||||
if let Some(parent_generics) = def_generics.as_ref().map(|p| p.iter_parent()) {
|
||||
for (_id, param) in parent_generics {
|
||||
if param.name == name::SELF_TYPE {
|
||||
if param.name == name![Self] {
|
||||
substs.push(receiver_ty.clone());
|
||||
} else {
|
||||
substs.push(Ty::Unknown);
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! This module provides the built-in trait implementations, e.g. to make
|
||||
//! closures implement `Fn`.
|
||||
use hir_def::{expr::Expr, lang_item::LangItemTarget, TraitId, TypeAliasId};
|
||||
use hir_expand::name;
|
||||
use hir_expand::name::name;
|
||||
use ra_db::CrateId;
|
||||
|
||||
use super::{AssocTyValue, Impl};
|
||||
@ -79,7 +79,7 @@ fn closure_fn_trait_impl_datum(
|
||||
// and don't want to return a valid value only to find out later that FnOnce
|
||||
// is broken
|
||||
let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?;
|
||||
let _output = db.trait_data(fn_once_trait).associated_type_by_name(&name::OUTPUT_TYPE)?;
|
||||
let _output = db.trait_data(fn_once_trait).associated_type_by_name(&name![Output])?;
|
||||
|
||||
let num_args: u16 = match &db.body(data.def.into())[data.expr] {
|
||||
Expr::Lambda { args, .. } => args.len() as u16,
|
||||
@ -137,7 +137,7 @@ fn closure_fn_trait_output_assoc_ty_value(
|
||||
|
||||
let output_ty_id = db
|
||||
.trait_data(fn_once_trait)
|
||||
.associated_type_by_name(&name::OUTPUT_TYPE)
|
||||
.associated_type_by_name(&name![Output])
|
||||
.expect("assoc ty value should not exist");
|
||||
|
||||
BuiltinImplAssocTyValueData {
|
||||
|
@ -10,10 +10,8 @@ use hir_def::{
|
||||
type_ref::TypeRef,
|
||||
ContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId,
|
||||
};
|
||||
use hir_expand::name::{self, Name};
|
||||
use hir_expand::name::{name, Name};
|
||||
|
||||
// FIXME: this is wrong, b/c it can't express `trait T: PartialEq<()>`.
|
||||
// We should return a `TraitREf` here.
|
||||
fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> {
|
||||
let resolver = trait_.resolver(db);
|
||||
// returning the iterator directly doesn't easily work because of
|
||||
@ -24,7 +22,7 @@ fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> {
|
||||
.where_predicates
|
||||
.iter()
|
||||
.filter_map(|pred| match &pred.type_ref {
|
||||
TypeRef::Path(p) if p.as_ident() == Some(&name::SELF_TYPE) => pred.bound.as_path(),
|
||||
TypeRef::Path(p) if p.as_ident() == Some(&name![Self]) => pred.bound.as_path(),
|
||||
_ => None,
|
||||
})
|
||||
.filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path) {
|
||||
|
Loading…
Reference in New Issue
Block a user