1507: Constify KnownName's r=matklad a=mominul

Closes #1503 

Co-authored-by: Muhammad Mominul Huque <mominul2082@gmail.com>
This commit is contained in:
bors[bot] 2019-07-08 08:29:19 +00:00
commit 5b19825e37
11 changed files with 82 additions and 164 deletions

10
Cargo.lock generated
View file

@ -1278,7 +1278,7 @@ dependencies = [
"ra_parser 0.1.0",
"ra_text_edit 0.1.0",
"rowan 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
"smol_str 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"smol_str 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
"test_utils 0.1.0",
"text_unit 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1308,7 +1308,7 @@ dependencies = [
name = "ra_tt"
version = "0.1.0"
dependencies = [
"smol_str 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"smol_str 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -1563,7 +1563,7 @@ dependencies = [
"colosseum 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"smol_str 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"smol_str 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
"text_unit 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1722,7 +1722,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "smol_str"
version = "0.1.11"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2280,7 +2280,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
"checksum slug 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b3bc762e6a4b6c6fcaade73e77f9ebc6991b676f88bb2358bddb56560f073373"
"checksum smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ab606a9c5e214920bb66c458cd7be8ef094f813f20fe77a54cc7dbfff220d4b7"
"checksum smol_str 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "e6507d018aa8dfcaa08aaab587605591cd2109df66a921486a2220e2daf9fa29"
"checksum smol_str 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "590700be3630457c56f8c73c0ea39881476ad7076cd84057d44f4f38f79914fb"
"checksum spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44363f6f51401c34e7be73db0db371c04705d35efbe9f7d6082e03a921a32c55"
"checksum stacker 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fb79482f57cf598af52094ec4cc3b3c42499d3ce5bd426f2ac41515b7e57404b"
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"

View file

@ -19,6 +19,10 @@ use crate::{
TypeAliasId,
},
impl_block::ImplBlock,
name::{
BOOL, CHAR, F32, F64, I128, I16, I32, I64, I8, ISIZE, SELF_TYPE, STR, U128, U16, U32, U64,
U8, USIZE,
},
nameres::{CrateModuleId, ImportId, ModuleScope, Namespace},
resolve::Resolver,
traits::{TraitData, TraitItem},
@ -28,7 +32,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 +100,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),
pub(crate) const ALL: &'static [(Name, BuiltinType)] = &[
(CHAR, BuiltinType::Char),
(BOOL, BuiltinType::Bool),
(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 })),
(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 })),
(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 })),
(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 })),
(KnownName::F32, BuiltinType::Float(FloatTy { bitness: FloatBitness::X32 })),
(KnownName::F64, BuiltinType::Float(FloatTy { bitness: FloatBitness::X64 })),
(F32, BuiltinType::Float(FloatTy { bitness: FloatBitness::X32 })),
(F64, BuiltinType::Float(FloatTy { bitness: FloatBitness::X64 })),
];
}
@ -560,7 +564,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 => {

View file

@ -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,
},

View file

@ -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,
@ -81,11 +82,7 @@ impl GenericParams {
GenericDef::Enum(it) => generics.fill(&*it.source(db).ast, start),
GenericDef::Trait(it) => {
// traits get the Self type as an implicit first type parameter
generics.params.push(GenericParam {
idx: start,
name: Name::self_type(),
default: None,
});
generics.params.push(GenericParam { idx: start, name: SELF_TYPE, default: None });
generics.fill(&*it.source(db).ast, start + 1);
}
GenericDef::TypeAlias(it) => generics.fill(&*it.source(db).ast, start),

View file

@ -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},
};

View file

@ -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<KnownName> {
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,31 @@ 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#";

View file

@ -65,8 +65,8 @@ use test_utils::tested_by;
use crate::{
diagnostics::DiagnosticSink, either::Either, ids::MacroDefId,
nameres::diagnostics::DefDiagnostic, AsName, AstDatabase, AstId, BuiltinType, Crate,
DefDatabase, HirFileId, MacroDef, Module, ModuleDef, Name, Path, PathKind, Trait,
nameres::diagnostics::DefDiagnostic, AstDatabase, AstId, BuiltinType, Crate, DefDatabase,
HirFileId, MacroDef, Module, ModuleDef, Name, Path, PathKind, Trait,
};
pub(crate) use self::raw::{ImportSourceMap, RawItems};
@ -138,8 +138,8 @@ pub struct ModuleScope {
static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = 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()
});

View file

@ -8,12 +8,13 @@ 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,
Static, Struct, Trait, TypeAlias, Union,
AstId, Const, DefDatabase, Enum, Function, HirFileId, MacroDef, Module, Name, Path, Static,
Struct, Trait, TypeAlias, Union,
};
pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
@ -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(

View file

@ -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<Resolution> {
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| {

View file

@ -40,6 +40,7 @@ use crate::{
PatId, Statement, UnaryOp,
},
generics::{GenericParams, HasGenericParams},
name::{INTO_ITERATOR, ITEM, ITER, SELF_TYPE, STD},
nameres::{Namespace, PerNs},
path::{GenericArg, GenericArgs, PathKind, PathSegment},
resolve::{
@ -48,8 +49,8 @@ use crate::{
},
ty::infer::diagnostics::InferenceDiagnostic,
type_ref::{Mutability, TypeRef},
AdtDef, AsName, ConstData, DefWithBody, FnData, Function, HirDatabase, ImplItem, KnownName,
ModuleDef, Name, Path, StructField,
AdtDef, ConstData, DefWithBody, FnData, Function, HirDatabase, ImplItem, ModuleDef, Name, Path,
StructField,
};
mod unify;
@ -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,
}

View file

@ -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" }