diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-12 13:52:30 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-12 13:52:30 +0000 |
commit | 55f3ff241a2105d2903266703474acbd24a85e84 (patch) | |
tree | d953d49eac9da39748afabd512b5390875e18ba1 | |
parent | 6149ee30ef9a379f642c8a645cea13f32c4f3d84 (diff) | |
parent | fe00db72b91d266a61b0541bca59e38e5f2a703c (diff) |
Merge #2222
2222: Remove owner from Body r=matklad a=matklad
cc @flodiebold
I do this so that it's easier to move lowering code to another crate (owner is the only thing that tethers Body to the rest of the code), but it's interesting that this is a net reduction of lines. I think this might be considered an evidence that it's a good idea to not add "parent pointers" / parent ids to data structures, and instead add them to `ctx` objects which are used when building data structures
bors r+
Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r-- | crates/ra_hir/src/expr.rs | 20 | ||||
-rw-r--r-- | crates/ra_hir/src/expr/lower.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 11 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer/expr.rs | 8 |
5 files changed, 19 insertions, 28 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index dd2bae9b4..ddf605111 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -22,8 +22,6 @@ pub use hir_def::expr::{ | |||
22 | /// The body of an item (function, const etc.). | 22 | /// The body of an item (function, const etc.). |
23 | #[derive(Debug, Eq, PartialEq)] | 23 | #[derive(Debug, Eq, PartialEq)] |
24 | pub struct Body { | 24 | pub struct Body { |
25 | /// The def of the item this body belongs to | ||
26 | owner: DefWithBody, | ||
27 | exprs: Arena<ExprId, Expr>, | 25 | exprs: Arena<ExprId, Expr>, |
28 | pats: Arena<PatId, Pat>, | 26 | pats: Arena<PatId, Pat>, |
29 | /// The patterns for the function's parameters. While the parameter types are | 27 | /// The patterns for the function's parameters. While the parameter types are |
@@ -86,7 +84,7 @@ impl Body { | |||
86 | } | 84 | } |
87 | }; | 85 | }; |
88 | 86 | ||
89 | let (body, source_map) = lower::lower(db, def.resolver(db), file_id, def, params, body); | 87 | let (body, source_map) = lower::lower(db, def.resolver(db), file_id, params, body); |
90 | (Arc::new(body), Arc::new(source_map)) | 88 | (Arc::new(body), Arc::new(source_map)) |
91 | } | 89 | } |
92 | 90 | ||
@@ -102,10 +100,6 @@ impl Body { | |||
102 | self.body_expr | 100 | self.body_expr |
103 | } | 101 | } |
104 | 102 | ||
105 | pub fn owner(&self) -> DefWithBody { | ||
106 | self.owner | ||
107 | } | ||
108 | |||
109 | pub fn exprs(&self) -> impl Iterator<Item = (ExprId, &Expr)> { | 103 | pub fn exprs(&self) -> impl Iterator<Item = (ExprId, &Expr)> { |
110 | self.exprs.iter() | 104 | self.exprs.iter() |
111 | } | 105 | } |
@@ -117,21 +111,21 @@ impl Body { | |||
117 | 111 | ||
118 | // needs arbitrary_self_types to be a method... or maybe move to the def? | 112 | // needs arbitrary_self_types to be a method... or maybe move to the def? |
119 | pub(crate) fn resolver_for_expr( | 113 | pub(crate) fn resolver_for_expr( |
120 | body: Arc<Body>, | ||
121 | db: &impl HirDatabase, | 114 | db: &impl HirDatabase, |
115 | owner: DefWithBody, | ||
122 | expr_id: ExprId, | 116 | expr_id: ExprId, |
123 | ) -> Resolver { | 117 | ) -> Resolver { |
124 | let scopes = db.expr_scopes(body.owner); | 118 | let scopes = db.expr_scopes(owner); |
125 | resolver_for_scope(body, db, scopes.scope_for(expr_id)) | 119 | resolver_for_scope(db, owner, scopes.scope_for(expr_id)) |
126 | } | 120 | } |
127 | 121 | ||
128 | pub(crate) fn resolver_for_scope( | 122 | pub(crate) fn resolver_for_scope( |
129 | body: Arc<Body>, | ||
130 | db: &impl HirDatabase, | 123 | db: &impl HirDatabase, |
124 | owner: DefWithBody, | ||
131 | scope_id: Option<scope::ScopeId>, | 125 | scope_id: Option<scope::ScopeId>, |
132 | ) -> Resolver { | 126 | ) -> Resolver { |
133 | let mut r = body.owner.resolver(db); | 127 | let mut r = owner.resolver(db); |
134 | let scopes = db.expr_scopes(body.owner); | 128 | let scopes = db.expr_scopes(owner); |
135 | let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>(); | 129 | let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>(); |
136 | for scope in scope_chain.into_iter().rev() { | 130 | for scope in scope_chain.into_iter().rev() { |
137 | r = r.push_expr_scope(Arc::clone(&scopes), scope); | 131 | r = r.push_expr_scope(Arc::clone(&scopes), scope); |
diff --git a/crates/ra_hir/src/expr/lower.rs b/crates/ra_hir/src/expr/lower.rs index 7b0bfd919..adc68b23c 100644 --- a/crates/ra_hir/src/expr/lower.rs +++ b/crates/ra_hir/src/expr/lower.rs | |||
@@ -20,8 +20,8 @@ use ra_syntax::{ | |||
20 | use test_utils::tested_by; | 20 | use test_utils::tested_by; |
21 | 21 | ||
22 | use crate::{ | 22 | use crate::{ |
23 | db::HirDatabase, AstId, DefWithBody, Either, HirFileId, MacroCallLoc, MacroFileKind, | 23 | db::HirDatabase, AstId, Either, HirFileId, MacroCallLoc, MacroFileKind, Mutability, Path, |
24 | Mutability, Path, Resolver, Source, | 24 | Resolver, Source, |
25 | }; | 25 | }; |
26 | 26 | ||
27 | use super::{ | 27 | use super::{ |
@@ -33,7 +33,6 @@ pub(super) fn lower( | |||
33 | db: &impl HirDatabase, | 33 | db: &impl HirDatabase, |
34 | resolver: Resolver, | 34 | resolver: Resolver, |
35 | file_id: HirFileId, | 35 | file_id: HirFileId, |
36 | owner: DefWithBody, | ||
37 | params: Option<ast::ParamList>, | 36 | params: Option<ast::ParamList>, |
38 | body: Option<ast::Expr>, | 37 | body: Option<ast::Expr>, |
39 | ) -> (Body, BodySourceMap) { | 38 | ) -> (Body, BodySourceMap) { |
@@ -44,7 +43,6 @@ pub(super) fn lower( | |||
44 | current_file_id: file_id, | 43 | current_file_id: file_id, |
45 | source_map: BodySourceMap::default(), | 44 | source_map: BodySourceMap::default(), |
46 | body: Body { | 45 | body: Body { |
47 | owner, | ||
48 | exprs: Arena::default(), | 46 | exprs: Arena::default(), |
49 | pats: Arena::default(), | 47 | pats: Arena::default(), |
50 | params: Vec::new(), | 48 | params: Vec::new(), |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index fe4211819..f28e9c931 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -150,7 +150,7 @@ impl SourceAnalyzer { | |||
150 | None => scope_for(&scopes, &source_map, &node), | 150 | None => scope_for(&scopes, &source_map, &node), |
151 | Some(offset) => scope_for_offset(&scopes, &source_map, file_id.into(), offset), | 151 | Some(offset) => scope_for_offset(&scopes, &source_map, file_id.into(), offset), |
152 | }; | 152 | }; |
153 | let resolver = expr::resolver_for_scope(def.body(db), db, scope); | 153 | let resolver = expr::resolver_for_scope(db, def, scope); |
154 | SourceAnalyzer { | 154 | SourceAnalyzer { |
155 | resolver, | 155 | resolver, |
156 | body_owner: Some(def), | 156 | body_owner: Some(def), |
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 2370e8d4f..f17c6c614 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -43,7 +43,7 @@ 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, FnData, Function, HasBody, Path, StructField, | 46 | Adt, AssocItem, ConstData, DefWithBody, FnData, Function, Path, StructField, |
47 | }; | 47 | }; |
48 | 48 | ||
49 | macro_rules! ty_app { | 49 | macro_rules! ty_app { |
@@ -64,9 +64,8 @@ mod coerce; | |||
64 | /// The entry point of type inference. | 64 | /// The entry point of type inference. |
65 | pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> { | 65 | pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> { |
66 | let _p = profile("infer_query"); | 66 | let _p = profile("infer_query"); |
67 | let body = def.body(db); | ||
68 | let resolver = def.resolver(db); | 67 | let resolver = def.resolver(db); |
69 | let mut ctx = InferenceContext::new(db, body, resolver); | 68 | let mut ctx = InferenceContext::new(db, def, resolver); |
70 | 69 | ||
71 | match def { | 70 | match def { |
72 | DefWithBody::Const(ref c) => ctx.collect_const(&c.data(db)), | 71 | DefWithBody::Const(ref c) => ctx.collect_const(&c.data(db)), |
@@ -187,6 +186,7 @@ impl Index<PatId> for InferenceResult { | |||
187 | #[derive(Clone, Debug)] | 186 | #[derive(Clone, Debug)] |
188 | struct InferenceContext<'a, D: HirDatabase> { | 187 | struct InferenceContext<'a, D: HirDatabase> { |
189 | db: &'a D, | 188 | db: &'a D, |
189 | owner: DefWithBody, | ||
190 | body: Arc<Body>, | 190 | body: Arc<Body>, |
191 | resolver: Resolver, | 191 | resolver: Resolver, |
192 | var_unification_table: InPlaceUnificationTable<TypeVarId>, | 192 | var_unification_table: InPlaceUnificationTable<TypeVarId>, |
@@ -204,7 +204,7 @@ struct InferenceContext<'a, D: HirDatabase> { | |||
204 | } | 204 | } |
205 | 205 | ||
206 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { | 206 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { |
207 | fn new(db: &'a D, body: Arc<Body>, resolver: Resolver) -> Self { | 207 | fn new(db: &'a D, owner: DefWithBody, resolver: Resolver) -> Self { |
208 | InferenceContext { | 208 | InferenceContext { |
209 | result: InferenceResult::default(), | 209 | result: InferenceResult::default(), |
210 | var_unification_table: InPlaceUnificationTable::new(), | 210 | var_unification_table: InPlaceUnificationTable::new(), |
@@ -213,7 +213,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
213 | trait_env: lower::trait_env(db, &resolver), | 213 | trait_env: lower::trait_env(db, &resolver), |
214 | coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver), | 214 | coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver), |
215 | db, | 215 | db, |
216 | body, | 216 | owner, |
217 | body: db.body(owner), | ||
217 | resolver, | 218 | resolver, |
218 | } | 219 | } |
219 | } | 220 | } |
diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index 6d9792391..c6802487a 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs | |||
@@ -130,10 +130,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
130 | TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 }, | 130 | TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 }, |
131 | Substs(sig_tys.into()), | 131 | Substs(sig_tys.into()), |
132 | ); | 132 | ); |
133 | let closure_ty = Ty::apply_one( | 133 | let closure_ty = |
134 | TypeCtor::Closure { def: self.body.owner(), expr: tgt_expr }, | 134 | Ty::apply_one(TypeCtor::Closure { def: self.owner, expr: tgt_expr }, sig_ty); |
135 | sig_ty, | ||
136 | ); | ||
137 | 135 | ||
138 | // Eagerly try to relate the closure type with the expected | 136 | // Eagerly try to relate the closure type with the expected |
139 | // type, otherwise we often won't have enough information to | 137 | // type, otherwise we often won't have enough information to |
@@ -184,7 +182,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
184 | } | 182 | } |
185 | Expr::Path(p) => { | 183 | Expr::Path(p) => { |
186 | // FIXME this could be more efficient... | 184 | // FIXME this could be more efficient... |
187 | let resolver = expr::resolver_for_expr(self.body.clone(), self.db, tgt_expr); | 185 | let resolver = expr::resolver_for_expr(self.db, self.owner, tgt_expr); |
188 | self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) | 186 | self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) |
189 | } | 187 | } |
190 | Expr::Continue => Ty::simple(TypeCtor::Never), | 188 | Expr::Continue => Ty::simple(TypeCtor::Never), |