From d157812cd16f58128646c05bf1b80522d43acfd8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 20:39:48 +0300 Subject: Docs --- crates/ra_hir_def/src/body.rs | 3 ++- crates/ra_hir_def/src/body/lower.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 45a36d793..d77ccb272 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -1,4 +1,5 @@ -//! FIXME: write short doc here +//! Defines `Body`: a lowered representation of bodies of functions, statics and +//! consts. mod lower; pub mod scope; diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index f4640dfa4..331736cb2 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -1,4 +1,5 @@ -//! FIXME: write short doc here +//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr` +//! representation. use hir_expand::{ either::Either, -- cgit v1.2.3 From e48430cbae32249e50dda1b39e0c98a31f118250 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 20:53:42 +0300 Subject: Simplify --- crates/ra_cli/src/analysis_stats.rs | 11 +++---- crates/ra_hir/src/code_model.rs | 57 ++++-------------------------------- crates/ra_hir/src/code_model/src.rs | 29 ++---------------- crates/ra_hir/src/from_source.rs | 8 ++--- crates/ra_hir/src/lib.rs | 9 +++--- crates/ra_hir/src/source_binder.rs | 9 +++--- crates/ra_hir/src/ty/infer.rs | 5 ++-- crates/ra_hir/src/ty/traits/chalk.rs | 6 ++-- 8 files changed, 30 insertions(+), 104 deletions(-) (limited to 'crates') diff --git a/crates/ra_cli/src/analysis_stats.rs b/crates/ra_cli/src/analysis_stats.rs index 135de7ffb..c4eb28245 100644 --- a/crates/ra_cli/src/analysis_stats.rs +++ b/crates/ra_cli/src/analysis_stats.rs @@ -3,7 +3,7 @@ use std::{collections::HashSet, fmt::Write, path::Path, time::Instant}; use ra_db::SourceDatabaseExt; -use ra_hir::{AssocItem, Crate, HasBodySource, HasSource, HirDisplay, ModuleDef, Ty, TypeWalk}; +use ra_hir::{AssocItem, Crate, HasSource, HirDisplay, ModuleDef, Ty, TypeWalk}; use ra_syntax::AstNode; use crate::{Result, Verbosity}; @@ -128,15 +128,16 @@ pub fn run( if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) { num_type_mismatches += 1; if verbosity.is_verbose() { - let src = f.expr_source(db, expr_id); + let src = f.body_source_map(db).expr_syntax(expr_id); if let Some(src) = src { // FIXME: it might be nice to have a function (on Analysis?) that goes from Source -> (LineCol, LineCol) directly let original_file = src.file_id.original_file(db); let path = db.file_relative_path(original_file); let line_index = host.analysis().file_line_index(original_file).unwrap(); - let text_range = src - .value - .either(|it| it.syntax().text_range(), |it| it.syntax().text_range()); + let text_range = src.value.either( + |it| it.syntax_node_ptr().range(), + |it| it.syntax_node_ptr().range(), + ); let (start, end) = ( line_index.line_col(text_range.start()), line_index.line_col(text_range.end()), diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index a98f2f247..263c557f3 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -6,7 +6,6 @@ use std::sync::Arc; use hir_def::{ adt::VariantData, - body::scope::ExprScopes, builtin_type::BuiltinType, docs::Documentation, per_ns::PerNs, @@ -28,7 +27,7 @@ use crate::{ db::{DefDatabase, HirDatabase}, expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId}, ty::{InferenceResult, Namespace, TraitRef}, - Either, HasSource, Name, Source, Ty, + Either, Name, Source, Ty, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -560,52 +559,6 @@ impl DefWithBody { } } -pub trait HasBody: Copy { - fn infer(self, db: &impl HirDatabase) -> Arc; - fn body(self, db: &impl HirDatabase) -> Arc; - fn body_source_map(self, db: &impl HirDatabase) -> Arc; - fn expr_scopes(self, db: &impl HirDatabase) -> Arc; -} - -impl HasBody for T -where - T: Into + Copy + HasSource, -{ - fn infer(self, db: &impl HirDatabase) -> Arc { - db.infer(self.into()) - } - - fn body(self, db: &impl HirDatabase) -> Arc { - self.into().body(db) - } - - fn body_source_map(self, db: &impl HirDatabase) -> Arc { - self.into().body_source_map(db) - } - - fn expr_scopes(self, db: &impl HirDatabase) -> Arc { - self.into().expr_scopes(db) - } -} - -impl HasBody for DefWithBody { - fn infer(self, db: &impl HirDatabase) -> Arc { - db.infer(self) - } - - fn body(self, db: &impl HirDatabase) -> Arc { - db.body(self.into()) - } - - fn body_source_map(self, db: &impl HirDatabase) -> Arc { - db.body_with_source_map(self.into()).1 - } - - fn expr_scopes(self, db: &impl HirDatabase) -> Arc { - db.expr_scopes(self.into()) - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Function { pub(crate) id: FunctionId, @@ -632,7 +585,7 @@ impl Function { db.function_data(self.id).params.clone() } - pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc { + pub fn body_source_map(self, db: &impl HirDatabase) -> Arc { db.body_with_source_map(self.id.into()).1 } @@ -966,7 +919,7 @@ pub struct Local { impl Local { pub fn name(self, db: &impl HirDatabase) -> Option { - let body = self.parent.body(db); + let body = db.body(self.parent.into()); match &body[self.pat_id] { Pat::Bind { name, .. } => Some(name.clone()), _ => None, @@ -978,7 +931,7 @@ impl Local { } pub fn is_mut(self, db: &impl HirDatabase) -> bool { - let body = self.parent.body(db); + let body = db.body(self.parent.into()); match &body[self.pat_id] { Pat::Bind { mode, .. } => match mode { BindingAnnotation::Mutable | BindingAnnotation::RefMut => true, @@ -1002,7 +955,7 @@ impl Local { } pub fn source(self, db: &impl HirDatabase) -> Source> { - let source_map = self.parent.body_source_map(db); + let (_body, source_map) = db.body_with_source_map(self.parent.into()); let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm... let root = src.file_syntax(db); src.map(|ast| ast.map(|it| it.cast().unwrap().to_node(&root), |it| it.to_node(&root))) diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index b9d21bdd7..a0e605603 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -2,11 +2,10 @@ use hir_def::{AstItemDef, HasChildSource, HasSource as _, Lookup, VariantId}; use hir_expand::either::Either; -use ra_syntax::ast::{self, AstNode}; +use ra_syntax::ast; use crate::{ - db::{DefDatabase, HirDatabase}, - Const, Enum, EnumVariant, FieldSource, Function, HasBody, Import, MacroDef, Module, + db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, Import, MacroDef, Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }; @@ -121,27 +120,3 @@ impl HasSource for Import { src.with_value(ptr.map(|it| it.to_node(&root), |it| it.to_node(&root))) } } - -pub trait HasBodySource: HasBody + HasSource -where - Self::Ast: AstNode, -{ - fn expr_source( - self, - db: &impl HirDatabase, - expr_id: crate::expr::ExprId, - ) -> Option>> { - let source_map = self.body_source_map(db); - let source_ptr = source_map.expr_syntax(expr_id)?; - let root = source_ptr.file_syntax(db); - let source = source_ptr.map(|ast| ast.map(|it| it.to_node(&root), |it| it.to_node(&root))); - Some(source) - } -} - -impl HasBodySource for T -where - T: HasBody + HasSource, - T::Ast: AstNode, -{ -} diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index f506bba70..72d059a27 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -9,9 +9,9 @@ use ra_syntax::{ use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, - AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource, - ImplBlock, Local, MacroDef, Module, ModuleDef, ModuleSource, Source, Static, Struct, - StructField, Trait, TypeAlias, Union, VariantDef, + AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, + Local, MacroDef, Module, ModuleDef, ModuleSource, Source, Static, Struct, StructField, Trait, + TypeAlias, Union, VariantDef, }; pub trait FromSource: Sized { @@ -221,7 +221,7 @@ impl Local { }; Some(res) })?; - let source_map = parent.body_source_map(db); + let (_body, source_map) = db.body_with_source_map(parent.into()); let src = src.map(ast::Pat::from); let pat_id = source_map.node_pat(src.as_ref())?; Some(Local { parent, pat_id }) diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 8c589c728..70bba2efb 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -49,11 +49,10 @@ mod marks; pub use crate::{ code_model::{ - src::{HasBodySource, HasSource}, - Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum, - EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasAttrs, HasBody, ImplBlock, - Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, - StructField, Trait, TypeAlias, Union, VariantDef, + src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, + DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam, + HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, + Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef, }, expr::ExprScopes, from_source::FromSource, diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 9e5ce5508..31390bb7f 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -28,8 +28,7 @@ use crate::{ expr::{BodySourceMap, ExprScopes, ScopeId}, ty::method_resolution::{self, implements_trait}, Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function, - GenericParam, HasBody, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Ty, - TypeAlias, + GenericParam, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Ty, TypeAlias, }; fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option { @@ -155,8 +154,8 @@ impl SourceAnalyzer { ) -> SourceAnalyzer { let def_with_body = def_with_body_from_child_node(db, node); if let Some(def) = def_with_body { - let source_map = def.body_source_map(db); - let scopes = def.expr_scopes(db); + let (_body, source_map) = db.body_with_source_map(def.into()); + let scopes = db.expr_scopes(def.into()); let scope = match offset { None => scope_for(&scopes, &source_map, node), Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)), @@ -166,7 +165,7 @@ impl SourceAnalyzer { resolver, body_owner: Some(def), body_source_map: Some(source_map), - infer: Some(def.infer(db)), + infer: Some(db.infer(def)), scopes: Some(scopes), file_id: node.file_id, } diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 0a9a83800..ddc7d262a 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -44,8 +44,7 @@ use crate::{ db::HirDatabase, expr::{BindingAnnotation, Body, ExprId, PatId}, ty::infer::diagnostics::InferenceDiagnostic, - Adt, AssocItem, DefWithBody, FloatTy, Function, HasBody, IntTy, Path, StructField, Trait, - VariantDef, + Adt, AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, Trait, VariantDef, }; macro_rules! ty_app { @@ -221,7 +220,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver), db, owner, - body: owner.body(db), + body: db.body(owner.into()), resolver, } } diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 9efdc53c4..1a93e5e50 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -19,7 +19,7 @@ use crate::{ db::HirDatabase, ty::display::HirDisplay, ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk}, - Crate, GenericDef, HasBody, ImplBlock, Trait, TypeAlias, + Crate, GenericDef, ImplBlock, Trait, TypeAlias, }; /// This represents a trait whose name we could not resolve. @@ -715,7 +715,7 @@ fn closure_fn_trait_impl_datum( let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?; fn_once_trait.associated_type_by_name(db, &name::OUTPUT_TYPE)?; - let num_args: u16 = match &data.def.body(db)[data.expr] { + let num_args: u16 = match &db.body(data.def.into())[data.expr] { crate::expr::Expr::Lambda { args, .. } => args.len() as u16, _ => { log::warn!("closure for closure type {:?} not found", data); @@ -805,7 +805,7 @@ fn closure_fn_trait_output_assoc_ty_value( ) -> Arc> { let impl_id = Impl::ClosureFnTraitImpl(data.clone()).to_chalk(db); - let num_args: u16 = match &data.def.body(db)[data.expr] { + let num_args: u16 = match &db.body(data.def.into())[data.expr] { crate::expr::Expr::Lambda { args, .. } => args.len() as u16, _ => { log::warn!("closure for closure type {:?} not found", data); -- cgit v1.2.3