From fe00db72b91d266a61b0541bca59e38e5f2a703c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 12 Nov 2019 16:46:27 +0300 Subject: Remove owner from Body --- crates/ra_hir/src/expr.rs | 20 +++++++------------- crates/ra_hir/src/expr/lower.rs | 6 ++---- crates/ra_hir/src/source_binder.rs | 2 +- crates/ra_hir/src/ty/infer.rs | 11 ++++++----- crates/ra_hir/src/ty/infer/expr.rs | 8 +++----- 5 files changed, 19 insertions(+), 28 deletions(-) (limited to 'crates') 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::{ /// The body of an item (function, const etc.). #[derive(Debug, Eq, PartialEq)] pub struct Body { - /// The def of the item this body belongs to - owner: DefWithBody, exprs: Arena, pats: Arena, /// The patterns for the function's parameters. While the parameter types are @@ -86,7 +84,7 @@ impl Body { } }; - let (body, source_map) = lower::lower(db, def.resolver(db), file_id, def, params, body); + let (body, source_map) = lower::lower(db, def.resolver(db), file_id, params, body); (Arc::new(body), Arc::new(source_map)) } @@ -102,10 +100,6 @@ impl Body { self.body_expr } - pub fn owner(&self) -> DefWithBody { - self.owner - } - pub fn exprs(&self) -> impl Iterator { self.exprs.iter() } @@ -117,21 +111,21 @@ impl Body { // needs arbitrary_self_types to be a method... or maybe move to the def? pub(crate) fn resolver_for_expr( - body: Arc, db: &impl HirDatabase, + owner: DefWithBody, expr_id: ExprId, ) -> Resolver { - let scopes = db.expr_scopes(body.owner); - resolver_for_scope(body, db, scopes.scope_for(expr_id)) + let scopes = db.expr_scopes(owner); + resolver_for_scope(db, owner, scopes.scope_for(expr_id)) } pub(crate) fn resolver_for_scope( - body: Arc, db: &impl HirDatabase, + owner: DefWithBody, scope_id: Option, ) -> Resolver { - let mut r = body.owner.resolver(db); - let scopes = db.expr_scopes(body.owner); + let mut r = owner.resolver(db); + let scopes = db.expr_scopes(owner); let scope_chain = scopes.scope_chain(scope_id).collect::>(); for scope in scope_chain.into_iter().rev() { 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::{ use test_utils::tested_by; use crate::{ - db::HirDatabase, AstId, DefWithBody, Either, HirFileId, MacroCallLoc, MacroFileKind, - Mutability, Path, Resolver, Source, + db::HirDatabase, AstId, Either, HirFileId, MacroCallLoc, MacroFileKind, Mutability, Path, + Resolver, Source, }; use super::{ @@ -33,7 +33,6 @@ pub(super) fn lower( db: &impl HirDatabase, resolver: Resolver, file_id: HirFileId, - owner: DefWithBody, params: Option, body: Option, ) -> (Body, BodySourceMap) { @@ -44,7 +43,6 @@ pub(super) fn lower( current_file_id: file_id, source_map: BodySourceMap::default(), body: Body { - owner, exprs: Arena::default(), pats: Arena::default(), 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 { None => scope_for(&scopes, &source_map, &node), Some(offset) => scope_for_offset(&scopes, &source_map, file_id.into(), offset), }; - let resolver = expr::resolver_for_scope(def.body(db), db, scope); + let resolver = expr::resolver_for_scope(db, def, scope); SourceAnalyzer { resolver, 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::{ expr::{BindingAnnotation, Body, ExprId, PatId}, resolve::{Resolver, TypeNs}, ty::infer::diagnostics::InferenceDiagnostic, - Adt, AssocItem, ConstData, DefWithBody, FnData, Function, HasBody, Path, StructField, + Adt, AssocItem, ConstData, DefWithBody, FnData, Function, Path, StructField, }; macro_rules! ty_app { @@ -64,9 +64,8 @@ mod coerce; /// The entry point of type inference. pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc { let _p = profile("infer_query"); - let body = def.body(db); let resolver = def.resolver(db); - let mut ctx = InferenceContext::new(db, body, resolver); + let mut ctx = InferenceContext::new(db, def, resolver); match def { DefWithBody::Const(ref c) => ctx.collect_const(&c.data(db)), @@ -187,6 +186,7 @@ impl Index for InferenceResult { #[derive(Clone, Debug)] struct InferenceContext<'a, D: HirDatabase> { db: &'a D, + owner: DefWithBody, body: Arc, resolver: Resolver, var_unification_table: InPlaceUnificationTable, @@ -204,7 +204,7 @@ struct InferenceContext<'a, D: HirDatabase> { } impl<'a, D: HirDatabase> InferenceContext<'a, D> { - fn new(db: &'a D, body: Arc, resolver: Resolver) -> Self { + fn new(db: &'a D, owner: DefWithBody, resolver: Resolver) -> Self { InferenceContext { result: InferenceResult::default(), var_unification_table: InPlaceUnificationTable::new(), @@ -213,7 +213,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { trait_env: lower::trait_env(db, &resolver), coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver), db, - body, + owner, + body: db.body(owner), resolver, } } 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> { TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 }, Substs(sig_tys.into()), ); - let closure_ty = Ty::apply_one( - TypeCtor::Closure { def: self.body.owner(), expr: tgt_expr }, - sig_ty, - ); + let closure_ty = + Ty::apply_one(TypeCtor::Closure { def: self.owner, expr: tgt_expr }, sig_ty); // Eagerly try to relate the closure type with the expected // type, otherwise we often won't have enough information to @@ -184,7 +182,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } Expr::Path(p) => { // FIXME this could be more efficient... - let resolver = expr::resolver_for_expr(self.body.clone(), self.db, tgt_expr); + let resolver = expr::resolver_for_expr(self.db, self.owner, tgt_expr); self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) } Expr::Continue => Ty::simple(TypeCtor::Never), -- cgit v1.2.3