From 259c42f00e2e85594c7373166bc8467ce375a045 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 13 Dec 2019 21:43:53 +0100 Subject: Add macros for known names and paths --- crates/ra_hir_def/src/body/lower.rs | 4 +-- crates/ra_hir_def/src/builtin_type.rs | 42 ++++++++++++++-------------- crates/ra_hir_def/src/data.rs | 4 +-- crates/ra_hir_def/src/generics.rs | 6 ++-- crates/ra_hir_def/src/nameres/collector.rs | 4 +-- crates/ra_hir_def/src/path.rs | 45 +++++++++++++----------------- crates/ra_hir_def/src/resolver.rs | 16 +++++------ 7 files changed, 58 insertions(+), 63 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index cc068ff94..6c760166f 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -2,7 +2,7 @@ //! representation. use either::Either; -use hir_expand::name::{self, AsName, Name}; +use hir_expand::name::{AsName, Name, N}; 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: N![self], mode: BindingAnnotation::Unannotated, subpat: None, }, diff --git a/crates/ra_hir_def/src/builtin_type.rs b/crates/ra_hir_def/src/builtin_type.rs index 5e8157144..757123f82 100644 --- a/crates/ra_hir_def/src/builtin_type.rs +++ b/crates/ra_hir_def/src/builtin_type.rs @@ -5,7 +5,7 @@ use std::fmt; -use hir_expand::name::{self, Name}; +use hir_expand::name::{Name, N}; #[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::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::F32, BuiltinType::Float(BuiltinFloat::F32)), - (name::F64, BuiltinType::Float(BuiltinFloat::F64)), + (N![char], BuiltinType::Char), + (N![bool], BuiltinType::Bool), + (N![str], BuiltinType::Str), + + (N![isize], BuiltinType::Int(BuiltinInt::ISIZE)), + (N![i8], BuiltinType::Int(BuiltinInt::I8)), + (N![i16], BuiltinType::Int(BuiltinInt::I16)), + (N![i32], BuiltinType::Int(BuiltinInt::I32)), + (N![i64], BuiltinType::Int(BuiltinInt::I64)), + (N![i128], BuiltinType::Int(BuiltinInt::I128)), + + (N![usize], BuiltinType::Int(BuiltinInt::USIZE)), + (N![u8], BuiltinType::Int(BuiltinInt::U8)), + (N![u16], BuiltinType::Int(BuiltinInt::U16)), + (N![u32], BuiltinType::Int(BuiltinInt::U32)), + (N![u64], BuiltinType::Int(BuiltinInt::U64)), + (N![u128], BuiltinType::Int(BuiltinInt::U128)), + + (N![f32], BuiltinType::Float(BuiltinFloat::F32)), + (N![f64], BuiltinType::Float(BuiltinFloat::F64)), ]; } diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index b2dac183e..afeac0ec2 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use hir_expand::{ - name::{self, AsName, Name}, + name::{AsName, Name, N}, 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(N![Self].into()); match self_param.kind() { ast::SelfParamKind::Owned => self_type, ast::SelfParamKind::Ref => { diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index e502dd798..2b13748f3 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use either::Either; use hir_expand::{ - name::{self, AsName, Name}, + name::{AsName, Name, N}, 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: N![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(N![Self].into()); generics.fill_bounds(&src.value, self_param); generics.fill(&mut sm, &src.value); diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 04aadead1..e81bac914 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -6,7 +6,7 @@ use hir_expand::{ builtin_derive::find_builtin_derive, builtin_macro::find_builtin_macro, - name::{self, AsName, Name}, + name::{AsName, Name, N}, 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(&N![macro_rules]) } #[cfg(test)] diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 50f0cad94..36ad27867 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -6,7 +6,7 @@ use std::{iter, sync::Arc}; use either::Either; use hir_expand::{ hygiene::Hygiene, - name::{self, AsName, Name}, + name::{AsName, Name, N}, }; use ra_db::CrateId; use ra_syntax::{ @@ -276,7 +276,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((N![Output], type_ref)) } if args.is_empty() && bindings.is_empty() { None @@ -297,68 +297,63 @@ impl From for Path { } pub mod known { - use hir_expand::name; + use hir_expand::name::N; 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 { - Path::from_simple_segments( - PathKind::Abs, - vec![name::STD, name::ITER, name::INTO_ITERATOR_TYPE], - ) + P![std::iter::IntoIterator] } 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 { - 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 { - 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 { - 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 { - Path::from_simple_segments( - PathKind::Abs, - vec![name::STD, name::OPS, name::RANGE_INCLUSIVE_TYPE], - ) + P![std::ops::RangeInclusive] } 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 { - Path::from_simple_segments( - PathKind::Abs, - vec![name::STD, name::OPS, name::RANGE_TO_INCLUSIVE_TYPE], - ) + P![std::ops::RangeToInclusive] } 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 { - Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::NOT_TYPE]) + P![std::ops::Not] } 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 { - Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::FUTURE, name::FUTURE_TYPE]) + P![std::future::Future] } pub fn std_boxed_box() -> Path { - Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::BOXED, name::BOX_TYPE]) + P![std::boxed::Box] } } diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 17b2169d2..0fb529527 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use hir_expand::{ - name::{self, Name}, + name::{Name, N}, MacroDefId, }; use ra_db::CrateId; @@ -163,13 +163,13 @@ impl Resolver { } } 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) }; return Some((TypeNs::SelfType(*impl_), idx)); } } 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) }; 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 = N![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 == &N![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 == &N![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(N![Self], ScopeDef::ImplSelfType((*i).into())); } Scope::AdtScope(i) => { - f(name::SELF_TYPE, ScopeDef::AdtSelfType((*i).into())); + f(N![Self], ScopeDef::AdtSelfType((*i).into())); } Scope::ExprScope(scope) => { scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| { -- cgit v1.2.3