introduce known names
This commit is contained in:
parent
d963042ca9
commit
a9f55029b9
@ -38,7 +38,7 @@ use ra_db::{LocationIntener, SourceRootId, FileId, Cancelable};
|
|||||||
use crate::{
|
use crate::{
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
arena::{Arena, Id},
|
arena::{Arena, Id},
|
||||||
name::AsName,
|
name::{AsName, KnownName},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
|
@ -17,9 +17,25 @@ impl fmt::Display for Name {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Name {
|
impl Name {
|
||||||
// TODO: get rid of this?
|
pub(crate) fn as_known_name(&self) -> Option<KnownName> {
|
||||||
pub(crate) fn as_str(&self) -> &str {
|
let name = match self.text.as_str() {
|
||||||
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))]
|
#[cfg(not(test))]
|
||||||
@ -54,3 +70,28 @@ impl AsName for ra_db::Dependency {
|
|||||||
Name::new(self.name.clone())
|
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,
|
||||||
|
}
|
||||||
|
@ -95,7 +95,7 @@ pub enum Ty {
|
|||||||
Tuple(Vec<Ty>),
|
Tuple(Vec<Ty>),
|
||||||
|
|
||||||
// The projection of an associated type. For example,
|
// The projection of an associated type. For example,
|
||||||
// `<T as Trait<..>>::N`.
|
// `<T as Trait<..>>::N`.pub
|
||||||
// Projection(ProjectionTy),
|
// Projection(ProjectionTy),
|
||||||
|
|
||||||
// Opaque (`impl Trait`) type found in a return type.
|
// Opaque (`impl Trait`) type found in a return type.
|
||||||
@ -180,12 +180,11 @@ impl Ty {
|
|||||||
path: &Path,
|
path: &Path,
|
||||||
) -> Cancelable<Self> {
|
) -> Cancelable<Self> {
|
||||||
if let Some(name) = path.as_ident() {
|
if let Some(name) = path.as_ident() {
|
||||||
let name = name.as_str(); // :-(
|
if let Some(int_ty) = primitive::IntTy::from_name(name) {
|
||||||
if let Some(int_ty) = primitive::IntTy::from_string(name) {
|
|
||||||
return Ok(Ty::Int(int_ty));
|
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));
|
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));
|
return Ok(Ty::Float(float_ty));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
use crate::{Name, KnownName};
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
|
||||||
pub enum IntTy {
|
pub enum IntTy {
|
||||||
Isize,
|
Isize,
|
||||||
@ -34,14 +36,14 @@ impl IntTy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_string(s: &str) -> Option<IntTy> {
|
pub fn from_name(name: &Name) -> Option<IntTy> {
|
||||||
match s {
|
match name.as_known_name()? {
|
||||||
"isize" => Some(IntTy::Isize),
|
KnownName::Isize => Some(IntTy::Isize),
|
||||||
"i8" => Some(IntTy::I8),
|
KnownName::I8 => Some(IntTy::I8),
|
||||||
"i16" => Some(IntTy::I16),
|
KnownName::I16 => Some(IntTy::I16),
|
||||||
"i32" => Some(IntTy::I32),
|
KnownName::I32 => Some(IntTy::I32),
|
||||||
"i64" => Some(IntTy::I64),
|
KnownName::I64 => Some(IntTy::I64),
|
||||||
"i128" => Some(IntTy::I128),
|
KnownName::I128 => Some(IntTy::I128),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,14 +71,14 @@ impl UintTy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_string(s: &str) -> Option<UintTy> {
|
pub fn from_name(name: &Name) -> Option<UintTy> {
|
||||||
match s {
|
match name.as_known_name()? {
|
||||||
"usize" => Some(UintTy::Usize),
|
KnownName::Usize => Some(UintTy::Usize),
|
||||||
"u8" => Some(UintTy::U8),
|
KnownName::U8 => Some(UintTy::U8),
|
||||||
"u16" => Some(UintTy::U16),
|
KnownName::U16 => Some(UintTy::U16),
|
||||||
"u32" => Some(UintTy::U32),
|
KnownName::U32 => Some(UintTy::U32),
|
||||||
"u64" => Some(UintTy::U64),
|
KnownName::U64 => Some(UintTy::U64),
|
||||||
"u128" => Some(UintTy::U128),
|
KnownName::U128 => Some(UintTy::U128),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,10 +122,10 @@ impl FloatTy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_string(s: &str) -> Option<FloatTy> {
|
pub fn from_name(name: &Name) -> Option<FloatTy> {
|
||||||
match s {
|
match name.as_known_name()? {
|
||||||
"f32" => Some(FloatTy::F32),
|
KnownName::F32 => Some(FloatTy::F32),
|
||||||
"f64" => Some(FloatTy::F64),
|
KnownName::F64 => Some(FloatTy::F64),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user