Refactor Name ready for hygienic macro

This commit is contained in:
uHOOCCOOHu 2019-09-27 02:04:47 +08:00
parent 53a30d9e69
commit 128dc5355b
No known key found for this signature in database
GPG key ID: CED392DE0C483D00
4 changed files with 95 additions and 85 deletions

View file

@ -133,7 +133,7 @@ impl VariantData {
.fields() .fields()
.enumerate() .enumerate()
.map(|(i, fd)| StructFieldData { .map(|(i, fd)| StructFieldData {
name: Name::tuple_field_name(i), name: Name::new_tuple_field(i),
type_ref: TypeRef::from_ast_opt(fd.type_ref()), type_ref: TypeRef::from_ast_opt(fd.type_ref()),
}) })
.collect(); .collect();

View file

@ -5,20 +5,21 @@ use ra_syntax::{ast, SmolStr};
/// `Name` is a wrapper around string, which is used in hir for both references /// `Name` is a wrapper around string, which is used in hir for both references
/// and declarations. In theory, names should also carry hygiene info, but we are /// and declarations. In theory, names should also carry hygiene info, but we are
/// not there yet! /// not there yet!
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Name { pub struct Name(Repr);
text: SmolStr,
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
enum Repr {
Text(SmolStr),
TupleField(usize),
} }
impl fmt::Display for Name { impl fmt::Display for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.text, f) match &self.0 {
Repr::Text(text) => fmt::Display::fmt(&text, f),
Repr::TupleField(idx) => fmt::Display::fmt(&idx, f),
} }
}
impl fmt::Debug for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.text, f)
} }
} }
@ -26,29 +27,38 @@ impl Name {
/// Note: this is private to make creating name from random string hard. /// Note: this is private to make creating name from random string hard.
/// Hopefully, this should allow us to integrate hygiene cleaner in the /// Hopefully, this should allow us to integrate hygiene cleaner in the
/// future, and to switch to interned representation of names. /// future, and to switch to interned representation of names.
const fn new(text: SmolStr) -> Name { const fn new_text(text: SmolStr) -> Name {
Name { text } Name(Repr::Text(text))
}
pub(crate) fn new_tuple_field(idx: usize) -> Name {
Name(Repr::TupleField(idx))
}
/// Shortcut to create inline plain text name
const fn new_inline_ascii(len: usize, text: &[u8]) -> Name {
Name::new_text(SmolStr::new_inline_from_ascii(len, text))
}
/// Resolve a name from the text of token.
fn resolve(raw_text: &SmolStr) -> Name {
let raw_start = "r#";
if raw_text.as_str().starts_with(raw_start) {
Name::new_text(SmolStr::new(&raw_text[raw_start.len()..]))
} else {
Name::new_text(raw_text.clone())
}
} }
pub(crate) fn missing() -> Name { pub(crate) fn missing() -> Name {
Name::new("[missing name]".into()) Name::new_text("[missing name]".into())
} }
pub(crate) fn tuple_field_name(idx: usize) -> Name { pub(crate) fn as_tuple_index(&self) -> Option<usize> {
Name::new(idx.to_string().into()) match self.0 {
Repr::TupleField(idx) => Some(idx),
_ => None,
} }
// There's should be no way to extract a string out of `Name`: `Name` in the
// future, `Name` will include hygiene information, and you can't encode
// hygiene into a String.
//
// If you need to compare something with `Name`, compare `Name`s directly.
//
// If you need to render `Name` for the user, use the `Display` impl, but be
// aware that it strips hygiene info.
#[deprecated(note = "use to_string instead")]
pub fn as_smolstr(&self) -> &SmolStr {
&self.text
} }
} }
@ -58,15 +68,16 @@ pub(crate) trait AsName {
impl AsName for ast::NameRef { impl AsName for ast::NameRef {
fn as_name(&self) -> Name { fn as_name(&self) -> Name {
let name = resolve_name(self.text()); match self.as_tuple_field() {
Name::new(name) Some(idx) => Name::new_tuple_field(idx),
None => Name::resolve(self.text()),
}
} }
} }
impl AsName for ast::Name { impl AsName for ast::Name {
fn as_name(&self) -> Name { fn as_name(&self) -> Name {
let name = resolve_name(self.text()); Name::resolve(self.text())
Name::new(name)
} }
} }
@ -74,66 +85,56 @@ impl AsName for ast::FieldKind {
fn as_name(&self) -> Name { fn as_name(&self) -> Name {
match self { match self {
ast::FieldKind::Name(nr) => nr.as_name(), ast::FieldKind::Name(nr) => nr.as_name(),
ast::FieldKind::Index(idx) => Name::new(idx.text().clone()), ast::FieldKind::Index(idx) => Name::new_tuple_field(idx.text().parse().unwrap()),
} }
} }
} }
impl AsName for ra_db::Dependency { impl AsName for ra_db::Dependency {
fn as_name(&self) -> Name { fn as_name(&self) -> Name {
Name::new(self.name.clone()) Name::new_text(self.name.clone())
} }
} }
// Primitives // Primitives
pub(crate) const ISIZE: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"isize")); pub(crate) const ISIZE: Name = Name::new_inline_ascii(5, b"isize");
pub(crate) const I8: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"i8")); pub(crate) const I8: Name = Name::new_inline_ascii(2, b"i8");
pub(crate) const I16: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i16")); pub(crate) const I16: Name = Name::new_inline_ascii(3, b"i16");
pub(crate) const I32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i32")); pub(crate) const I32: Name = Name::new_inline_ascii(3, b"i32");
pub(crate) const I64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i64")); pub(crate) const I64: Name = Name::new_inline_ascii(3, b"i64");
pub(crate) const I128: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"i128")); pub(crate) const I128: Name = Name::new_inline_ascii(4, b"i128");
pub(crate) const USIZE: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"usize")); pub(crate) const USIZE: Name = Name::new_inline_ascii(5, b"usize");
pub(crate) const U8: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"u8")); pub(crate) const U8: Name = Name::new_inline_ascii(2, b"u8");
pub(crate) const U16: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u16")); pub(crate) const U16: Name = Name::new_inline_ascii(3, b"u16");
pub(crate) const U32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u32")); pub(crate) const U32: Name = Name::new_inline_ascii(3, b"u32");
pub(crate) const U64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u64")); pub(crate) const U64: Name = Name::new_inline_ascii(3, b"u64");
pub(crate) const U128: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"u128")); pub(crate) const U128: Name = Name::new_inline_ascii(4, b"u128");
pub(crate) const F32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"f32")); pub(crate) const F32: Name = Name::new_inline_ascii(3, b"f32");
pub(crate) const F64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"f64")); pub(crate) const F64: Name = Name::new_inline_ascii(3, b"f64");
pub(crate) const BOOL: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"bool")); pub(crate) const BOOL: Name = Name::new_inline_ascii(4, b"bool");
pub(crate) const CHAR: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"char")); pub(crate) const CHAR: Name = Name::new_inline_ascii(4, b"char");
pub(crate) const STR: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"str")); pub(crate) const STR: Name = Name::new_inline_ascii(3, b"str");
// Special names // Special names
pub(crate) const SELF_PARAM: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"self")); pub(crate) const SELF_PARAM: Name = Name::new_inline_ascii(4, b"self");
pub(crate) const SELF_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Self")); pub(crate) const SELF_TYPE: Name = Name::new_inline_ascii(4, b"Self");
pub(crate) const MACRO_RULES: Name = Name::new(SmolStr::new_inline_from_ascii(11, b"macro_rules")); pub(crate) const MACRO_RULES: Name = Name::new_inline_ascii(11, b"macro_rules");
// Components of known path (value or mod name) // Components of known path (value or mod name)
pub(crate) const STD: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"std")); pub(crate) const STD: Name = Name::new_inline_ascii(3, b"std");
pub(crate) const ITER: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"iter")); pub(crate) const ITER: Name = Name::new_inline_ascii(4, b"iter");
pub(crate) const OPS: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"ops")); pub(crate) const OPS: Name = Name::new_inline_ascii(3, b"ops");
pub(crate) const FUTURE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"future")); pub(crate) const FUTURE: Name = Name::new_inline_ascii(6, b"future");
pub(crate) const RESULT: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"result")); pub(crate) const RESULT: Name = Name::new_inline_ascii(6, b"result");
pub(crate) const BOXED: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"boxed")); pub(crate) const BOXED: Name = Name::new_inline_ascii(5, b"boxed");
// Components of known path (type name) // Components of known path (type name)
pub(crate) const INTO_ITERATOR_TYPE: Name = pub(crate) const INTO_ITERATOR_TYPE: Name = Name::new_inline_ascii(12, b"IntoIterator");
Name::new(SmolStr::new_inline_from_ascii(12, b"IntoIterator")); pub(crate) const ITEM_TYPE: Name = Name::new_inline_ascii(4, b"Item");
pub(crate) const ITEM_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Item")); pub(crate) const TRY_TYPE: Name = Name::new_inline_ascii(3, b"Try");
pub(crate) const TRY_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"Try")); pub(crate) const OK_TYPE: Name = Name::new_inline_ascii(2, b"Ok");
pub(crate) const OK_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"Ok")); pub(crate) const FUTURE_TYPE: Name = Name::new_inline_ascii(6, b"Future");
pub(crate) const FUTURE_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Future")); pub(crate) const RESULT_TYPE: Name = Name::new_inline_ascii(6, b"Result");
pub(crate) const RESULT_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Result")); pub(crate) const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output");
pub(crate) const OUTPUT_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Output")); pub(crate) const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
pub(crate) const TARGET_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(6, b"Target")); pub(crate) const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
pub(crate) const BOX_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"Box"));
fn resolve_name(text: &SmolStr) -> SmolStr {
let raw_start = "r#";
if text.as_str().starts_with(raw_start) {
SmolStr::new(&text[raw_start.len()..])
} else {
text.clone()
}
}

