From 012fec54dc508896493d5d3073b4786c93d66fe9 Mon Sep 17 00:00:00 2001 From: Muhammad Mominul Huque Date: Mon, 8 Jul 2019 03:29:38 +0600 Subject: Constify KnownName's --- crates/ra_hir/src/code_model.rs | 47 ++++++------ crates/ra_hir/src/expr.rs | 4 +- crates/ra_hir/src/generics.rs | 3 +- crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/name.rs | 136 ++++++--------------------------- crates/ra_hir/src/nameres.rs | 6 +- crates/ra_hir/src/nameres/collector.rs | 5 +- crates/ra_hir/src/resolve.rs | 10 +-- crates/ra_hir/src/ty/infer.rs | 13 ++-- crates/ra_syntax/Cargo.toml | 2 +- 10 files changed, 73 insertions(+), 155 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 6cf2c620d..8922163ad 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -19,6 +19,7 @@ use crate::{ TypeAliasId, }, impl_block::ImplBlock, + name::{BOOL, CHAR, F32, F64, I128, I16, I32, I64, I8, ISIZE, STR, U128, U16, U32, U64, U8, USIZE, SELF_TYPE}, nameres::{CrateModuleId, ImportId, ModuleScope, Namespace}, resolve::Resolver, traits::{TraitData, TraitItem}, @@ -28,7 +29,7 @@ use crate::{ }, type_ref::Mutability, type_ref::TypeRef, - AsName, AstDatabase, AstId, DefDatabase, Either, HasSource, HirDatabase, KnownName, Name, Ty, + AsName, AstDatabase, AstId, DefDatabase, Either, HasSource, HirDatabase, Name, Ty, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -96,27 +97,27 @@ pub enum BuiltinType { impl BuiltinType { #[rustfmt::skip] - pub(crate) const ALL: &'static [(KnownName, BuiltinType)] = &[ - (KnownName::Char, BuiltinType::Char), - (KnownName::Bool, BuiltinType::Bool), - (KnownName::Str, BuiltinType::Str), - - (KnownName::Isize, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize })), - (KnownName::I8, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 })), - (KnownName::I16, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 })), - (KnownName::I32, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 })), - (KnownName::I64, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 })), - (KnownName::I128, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 })), - - (KnownName::Usize, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize })), - (KnownName::U8, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 })), - (KnownName::U16, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 })), - (KnownName::U32, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 })), - (KnownName::U64, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 })), - (KnownName::U128, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 })), - - (KnownName::F32, BuiltinType::Float(FloatTy { bitness: FloatBitness::X32 })), - (KnownName::F64, BuiltinType::Float(FloatTy { bitness: FloatBitness::X64 })), + pub(crate) const ALL: &'static [(Name, BuiltinType)] = &[ + (CHAR, BuiltinType::Char), + (BOOL, BuiltinType::Bool), + (STR, BuiltinType::Str), + + (ISIZE, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize })), + (I8, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 })), + (I16, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 })), + (I32, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 })), + (I64, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 })), + (I128, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 })), + + (USIZE, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize })), + (U8, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 })), + (U16, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 })), + (U32, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 })), + (U64, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 })), + (U128, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 })), + + (F32, BuiltinType::Float(FloatTy { bitness: FloatBitness::X32 })), + (F64, BuiltinType::Float(FloatTy { bitness: FloatBitness::X64 })), ]; } @@ -560,7 +561,7 @@ impl FnData { 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(SELF_TYPE.into()); match self_param.kind() { ast::SelfParamKind::Owned => self_type, ast::SelfParamKind::Ref => { diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 3e763fef0..3a97d97ce 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -13,7 +13,7 @@ use ra_syntax::{ }; use crate::{ - name::AsName, + name::{AsName, SELF_PARAM}, type_ref::{Mutability, TypeRef}, DefWithBody, Either, HasSource, HirDatabase, HirFileId, MacroCallLoc, MacroFileKind, Name, Path, Resolver, @@ -981,7 +981,7 @@ where let ptr = AstPtr::new(self_param); let param_pat = self.alloc_pat( Pat::Bind { - name: Name::self_param(), + name: SELF_PARAM, mode: BindingAnnotation::Unannotated, subpat: None, }, diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index 521e47090..3ad2c551a 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -9,6 +9,7 @@ use ra_syntax::ast::{self, DefaultTypeParamOwner, NameOwner, TypeBoundsOwner, Ty use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, + name::SELF_TYPE, path::Path, type_ref::TypeRef, AdtDef, AsName, Container, Enum, EnumVariant, Function, HasSource, ImplBlock, Name, Struct, @@ -83,7 +84,7 @@ impl GenericParams { // traits get the Self type as an implicit first type parameter generics.params.push(GenericParam { idx: start, - name: Name::self_type(), + name: SELF_TYPE, default: None, }); generics.fill(&*it.source(db).ast, start + 1); diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index ec0676783..56831ba05 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -49,7 +49,7 @@ mod marks; use crate::{ db::{AstDatabase, DefDatabase, HirDatabase, InternDatabase}, ids::MacroFileKind, - name::{AsName, KnownName}, + name::AsName, resolve::Resolver, source_id::{AstId, FileAstId}, }; diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs index b71590560..718795314 100644 --- a/crates/ra_hir/src/name.rs +++ b/crates/ra_hir/src/name.rs @@ -26,7 +26,7 @@ impl Name { /// Note: this is private to make creating name from random string hard. /// Hopefully, this should allow us to integrate hygiene cleaner in the /// future, and to switch to interned representation of names. - fn new(text: SmolStr) -> Name { + const fn new(text: SmolStr) -> Name { Name { text } } @@ -34,14 +34,6 @@ impl Name { Name::new("[missing name]".into()) } - pub(crate) fn self_param() -> Name { - Name::new("self".into()) - } - - pub(crate) fn self_type() -> Name { - Name::new("Self".into()) - } - pub(crate) fn tuple_field_name(idx: usize) -> Name { Name::new(idx.to_string().into()) } @@ -63,38 +55,6 @@ impl Name { pub fn as_smolstr(&self) -> &SmolStr { &self.text } - - pub(crate) fn as_known_name(&self) -> Option { - let name = match self.text.as_str() { - "isize" => KnownName::Isize, - "i8" => KnownName::I8, - "i16" => KnownName::I16, - "i32" => KnownName::I32, - "i64" => KnownName::I64, - "i128" => KnownName::I128, - "usize" => KnownName::Usize, - "u8" => KnownName::U8, - "u16" => KnownName::U16, - "u32" => KnownName::U32, - "u64" => KnownName::U64, - "u128" => KnownName::U128, - "f32" => KnownName::F32, - "f64" => KnownName::F64, - "bool" => KnownName::Bool, - "char" => KnownName::Char, - "str" => KnownName::Str, - "Self" => KnownName::SelfType, - "self" => KnownName::SelfParam, - "macro_rules" => KnownName::MacroRules, - - "std" => KnownName::Std, - "iter" => KnownName::Iter, - "IntoIterator" => KnownName::IntoIterator, - "Item" => KnownName::Item, - _ => return None, - }; - Some(name) - } } pub(crate) trait AsName { @@ -130,76 +90,30 @@ impl AsName for ra_db::Dependency { } } -// Ideally, should be replaced with -// ``` -// const ISIZE: Name = Name::new("isize") -// ``` -// but const-fn is not that powerful yet. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum KnownName { - Isize, - I8, - I16, - I32, - I64, - I128, - - Usize, - U8, - U16, - U32, - U64, - U128, - - F32, - F64, - - Bool, - Char, - Str, - - SelfType, - SelfParam, - - MacroRules, - - Std, - Iter, - IntoIterator, - Item, -} - -impl AsName for KnownName { - fn as_name(&self) -> Name { - let s = match self { - KnownName::Isize => "isize", - KnownName::I8 => "i8", - KnownName::I16 => "i16", - KnownName::I32 => "i32", - KnownName::I64 => "i64", - KnownName::I128 => "i128", - KnownName::Usize => "usize", - KnownName::U8 => "u8", - KnownName::U16 => "u16", - KnownName::U32 => "u32", - KnownName::U64 => "u64", - KnownName::U128 => "u128", - KnownName::F32 => "f32", - KnownName::F64 => "f64", - KnownName::Bool => "bool", - KnownName::Char => "char", - KnownName::Str => "str", - KnownName::SelfType => "Self", - KnownName::SelfParam => "self", - KnownName::MacroRules => "macro_rules", - KnownName::Std => "std", - KnownName::Iter => "iter", - KnownName::IntoIterator => "IntoIterator", - KnownName::Item => "Item", - }; - Name::new(s.into()) - } -} +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")); +pub(crate) const I32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i32")); +pub(crate) const I64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i64")); +pub(crate) const I128: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"i128")); +pub(crate) const USIZE: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"usize")); +pub(crate) const U8: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"u8")); +pub(crate) const U16: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u16")); +pub(crate) const U32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u32")); +pub(crate) const U64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u64")); +pub(crate) const U128: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"u128")); +pub(crate) const F32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"f32")); +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")); +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")); +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")); fn resolve_name(text: &SmolStr) -> SmolStr { let raw_start = "r#"; diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index c84d2eada..4c5623063 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -65,7 +65,7 @@ use test_utils::tested_by; use crate::{ diagnostics::DiagnosticSink, either::Either, ids::MacroDefId, - nameres::diagnostics::DefDiagnostic, AsName, AstDatabase, AstId, BuiltinType, Crate, + nameres::diagnostics::DefDiagnostic, AstDatabase, AstId, BuiltinType, Crate, DefDatabase, HirFileId, MacroDef, Module, ModuleDef, Name, Path, PathKind, Trait, }; @@ -138,8 +138,8 @@ pub struct ModuleScope { static BUILTIN_SCOPE: Lazy> = Lazy::new(|| { BuiltinType::ALL .iter() - .map(|&(known_name, ty)| { - (known_name.as_name(), Resolution { def: PerNs::types(ty.into()), import: None }) + .map(|(name, ty)| { + (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: None }) }) .collect() }); diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 9f197bb58..1c3b453bd 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -8,11 +8,12 @@ use test_utils::tested_by; use crate::{ either::Either, ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind}, + name::MACRO_RULES, nameres::{ diagnostics::DefDiagnostic, raw, CrateDefMap, CrateModuleId, ItemOrMacro, ModuleData, ModuleDef, PerNs, ReachedFixedPoint, Resolution, ResolveMode, }, - AstId, Const, DefDatabase, Enum, Function, HirFileId, KnownName, MacroDef, Module, Name, Path, + AstId, Const, DefDatabase, Enum, Function, HirFileId, MacroDef, Module, Name, Path, Static, Struct, Trait, TypeAlias, Union, }; @@ -624,7 +625,7 @@ where } fn is_macro_rules(path: &Path) -> bool { - path.as_ident().and_then(Name::as_known_name) == Some(KnownName::MacroRules) + path.as_ident() == Some(&MACRO_RULES) } fn resolve_submodule( diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index e2a7639b0..ef75308f6 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -13,7 +13,7 @@ use crate::{ }, generics::GenericParams, impl_block::ImplBlock, - name::{KnownName, Name}, + name::{Name, SELF_PARAM, SELF_TYPE}, nameres::{CrateDefMap, CrateModuleId, PerNs}, path::Path, MacroDef, ModuleDef, Trait, @@ -151,7 +151,7 @@ impl Resolver { if let Some(name) = path.as_ident() { PathResult::from_resolution(self.resolve_name(db, name)) } else if path.is_self() { - PathResult::from_resolution(self.resolve_name(db, &Name::self_param())) + PathResult::from_resolution(self.resolve_name(db, &SELF_PARAM)) } else { let (item_map, module) = match self.module() { Some(it) => it, @@ -270,7 +270,7 @@ impl Scope { fn resolve_name(&self, db: &impl HirDatabase, name: &Name) -> PerNs { match self { Scope::ModuleScope(m) => { - if let Some(KnownName::SelfParam) = name.as_known_name() { + if name == &SELF_PARAM { PerNs::types(Resolution::Def(m.crate_def_map.mk_module(m.module_id).into())) } else { m.crate_def_map @@ -283,7 +283,7 @@ impl Scope { None => PerNs::none(), }, Scope::ImplBlockScope(i) => { - if name.as_known_name() == Some(KnownName::SelfType) { + if name == &SELF_TYPE { PerNs::types(Resolution::SelfType(*i)) } else { PerNs::none() @@ -329,7 +329,7 @@ impl Scope { } } Scope::ImplBlockScope(i) => { - f(Name::self_type(), PerNs::types(Resolution::SelfType(*i))); + f(SELF_TYPE, PerNs::types(Resolution::SelfType(*i))); } Scope::ExprScope(e) => { e.expr_scopes.entries(e.scope_id).iter().for_each(|e| { diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 973de70df..41980a325 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -40,6 +40,7 @@ use crate::{ PatId, Statement, UnaryOp, }, generics::{GenericParams, HasGenericParams}, + name::{SELF_TYPE, INTO_ITERATOR, ITEM, ITER, STD}, nameres::{Namespace, PerNs}, path::{GenericArg, GenericArgs, PathKind, PathSegment}, resolve::{ @@ -48,7 +49,7 @@ use crate::{ }, ty::infer::diagnostics::InferenceDiagnostic, type_ref::{Mutability, TypeRef}, - AdtDef, AsName, ConstData, DefWithBody, FnData, Function, HirDatabase, ImplItem, KnownName, + AdtDef, ConstData, DefWithBody, FnData, Function, HirDatabase, ImplItem, ModuleDef, Name, Path, StructField, }; @@ -842,7 +843,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { // Parent arguments are unknown, except for the receiver type if let Some(parent_generics) = def_generics.and_then(|p| p.parent_params.clone()) { for param in &parent_generics.params { - if param.name.as_known_name() == Some(crate::KnownName::SelfType) { + if param.name == SELF_TYPE { substs.push(receiver_ty.clone()); } else { substs.push(Ty::Unknown); @@ -1346,15 +1347,15 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let into_iter_path = Path { kind: PathKind::Abs, segments: vec![ - PathSegment { name: KnownName::Std.as_name(), args_and_bindings: None }, - PathSegment { name: KnownName::Iter.as_name(), args_and_bindings: None }, - PathSegment { name: KnownName::IntoIterator.as_name(), args_and_bindings: None }, + PathSegment { name: STD, args_and_bindings: None }, + PathSegment { name: ITER, args_and_bindings: None }, + PathSegment { name: INTO_ITERATOR, args_and_bindings: None }, ], }; match self.resolver.resolve_path_segments(self.db, &into_iter_path).into_fully_resolved() { PerNs { types: Some(Def(Trait(trait_))), .. } => { - Some(trait_.associated_type_by_name(self.db, KnownName::Item.as_name())?) + Some(trait_.associated_type_by_name(self.db, ITEM)?) } _ => None, } diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml index 95ff6b1d8..156991358 100644 --- a/crates/ra_syntax/Cargo.toml +++ b/crates/ra_syntax/Cargo.toml @@ -17,7 +17,7 @@ rowan = "0.5.0" # ideally, `serde` should be enabled by `ra_lsp_server`, but we enable it here # to reduce number of compilations text_unit = { version = "0.1.8", features = ["serde"] } -smol_str = { version = "0.1.11", features = ["serde"] } +smol_str = { version = "0.1.12", features = ["serde"] } ra_text_edit = { path = "../ra_text_edit" } ra_parser = { path = "../ra_parser" } -- cgit v1.2.3