aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/expr
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-04-10 08:46:43 +0100
committerAleksey Kladov <[email protected]>2019-04-10 08:48:15 +0100
commit10726fdb65fda9144a5f9201272d065a268fc1b7 (patch)
treef5640991cbf0db2bfdad29b911435827cadfb4a8 /crates/ra_hir/src/expr
parent1cd184d6539478c7e54c92835902921976dce5d1 (diff)
type-safer source-map for bindings
Diffstat (limited to 'crates/ra_hir/src/expr')
-rw-r--r--crates/ra_hir/src/expr/scope.rs15
1 files changed, 8 insertions, 7 deletions
diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs
index f1e6e0f02..725b6c00e 100644
--- a/crates/ra_hir/src/expr/scope.rs
+++ b/crates/ra_hir/src/expr/scope.rs
@@ -3,14 +3,14 @@ use std::sync::Arc;
3use rustc_hash::{FxHashMap, FxHashSet}; 3use rustc_hash::{FxHashMap, FxHashSet};
4 4
5use ra_syntax::{ 5use ra_syntax::{
6 AstNode, SyntaxNode, TextUnit, TextRange, SyntaxNodePtr, 6 AstNode, SyntaxNode, TextUnit, TextRange, SyntaxNodePtr, AstPtr,
7 algo::generate, 7 algo::generate,
8 ast, 8 ast,
9}; 9};
10use ra_arena::{Arena, RawId, impl_arena_id}; 10use ra_arena::{Arena, RawId, impl_arena_id};
11 11
12use crate::{ 12use crate::{
13 Name, AsName,DefWithBody, 13 Name, AsName,DefWithBody, Either,
14 expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySourceMap}, 14 expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySourceMap},
15 HirDatabase, 15 HirDatabase,
16}; 16};
@@ -116,7 +116,7 @@ pub struct ScopesWithSourceMap {
116#[derive(Debug, Clone, PartialEq, Eq)] 116#[derive(Debug, Clone, PartialEq, Eq)]
117pub struct ScopeEntryWithSyntax { 117pub struct ScopeEntryWithSyntax {
118 name: Name, 118 name: Name,
119 ptr: SyntaxNodePtr, 119 ptr: Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>,
120} 120}
121 121
122impl ScopeEntryWithSyntax { 122impl ScopeEntryWithSyntax {
@@ -124,7 +124,7 @@ impl ScopeEntryWithSyntax {
124 &self.name 124 &self.name
125 } 125 }
126 126
127 pub fn ptr(&self) -> SyntaxNodePtr { 127 pub fn ptr(&self) -> Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>> {
128 self.ptr 128 self.ptr
129 } 129 }
130} 130}
@@ -192,14 +192,14 @@ impl ScopesWithSourceMap {
192 192
193 pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> { 193 pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> {
194 let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); 194 let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
195 let name_ptr = SyntaxNodePtr::new(pat.syntax()); 195 let ptr = Either::A(AstPtr::new(pat.into()));
196 fn_def 196 fn_def
197 .syntax() 197 .syntax()
198 .descendants() 198 .descendants()
199 .filter_map(ast::NameRef::cast) 199 .filter_map(ast::NameRef::cast)
200 .filter(|name_ref| match self.resolve_local_name(*name_ref) { 200 .filter(|name_ref| match self.resolve_local_name(*name_ref) {
201 None => false, 201 None => false,
202 Some(entry) => entry.ptr() == name_ptr, 202 Some(entry) => entry.ptr() == ptr,
203 }) 203 })
204 .map(|name_ref| ReferenceDescriptor { 204 .map(|name_ref| ReferenceDescriptor {
205 name: name_ref.syntax().text().to_string(), 205 name: name_ref.syntax().text().to_string(),
@@ -429,7 +429,8 @@ mod tests {
429 let scopes = 429 let scopes =
430 ScopesWithSourceMap { scopes: Arc::new(scopes), source_map: Arc::new(source_map) }; 430 ScopesWithSourceMap { scopes: Arc::new(scopes), source_map: Arc::new(source_map) };
431 let local_name_entry = scopes.resolve_local_name(name_ref).unwrap(); 431 let local_name_entry = scopes.resolve_local_name(name_ref).unwrap();
432 let local_name = local_name_entry.ptr(); 432 let local_name =
433 local_name_entry.ptr().either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr());
433 assert_eq!(local_name.range(), expected_name.syntax().range()); 434 assert_eq!(local_name.range(), expected_name.syntax().range());
434 } 435 }
435 436