diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-20 22:08:30 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-20 22:08:30 +0000 |
commit | 1eb3bf41d75a8c744a0ecb586d4a2e92a1175c08 (patch) | |
tree | 6c2ddb0c296c566e9074eb316fafdce8807bcef2 /crates | |
parent | 8b61ea127c596e4305acffa0d9180e48bd57f6c4 (diff) | |
parent | f5165af9a7cdf9e43db0a95e717a47fa2bdf6c25 (diff) |
Merge #1003
1003: make Name::new private r=flodiebold a=matklad
This maybe is overengineering, but it seems cool to keep names completely opaque.
r? @flodiebold
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/expr.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir/src/name.rs | 5 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/primitive.rs | 66 |
4 files changed, 62 insertions, 20 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 6c7489e63..486314cc5 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -723,8 +723,7 @@ impl ExprCollector { | |||
723 | 723 | ||
724 | let lit = match child.flavor() { | 724 | let lit = match child.flavor() { |
725 | LiteralFlavor::IntNumber { suffix } => { | 725 | LiteralFlavor::IntNumber { suffix } => { |
726 | let known_name = | 726 | let known_name = suffix.and_then(|it| UncertainIntTy::from_suffix(&it)); |
727 | suffix.map(Name::new).and_then(|name| UncertainIntTy::from_name(&name)); | ||
728 | 727 | ||
729 | Literal::Int( | 728 | Literal::Int( |
730 | Default::default(), | 729 | Default::default(), |
@@ -732,9 +731,7 @@ impl ExprCollector { | |||
732 | ) | 731 | ) |
733 | } | 732 | } |
734 | LiteralFlavor::FloatNumber { suffix } => { | 733 | LiteralFlavor::FloatNumber { suffix } => { |
735 | let known_name = suffix | 734 | let known_name = suffix.and_then(|it| UncertainFloatTy::from_suffix(&it)); |
736 | .map(Name::new) | ||
737 | .and_then(|name| UncertainFloatTy::from_name(&name)); | ||
738 | 735 | ||
739 | Literal::Float( | 736 | Literal::Float( |
740 | Default::default(), | 737 | Default::default(), |
diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs index 06bafa6f0..677d18efc 100644 --- a/crates/ra_hir/src/name.rs +++ b/crates/ra_hir/src/name.rs | |||
@@ -23,7 +23,10 @@ impl fmt::Debug for Name { | |||
23 | } | 23 | } |
24 | 24 | ||
25 | impl Name { | 25 | impl Name { |
26 | pub(crate) fn new(text: SmolStr) -> Name { | 26 | /// Note: this is private to make creating name from random string hard. |
27 | /// Hopefully, this should allow us to integrate hygiene cleaner in the | ||
28 | /// future, and to switch to interned representation of names. | ||
29 | fn new(text: SmolStr) -> Name { | ||
27 | Name { text } | 30 | Name { text } |
28 | } | 31 | } |
29 | 32 | ||
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 278f592d3..389a2fc68 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs | |||
@@ -63,9 +63,9 @@ impl Ty { | |||
63 | pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Self { | 63 | pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Self { |
64 | if let Some(name) = path.as_ident() { | 64 | if let Some(name) = path.as_ident() { |
65 | // TODO handle primitive type names in resolver as well? | 65 | // TODO handle primitive type names in resolver as well? |
66 | if let Some(int_ty) = primitive::UncertainIntTy::from_name(name) { | 66 | if let Some(int_ty) = primitive::UncertainIntTy::from_type_name(name) { |
67 | return Ty::Int(int_ty); | 67 | return Ty::Int(int_ty); |
68 | } else if let Some(float_ty) = primitive::UncertainFloatTy::from_name(name) { | 68 | } else if let Some(float_ty) = primitive::UncertainFloatTy::from_type_name(name) { |
69 | return Ty::Float(float_ty); | 69 | return Ty::Float(float_ty); |
70 | } else if let Some(known) = name.as_known_name() { | 70 | } else if let Some(known) = name.as_known_name() { |
71 | match known { | 71 | match known { |
diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs index 30aeac48e..421f7e980 100644 --- a/crates/ra_hir/src/ty/primitive.rs +++ b/crates/ra_hir/src/ty/primitive.rs | |||
@@ -10,10 +10,20 @@ pub enum UncertainIntTy { | |||
10 | } | 10 | } |
11 | 11 | ||
12 | impl UncertainIntTy { | 12 | impl UncertainIntTy { |
13 | pub fn from_name(name: &Name) -> Option<UncertainIntTy> { | 13 | pub(crate) fn from_type_name(name: &Name) -> Option<UncertainIntTy> { |
14 | if let Some(ty) = IntTy::from_name(name) { | 14 | if let Some(ty) = IntTy::from_type_name(name) { |
15 | Some(UncertainIntTy::Signed(ty)) | 15 | Some(UncertainIntTy::Signed(ty)) |
16 | } else if let Some(ty) = UintTy::from_name(name) { | 16 | } else if let Some(ty) = UintTy::from_type_name(name) { |
17 | Some(UncertainIntTy::Unsigned(ty)) | ||
18 | } else { | ||
19 | None | ||
20 | } | ||
21 | } | ||
22 | |||
23 | pub(crate) fn from_suffix(suffix: &str) -> Option<UncertainIntTy> { | ||
24 | if let Some(ty) = IntTy::from_suffix(suffix) { | ||
25 | Some(UncertainIntTy::Signed(ty)) | ||
26 | } else if let Some(ty) = UintTy::from_suffix(suffix) { | ||
17 | Some(UncertainIntTy::Unsigned(ty)) | 27 | Some(UncertainIntTy::Unsigned(ty)) |
18 | } else { | 28 | } else { |
19 | None | 29 | None |
@@ -38,12 +48,12 @@ pub enum UncertainFloatTy { | |||
38 | } | 48 | } |
39 | 49 | ||
40 | impl UncertainFloatTy { | 50 | impl UncertainFloatTy { |
41 | pub fn from_name(name: &Name) -> Option<UncertainFloatTy> { | 51 | pub(crate) fn from_type_name(name: &Name) -> Option<UncertainFloatTy> { |
42 | if let Some(ty) = FloatTy::from_name(name) { | 52 | FloatTy::from_type_name(name).map(UncertainFloatTy::Known) |
43 | Some(UncertainFloatTy::Known(ty)) | 53 | } |
44 | } else { | 54 | |
45 | None | 55 | pub(crate) fn from_suffix(suffix: &str) -> Option<UncertainFloatTy> { |
46 | } | 56 | FloatTy::from_suffix(suffix).map(UncertainFloatTy::Known) |
47 | } | 57 | } |
48 | } | 58 | } |
49 | 59 | ||
@@ -87,7 +97,7 @@ impl fmt::Display for IntTy { | |||
87 | } | 97 | } |
88 | 98 | ||
89 | impl IntTy { | 99 | impl IntTy { |
90 | pub fn from_name(name: &Name) -> Option<IntTy> { | 100 | fn from_type_name(name: &Name) -> Option<IntTy> { |
91 | match name.as_known_name()? { | 101 | match name.as_known_name()? { |
92 | KnownName::Isize => Some(IntTy::Isize), | 102 | KnownName::Isize => Some(IntTy::Isize), |
93 | KnownName::I8 => Some(IntTy::I8), | 103 | KnownName::I8 => Some(IntTy::I8), |
@@ -98,6 +108,18 @@ impl IntTy { | |||
98 | _ => None, | 108 | _ => None, |
99 | } | 109 | } |
100 | } | 110 | } |
111 | |||
112 | fn from_suffix(suffix: &str) -> Option<IntTy> { | ||
113 | match suffix { | ||
114 | "isize" => Some(IntTy::Isize), | ||
115 | "i8" => Some(IntTy::I8), | ||
116 | "i16" => Some(IntTy::I16), | ||
117 | "i32" => Some(IntTy::I32), | ||
118 | "i64" => Some(IntTy::I64), | ||
119 | "i128" => Some(IntTy::I128), | ||
120 | _ => None, | ||
121 | } | ||
122 | } | ||
101 | } | 123 | } |
102 | 124 | ||
103 | #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] | 125 | #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] |
@@ -125,7 +147,7 @@ impl fmt::Display for UintTy { | |||
125 | } | 147 | } |
126 | 148 | ||
127 | impl UintTy { | 149 | impl UintTy { |
128 | pub fn from_name(name: &Name) -> Option<UintTy> { | 150 | fn from_type_name(name: &Name) -> Option<UintTy> { |
129 | match name.as_known_name()? { | 151 | match name.as_known_name()? { |
130 | KnownName::Usize => Some(UintTy::Usize), | 152 | KnownName::Usize => Some(UintTy::Usize), |
131 | KnownName::U8 => Some(UintTy::U8), | 153 | KnownName::U8 => Some(UintTy::U8), |
@@ -136,6 +158,18 @@ impl UintTy { | |||
136 | _ => None, | 158 | _ => None, |
137 | } | 159 | } |
138 | } | 160 | } |
161 | |||
162 | fn from_suffix(suffix: &str) -> Option<UintTy> { | ||
163 | match suffix { | ||
164 | "usize" => Some(UintTy::Usize), | ||
165 | "u8" => Some(UintTy::U8), | ||
166 | "u16" => Some(UintTy::U16), | ||
167 | "u32" => Some(UintTy::U32), | ||
168 | "u64" => Some(UintTy::U64), | ||
169 | "u128" => Some(UintTy::U128), | ||
170 | _ => None, | ||
171 | } | ||
172 | } | ||
139 | } | 173 | } |
140 | 174 | ||
141 | impl fmt::Debug for UintTy { | 175 | impl fmt::Debug for UintTy { |
@@ -170,11 +204,19 @@ impl FloatTy { | |||
170 | } | 204 | } |
171 | } | 205 | } |
172 | 206 | ||
173 | pub fn from_name(name: &Name) -> Option<FloatTy> { | 207 | fn from_type_name(name: &Name) -> Option<FloatTy> { |
174 | match name.as_known_name()? { | 208 | match name.as_known_name()? { |
175 | KnownName::F32 => Some(FloatTy::F32), | 209 | KnownName::F32 => Some(FloatTy::F32), |
176 | KnownName::F64 => Some(FloatTy::F64), | 210 | KnownName::F64 => Some(FloatTy::F64), |
177 | _ => None, | 211 | _ => None, |
178 | } | 212 | } |
179 | } | 213 | } |
214 | |||
215 | fn from_suffix(suffix: &str) -> Option<FloatTy> { | ||
216 | match suffix { | ||
217 | "f32" => Some(FloatTy::F32), | ||
218 | "f64" => Some(FloatTy::F64), | ||
219 | _ => None, | ||
220 | } | ||
221 | } | ||
180 | } | 222 | } |