aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_cli/src/analysis_stats.rs11
-rw-r--r--crates/ra_hir/src/code_model.rs57
-rw-r--r--crates/ra_hir/src/code_model/src.rs29
-rw-r--r--crates/ra_hir/src/from_source.rs8
-rw-r--r--crates/ra_hir/src/lib.rs9
-rw-r--r--crates/ra_hir/src/source_binder.rs9
-rw-r--r--crates/ra_hir/src/ty/infer.rs5
-rw-r--r--crates/ra_hir/src/ty/traits/chalk.rs6
-rw-r--r--crates/ra_hir_def/src/body.rs3
-rw-r--r--crates/ra_hir_def/src/body/lower.rs3
10 files changed, 34 insertions, 106 deletions
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 @@
3use std::{collections::HashSet, fmt::Write, path::Path, time::Instant}; 3use std::{collections::HashSet, fmt::Write, path::Path, time::Instant};
4 4
5use ra_db::SourceDatabaseExt; 5use ra_db::SourceDatabaseExt;
6use ra_hir::{AssocItem, Crate, HasBodySource, HasSource, HirDisplay, ModuleDef, Ty, TypeWalk}; 6use ra_hir::{AssocItem, Crate, HasSource, HirDisplay, ModuleDef, Ty, TypeWalk};
7use ra_syntax::AstNode; 7use ra_syntax::AstNode;
8 8
9use crate::{Result, Verbosity}; 9use crate::{Result, Verbosity};
@@ -128,15 +128,16 @@ pub fn run(
128 if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) { 128 if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) {
129 num_type_mismatches += 1; 129 num_type_mismatches += 1;
130 if verbosity.is_verbose() { 130 if verbosity.is_verbose() {
131 let src = f.expr_source(db, expr_id); 131 let src = f.body_source_map(db).expr_syntax(expr_id);
132 if let Some(src) = src { 132 if let Some(src) = src {
133 // FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly 133 // FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly
134 let original_file = src.file_id.original_file(db); 134 let original_file = src.file_id.original_file(db);
135 let path = db.file_relative_path(original_file); 135 let path = db.file_relative_path(original_file);
136 let line_index = host.analysis().file_line_index(original_file).unwrap(); 136 let line_index = host.analysis().file_line_index(original_file).unwrap();
137 let text_range = src 137 let text_range = src.value.either(
138 .value 138 |it| it.syntax_node_ptr().range(),
139 .either(|it| it.syntax().text_range(), |it| it.syntax().text_range()); 139 |it| it.syntax_node_ptr().range(),
140 );
140 let (start, end) = ( 141 let (start, end) = (
141 line_index.line_col(text_range.start()), 142 line_index.line_col(text_range.start()),
142 line_index.line_col(text_range.end()), 143 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;
6 6
7use hir_def::{ 7use hir_def::{
8 adt::VariantData, 8 adt::VariantData,
9 body::scope::ExprScopes,
10 builtin_type::BuiltinType, 9 builtin_type::BuiltinType,
11 docs::Documentation, 10 docs::Documentation,
12 per_ns::PerNs, 11 per_ns::PerNs,
@@ -28,7 +27,7 @@ use crate::{
28 db::{DefDatabase, HirDatabase}, 27 db::{DefDatabase, HirDatabase},
29 expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId}, 28 expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId},
30 ty::{InferenceResult, Namespace, TraitRef}, 29 ty::{InferenceResult, Namespace, TraitRef},
31 Either, HasSource, Name, Source, Ty, 30 Either, Name, Source, Ty,
32}; 31};
33 32
34/// hir::Crate describes a single crate. It's the main interface with which 33/// hir::Crate describes a single crate. It's the main interface with which
@@ -560,52 +559,6 @@ impl DefWithBody {
560 } 559 }
561} 560}
562 561
563pub trait HasBody: Copy {
564 fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult>;
565 fn body(self, db: &impl HirDatabase) -> Arc<Body>;
566 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap>;
567 fn expr_scopes(self, db: &impl HirDatabase) -> Arc<ExprScopes>;
568}
569
570impl<T> HasBody for T
571where
572 T: Into<DefWithBody> + Copy + HasSource,
573{
574 fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
575 db.infer(self.into())
576 }
577
578 fn body(self, db: &impl HirDatabase) -> Arc<Body> {
579 self.into().body(db)
580 }
581
582 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
583 self.into().body_source_map(db)
584 }
585
586 fn expr_scopes(self, db: &impl HirDatabase) -> Arc<ExprScopes> {
587 self.into().expr_scopes(db)
588 }
589}
590
591impl HasBody for DefWithBody {
592 fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
593 db.infer(self)
594 }
595
596 fn body(self, db: &impl HirDatabase) -> Arc<Body> {
597 db.body(self.into())
598 }
599
600 fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
601 db.body_with_source_map(self.into()).1
602 }
603
604 fn expr_scopes(self, db: &impl HirDatabase) -> Arc<ExprScopes> {
605 db.expr_scopes(self.into())
606 }
607}
608
609#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 562#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
610pub struct Function { 563pub struct Function {
611 pub(crate) id: FunctionId, 564 pub(crate) id: FunctionId,
@@ -632,7 +585,7 @@ impl Function {
632 db.function_data(self.id).params.clone() 585 db.function_data(self.id).params.clone()
633 } 586 }
634 587
635 pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> { 588 pub fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
636 db.body_with_source_map(self.id.into()).1 589 db.body_with_source_map(self.id.into()).1
637 } 590 }
638 591
@@ -966,7 +919,7 @@ pub struct Local {
966 919
967impl Local { 920impl Local {
968 pub fn name(self, db: &impl HirDatabase) -> Option<Name> { 921 pub fn name(self, db: &impl HirDatabase) -> Option<Name> {
969 let body = self.parent.body(db); 922 let body = db.body(self.parent.into());
970 match &body[self.pat_id] { 923 match &body[self.pat_id] {
971 Pat::Bind { name, .. } => Some(name.clone()), 924 Pat::Bind { name, .. } => Some(name.clone()),
972 _ => None, 925 _ => None,
@@ -978,7 +931,7 @@ impl Local {
978 } 931 }
979 932
980 pub fn is_mut(self, db: &impl HirDatabase) -> bool { 933 pub fn is_mut(self, db: &impl HirDatabase) -> bool {
981 let body = self.parent.body(db); 934 let body = db.body(self.parent.into());
982 match &body[self.pat_id] { 935 match &body[self.pat_id] {
983 Pat::Bind { mode, .. } => match mode { 936 Pat::Bind { mode, .. } => match mode {
984 BindingAnnotation::Mutable | BindingAnnotation::RefMut => true, 937 BindingAnnotation::Mutable | BindingAnnotation::RefMut => true,
@@ -1002,7 +955,7 @@ impl Local {
1002 } 955 }
1003 956
1004 pub fn source(self, db: &impl HirDatabase) -> Source<Either<ast::BindPat, ast::SelfParam>> { 957 pub fn source(self, db: &impl HirDatabase) -> Source<Either<ast::BindPat, ast::SelfParam>> {
1005 let source_map = self.parent.body_source_map(db); 958 let (_body, source_map) = db.body_with_source_map(self.parent.into());
1006 let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm... 959 let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm...
1007 let root = src.file_syntax(db); 960 let root = src.file_syntax(db);
1008 src.map(|ast| ast.map(|it| it.cast().unwrap().to_node(&root), |it| it.to_node(&root))) 961 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 @@
2 2
3use hir_def::{AstItemDef, HasChildSource, HasSource as _, Lookup, VariantId}; 3use hir_def::{AstItemDef, HasChildSource, HasSource as _, Lookup, VariantId};
4use hir_expand::either::Either; 4use hir_expand::either::Either;
5use ra_syntax::ast::{self, AstNode}; 5use ra_syntax::ast;
6 6
7use crate::{ 7use crate::{
8 db::{DefDatabase, HirDatabase}, 8 db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, Import, MacroDef, Module,
9 Const, Enum, EnumVariant, FieldSource, Function, HasBody, Import, MacroDef, Module,
10 ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, 9 ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
11}; 10};
12 11
@@ -121,27 +120,3 @@ impl HasSource for Import {
121 src.with_value(ptr.map(|it| it.to_node(&root), |it| it.to_node(&root))) 120 src.with_value(ptr.map(|it| it.to_node(&root), |it| it.to_node(&root)))
122 } 121 }
123} 122}
124
125pub trait HasBodySource: HasBody + HasSource
126where
127 Self::Ast: AstNode,
128{
129 fn expr_source(
130 self,
131 db: &impl HirDatabase,
132 expr_id: crate::expr::ExprId,
133 ) -> Option<Source<Either<ast::Expr, ast::RecordField>>> {
134 let source_map = self.body_source_map(db);
135 let source_ptr = source_map.expr_syntax(expr_id)?;
136 let root = source_ptr.file_syntax(db);
137 let source = source_ptr.map(|ast| ast.map(|it| it.to_node(&root), |it| it.to_node(&root)));
138 Some(source)
139 }
140}
141
142impl<T> HasBodySource for T
143where
144 T: HasBody + HasSource,
145 T::Ast: AstNode,
146{
147}
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::{
9 9
10use crate::{ 10use crate::{
11 db::{AstDatabase, DefDatabase, HirDatabase}, 11 db::{AstDatabase, DefDatabase, HirDatabase},
12 AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource, 12 AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock,
13 ImplBlock, Local, MacroDef, Module, ModuleDef, ModuleSource, Source, Static, Struct, 13 Local, MacroDef, Module, ModuleDef, ModuleSource, Source, Static, Struct, StructField, Trait,
14 StructField, Trait, TypeAlias, Union, VariantDef, 14 TypeAlias, Union, VariantDef,
15}; 15};
16 16
17pub trait FromSource: Sized { 17pub trait FromSource: Sized {
@@ -221,7 +221,7 @@ impl Local {
221 }; 221 };
222 Some(res) 222 Some(res)
223 })?; 223 })?;
224 let source_map = parent.body_source_map(db); 224 let (_body, source_map) = db.body_with_source_map(parent.into());
225 let src = src.map(ast::Pat::from); 225 let src = src.map(ast::Pat::from);
226 let pat_id = source_map.node_pat(src.as_ref())?; 226 let pat_id = source_map.node_pat(src.as_ref())?;
227 Some(Local { parent, pat_id }) 227 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;
49 49
50pub use crate::{ 50pub use crate::{
51 code_model::{ 51 code_model::{
52 src::{HasBodySource, HasSource}, 52 src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency,
53 Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum, 53 DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam,
54 EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasAttrs, HasBody, ImplBlock, 54 HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef,
55 Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, 55 Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef,
56 StructField, Trait, TypeAlias, Union, VariantDef,
57 }, 56 },
58 expr::ExprScopes, 57 expr::ExprScopes,
59 from_source::FromSource, 58 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::{
28 expr::{BodySourceMap, ExprScopes, ScopeId}, 28 expr::{BodySourceMap, ExprScopes, ScopeId},
29 ty::method_resolution::{self, implements_trait}, 29 ty::method_resolution::{self, implements_trait},
30 Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function, 30 Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
31 GenericParam, HasBody, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Ty, 31 GenericParam, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Ty, TypeAlias,
32 TypeAlias,
33}; 32};
34 33
35fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> { 34fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> {
@@ -155,8 +154,8 @@ impl SourceAnalyzer {
155 ) -> SourceAnalyzer { 154 ) -> SourceAnalyzer {
156 let def_with_body = def_with_body_from_child_node(db, node); 155 let def_with_body = def_with_body_from_child_node(db, node);
157 if let Some(def) = def_with_body { 156 if let Some(def) = def_with_body {
158 let source_map = def.body_source_map(db); 157 let (_body, source_map) = db.body_with_source_map(def.into());
159 let scopes = def.expr_scopes(db); 158 let scopes = db.expr_scopes(def.into());
160 let scope = match offset { 159 let scope = match offset {
161 None => scope_for(&scopes, &source_map, node), 160 None => scope_for(&scopes, &source_map, node),
162 Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)), 161 Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)),
@@ -166,7 +165,7 @@ impl SourceAnalyzer {
166 resolver, 165 resolver,
167 body_owner: Some(def), 166 body_owner: Some(def),
168 body_source_map: Some(source_map), 167 body_source_map: Some(source_map),
169 infer: Some(def.infer(db)), 168 infer: Some(db.infer(def)),
170 scopes: Some(scopes), 169 scopes: Some(scopes),
171 file_id: node.file_id, 170 file_id: node.file_id,
172 } 171 }
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::{
44 db::HirDatabase, 44 db::HirDatabase,
45 expr::{BindingAnnotation, Body, ExprId, PatId}, 45 expr::{BindingAnnotation, Body, ExprId, PatId},
46 ty::infer::diagnostics::InferenceDiagnostic, 46 ty::infer::diagnostics::InferenceDiagnostic,
47 Adt, AssocItem, DefWithBody, FloatTy, Function, HasBody, IntTy, Path, StructField, Trait, 47 Adt, AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, Trait, VariantDef,
48 VariantDef,
49}; 48};
50 49
51macro_rules! ty_app { 50macro_rules! ty_app {
@@ -221,7 +220,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
221 coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver), 220 coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver),
222 db, 221 db,
223 owner, 222 owner,
224 body: owner.body(db), 223 body: db.body(owner.into()),
225 resolver, 224 resolver,
226 } 225 }
227 } 226 }
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::{
19 db::HirDatabase, 19 db::HirDatabase,
20 ty::display::HirDisplay, 20 ty::display::HirDisplay,
21 ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk}, 21 ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk},
22 Crate, GenericDef, HasBody, ImplBlock, Trait, TypeAlias, 22 Crate, GenericDef, ImplBlock, Trait, TypeAlias,
23}; 23};
24 24
25/// This represents a trait whose name we could not resolve. 25/// This represents a trait whose name we could not resolve.
@@ -715,7 +715,7 @@ fn closure_fn_trait_impl_datum(
715 let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?; 715 let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?;
716 fn_once_trait.associated_type_by_name(db, &name::OUTPUT_TYPE)?; 716 fn_once_trait.associated_type_by_name(db, &name::OUTPUT_TYPE)?;
717 717
718 let num_args: u16 = match &data.def.body(db)[data.expr] { 718 let num_args: u16 = match &db.body(data.def.into())[data.expr] {
719 crate::expr::Expr::Lambda { args, .. } => args.len() as u16, 719 crate::expr::Expr::Lambda { args, .. } => args.len() as u16,
720 _ => { 720 _ => {
721 log::warn!("closure for closure type {:?} not found", data); 721 log::warn!("closure for closure type {:?} not found", data);
@@ -805,7 +805,7 @@ fn closure_fn_trait_output_assoc_ty_value(
805) -> Arc<AssociatedTyValue<ChalkIr>> { 805) -> Arc<AssociatedTyValue<ChalkIr>> {
806 let impl_id = Impl::ClosureFnTraitImpl(data.clone()).to_chalk(db); 806 let impl_id = Impl::ClosureFnTraitImpl(data.clone()).to_chalk(db);
807 807
808 let num_args: u16 = match &data.def.body(db)[data.expr] { 808 let num_args: u16 = match &db.body(data.def.into())[data.expr] {
809 crate::expr::Expr::Lambda { args, .. } => args.len() as u16, 809 crate::expr::Expr::Lambda { args, .. } => args.len() as u16,
810 _ => { 810 _ => {
811 log::warn!("closure for closure type {:?} not found", data); 811 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 45a36d793..d77ccb272 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -1,4 +1,5 @@
1//! FIXME: write short doc here 1//! Defines `Body`: a lowered representation of bodies of functions, statics and
2//! consts.
2mod lower; 3mod lower;
3pub mod scope; 4pub mod scope;
4 5
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 @@
1//! FIXME: write short doc here 1//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
2//! representation.
2 3
3use hir_expand::{ 4use hir_expand::{
4 either::Either, 5 either::Either,