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