aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/expr.rs7
-rw-r--r--crates/ra_hir/src/ids.rs34
-rw-r--r--crates/ra_hir/src/name.rs5
-rw-r--r--crates/ra_hir/src/nameres/collector.rs2
-rw-r--r--crates/ra_hir/src/ty/lower.rs4
-rw-r--r--crates/ra_hir/src/ty/primitive.rs66
6 files changed, 80 insertions, 38 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/ids.rs b/crates/ra_hir/src/ids.rs
index 9596488d3..3d0a881c2 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -4,7 +4,7 @@ use std::{
4 sync::Arc, 4 sync::Arc,
5}; 5};
6 6
7use ra_db::{LocationIntener, FileId}; 7use ra_db::{LocationInterner, FileId};
8use ra_syntax::{TreeArc, SyntaxNode, SourceFile, AstNode, SyntaxNodePtr, ast}; 8use ra_syntax::{TreeArc, SyntaxNode, SourceFile, AstNode, SyntaxNodePtr, ast};
9use ra_arena::{Arena, RawId, ArenaId, impl_arena_id}; 9use ra_arena::{Arena, RawId, ArenaId, impl_arena_id};
10 10
@@ -15,14 +15,14 @@ use crate::{
15 15
16#[derive(Debug, Default)] 16#[derive(Debug, Default)]
17pub struct HirInterner { 17pub struct HirInterner {
18 macros: LocationIntener<MacroCallLoc, MacroCallId>, 18 macros: LocationInterner<MacroCallLoc, MacroCallId>,
19 fns: LocationIntener<ItemLoc<ast::FnDef>, FunctionId>, 19 fns: LocationInterner<ItemLoc<ast::FnDef>, FunctionId>,
20 structs: LocationIntener<ItemLoc<ast::StructDef>, StructId>, 20 structs: LocationInterner<ItemLoc<ast::StructDef>, StructId>,
21 enums: LocationIntener<ItemLoc<ast::EnumDef>, EnumId>, 21 enums: LocationInterner<ItemLoc<ast::EnumDef>, EnumId>,
22 consts: LocationIntener<ItemLoc<ast::ConstDef>, ConstId>, 22 consts: LocationInterner<ItemLoc<ast::ConstDef>, ConstId>,
23 statics: LocationIntener<ItemLoc<ast::StaticDef>, StaticId>, 23 statics: LocationInterner<ItemLoc<ast::StaticDef>, StaticId>,
24 traits: LocationIntener<ItemLoc<ast::TraitDef>, TraitId>, 24 traits: LocationInterner<ItemLoc<ast::TraitDef>, TraitId>,
25 types: LocationIntener<ItemLoc<ast::TypeAliasDef>, TypeId>, 25 types: LocationInterner<ItemLoc<ast::TypeAliasDef>, TypeId>,
26} 26}
27 27
28impl HirInterner { 28impl HirInterner {
@@ -204,7 +204,7 @@ impl<'a, DB: PersistentHirDatabase> LocationCtx<&'a DB> {
204} 204}
205 205
206pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone { 206pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone {
207 fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<N>, Self>; 207 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<N>, Self>;
208 fn from_ast(ctx: LocationCtx<&impl PersistentHirDatabase>, ast: &N) -> Self { 208 fn from_ast(ctx: LocationCtx<&impl PersistentHirDatabase>, ast: &N) -> Self {
209 let items = ctx.db.file_items(ctx.file_id); 209 let items = ctx.db.file_items(ctx.file_id);
210 let item_id = items.id_of(ctx.file_id, ast.syntax()); 210 let item_id = items.id_of(ctx.file_id, ast.syntax());
@@ -238,7 +238,7 @@ pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone {
238pub struct FunctionId(RawId); 238pub struct FunctionId(RawId);
239impl_arena_id!(FunctionId); 239impl_arena_id!(FunctionId);
240impl AstItemDef<ast::FnDef> for FunctionId { 240impl AstItemDef<ast::FnDef> for FunctionId {
241 fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::FnDef>, Self> { 241 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::FnDef>, Self> {
242 &interner.fns 242 &interner.fns
243 } 243 }
244} 244}
@@ -247,7 +247,7 @@ impl AstItemDef<ast::FnDef> for FunctionId {
247pub struct StructId(RawId); 247pub struct StructId(RawId);
248impl_arena_id!(StructId); 248impl_arena_id!(StructId);
249impl AstItemDef<ast::StructDef> for StructId { 249impl AstItemDef<ast::StructDef> for StructId {
250 fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::StructDef>, Self> { 250 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::StructDef>, Self> {
251 &interner.structs 251 &interner.structs
252 } 252 }
253} 253}
@@ -256,7 +256,7 @@ impl AstItemDef<ast::StructDef> for StructId {
256pub struct EnumId(RawId); 256pub struct EnumId(RawId);
257impl_arena_id!(EnumId); 257impl_arena_id!(EnumId);
258impl AstItemDef<ast::EnumDef> for EnumId { 258impl AstItemDef<ast::EnumDef> for EnumId {
259 fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::EnumDef>, Self> { 259 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::EnumDef>, Self> {
260 &interner.enums 260 &interner.enums
261 } 261 }
262} 262}
@@ -265,7 +265,7 @@ impl AstItemDef<ast::EnumDef> for EnumId {
265pub struct ConstId(RawId); 265pub struct ConstId(RawId);
266impl_arena_id!(ConstId); 266impl_arena_id!(ConstId);
267impl AstItemDef<ast::ConstDef> for ConstId { 267impl AstItemDef<ast::ConstDef> for ConstId {
268 fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::ConstDef>, Self> { 268 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::ConstDef>, Self> {
269 &interner.consts 269 &interner.consts
270 } 270 }
271} 271}
@@ -274,7 +274,7 @@ impl AstItemDef<ast::ConstDef> for ConstId {
274pub struct StaticId(RawId); 274pub struct StaticId(RawId);
275impl_arena_id!(StaticId); 275impl_arena_id!(StaticId);
276impl AstItemDef<ast::StaticDef> for StaticId { 276impl AstItemDef<ast::StaticDef> for StaticId {
277 fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::StaticDef>, Self> { 277 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::StaticDef>, Self> {
278 &interner.statics 278 &interner.statics
279 } 279 }
280} 280}
@@ -283,7 +283,7 @@ impl AstItemDef<ast::StaticDef> for StaticId {
283pub struct TraitId(RawId); 283pub struct TraitId(RawId);
284impl_arena_id!(TraitId); 284impl_arena_id!(TraitId);
285impl AstItemDef<ast::TraitDef> for TraitId { 285impl AstItemDef<ast::TraitDef> for TraitId {
286 fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::TraitDef>, Self> { 286 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::TraitDef>, Self> {
287 &interner.traits 287 &interner.traits
288 } 288 }
289} 289}
@@ -292,7 +292,7 @@ impl AstItemDef<ast::TraitDef> for TraitId {
292pub struct TypeId(RawId); 292pub struct TypeId(RawId);
293impl_arena_id!(TypeId); 293impl_arena_id!(TypeId);
294impl AstItemDef<ast::TypeAliasDef> for TypeId { 294impl AstItemDef<ast::TypeAliasDef> for TypeId {
295 fn interner(interner: &HirInterner) -> &LocationIntener<ItemLoc<ast::TypeAliasDef>, Self> { 295 fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<ast::TypeAliasDef>, Self> {
296 &interner.types 296 &interner.types
297 } 297 }
298} 298}
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
25impl Name { 25impl 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/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs
index 12ed49a0a..3ea8d592c 100644
--- a/crates/ra_hir/src/nameres/collector.rs
+++ b/crates/ra_hir/src/nameres/collector.rs
@@ -124,7 +124,7 @@ where
124 } 124 }
125 125
126 fn resolve_import( 126 fn resolve_import(
127 &mut self, 127 &self,
128 module_id: CrateModuleId, 128 module_id: CrateModuleId,
129 import: &raw::ImportData, 129 import: &raw::ImportData,
130 ) -> (PerNs<ModuleDef>, ReachedFixedPoint) { 130 ) -> (PerNs<ModuleDef>, ReachedFixedPoint) {
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
12impl UncertainIntTy { 12impl 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
40impl UncertainFloatTy { 50impl 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
89impl IntTy { 99impl 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
127impl UintTy { 149impl 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
141impl fmt::Debug for UintTy { 175impl 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}