aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2019-01-06 22:57:39 +0000
committerFlorian Diebold <[email protected]>2019-01-06 23:05:19 +0000
commit71f7d82e45145281b9aec5bcdc694524864e552b (patch)
treedab214dd290cfb87c85b56b478a477eeaf4edc6e /crates/ra_hir/src
parentcf49a11263c4d48720250db0c448b97dbec3d8b9 (diff)
Introduce ArenaMap
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/expr.rs18
-rw-r--r--crates/ra_hir/src/ty.rs24
-rw-r--r--crates/ra_hir/src/ty/tests.rs8
3 files changed, 25 insertions, 25 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index c87d76735..69144e94f 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -3,7 +3,7 @@ use std::sync::Arc;
3 3
4use rustc_hash::FxHashMap; 4use rustc_hash::FxHashMap;
5 5
6use ra_arena::{Arena, RawId, impl_arena_id}; 6use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
7use ra_db::{LocalSyntaxPtr, Cancelable}; 7use ra_db::{LocalSyntaxPtr, Cancelable};
8use ra_syntax::{SyntaxNodeRef, ast::{self, AstNode, LoopBodyOwner, ArgListOwner, NameOwner}}; 8use ra_syntax::{SyntaxNodeRef, ast::{self, AstNode, LoopBodyOwner, ArgListOwner, NameOwner}};
9 9
@@ -39,9 +39,9 @@ pub struct Body {
39pub struct BodySyntaxMapping { 39pub struct BodySyntaxMapping {
40 body: Arc<Body>, 40 body: Arc<Body>,
41 expr_syntax_mapping: FxHashMap<LocalSyntaxPtr, ExprId>, 41 expr_syntax_mapping: FxHashMap<LocalSyntaxPtr, ExprId>,
42 expr_syntax_mapping_back: FxHashMap<ExprId, LocalSyntaxPtr>, 42 expr_syntax_mapping_back: ArenaMap<ExprId, LocalSyntaxPtr>,
43 pat_syntax_mapping: FxHashMap<LocalSyntaxPtr, PatId>, 43 pat_syntax_mapping: FxHashMap<LocalSyntaxPtr, PatId>,
44 pat_syntax_mapping_back: FxHashMap<PatId, LocalSyntaxPtr>, 44 pat_syntax_mapping_back: ArenaMap<PatId, LocalSyntaxPtr>,
45} 45}
46 46
47impl Body { 47impl Body {
@@ -72,7 +72,7 @@ impl Index<PatId> for Body {
72 72
73impl BodySyntaxMapping { 73impl BodySyntaxMapping {
74 pub fn expr_syntax(&self, expr: ExprId) -> Option<LocalSyntaxPtr> { 74 pub fn expr_syntax(&self, expr: ExprId) -> Option<LocalSyntaxPtr> {
75 self.expr_syntax_mapping_back.get(&expr).cloned() 75 self.expr_syntax_mapping_back.get(expr).cloned()
76 } 76 }
77 pub fn syntax_expr(&self, ptr: LocalSyntaxPtr) -> Option<ExprId> { 77 pub fn syntax_expr(&self, ptr: LocalSyntaxPtr) -> Option<ExprId> {
78 self.expr_syntax_mapping.get(&ptr).cloned() 78 self.expr_syntax_mapping.get(&ptr).cloned()
@@ -83,7 +83,7 @@ impl BodySyntaxMapping {
83 .cloned() 83 .cloned()
84 } 84 }
85 pub fn pat_syntax(&self, pat: PatId) -> Option<LocalSyntaxPtr> { 85 pub fn pat_syntax(&self, pat: PatId) -> Option<LocalSyntaxPtr> {
86 self.pat_syntax_mapping_back.get(&pat).cloned() 86 self.pat_syntax_mapping_back.get(pat).cloned()
87 } 87 }
88 pub fn syntax_pat(&self, ptr: LocalSyntaxPtr) -> Option<PatId> { 88 pub fn syntax_pat(&self, ptr: LocalSyntaxPtr) -> Option<PatId> {
89 self.pat_syntax_mapping.get(&ptr).cloned() 89 self.pat_syntax_mapping.get(&ptr).cloned()
@@ -334,9 +334,9 @@ struct ExprCollector {
334 exprs: Arena<ExprId, Expr>, 334 exprs: Arena<ExprId, Expr>,
335 pats: Arena<PatId, Pat>, 335 pats: Arena<PatId, Pat>,
336 expr_syntax_mapping: FxHashMap<LocalSyntaxPtr, ExprId>, 336 expr_syntax_mapping: FxHashMap<LocalSyntaxPtr, ExprId>,
337 expr_syntax_mapping_back: FxHashMap<ExprId, LocalSyntaxPtr>, 337 expr_syntax_mapping_back: ArenaMap<ExprId, LocalSyntaxPtr>,
338 pat_syntax_mapping: FxHashMap<LocalSyntaxPtr, PatId>, 338 pat_syntax_mapping: FxHashMap<LocalSyntaxPtr, PatId>,
339 pat_syntax_mapping_back: FxHashMap<PatId, LocalSyntaxPtr>, 339 pat_syntax_mapping_back: ArenaMap<PatId, LocalSyntaxPtr>,
340} 340}
341 341
342impl ExprCollector { 342impl ExprCollector {
@@ -345,9 +345,9 @@ impl ExprCollector {
345 exprs: Arena::default(), 345 exprs: Arena::default(),
346 pats: Arena::default(), 346 pats: Arena::default(),
347 expr_syntax_mapping: FxHashMap::default(), 347 expr_syntax_mapping: FxHashMap::default(),
348 expr_syntax_mapping_back: FxHashMap::default(), 348 expr_syntax_mapping_back: ArenaMap::default(),
349 pat_syntax_mapping: FxHashMap::default(), 349 pat_syntax_mapping: FxHashMap::default(),
350 pat_syntax_mapping_back: FxHashMap::default(), 350 pat_syntax_mapping_back: ArenaMap::default(),
351 } 351 }
352 } 352 }
353 353
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 6bdfdd7b4..d57990cd2 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -22,8 +22,8 @@ use std::sync::Arc;
22use std::{fmt, mem}; 22use std::{fmt, mem};
23 23
24use log; 24use log;
25use rustc_hash::FxHashMap;
26use ena::unify::{InPlaceUnificationTable, UnifyKey, UnifyValue, NoError}; 25use ena::unify::{InPlaceUnificationTable, UnifyKey, UnifyValue, NoError};
26use ra_arena::map::ArenaMap;
27 27
28use ra_db::Cancelable; 28use ra_db::Cancelable;
29 29
@@ -470,15 +470,15 @@ pub(super) fn type_for_field(db: &impl HirDatabase, def_id: DefId, field: Name)
470/// The result of type inference: A mapping from expressions and patterns to types. 470/// The result of type inference: A mapping from expressions and patterns to types.
471#[derive(Clone, PartialEq, Eq, Debug)] 471#[derive(Clone, PartialEq, Eq, Debug)]
472pub struct InferenceResult { 472pub struct InferenceResult {
473 type_of_expr: FxHashMap<ExprId, Ty>, 473 type_of_expr: ArenaMap<ExprId, Ty>,
474 type_of_pat: FxHashMap<PatId, Ty>, 474 type_of_pat: ArenaMap<PatId, Ty>,
475} 475}
476 476
477impl Index<ExprId> for InferenceResult { 477impl Index<ExprId> for InferenceResult {
478 type Output = Ty; 478 type Output = Ty;
479 479
480 fn index(&self, expr: ExprId) -> &Ty { 480 fn index(&self, expr: ExprId) -> &Ty {
481 self.type_of_expr.get(&expr).unwrap_or(&Ty::Unknown) 481 self.type_of_expr.get(expr).unwrap_or(&Ty::Unknown)
482 } 482 }
483} 483}
484 484
@@ -486,7 +486,7 @@ impl Index<PatId> for InferenceResult {
486 type Output = Ty; 486 type Output = Ty;
487 487
488 fn index(&self, pat: PatId) -> &Ty { 488 fn index(&self, pat: PatId) -> &Ty {
489 self.type_of_pat.get(&pat).unwrap_or(&Ty::Unknown) 489 self.type_of_pat.get(pat).unwrap_or(&Ty::Unknown)
490 } 490 }
491} 491}
492 492
@@ -499,8 +499,8 @@ struct InferenceContext<'a, D: HirDatabase> {
499 module: Module, 499 module: Module,
500 impl_block: Option<ImplBlock>, 500 impl_block: Option<ImplBlock>,
501 var_unification_table: InPlaceUnificationTable<TypeVarId>, 501 var_unification_table: InPlaceUnificationTable<TypeVarId>,
502 type_of_expr: FxHashMap<ExprId, Ty>, 502 type_of_expr: ArenaMap<ExprId, Ty>,
503 type_of_pat: FxHashMap<PatId, Ty>, 503 type_of_pat: ArenaMap<PatId, Ty>,
504 /// The return type of the function being inferred. 504 /// The return type of the function being inferred.
505 return_ty: Ty, 505 return_ty: Ty,
506} 506}
@@ -528,8 +528,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
528 impl_block: Option<ImplBlock>, 528 impl_block: Option<ImplBlock>,
529 ) -> Self { 529 ) -> Self {
530 InferenceContext { 530 InferenceContext {
531 type_of_expr: FxHashMap::default(), 531 type_of_expr: ArenaMap::default(),
532 type_of_pat: FxHashMap::default(), 532 type_of_pat: ArenaMap::default(),
533 var_unification_table: InPlaceUnificationTable::new(), 533 var_unification_table: InPlaceUnificationTable::new(),
534 return_ty: Ty::Unknown, // set in collect_fn_signature 534 return_ty: Ty::Unknown, // set in collect_fn_signature
535 db, 535 db,
@@ -541,12 +541,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
541 } 541 }
542 542
543 fn resolve_all(mut self) -> InferenceResult { 543 fn resolve_all(mut self) -> InferenceResult {
544 let mut expr_types = mem::replace(&mut self.type_of_expr, FxHashMap::default()); 544 let mut expr_types = mem::replace(&mut self.type_of_expr, ArenaMap::default());
545 for ty in expr_types.values_mut() { 545 for ty in expr_types.values_mut() {
546 let resolved = self.resolve_ty_completely(mem::replace(ty, Ty::Unknown)); 546 let resolved = self.resolve_ty_completely(mem::replace(ty, Ty::Unknown));
547 *ty = resolved; 547 *ty = resolved;
548 } 548 }
549 let mut pat_types = mem::replace(&mut self.type_of_pat, FxHashMap::default()); 549 let mut pat_types = mem::replace(&mut self.type_of_pat, ArenaMap::default());
550 for ty in pat_types.values_mut() { 550 for ty in pat_types.values_mut() {
551 let resolved = self.resolve_ty_completely(mem::replace(ty, Ty::Unknown)); 551 let resolved = self.resolve_ty_completely(mem::replace(ty, Ty::Unknown));
552 *ty = resolved; 552 *ty = resolved;
@@ -666,7 +666,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
666 // resolve locally 666 // resolve locally
667 let name = path.as_ident().cloned().unwrap_or_else(Name::self_param); 667 let name = path.as_ident().cloned().unwrap_or_else(Name::self_param);
668 if let Some(scope_entry) = self.scopes.resolve_local_name(expr, name) { 668 if let Some(scope_entry) = self.scopes.resolve_local_name(expr, name) {
669 let ty = ctry!(self.type_of_pat.get(&scope_entry.pat())); 669 let ty = ctry!(self.type_of_pat.get(scope_entry.pat()));
670 let ty = self.resolve_ty_as_possible(ty.clone()); 670 let ty = self.resolve_ty_as_possible(ty.clone());
671 return Ok(Some(ty)); 671 return Ok(Some(ty));
672 }; 672 };
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 20fb783b8..030a20f1b 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -195,16 +195,16 @@ fn infer(content: &str) -> String {
195 let inference_result = func.infer(&db).unwrap(); 195 let inference_result = func.infer(&db).unwrap();
196 let body_syntax_mapping = func.body_syntax_mapping(&db).unwrap(); 196 let body_syntax_mapping = func.body_syntax_mapping(&db).unwrap();
197 let mut types = Vec::new(); 197 let mut types = Vec::new();
198 for (pat, ty) in &inference_result.type_of_pat { 198 for (pat, ty) in inference_result.type_of_pat.iter() {
199 let syntax_ptr = if let Some(sp) = body_syntax_mapping.pat_syntax(*pat) { 199 let syntax_ptr = if let Some(sp) = body_syntax_mapping.pat_syntax(pat) {
200 sp 200 sp
201 } else { 201 } else {
202 continue; 202 continue;
203 }; 203 };
204 types.push((syntax_ptr, ty)); 204 types.push((syntax_ptr, ty));
205 } 205 }
206 for (expr, ty) in &inference_result.type_of_expr { 206 for (expr, ty) in inference_result.type_of_expr.iter() {
207 let syntax_ptr = if let Some(sp) = body_syntax_mapping.expr_syntax(*expr) { 207 let syntax_ptr = if let Some(sp) = body_syntax_mapping.expr_syntax(expr) {
208 sp 208 sp
209 } else { 209 } else {
210 continue; 210 continue;