From a9f55029b9db3bcd439d31c5007785299f7d4025 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 27 Dec 2018 20:26:15 +0300 Subject: introduce known names --- crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/name.rs | 47 ++++++++++++++++++++++++++++++++++++--- crates/ra_hir/src/ty.rs | 9 ++++---- crates/ra_hir/src/ty/primitive.rs | 42 +++++++++++++++++----------------- 4 files changed, 71 insertions(+), 29 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index bf43cd0ae..5bbb09c01 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -38,7 +38,7 @@ use ra_db::{LocationIntener, SourceRootId, FileId, Cancelable}; use crate::{ db::HirDatabase, arena::{Arena, Id}, - name::AsName, + name::{AsName, KnownName}, }; pub use self::{ diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs index 7f42c9f04..cdad31be7 100644 --- a/crates/ra_hir/src/name.rs +++ b/crates/ra_hir/src/name.rs @@ -17,9 +17,25 @@ impl fmt::Display for Name { } impl Name { - // TODO: get rid of this? - pub(crate) fn as_str(&self) -> &str { - self.text.as_str() + 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, + _ => return None, + }; + Some(name) } #[cfg(not(test))] @@ -54,3 +70,28 @@ impl AsName for ra_db::Dependency { Name::new(self.name.clone()) } } + +// Ideally, should be replaced with +// ``` +// const ISIZE: Name = Name::new("isize") +// ``` +// but const-fn is not that powerful yet. +#[derive(Debug)] +pub(crate) enum KnownName { + Isize, + I8, + I16, + I32, + I64, + I128, + + Usize, + U8, + U16, + U32, + U64, + U128, + + F32, + F64, +} diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index dc3323b1a..ad097d1f1 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -95,7 +95,7 @@ pub enum Ty { Tuple(Vec), // The projection of an associated type. For example, - // `>::N`. + // `>::N`.pub // Projection(ProjectionTy), // Opaque (`impl Trait`) type found in a return type. @@ -180,12 +180,11 @@ impl Ty { path: &Path, ) -> Cancelable { if let Some(name) = path.as_ident() { - let name = name.as_str(); // :-( - if let Some(int_ty) = primitive::IntTy::from_string(name) { + if let Some(int_ty) = primitive::IntTy::from_name(name) { return Ok(Ty::Int(int_ty)); - } else if let Some(uint_ty) = primitive::UintTy::from_string(name) { + } else if let Some(uint_ty) = primitive::UintTy::from_name(name) { return Ok(Ty::Uint(uint_ty)); - } else if let Some(float_ty) = primitive::FloatTy::from_string(name) { + } else if let Some(float_ty) = primitive::FloatTy::from_name(name) { return Ok(Ty::Float(float_ty)); } } diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs index ad79b17e4..498d42d52 100644 --- a/crates/ra_hir/src/ty/primitive.rs +++ b/crates/ra_hir/src/ty/primitive.rs @@ -1,5 +1,7 @@ use std::fmt; +use crate::{Name, KnownName}; + #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] pub enum IntTy { Isize, @@ -34,14 +36,14 @@ impl IntTy { } } - pub fn from_string(s: &str) -> Option { - match s { - "isize" => Some(IntTy::Isize), - "i8" => Some(IntTy::I8), - "i16" => Some(IntTy::I16), - "i32" => Some(IntTy::I32), - "i64" => Some(IntTy::I64), - "i128" => Some(IntTy::I128), + pub fn from_name(name: &Name) -> Option { + match name.as_known_name()? { + KnownName::Isize => Some(IntTy::Isize), + KnownName::I8 => Some(IntTy::I8), + KnownName::I16 => Some(IntTy::I16), + KnownName::I32 => Some(IntTy::I32), + KnownName::I64 => Some(IntTy::I64), + KnownName::I128 => Some(IntTy::I128), _ => None, } } @@ -69,14 +71,14 @@ impl UintTy { } } - pub fn from_string(s: &str) -> Option { - match s { - "usize" => Some(UintTy::Usize), - "u8" => Some(UintTy::U8), - "u16" => Some(UintTy::U16), - "u32" => Some(UintTy::U32), - "u64" => Some(UintTy::U64), - "u128" => Some(UintTy::U128), + pub fn from_name(name: &Name) -> Option { + match name.as_known_name()? { + KnownName::Usize => Some(UintTy::Usize), + KnownName::U8 => Some(UintTy::U8), + KnownName::U16 => Some(UintTy::U16), + KnownName::U32 => Some(UintTy::U32), + KnownName::U64 => Some(UintTy::U64), + KnownName::U128 => Some(UintTy::U128), _ => None, } } @@ -120,10 +122,10 @@ impl FloatTy { } } - pub fn from_string(s: &str) -> Option { - match s { - "f32" => Some(FloatTy::F32), - "f64" => Some(FloatTy::F64), + pub fn from_name(name: &Name) -> Option { + match name.as_known_name()? { + KnownName::F32 => Some(FloatTy::F32), + KnownName::F64 => Some(FloatTy::F64), _ => None, } } -- cgit v1.2.3