aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-11-14 14:38:01 +0000
committerGitHub <[email protected]>2019-11-14 14:38:01 +0000
commitf2c64ba15d3ef87f731f17d90c873383d5f3e432 (patch)
treeff8a962228a7532a5f468862bc4b0c62535f5069
parent5c3ccc55082838524f695ffe40138fd8e805db70 (diff)
parent1583ab1558022d0fdbbc10d3a440a2d3daa4a840 (diff)
Merge #2243
2243: Move body queries to hir_def r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r--crates/ra_hir/src/code_model.rs28
-rw-r--r--crates/ra_hir/src/db.rs19
-rw-r--r--crates/ra_hir/src/expr.rs43
-rw-r--r--crates/ra_hir/src/from_id.rs14
-rw-r--r--crates/ra_hir/src/from_source.rs4
-rw-r--r--crates/ra_hir/src/source_binder.rs2
-rw-r--r--crates/ra_hir/src/ty/infer.rs5
-rw-r--r--crates/ra_hir/src/ty/traits/chalk.rs4
-rw-r--r--crates/ra_hir_def/src/body.rs34
-rw-r--r--crates/ra_hir_def/src/body/scope.rs10
-rw-r--r--crates/ra_hir_def/src/db.rs12
-rw-r--r--crates/ra_hir_def/src/lib.rs10
-rw-r--r--crates/ra_parser/src/grammar/expressions/atom.rs34
-rw-r--r--crates/ra_parser/src/token_set.rs4
14 files changed, 129 insertions, 94 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 2fd4ccb10..962d5a8c1 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -8,6 +8,7 @@ use std::sync::Arc;
8 8
9use hir_def::{ 9use hir_def::{
10 adt::VariantData, 10 adt::VariantData,
11 body::scope::ExprScopes,
11 builtin_type::BuiltinType, 12 builtin_type::BuiltinType,
12 type_ref::{Mutability, TypeRef}, 13 type_ref::{Mutability, TypeRef},
13 CrateModuleId, LocalEnumVariantId, LocalStructFieldId, ModuleId, UnionId, 14 CrateModuleId, LocalEnumVariantId, LocalStructFieldId, ModuleId, UnionId,
@@ -539,6 +540,7 @@ pub trait HasBody: Copy {
539 fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult>; 540 fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult>;
540 fn body(self, db: &impl HirDatabase) -> Arc<Body>; 541 fn body(self, db: &impl HirDatabase) -> Arc<Body>;
541 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap>; 542 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap>;
543 fn expr_scopes(self, db: &impl HirDatabase) -> Arc<ExprScopes>;
542} 544}
543 545
544impl<T> HasBody for T 546impl<T> HasBody for T
@@ -550,11 +552,15 @@ where
550 } 552 }
551 553
552 fn body(self, db: &impl HirDatabase) -> Arc<Body> { 554 fn body(self, db: &impl HirDatabase) -> Arc<Body> {
553 db.body(self.into()) 555 self.into().body(db)
554 } 556 }
555 557
556 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> { 558 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
557 db.body_with_source_map(self.into()).1 559 self.into().body_source_map(db)
560 }
561
562 fn expr_scopes(self, db: &impl HirDatabase) -> Arc<ExprScopes> {
563 self.into().expr_scopes(db)
558 } 564 }
559} 565}
560 566
@@ -564,11 +570,15 @@ impl HasBody for DefWithBody {
564 } 570 }
565 571
566 fn body(self, db: &impl HirDatabase) -> Arc<Body> { 572 fn body(self, db: &impl HirDatabase) -> Arc<Body> {
567 db.body(self) 573 db.body(self.into())
568 } 574 }
569 575
570 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> { 576 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
571 db.body_with_source_map(self).1 577 db.body_with_source_map(self.into()).1
578 }
579
580 fn expr_scopes(self, db: &impl HirDatabase) -> Arc<ExprScopes> {
581 db.expr_scopes(self.into())
572 } 582 }
573} 583}
574 584
@@ -662,11 +672,11 @@ impl Function {
662 } 672 }
663 673
664 pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> { 674 pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
665 db.body_with_source_map(self.into()).1 675 db.body_with_source_map(self.id.into()).1
666 } 676 }
667 677
668 pub fn body(self, db: &impl HirDatabase) -> Arc<Body> { 678 pub fn body(self, db: &impl HirDatabase) -> Arc<Body> {
669 db.body(self.into()) 679 db.body(self.id.into())
670 } 680 }
671 681
672 pub fn ty(self, db: &impl HirDatabase) -> Ty { 682 pub fn ty(self, db: &impl HirDatabase) -> Ty {
@@ -1079,7 +1089,7 @@ pub struct Local {
1079 1089
1080impl Local { 1090impl Local {
1081 pub fn name(self, db: &impl HirDatabase) -> Option<Name> { 1091 pub fn name(self, db: &impl HirDatabase) -> Option<Name> {
1082 let body = db.body(self.parent); 1092 let body = self.parent.body(db);
1083 match &body[self.pat_id] { 1093 match &body[self.pat_id] {
1084 Pat::Bind { name, .. } => Some(name.clone()), 1094 Pat::Bind { name, .. } => Some(name.clone()),
1085 _ => None, 1095 _ => None,
@@ -1091,7 +1101,7 @@ impl Local {
1091 } 1101 }
1092 1102
1093 pub fn is_mut(self, db: &impl HirDatabase) -> bool { 1103 pub fn is_mut(self, db: &impl HirDatabase) -> bool {
1094 let body = db.body(self.parent); 1104 let body = self.parent.body(db);
1095 match &body[self.pat_id] { 1105 match &body[self.pat_id] {
1096 Pat::Bind { mode, .. } => match mode { 1106 Pat::Bind { mode, .. } => match mode {
1097 BindingAnnotation::Mutable | BindingAnnotation::RefMut => true, 1107 BindingAnnotation::Mutable | BindingAnnotation::RefMut => true,
@@ -1115,7 +1125,7 @@ impl Local {
1115 } 1125 }
1116 1126
1117 pub fn source(self, db: &impl HirDatabase) -> Source<Either<ast::BindPat, ast::SelfParam>> { 1127 pub fn source(self, db: &impl HirDatabase) -> Source<Either<ast::BindPat, ast::SelfParam>> {
1118 let (_body, source_map) = db.body_with_source_map(self.parent); 1128 let source_map = self.parent.body_source_map(db);
1119 let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm... 1129 let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm...
1120 let root = src.file_syntax(db); 1130 let root = src.file_syntax(db);
1121 src.map(|ast| ast.map(|it| it.cast().unwrap().to_node(&root), |it| it.to_node(&root))) 1131 src.map(|ast| ast.map(|it| it.cast().unwrap().to_node(&root), |it| it.to_node(&root)))
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 14f6b5df4..c60029c01 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -8,7 +8,6 @@ use ra_syntax::SmolStr;
8 8
9use crate::{ 9use crate::{
10 debug::HirDebugDatabase, 10 debug::HirDebugDatabase,
11 expr::{Body, BodySourceMap},
12 generics::{GenericDef, GenericParams}, 11 generics::{GenericDef, GenericParams},
13 ids, 12 ids,
14 impl_block::{ImplBlock, ImplSourceMap, ModuleImplBlocks}, 13 impl_block::{ImplBlock, ImplSourceMap, ModuleImplBlocks},
@@ -19,13 +18,14 @@ use crate::{
19 InferenceResult, Namespace, Substs, Ty, TypableDef, TypeCtor, 18 InferenceResult, Namespace, Substs, Ty, TypableDef, TypeCtor,
20 }, 19 },
21 type_alias::TypeAliasData, 20 type_alias::TypeAliasData,
22 Const, ConstData, Crate, DefWithBody, ExprScopes, FnData, Function, Module, Static, 21 Const, ConstData, Crate, DefWithBody, FnData, Function, Module, Static, StructField, Trait,
23 StructField, Trait, TypeAlias, 22 TypeAlias,
24}; 23};
25 24
26pub use hir_def::db::{ 25pub use hir_def::db::{
27 CrateDefMapQuery, DefDatabase2, DefDatabase2Storage, EnumDataQuery, InternDatabase, 26 BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage,
28 InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery, 27 EnumDataQuery, ExprScopesQuery, InternDatabase, InternDatabaseStorage, RawItemsQuery,
28 RawItemsWithSourceMapQuery, StructDataQuery,
29}; 29};
30pub use hir_expand::db::{ 30pub use hir_expand::db::{
31 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, 31 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
@@ -85,9 +85,6 @@ pub trait DefDatabase: HirDebugDatabase + DefDatabase2 {
85#[salsa::query_group(HirDatabaseStorage)] 85#[salsa::query_group(HirDatabaseStorage)]
86#[salsa::requires(salsa::Database)] 86#[salsa::requires(salsa::Database)]
87pub trait HirDatabase: DefDatabase + AstDatabase { 87pub trait HirDatabase: DefDatabase + AstDatabase {
88 #[salsa::invoke(crate::expr::expr_scopes_query)]
89 fn expr_scopes(&self, def: DefWithBody) -> Arc<ExprScopes>;
90
91 #[salsa::invoke(crate::ty::infer_query)] 88 #[salsa::invoke(crate::ty::infer_query)]
92 fn infer(&self, def: DefWithBody) -> Arc<InferenceResult>; 89 fn infer(&self, def: DefWithBody) -> Arc<InferenceResult>;
93 90
@@ -113,12 +110,6 @@ pub trait HirDatabase: DefDatabase + AstDatabase {
113 #[salsa::invoke(crate::ty::generic_defaults_query)] 110 #[salsa::invoke(crate::ty::generic_defaults_query)]
114 fn generic_defaults(&self, def: GenericDef) -> Substs; 111 fn generic_defaults(&self, def: GenericDef) -> Substs;
115 112
116 #[salsa::invoke(crate::expr::body_with_source_map_query)]
117 fn body_with_source_map(&self, def: DefWithBody) -> (Arc<Body>, Arc<BodySourceMap>);
118
119 #[salsa::invoke(crate::expr::body_query)]
120 fn body(&self, def: DefWithBody) -> Arc<Body>;
121
122 #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)] 113 #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)]
123 fn impls_in_crate(&self, krate: Crate) -> Arc<CrateImplBlocks>; 114 fn impls_in_crate(&self, krate: Crate) -> Arc<CrateImplBlocks>;
124 115
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index f02104b2d..9262325f2 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -4,9 +4,9 @@ pub(crate) mod validation;
4 4
5use std::sync::Arc; 5use std::sync::Arc;
6 6
7use ra_syntax::{ast, AstPtr}; 7use ra_syntax::AstPtr;
8 8
9use crate::{db::HirDatabase, DefWithBody, HasSource, Resolver}; 9use crate::{db::HirDatabase, DefWithBody, HasBody, Resolver};
10 10
11pub use hir_def::{ 11pub use hir_def::{
12 body::{ 12 body::{
@@ -19,48 +19,13 @@ pub use hir_def::{
19 }, 19 },
20}; 20};
21 21
22pub(crate) fn body_with_source_map_query(
23 db: &impl HirDatabase,
24 def: DefWithBody,
25) -> (Arc<Body>, Arc<BodySourceMap>) {
26 let mut params = None;
27
28 let (file_id, body) = match def {
29 DefWithBody::Function(f) => {
30 let src = f.source(db);
31 params = src.ast.param_list();
32 (src.file_id, src.ast.body().map(ast::Expr::from))
33 }
34 DefWithBody::Const(c) => {
35 let src = c.source(db);
36 (src.file_id, src.ast.body())
37 }
38 DefWithBody::Static(s) => {
39 let src = s.source(db);
40 (src.file_id, src.ast.body())
41 }
42 };
43 let expander = hir_def::body::Expander::new(db, file_id, def.module(db).id);
44 let (body, source_map) = Body::new(db, expander, params, body);
45 (Arc::new(body), Arc::new(source_map))
46}
47
48pub(crate) fn body_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> {
49 db.body_with_source_map(def).0
50}
51
52pub(crate) fn expr_scopes_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<ExprScopes> {
53 let body = db.body(def);
54 Arc::new(ExprScopes::new(&*body))
55}
56
57// needs arbitrary_self_types to be a method... or maybe move to the def? 22// needs arbitrary_self_types to be a method... or maybe move to the def?
58pub(crate) fn resolver_for_expr( 23pub(crate) fn resolver_for_expr(
59 db: &impl HirDatabase, 24 db: &impl HirDatabase,
60 owner: DefWithBody, 25 owner: DefWithBody,
61 expr_id: ExprId, 26 expr_id: ExprId,
62) -> Resolver { 27) -> Resolver {
63 let scopes = db.expr_scopes(owner); 28 let scopes = owner.expr_scopes(db);
64 resolver_for_scope(db, owner, scopes.scope_for(expr_id)) 29 resolver_for_scope(db, owner, scopes.scope_for(expr_id))
65} 30}
66 31
@@ -70,7 +35,7 @@ pub(crate) fn resolver_for_scope(
70 scope_id: Option<ScopeId>, 35 scope_id: Option<ScopeId>,
71) -> Resolver { 36) -> Resolver {
72 let mut r = owner.resolver(db); 37 let mut r = owner.resolver(db);
73 let scopes = db.expr_scopes(owner); 38 let scopes = owner.expr_scopes(db);
74 let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>(); 39 let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
75 for scope in scope_chain.into_iter().rev() { 40 for scope in scope_chain.into_iter().rev() {
76 r = r.push_expr_scope(Arc::clone(&scopes), scope); 41 r = r.push_expr_scope(Arc::clone(&scopes), scope);
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs
index 089dbc908..9633ef586 100644
--- a/crates/ra_hir/src/from_id.rs
+++ b/crates/ra_hir/src/from_id.rs
@@ -3,9 +3,9 @@
3//! It's unclear if we need this long-term, but it's definitelly useful while we 3//! It's unclear if we need this long-term, but it's definitelly useful while we
4//! are splitting the hir. 4//! are splitting the hir.
5 5
6use hir_def::{AdtId, EnumVariantId, ModuleDefId}; 6use hir_def::{AdtId, DefWithBodyId, EnumVariantId, ModuleDefId};
7 7
8use crate::{Adt, EnumVariant, ModuleDef}; 8use crate::{Adt, DefWithBody, EnumVariant, ModuleDef};
9 9
10macro_rules! from_id { 10macro_rules! from_id {
11 ($(($id:path, $ty:path)),*) => {$( 11 ($(($id:path, $ty:path)),*) => {$(
@@ -61,3 +61,13 @@ impl From<ModuleDefId> for ModuleDef {
61 } 61 }
62 } 62 }
63} 63}
64
65impl From<DefWithBody> for DefWithBodyId {
66 fn from(def: DefWithBody) -> Self {
67 match def {
68 DefWithBody::Function(it) => DefWithBodyId::FunctionId(it.id),
69 DefWithBody::Static(it) => DefWithBodyId::StaticId(it.id),
70 DefWithBody::Const(it) => DefWithBodyId::ConstId(it.id),
71 }
72 }
73}
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index 4b561c63d..9793af858 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -10,7 +10,7 @@ use ra_syntax::{
10use crate::{ 10use crate::{
11 db::{AstDatabase, DefDatabase, HirDatabase}, 11 db::{AstDatabase, DefDatabase, HirDatabase},
12 ids::{AstItemDef, LocationCtx}, 12 ids::{AstItemDef, LocationCtx},
13 AstId, Const, Crate, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, 13 AstId, Const, Crate, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource,
14 ImplBlock, Local, Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, 14 ImplBlock, Local, Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias,
15 Union, VariantDef, 15 Union, VariantDef,
16}; 16};
@@ -144,7 +144,7 @@ impl Local {
144 }; 144 };
145 Some(res) 145 Some(res)
146 })?; 146 })?;
147 let (_body, source_map) = db.body_with_source_map(parent); 147 let source_map = parent.body_source_map(db);
148 let src = src.map(ast::Pat::from); 148 let src = src.map(ast::Pat::from);
149 let pat_id = source_map.node_pat(src.as_ref())?; 149 let pat_id = source_map.node_pat(src.as_ref())?;
150 Some(Local { parent, pat_id }) 150 Some(Local { parent, pat_id })
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index e337a3d4a..ca40e3b54 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -146,7 +146,7 @@ impl SourceAnalyzer {
146 let def_with_body = def_with_body_from_child_node(db, file_id, node); 146 let def_with_body = def_with_body_from_child_node(db, file_id, node);
147 if let Some(def) = def_with_body { 147 if let Some(def) = def_with_body {
148 let source_map = def.body_source_map(db); 148 let source_map = def.body_source_map(db);
149 let scopes = db.expr_scopes(def); 149 let scopes = def.expr_scopes(db);
150 let scope = match offset { 150 let scope = match offset {
151 None => scope_for(&scopes, &source_map, file_id.into(), &node), 151 None => scope_for(&scopes, &source_map, file_id.into(), &node),
152 Some(offset) => scope_for_offset(&scopes, &source_map, file_id.into(), offset), 152 Some(offset) => scope_for_offset(&scopes, &source_map, file_id.into(), offset),
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index c09260864..c35378cc4 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -43,7 +43,8 @@ use crate::{
43 expr::{BindingAnnotation, Body, ExprId, PatId}, 43 expr::{BindingAnnotation, Body, ExprId, PatId},
44 resolve::{Resolver, TypeNs}, 44 resolve::{Resolver, TypeNs},
45 ty::infer::diagnostics::InferenceDiagnostic, 45 ty::infer::diagnostics::InferenceDiagnostic,
46 Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, IntTy, Path, StructField, 46 Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path,
47 StructField,
47}; 48};
48 49
49macro_rules! ty_app { 50macro_rules! ty_app {
@@ -214,7 +215,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
214 coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver), 215 coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver),
215 db, 216 db,
216 owner, 217 owner,
217 body: db.body(owner), 218 body: owner.body(db),
218 resolver, 219 resolver,
219 } 220 }
220 } 221 }
diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs
index de322dd52..75351c17d 100644
--- a/crates/ra_hir/src/ty/traits/chalk.rs
+++ b/crates/ra_hir/src/ty/traits/chalk.rs
@@ -22,7 +22,7 @@ use crate::{
22 ApplicationTy, GenericPredicate, Namespace, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, 22 ApplicationTy, GenericPredicate, Namespace, ProjectionTy, Substs, TraitRef, Ty, TypeCtor,
23 TypeWalk, 23 TypeWalk,
24 }, 24 },
25 AssocItem, Crate, ImplBlock, Trait, TypeAlias, 25 AssocItem, Crate, HasBody, ImplBlock, Trait, TypeAlias,
26}; 26};
27 27
28/// This represents a trait whose name we could not resolve. 28/// This represents a trait whose name we could not resolve.
@@ -714,7 +714,7 @@ fn closure_fn_trait_impl_datum(
714 let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?; 714 let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?;
715 let trait_ = get_fn_trait(db, krate, data.fn_trait)?; // get corresponding fn trait 715 let trait_ = get_fn_trait(db, krate, data.fn_trait)?; // get corresponding fn trait
716 716
717 let num_args: u16 = match &db.body(data.def)[data.expr] { 717 let num_args: u16 = match &data.def.body(db)[data.expr] {
718 crate::expr::Expr::Lambda { args, .. } => args.len() as u16, 718 crate::expr::Expr::Lambda { args, .. } => args.len() as u16,
719 _ => { 719 _ => {
720 log::warn!("closure for closure type {:?} not found", data); 720 log::warn!("closure for closure type {:?} not found", data);
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index c3e9d0c23..85dc4feb0 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -17,7 +17,7 @@ use crate::{
17 expr::{Expr, ExprId, Pat, PatId}, 17 expr::{Expr, ExprId, Pat, PatId},
18 nameres::CrateDefMap, 18 nameres::CrateDefMap,
19 path::Path, 19 path::Path,
20 ModuleId, 20 AstItemDef, DefWithBodyId, ModuleId,
21}; 21};
22 22
23pub struct Expander { 23pub struct Expander {
@@ -141,7 +141,37 @@ pub struct BodySourceMap {
141} 141}
142 142
143impl Body { 143impl Body {
144 pub fn new( 144 pub(crate) fn body_with_source_map_query(
145 db: &impl DefDatabase2,
146 def: DefWithBodyId,
147 ) -> (Arc<Body>, Arc<BodySourceMap>) {
148 let mut params = None;
149
150 let (file_id, module, body) = match def {
151 DefWithBodyId::FunctionId(f) => {
152 let src = f.source(db);
153 params = src.ast.param_list();
154 (src.file_id, f.module(db), src.ast.body().map(ast::Expr::from))
155 }
156 DefWithBodyId::ConstId(c) => {
157 let src = c.source(db);
158 (src.file_id, c.module(db), src.ast.body())
159 }
160 DefWithBodyId::StaticId(s) => {
161 let src = s.source(db);
162 (src.file_id, s.module(db), src.ast.body())
163 }
164 };
165 let expander = Expander::new(db, file_id, module);
166 let (body, source_map) = Body::new(db, expander, params, body);
167 (Arc::new(body), Arc::new(source_map))
168 }
169
170 pub(crate) fn body_query(db: &impl DefDatabase2, def: DefWithBodyId) -> Arc<Body> {
171 db.body_with_source_map(def).0
172 }
173
174 fn new(
145 db: &impl DefDatabase2, 175 db: &impl DefDatabase2,
146 expander: Expander, 176 expander: Expander,
147 params: Option<ast::ParamList>, 177 params: Option<ast::ParamList>,
diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs
index dd8d06d11..09a39e721 100644
--- a/crates/ra_hir_def/src/body/scope.rs
+++ b/crates/ra_hir_def/src/body/scope.rs
@@ -1,4 +1,5 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2use std::sync::Arc;
2 3
3use hir_expand::name::Name; 4use hir_expand::name::Name;
4use ra_arena::{impl_arena_id, Arena, RawId}; 5use ra_arena::{impl_arena_id, Arena, RawId};
@@ -6,7 +7,9 @@ use rustc_hash::FxHashMap;
6 7
7use crate::{ 8use crate::{
8 body::Body, 9 body::Body,
10 db::DefDatabase2,
9 expr::{Expr, ExprId, Pat, PatId, Statement}, 11 expr::{Expr, ExprId, Pat, PatId, Statement},
12 DefWithBodyId,
10}; 13};
11 14
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 15#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -42,7 +45,12 @@ pub struct ScopeData {
42} 45}
43 46
44impl ExprScopes { 47impl ExprScopes {
45 pub fn new(body: &Body) -> ExprScopes { 48 pub(crate) fn expr_scopes_query(db: &impl DefDatabase2, def: DefWithBodyId) -> Arc<ExprScopes> {
49 let body = db.body(def);
50 Arc::new(ExprScopes::new(&*body))
51 }
52
53 fn new(body: &Body) -> ExprScopes {
46 let mut scopes = 54 let mut scopes =
47 ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() }; 55 ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() };
48 let root = scopes.root_scope(); 56 let root = scopes.root_scope();
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index 29cf71a59..40b5920d9 100644
--- a/crates/ra_hir_def/src/db.rs
+++ b/crates/ra_hir_def/src/db.rs
@@ -7,11 +7,12 @@ use ra_syntax::ast;
7 7
8use crate::{ 8use crate::{
9 adt::{EnumData, StructData}, 9 adt::{EnumData, StructData},
10 body::{scope::ExprScopes, Body, BodySourceMap},
10 nameres::{ 11 nameres::{
11 raw::{ImportSourceMap, RawItems}, 12 raw::{ImportSourceMap, RawItems},
12 CrateDefMap, 13 CrateDefMap,
13 }, 14 },
14 EnumId, StructOrUnionId, 15 DefWithBodyId, EnumId, StructOrUnionId,
15}; 16};
16 17
17#[salsa::query_group(InternDatabaseStorage)] 18#[salsa::query_group(InternDatabaseStorage)]
@@ -52,4 +53,13 @@ pub trait DefDatabase2: InternDatabase + AstDatabase {
52 53
53 #[salsa::invoke(EnumData::enum_data_query)] 54 #[salsa::invoke(EnumData::enum_data_query)]
54 fn enum_data(&self, e: EnumId) -> Arc<EnumData>; 55 fn enum_data(&self, e: EnumId) -> Arc<EnumData>;
56
57 #[salsa::invoke(Body::body_with_source_map_query)]
58 fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
59
60 #[salsa::invoke(Body::body_query)]
61 fn body(&self, def: DefWithBodyId) -> Arc<Body>;
62
63 #[salsa::invoke(ExprScopes::expr_scopes_query)]
64 fn expr_scopes(&self, def: DefWithBodyId) -> Arc<ExprScopes>;
55} 65}
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 4a758bb83..3fab7965c 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -374,3 +374,13 @@ impl_froms!(
374 TypeAliasId, 374 TypeAliasId,
375 BuiltinType 375 BuiltinType
376); 376);
377
378/// The defs which have a body.
379#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
380pub enum DefWithBodyId {
381 FunctionId(FunctionId),
382 StaticId(StaticId),
383 ConstId(ConstId),
384}
385
386impl_froms!(DefWithBodyId: FunctionId, ConstId, StaticId);
diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs
index 4952bd189..f06191963 100644
--- a/crates/ra_parser/src/grammar/expressions/atom.rs
+++ b/crates/ra_parser/src/grammar/expressions/atom.rs
@@ -40,24 +40,24 @@ pub(crate) fn literal(p: &mut Parser) -> Option<CompletedMarker> {
40// E.g. for after the break in `if break {}`, this should not match 40// E.g. for after the break in `if break {}`, this should not match
41pub(super) const ATOM_EXPR_FIRST: TokenSet = 41pub(super) const ATOM_EXPR_FIRST: TokenSet =
42 LITERAL_FIRST.union(paths::PATH_FIRST).union(token_set![ 42 LITERAL_FIRST.union(paths::PATH_FIRST).union(token_set![
43 L_PAREN, 43 T!['('],
44 L_CURLY, 44 T!['{'],
45 L_BRACK, 45 T!['['],
46 PIPE, 46 T![|],
47 MOVE_KW, 47 T![move],
48 BOX_KW, 48 T![box],
49 IF_KW, 49 T![if],
50 WHILE_KW, 50 T![while],
51 MATCH_KW, 51 T![match],
52 UNSAFE_KW, 52 T![unsafe],
53 RETURN_KW, 53 T![return],
54 BREAK_KW, 54 T![break],
55 CONTINUE_KW, 55 T![continue],
56 T![async],
57 T![try],
58 T![loop],
59 T![for],
56 LIFETIME, 60 LIFETIME,
57 ASYNC_KW,
58 TRY_KW,
59 LOOP_KW,
60 FOR_KW,
61 ]); 61 ]);
62 62
63const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW]; 63const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW];
diff --git a/crates/ra_parser/src/token_set.rs b/crates/ra_parser/src/token_set.rs
index 6dc061889..2a6952c01 100644
--- a/crates/ra_parser/src/token_set.rs
+++ b/crates/ra_parser/src/token_set.rs
@@ -30,8 +30,8 @@ const fn mask(kind: SyntaxKind) -> u128 {
30 30
31#[macro_export] 31#[macro_export]
32macro_rules! token_set { 32macro_rules! token_set {
33 ($($t:ident),*) => { TokenSet::empty()$(.union(TokenSet::singleton($t)))* }; 33 ($($t:expr),*) => { TokenSet::empty()$(.union(TokenSet::singleton($t)))* };
34 ($($t:ident),* ,) => { token_set!($($t),*) }; 34 ($($t:expr),* ,) => { token_set!($($t),*) };
35} 35}
36 36
37#[test] 37#[test]