View file

@ -609,7 +609,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
for (i, &subpat) in subpats.iter().enumerate() { for (i, &subpat) in subpats.iter().enumerate() {
let expected_ty = def let expected_ty = def
.and_then(|d| d.field(self.db, &Name::tuple_field_name(i))) .and_then(|d| d.field(self.db, &Name::new_tuple_field(i)))
.map_or(Ty::Unknown, |field| field.ty(self.db)) .map_or(Ty::Unknown, |field| field.ty(self.db))
.subst(&substs); .subst(&substs);
let expected_ty = self.normalize_associated_types_in(expected_ty); let expected_ty = self.normalize_associated_types_in(expected_ty);
@ -1375,10 +1375,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
) )
.find_map(|derefed_ty| match canonicalized.decanonicalize_ty(derefed_ty.value) { .find_map(|derefed_ty| match canonicalized.decanonicalize_ty(derefed_ty.value) {
Ty::Apply(a_ty) => match a_ty.ctor { Ty::Apply(a_ty) => match a_ty.ctor {
TypeCtor::Tuple { .. } => { TypeCtor::Tuple { .. } => name
let i = name.to_string().parse::<usize>().ok(); .as_tuple_index()
i.and_then(|i| a_ty.parameters.0.get(i).cloned()) .and_then(|idx| a_ty.parameters.0.get(idx).cloned()),
}
TypeCtor::Adt(Adt::Struct(s)) => s.field(self.db, name).map(|field| { TypeCtor::Adt(Adt::Struct(s)) => s.field(self.db, name).map(|field| {
self.write_field_resolution(tgt_expr, field); self.write_field_resolution(tgt_expr, field);
field.ty(self.db).subst(&a_ty.parameters) field.ty(self.db).subst(&a_ty.parameters)

View file

@ -21,6 +21,16 @@ impl ast::NameRef {
pub fn text(&self) -> &SmolStr { pub fn text(&self) -> &SmolStr {
text_of_first_token(self.syntax()) text_of_first_token(self.syntax())
} }
pub fn as_tuple_field(&self) -> Option<usize> {
self.syntax().children_with_tokens().find_map(|c| {
if c.kind() == SyntaxKind::INT_NUMBER {
c.as_token().and_then(|tok| tok.text().as_str().parse().ok())
} else {
None
}
})
}
} }
fn text_of_first_token(node: &SyntaxNode) -> &SmolStr { fn text_of_first_token(node: &SyntaxNode) -> &SmolStr {