diff options
Diffstat (limited to 'crates/ra_hir_def/src')
-rw-r--r-- | crates/ra_hir_def/src/adt.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir_def/src/attr.rs | 9 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body.rs | 17 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body/lower.rs | 11 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body/scope.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir_def/src/data.rs | 5 | ||||
-rw-r--r-- | crates/ra_hir_def/src/diagnostics.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir_def/src/docs.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir_def/src/expr.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir_def/src/generics.rs | 3 | ||||
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 60 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres.rs | 16 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/raw.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests/mod_resolution.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_def/src/path.rs | 34 | ||||
-rw-r--r-- | crates/ra_hir_def/src/src.rs | 54 |
16 files changed, 163 insertions, 96 deletions
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index 3666529b0..9ab829aab 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs | |||
@@ -5,13 +5,13 @@ use std::sync::Arc; | |||
5 | use hir_expand::{ | 5 | use hir_expand::{ |
6 | either::Either, | 6 | either::Either, |
7 | name::{AsName, Name}, | 7 | name::{AsName, Name}, |
8 | Source, | 8 | InFile, |
9 | }; | 9 | }; |
10 | use ra_arena::{map::ArenaMap, Arena}; | 10 | use ra_arena::{map::ArenaMap, Arena}; |
11 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; | 11 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | db::DefDatabase, trace::Trace, type_ref::TypeRef, AstItemDef, EnumId, HasChildSource, | 14 | db::DefDatabase, src::HasChildSource, trace::Trace, type_ref::TypeRef, AstItemDef, EnumId, |
15 | LocalEnumVariantId, LocalStructFieldId, StructId, UnionId, VariantId, | 15 | LocalEnumVariantId, LocalStructFieldId, StructId, UnionId, VariantId, |
16 | }; | 16 | }; |
17 | 17 | ||
@@ -88,7 +88,7 @@ impl EnumData { | |||
88 | impl HasChildSource for EnumId { | 88 | impl HasChildSource for EnumId { |
89 | type ChildId = LocalEnumVariantId; | 89 | type ChildId = LocalEnumVariantId; |
90 | type Value = ast::EnumVariant; | 90 | type Value = ast::EnumVariant; |
91 | fn child_source(&self, db: &impl DefDatabase) -> Source<ArenaMap<Self::ChildId, Self::Value>> { | 91 | fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> { |
92 | let src = self.source(db); | 92 | let src = self.source(db); |
93 | let mut trace = Trace::new_for_map(); | 93 | let mut trace = Trace::new_for_map(); |
94 | lower_enum(&mut trace, &src.value); | 94 | lower_enum(&mut trace, &src.value); |
@@ -145,7 +145,7 @@ impl HasChildSource for VariantId { | |||
145 | type ChildId = LocalStructFieldId; | 145 | type ChildId = LocalStructFieldId; |
146 | type Value = Either<ast::TupleFieldDef, ast::RecordFieldDef>; | 146 | type Value = Either<ast::TupleFieldDef, ast::RecordFieldDef>; |
147 | 147 | ||
148 | fn child_source(&self, db: &impl DefDatabase) -> Source<ArenaMap<Self::ChildId, Self::Value>> { | 148 | fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> { |
149 | let src = match self { | 149 | let src = match self { |
150 | VariantId::EnumVariantId(it) => { | 150 | VariantId::EnumVariantId(it) => { |
151 | // I don't really like the fact that we call into parent source | 151 | // I don't really like the fact that we call into parent source |
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index fffb22201..bc7ade921 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | use std::{ops, sync::Arc}; | 3 | use std::{ops, sync::Arc}; |
4 | 4 | ||
5 | use hir_expand::{either::Either, hygiene::Hygiene, AstId, Source}; | 5 | use hir_expand::{either::Either, hygiene::Hygiene, AstId, InFile}; |
6 | use mbe::ast_to_token_tree; | 6 | use mbe::ast_to_token_tree; |
7 | use ra_syntax::{ | 7 | use ra_syntax::{ |
8 | ast::{self, AstNode, AttrsOwner}, | 8 | ast::{self, AstNode, AttrsOwner}, |
@@ -11,7 +11,8 @@ use ra_syntax::{ | |||
11 | use tt::Subtree; | 11 | use tt::Subtree; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | db::DefDatabase, path::Path, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, Lookup, | 14 | db::DefDatabase, path::Path, src::HasChildSource, src::HasSource, AdtId, AstItemDef, AttrDefId, |
15 | Lookup, | ||
15 | }; | 16 | }; |
16 | 17 | ||
17 | #[derive(Default, Debug, Clone, PartialEq, Eq)] | 18 | #[derive(Default, Debug, Clone, PartialEq, Eq)] |
@@ -68,7 +69,7 @@ impl Attrs { | |||
68 | } | 69 | } |
69 | } | 70 | } |
70 | 71 | ||
71 | fn from_attrs_owner(db: &impl DefDatabase, owner: Source<&dyn AttrsOwner>) -> Attrs { | 72 | fn from_attrs_owner(db: &impl DefDatabase, owner: InFile<&dyn AttrsOwner>) -> Attrs { |
72 | let hygiene = Hygiene::new(db, owner.file_id); | 73 | let hygiene = Hygiene::new(db, owner.file_id); |
73 | Attrs::new(owner.value, &hygiene) | 74 | Attrs::new(owner.value, &hygiene) |
74 | } | 75 | } |
@@ -157,7 +158,7 @@ where | |||
157 | N: ast::AttrsOwner, | 158 | N: ast::AttrsOwner, |
158 | D: DefDatabase, | 159 | D: DefDatabase, |
159 | { | 160 | { |
160 | let src = Source::new(src.file_id(), src.to_node(db)); | 161 | let src = InFile::new(src.file_id, src.to_node(db)); |
161 | Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner)) | 162 | Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner)) |
162 | } | 163 | } |
163 | 164 | ||
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index a57a0176d..69508dd8a 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs | |||
@@ -6,7 +6,7 @@ pub mod scope; | |||
6 | use std::{ops::Index, sync::Arc}; | 6 | use std::{ops::Index, sync::Arc}; |
7 | 7 | ||
8 | use hir_expand::{ | 8 | use hir_expand::{ |
9 | either::Either, hygiene::Hygiene, AstId, HirFileId, MacroDefId, MacroFileKind, Source, | 9 | either::Either, hygiene::Hygiene, AstId, HirFileId, InFile, MacroDefId, MacroFileKind, |
10 | }; | 10 | }; |
11 | use ra_arena::{map::ArenaMap, Arena}; | 11 | use ra_arena::{map::ArenaMap, Arena}; |
12 | use ra_syntax::{ast, AstNode, AstPtr}; | 12 | use ra_syntax::{ast, AstNode, AstPtr}; |
@@ -17,7 +17,8 @@ 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 | DefWithBodyId, HasModule, HasSource, Lookup, ModuleId, | 20 | src::HasSource, |
21 | DefWithBodyId, HasModule, Lookup, ModuleId, | ||
21 | }; | 22 | }; |
22 | 23 | ||
23 | struct Expander { | 24 | struct Expander { |
@@ -73,8 +74,8 @@ impl Expander { | |||
73 | std::mem::forget(mark); | 74 | std::mem::forget(mark); |
74 | } | 75 | } |
75 | 76 | ||
76 | fn to_source<T>(&self, value: T) -> Source<T> { | 77 | fn to_source<T>(&self, value: T) -> InFile<T> { |
77 | Source { file_id: self.current_file_id, value } | 78 | InFile { file_id: self.current_file_id, value } |
78 | } | 79 | } |
79 | 80 | ||
80 | fn parse_path(&mut self, path: ast::Path) -> Option<Path> { | 81 | fn parse_path(&mut self, path: ast::Path) -> Option<Path> { |
@@ -115,10 +116,10 @@ pub struct Body { | |||
115 | } | 116 | } |
116 | 117 | ||
117 | pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>; | 118 | pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>; |
118 | pub type ExprSource = Source<ExprPtr>; | 119 | pub type ExprSource = InFile<ExprPtr>; |
119 | 120 | ||
120 | pub type PatPtr = Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>; | 121 | pub type PatPtr = Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>; |
121 | pub type PatSource = Source<PatPtr>; | 122 | pub type PatSource = InFile<PatPtr>; |
122 | 123 | ||
123 | /// An item body together with the mapping from syntax nodes to HIR expression | 124 | /// An item body together with the mapping from syntax nodes to HIR expression |
124 | /// IDs. This is needed to go from e.g. a position in a file to the HIR | 125 | /// IDs. This is needed to go from e.g. a position in a file to the HIR |
@@ -205,7 +206,7 @@ impl BodySourceMap { | |||
205 | self.expr_map_back.get(expr).copied() | 206 | self.expr_map_back.get(expr).copied() |
206 | } | 207 | } |
207 | 208 | ||
208 | pub fn node_expr(&self, node: Source<&ast::Expr>) -> Option<ExprId> { | 209 | pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> { |
209 | let src = node.map(|it| Either::A(AstPtr::new(it))); | 210 | let src = node.map(|it| Either::A(AstPtr::new(it))); |
210 | self.expr_map.get(&src).cloned() | 211 | self.expr_map.get(&src).cloned() |
211 | } | 212 | } |
@@ -214,7 +215,7 @@ impl BodySourceMap { | |||
214 | self.pat_map_back.get(pat).copied() | 215 | self.pat_map_back.get(pat).copied() |
215 | } | 216 | } |
216 | 217 | ||
217 | pub fn node_pat(&self, node: Source<&ast::Pat>) -> Option<PatId> { | 218 | pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> { |
218 | let src = node.map(|it| Either::A(AstPtr::new(it))); | 219 | let src = node.map(|it| Either::A(AstPtr::new(it))); |
219 | self.pat_map.get(&src).cloned() | 220 | self.pat_map.get(&src).cloned() |
220 | } | 221 | } |
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 331736cb2..be1eaa523 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
@@ -429,10 +429,19 @@ where | |||
429 | let index = self.collect_expr_opt(e.index()); | 429 | let index = self.collect_expr_opt(e.index()); |
430 | self.alloc_expr(Expr::Index { base, index }, syntax_ptr) | 430 | self.alloc_expr(Expr::Index { base, index }, syntax_ptr) |
431 | } | 431 | } |
432 | ast::Expr::RangeExpr(e) => { | ||
433 | let lhs = e.start().map(|lhs| self.collect_expr(lhs)); | ||
434 | let rhs = e.end().map(|rhs| self.collect_expr(rhs)); | ||
435 | match e.op_kind() { | ||
436 | Some(range_type) => { | ||
437 | self.alloc_expr(Expr::Range { lhs, rhs, range_type }, syntax_ptr) | ||
438 | } | ||
439 | None => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
440 | } | ||
441 | } | ||
432 | 442 | ||
433 | // FIXME implement HIR for these: | 443 | // FIXME implement HIR for these: |
434 | ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | 444 | ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), |
435 | ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
436 | ast::Expr::MacroCall(e) => match self.expander.enter_expand(self.db, e) { | 445 | ast::Expr::MacroCall(e) => match self.expander.enter_expand(self.db, e) { |
437 | Some((mark, expansion)) => { | 446 | Some((mark, expansion)) => { |
438 | let id = self.collect_expr(expansion); | 447 | let id = self.collect_expr(expansion); |
diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index 625aa39dd..ab6599b23 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs | |||
@@ -171,7 +171,7 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope | |||
171 | 171 | ||
172 | #[cfg(test)] | 172 | #[cfg(test)] |
173 | mod tests { | 173 | mod tests { |
174 | use hir_expand::{name::AsName, Source}; | 174 | use hir_expand::{name::AsName, InFile}; |
175 | use ra_db::{fixture::WithFixture, FileId, SourceDatabase}; | 175 | use ra_db::{fixture::WithFixture, FileId, SourceDatabase}; |
176 | use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; | 176 | use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; |
177 | use test_utils::{assert_eq_text, covers, extract_offset}; | 177 | use test_utils::{assert_eq_text, covers, extract_offset}; |
@@ -211,7 +211,7 @@ mod tests { | |||
211 | let (_body, source_map) = db.body_with_source_map(function.into()); | 211 | let (_body, source_map) = db.body_with_source_map(function.into()); |
212 | 212 | ||
213 | let expr_id = source_map | 213 | let expr_id = source_map |
214 | .node_expr(Source { file_id: file_id.into(), value: &marker.into() }) | 214 | .node_expr(InFile { file_id: file_id.into(), value: &marker.into() }) |
215 | .unwrap(); | 215 | .unwrap(); |
216 | let scope = scopes.scope_for(expr_id); | 216 | let scope = scopes.scope_for(expr_id); |
217 | 217 | ||
@@ -318,7 +318,7 @@ mod tests { | |||
318 | let expr_scope = { | 318 | let expr_scope = { |
319 | let expr_ast = name_ref.syntax().ancestors().find_map(ast::Expr::cast).unwrap(); | 319 | let expr_ast = name_ref.syntax().ancestors().find_map(ast::Expr::cast).unwrap(); |
320 | let expr_id = | 320 | let expr_id = |
321 | source_map.node_expr(Source { file_id: file_id.into(), value: &expr_ast }).unwrap(); | 321 | source_map.node_expr(InFile { file_id: file_id.into(), value: &expr_ast }).unwrap(); |
322 | scopes.scope_for(expr_id).unwrap() | 322 | scopes.scope_for(expr_id).unwrap() |
323 | }; | 323 | }; |
324 | 324 | ||
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index fee10b237..095d7064a 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs | |||
@@ -10,9 +10,10 @@ use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; | |||
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | db::DefDatabase, | 12 | db::DefDatabase, |
13 | src::HasSource, | ||
13 | type_ref::{Mutability, TypeRef}, | 14 | type_ref::{Mutability, TypeRef}, |
14 | AssocItemId, AstItemDef, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, HasSource, | 15 | AssocItemId, AstItemDef, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId, |
15 | ImplId, Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, | 16 | Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, |
16 | }; | 17 | }; |
17 | 18 | ||
18 | #[derive(Debug, Clone, PartialEq, Eq)] | 19 | #[derive(Debug, Clone, PartialEq, Eq)] |
diff --git a/crates/ra_hir_def/src/diagnostics.rs b/crates/ra_hir_def/src/diagnostics.rs index eda9b2269..095498429 100644 --- a/crates/ra_hir_def/src/diagnostics.rs +++ b/crates/ra_hir_def/src/diagnostics.rs | |||
@@ -6,7 +6,7 @@ use hir_expand::diagnostics::Diagnostic; | |||
6 | use ra_db::RelativePathBuf; | 6 | use ra_db::RelativePathBuf; |
7 | use ra_syntax::{ast, AstPtr, SyntaxNodePtr}; | 7 | use ra_syntax::{ast, AstPtr, SyntaxNodePtr}; |
8 | 8 | ||
9 | use hir_expand::{HirFileId, Source}; | 9 | use hir_expand::{HirFileId, InFile}; |
10 | 10 | ||
11 | #[derive(Debug)] | 11 | #[derive(Debug)] |
12 | pub struct UnresolvedModule { | 12 | pub struct UnresolvedModule { |
@@ -19,8 +19,8 @@ impl Diagnostic for UnresolvedModule { | |||
19 | fn message(&self) -> String { | 19 | fn message(&self) -> String { |
20 | "unresolved module".to_string() | 20 | "unresolved module".to_string() |
21 | } | 21 | } |
22 | fn source(&self) -> Source<SyntaxNodePtr> { | 22 | fn source(&self) -> InFile<SyntaxNodePtr> { |
23 | Source { file_id: self.file, value: self.decl.into() } | 23 | InFile { file_id: self.file, value: self.decl.into() } |
24 | } | 24 | } |
25 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | 25 | fn as_any(&self) -> &(dyn Any + Send + 'static) { |
26 | self | 26 | self |
diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs index 34ed9b7a5..ec944373d 100644 --- a/crates/ra_hir_def/src/docs.rs +++ b/crates/ra_hir_def/src/docs.rs | |||
@@ -8,7 +8,11 @@ use std::sync::Arc; | |||
8 | use hir_expand::either::Either; | 8 | use hir_expand::either::Either; |
9 | use ra_syntax::ast; | 9 | use ra_syntax::ast; |
10 | 10 | ||
11 | use crate::{db::DefDatabase, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, Lookup}; | 11 | use crate::{ |
12 | db::DefDatabase, | ||
13 | src::{HasChildSource, HasSource}, | ||
14 | AdtId, AstItemDef, AttrDefId, Lookup, | ||
15 | }; | ||
12 | 16 | ||
13 | /// Holds documentation | 17 | /// Holds documentation |
14 | #[derive(Debug, Clone, PartialEq, Eq)] | 18 | #[derive(Debug, Clone, PartialEq, Eq)] |
diff --git a/crates/ra_hir_def/src/expr.rs b/crates/ra_hir_def/src/expr.rs index 04c1d8f69..6fad80a8d 100644 --- a/crates/ra_hir_def/src/expr.rs +++ b/crates/ra_hir_def/src/expr.rs | |||
@@ -14,6 +14,7 @@ | |||
14 | 14 | ||
15 | use hir_expand::name::Name; | 15 | use hir_expand::name::Name; |
16 | use ra_arena::{impl_arena_id, RawId}; | 16 | use ra_arena::{impl_arena_id, RawId}; |
17 | use ra_syntax::ast::RangeOp; | ||
17 | 18 | ||
18 | use crate::{ | 19 | use crate::{ |
19 | builtin_type::{BuiltinFloat, BuiltinInt}, | 20 | builtin_type::{BuiltinFloat, BuiltinInt}, |
@@ -130,6 +131,11 @@ pub enum Expr { | |||
130 | rhs: ExprId, | 131 | rhs: ExprId, |
131 | op: Option<BinaryOp>, | 132 | op: Option<BinaryOp>, |
132 | }, | 133 | }, |
134 | Range { | ||
135 | lhs: Option<ExprId>, | ||
136 | rhs: Option<ExprId>, | ||
137 | range_type: RangeOp, | ||
138 | }, | ||
133 | Index { | 139 | Index { |
134 | base: ExprId, | 140 | base: ExprId, |
135 | index: ExprId, | 141 | index: ExprId, |
@@ -288,6 +294,14 @@ impl Expr { | |||
288 | f(*lhs); | 294 | f(*lhs); |
289 | f(*rhs); | 295 | f(*rhs); |
290 | } | 296 | } |
297 | Expr::Range { lhs, rhs, .. } => { | ||
298 | if let Some(lhs) = rhs { | ||
299 | f(*lhs); | ||
300 | } | ||
301 | if let Some(rhs) = lhs { | ||
302 | f(*rhs); | ||
303 | } | ||
304 | } | ||
291 | Expr::Index { base, index } => { | 305 | Expr::Index { base, index } => { |
292 | f(*base); | 306 | f(*base); |
293 | f(*index); | 307 | f(*index); |
diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 3f94e40e4..5f648ffc3 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs | |||
@@ -9,8 +9,9 @@ use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner}; | |||
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | db::DefDatabase, | 11 | db::DefDatabase, |
12 | src::HasSource, | ||
12 | type_ref::{TypeBound, TypeRef}, | 13 | type_ref::{TypeBound, TypeRef}, |
13 | AdtId, AstItemDef, ContainerId, GenericDefId, HasSource, Lookup, | 14 | AdtId, AstItemDef, ContainerId, GenericDefId, Lookup, |
14 | }; | 15 | }; |
15 | 16 | ||
16 | /// Data about a generic parameter (to a function, struct, impl, ...). | 17 | /// Data about a generic parameter (to a function, struct, impl, ...). |
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index bc5530896..cfeacfded 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -29,6 +29,8 @@ pub mod resolver; | |||
29 | mod trace; | 29 | mod trace; |
30 | pub mod nameres; | 30 | pub mod nameres; |
31 | 31 | ||
32 | pub mod src; | ||
33 | |||
32 | #[cfg(test)] | 34 | #[cfg(test)] |
33 | mod test_db; | 35 | mod test_db; |
34 | #[cfg(test)] | 36 | #[cfg(test)] |
@@ -36,8 +38,8 @@ mod marks; | |||
36 | 38 | ||
37 | use std::hash::{Hash, Hasher}; | 39 | use std::hash::{Hash, Hasher}; |
38 | 40 | ||
39 | use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, MacroDefId, Source}; | 41 | use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, InFile, MacroDefId}; |
40 | use ra_arena::{impl_arena_id, map::ArenaMap, RawId}; | 42 | use ra_arena::{impl_arena_id, RawId}; |
41 | use ra_db::{impl_intern_key, salsa, CrateId}; | 43 | use ra_db::{impl_intern_key, salsa, CrateId}; |
42 | use ra_syntax::{ast, AstNode}; | 44 | use ra_syntax::{ast, AstNode}; |
43 | 45 | ||
@@ -105,10 +107,10 @@ pub trait AstItemDef<N: AstNode>: salsa::InternKey + Clone { | |||
105 | let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) }; | 107 | let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) }; |
106 | Self::intern(ctx.db, loc) | 108 | Self::intern(ctx.db, loc) |
107 | } | 109 | } |
108 | fn source(self, db: &(impl AstDatabase + InternDatabase)) -> Source<N> { | 110 | fn source(self, db: &(impl AstDatabase + InternDatabase)) -> InFile<N> { |
109 | let loc = self.lookup_intern(db); | 111 | let loc = self.lookup_intern(db); |
110 | let value = loc.ast_id.to_node(db); | 112 | let value = loc.ast_id.to_node(db); |
111 | Source { file_id: loc.ast_id.file_id(), value } | 113 | InFile { file_id: loc.ast_id.file_id, value } |
112 | } | 114 | } |
113 | fn module(self, db: &impl InternDatabase) -> ModuleId { | 115 | fn module(self, db: &impl InternDatabase) -> ModuleId { |
114 | let loc = self.lookup_intern(db); | 116 | let loc = self.lookup_intern(db); |
@@ -514,53 +516,3 @@ impl HasModule for StaticLoc { | |||
514 | self.container | 516 | self.container |
515 | } | 517 | } |
516 | } | 518 | } |
517 | |||
518 | pub trait HasSource { | ||
519 | type Value; | ||
520 | fn source(&self, db: &impl db::DefDatabase) -> Source<Self::Value>; | ||
521 | } | ||
522 | |||
523 | impl HasSource for FunctionLoc { | ||
524 | type Value = ast::FnDef; | ||
525 | |||
526 | fn source(&self, db: &impl db::DefDatabase) -> Source<ast::FnDef> { | ||
527 | let node = self.ast_id.to_node(db); | ||
528 | Source::new(self.ast_id.file_id(), node) | ||
529 | } | ||
530 | } | ||
531 | |||
532 | impl HasSource for TypeAliasLoc { | ||
533 | type Value = ast::TypeAliasDef; | ||
534 | |||
535 | fn source(&self, db: &impl db::DefDatabase) -> Source<ast::TypeAliasDef> { | ||
536 | let node = self.ast_id.to_node(db); | ||
537 | Source::new(self.ast_id.file_id(), node) | ||
538 | } | ||
539 | } | ||
540 | |||
541 | impl HasSource for ConstLoc { | ||
542 | type Value = ast::ConstDef; | ||
543 | |||
544 | fn source(&self, db: &impl db::DefDatabase) -> Source<ast::ConstDef> { | ||
545 | let node = self.ast_id.to_node(db); | ||
546 | Source::new(self.ast_id.file_id(), node) | ||
547 | } | ||
548 | } | ||
549 | |||
550 | impl HasSource for StaticLoc { | ||
551 | type Value = ast::StaticDef; | ||
552 | |||
553 | fn source(&self, db: &impl db::DefDatabase) -> Source<ast::StaticDef> { | ||
554 | let node = self.ast_id.to_node(db); | ||
555 | Source::new(self.ast_id.file_id(), node) | ||
556 | } | ||
557 | } | ||
558 | |||
559 | pub trait HasChildSource { | ||
560 | type ChildId; | ||
561 | type Value; | ||
562 | fn child_source( | ||
563 | &self, | ||
564 | db: &impl db::DefDatabase, | ||
565 | ) -> Source<ArenaMap<Self::ChildId, Self::Value>>; | ||
566 | } | ||
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 2359386c2..df42ea84a 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs | |||
@@ -58,8 +58,8 @@ mod tests; | |||
58 | use std::sync::Arc; | 58 | use std::sync::Arc; |
59 | 59 | ||
60 | use hir_expand::{ | 60 | use hir_expand::{ |
61 | ast_id_map::FileAstId, diagnostics::DiagnosticSink, either::Either, name::Name, MacroDefId, | 61 | ast_id_map::FileAstId, diagnostics::DiagnosticSink, either::Either, name::Name, InFile, |
62 | Source, | 62 | MacroDefId, |
63 | }; | 63 | }; |
64 | use once_cell::sync::Lazy; | 64 | use once_cell::sync::Lazy; |
65 | use ra_arena::Arena; | 65 | use ra_arena::Arena; |
@@ -261,21 +261,21 @@ impl ModuleData { | |||
261 | pub fn definition_source( | 261 | pub fn definition_source( |
262 | &self, | 262 | &self, |
263 | db: &impl DefDatabase, | 263 | db: &impl DefDatabase, |
264 | ) -> Source<Either<ast::SourceFile, ast::Module>> { | 264 | ) -> InFile<Either<ast::SourceFile, ast::Module>> { |
265 | if let Some(file_id) = self.definition { | 265 | if let Some(file_id) = self.definition { |
266 | let sf = db.parse(file_id).tree(); | 266 | let sf = db.parse(file_id).tree(); |
267 | return Source::new(file_id.into(), Either::A(sf)); | 267 | return InFile::new(file_id.into(), Either::A(sf)); |
268 | } | 268 | } |
269 | let decl = self.declaration.unwrap(); | 269 | let decl = self.declaration.unwrap(); |
270 | Source::new(decl.file_id(), Either::B(decl.to_node(db))) | 270 | InFile::new(decl.file_id, Either::B(decl.to_node(db))) |
271 | } | 271 | } |
272 | 272 | ||
273 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. | 273 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. |
274 | /// `None` for the crate root. | 274 | /// `None` for the crate root. |
275 | pub fn declaration_source(&self, db: &impl DefDatabase) -> Option<Source<ast::Module>> { | 275 | pub fn declaration_source(&self, db: &impl DefDatabase) -> Option<InFile<ast::Module>> { |
276 | let decl = self.declaration?; | 276 | let decl = self.declaration?; |
277 | let value = decl.to_node(db); | 277 | let value = decl.to_node(db); |
278 | Some(Source { file_id: decl.file_id(), value }) | 278 | Some(InFile { file_id: decl.file_id, value }) |
279 | } | 279 | } |
280 | } | 280 | } |
281 | 281 | ||
@@ -309,7 +309,7 @@ mod diagnostics { | |||
309 | } | 309 | } |
310 | let decl = declaration.to_node(db); | 310 | let decl = declaration.to_node(db); |
311 | sink.push(UnresolvedModule { | 311 | sink.push(UnresolvedModule { |
312 | file: declaration.file_id(), | 312 | file: declaration.file_id, |
313 | decl: AstPtr::new(&decl), | 313 | decl: AstPtr::new(&decl), |
314 | candidate: candidate.clone(), | 314 | candidate: candidate.clone(), |
315 | }) | 315 | }) |
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 6eb106094..5196b67ca 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs | |||
@@ -22,8 +22,8 @@ use ra_syntax::{ | |||
22 | use test_utils::tested_by; | 22 | use test_utils::tested_by; |
23 | 23 | ||
24 | use crate::{ | 24 | use crate::{ |
25 | attr::Attrs, db::DefDatabase, path::Path, trace::Trace, FileAstId, HirFileId, LocalImportId, | 25 | attr::Attrs, db::DefDatabase, path::Path, trace::Trace, FileAstId, HirFileId, InFile, |
26 | Source, | 26 | LocalImportId, |
27 | }; | 27 | }; |
28 | 28 | ||
29 | /// `RawItems` is a set of top-level items in a file (except for impls). | 29 | /// `RawItems` is a set of top-level items in a file (except for impls). |
@@ -313,7 +313,7 @@ impl RawItemsCollector { | |||
313 | 313 | ||
314 | let mut buf = Vec::new(); | 314 | let mut buf = Vec::new(); |
315 | Path::expand_use_item( | 315 | Path::expand_use_item( |
316 | Source { value: use_item, file_id: self.file_id }, | 316 | InFile { value: use_item, file_id: self.file_id }, |
317 | &self.hygiene, | 317 | &self.hygiene, |
318 | |path, use_tree, is_glob, alias| { | 318 | |path, use_tree, is_glob, alias| { |
319 | let import_data = ImportData { | 319 | let import_data = ImportData { |
diff --git a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs index e11530062..e800cc68e 100644 --- a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs | |||
@@ -668,7 +668,7 @@ fn unresolved_module_diagnostics() { | |||
668 | module: LocalModuleId( | 668 | module: LocalModuleId( |
669 | 0, | 669 | 0, |
670 | ), | 670 | ), |
671 | declaration: AstId { | 671 | declaration: InFile { |
672 | file_id: HirFileId( | 672 | file_id: HirFileId( |
673 | FileId( | 673 | FileId( |
674 | FileId( | 674 | FileId( |
@@ -676,7 +676,7 @@ fn unresolved_module_diagnostics() { | |||
676 | ), | 676 | ), |
677 | ), | 677 | ), |
678 | ), | 678 | ), |
679 | file_ast_id: FileAstId { | 679 | value: FileAstId { |
680 | raw: ErasedFileAstId( | 680 | raw: ErasedFileAstId( |
681 | 1, | 681 | 1, |
682 | ), | 682 | ), |
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 6810a26db..ff252fe44 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs | |||
@@ -13,7 +13,7 @@ use ra_syntax::{ | |||
13 | AstNode, | 13 | AstNode, |
14 | }; | 14 | }; |
15 | 15 | ||
16 | use crate::{type_ref::TypeRef, Source}; | 16 | use crate::{type_ref::TypeRef, InFile}; |
17 | 17 | ||
18 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 18 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
19 | pub struct Path { | 19 | pub struct Path { |
@@ -67,7 +67,7 @@ pub enum PathKind { | |||
67 | impl Path { | 67 | impl Path { |
68 | /// Calls `cb` with all paths, represented by this use item. | 68 | /// Calls `cb` with all paths, represented by this use item. |
69 | pub(crate) fn expand_use_item( | 69 | pub(crate) fn expand_use_item( |
70 | item_src: Source<ast::UseItem>, | 70 | item_src: InFile<ast::UseItem>, |
71 | hygiene: &Hygiene, | 71 | hygiene: &Hygiene, |
72 | mut cb: impl FnMut(Path, &ast::UseTree, bool, Option<Name>), | 72 | mut cb: impl FnMut(Path, &ast::UseTree, bool, Option<Name>), |
73 | ) { | 73 | ) { |
@@ -409,6 +409,36 @@ pub mod known { | |||
409 | Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::TRY_TYPE]) | 409 | Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::TRY_TYPE]) |
410 | } | 410 | } |
411 | 411 | ||
412 | pub fn std_ops_range() -> Path { | ||
413 | Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::RANGE_TYPE]) | ||
414 | } | ||
415 | |||
416 | pub fn std_ops_range_from() -> Path { | ||
417 | Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::RANGE_FROM_TYPE]) | ||
418 | } | ||
419 | |||
420 | pub fn std_ops_range_full() -> Path { | ||
421 | Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::RANGE_FULL_TYPE]) | ||
422 | } | ||
423 | |||
424 | pub fn std_ops_range_inclusive() -> Path { | ||
425 | Path::from_simple_segments( | ||
426 | PathKind::Abs, | ||
427 | vec![name::STD, name::OPS, name::RANGE_INCLUSIVE_TYPE], | ||
428 | ) | ||
429 | } | ||
430 | |||
431 | pub fn std_ops_range_to() -> Path { | ||
432 | Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::OPS, name::RANGE_TO_TYPE]) | ||
433 | } | ||
434 | |||
435 | pub fn std_ops_range_to_inclusive() -> Path { | ||
436 | Path::from_simple_segments( | ||
437 | PathKind::Abs, | ||
438 | vec![name::STD, name::OPS, name::RANGE_TO_INCLUSIVE_TYPE], | ||
439 | ) | ||
440 | } | ||
441 | |||
412 | pub fn std_result_result() -> Path { | 442 | pub fn std_result_result() -> Path { |
413 | Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::RESULT, name::RESULT_TYPE]) | 443 | Path::from_simple_segments(PathKind::Abs, vec![name::STD, name::RESULT, name::RESULT_TYPE]) |
414 | } | 444 | } |
diff --git a/crates/ra_hir_def/src/src.rs b/crates/ra_hir_def/src/src.rs new file mode 100644 index 000000000..27caa02cc --- /dev/null +++ b/crates/ra_hir_def/src/src.rs | |||
@@ -0,0 +1,54 @@ | |||
1 | //! Utilities for mapping between hir IDs and the surface syntax. | ||
2 | |||
3 | use hir_expand::InFile; | ||
4 | use ra_arena::map::ArenaMap; | ||
5 | use ra_syntax::ast; | ||
6 | |||
7 | use crate::{db::DefDatabase, ConstLoc, FunctionLoc, StaticLoc, TypeAliasLoc}; | ||
8 | |||
9 | pub trait HasSource { | ||
10 | type Value; | ||
11 | fn source(&self, db: &impl DefDatabase) -> InFile<Self::Value>; | ||
12 | } | ||
13 | |||
14 | impl HasSource for FunctionLoc { | ||
15 | type Value = ast::FnDef; | ||
16 | |||
17 | fn source(&self, db: &impl DefDatabase) -> InFile<ast::FnDef> { | ||
18 | let node = self.ast_id.to_node(db); | ||
19 | InFile::new(self.ast_id.file_id, node) | ||
20 | } | ||
21 | } | ||
22 | |||
23 | impl HasSource for TypeAliasLoc { | ||
24 | type Value = ast::TypeAliasDef; | ||
25 | |||
26 | fn source(&self, db: &impl DefDatabase) -> InFile<ast::TypeAliasDef> { | ||
27 | let node = self.ast_id.to_node(db); | ||
28 | InFile::new(self.ast_id.file_id, node) | ||
29 | } | ||
30 | } | ||
31 | |||
32 | impl HasSource for ConstLoc { | ||
33 | type Value = ast::ConstDef; | ||
34 | |||
35 | fn source(&self, db: &impl DefDatabase) -> InFile<ast::ConstDef> { | ||
36 | let node = self.ast_id.to_node(db); | ||
37 | InFile::new(self.ast_id.file_id, node) | ||
38 | } | ||
39 | } | ||
40 | |||
41 | impl HasSource for StaticLoc { | ||
42 | type Value = ast::StaticDef; | ||
43 | |||
44 | fn source(&self, db: &impl DefDatabase) -> InFile<ast::StaticDef> { | ||
45 | let node = self.ast_id.to_node(db); | ||
46 | InFile::new(self.ast_id.file_id, node) | ||
47 | } | ||
48 | } | ||
49 | |||
50 | pub trait HasChildSource { | ||
51 | type ChildId; | ||
52 | type Value; | ||
53 | fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>>; | ||
54 | } | ||