From 65864d85f992300bad3913438a542af1bd736a24 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 19 Jan 2019 19:47:56 +0100 Subject: Rename FnScopes -> ExprScopes The reason for this is that it describes scopes for any body expression, not just that of a function. It did not actually refer to functions at all anymore. --- crates/ra_hir/src/code_model_api.rs | 4 +- crates/ra_hir/src/code_model_impl/function.rs | 4 - .../ra_hir/src/code_model_impl/function/scope.rs | 510 --------------------- crates/ra_hir/src/db.rs | 6 +- crates/ra_hir/src/expr.rs | 4 + crates/ra_hir/src/expr/scope.rs | 510 +++++++++++++++++++++ crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/query_definitions.rs | 6 +- crates/ra_hir/src/ty.rs | 8 +- 9 files changed, 527 insertions(+), 527 deletions(-) delete mode 100644 crates/ra_hir/src/code_model_impl/function/scope.rs create mode 100644 crates/ra_hir/src/expr/scope.rs (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index c59b7fbc5..9405aa8ad 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -396,7 +396,7 @@ pub struct Function { pub(crate) id: FunctionId, } -pub use crate::code_model_impl::function::ScopeEntryWithSyntax; +pub use crate::expr::ScopeEntryWithSyntax; /// The declared signature of a function. #[derive(Debug, Clone, PartialEq, Eq)] @@ -447,7 +447,7 @@ impl Function { } pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSyntaxMapping { - let scopes = db.fn_scopes(*self); + let scopes = db.expr_scopes(*self); let syntax_mapping = db.body_syntax_mapping(*self); ScopesWithSyntaxMapping { scopes, diff --git a/crates/ra_hir/src/code_model_impl/function.rs b/crates/ra_hir/src/code_model_impl/function.rs index e0dd4d629..422643996 100644 --- a/crates/ra_hir/src/code_model_impl/function.rs +++ b/crates/ra_hir/src/code_model_impl/function.rs @@ -1,5 +1,3 @@ -mod scope; - use std::sync::Arc; use ra_syntax::ast::{self, NameOwner}; @@ -11,8 +9,6 @@ use crate::{ impl_block::ImplBlock, }; -pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax}; - impl Function { pub(crate) fn body(&self, db: &impl HirDatabase) -> Arc { db.body_hir(*self) diff --git a/crates/ra_hir/src/code_model_impl/function/scope.rs b/crates/ra_hir/src/code_model_impl/function/scope.rs deleted file mode 100644 index c5d1de5eb..000000000 --- a/crates/ra_hir/src/code_model_impl/function/scope.rs +++ /dev/null @@ -1,510 +0,0 @@ -use std::sync::Arc; - -use rustc_hash::{FxHashMap, FxHashSet}; - -use ra_syntax::{ - AstNode, SyntaxNode, TextUnit, TextRange, SyntaxNodePtr, - algo::generate, - ast, -}; -use ra_arena::{Arena, RawId, impl_arena_id}; - -use crate::{Name, AsName, expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySyntaxMapping}}; - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct ScopeId(RawId); -impl_arena_id!(ScopeId); - -#[derive(Debug, PartialEq, Eq)] -pub struct FnScopes { - body: Arc, - scopes: Arena, - scope_for: FxHashMap, -} - -#[derive(Debug, PartialEq, Eq)] -pub struct ScopeEntry { - name: Name, - pat: PatId, -} - -#[derive(Debug, PartialEq, Eq)] -pub struct ScopeData { - parent: Option, - entries: Vec, -} - -impl FnScopes { - pub(crate) fn new(body: Arc) -> FnScopes { - let mut scopes = FnScopes { - body: body.clone(), - scopes: Arena::default(), - scope_for: FxHashMap::default(), - }; - let root = scopes.root_scope(); - scopes.add_params_bindings(root, body.params()); - compute_expr_scopes(body.body_expr(), &body, &mut scopes, root); - scopes - } - - pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { - &self.scopes[scope].entries - } - - pub fn scope_chain_for<'a>(&'a self, expr: ExprId) -> impl Iterator + 'a { - generate(self.scope_for(expr), move |&scope| { - self.scopes[scope].parent - }) - } - - pub fn resolve_local_name<'a>( - &'a self, - context_expr: ExprId, - name: Name, - ) -> Option<&'a ScopeEntry> { - let mut shadowed = FxHashSet::default(); - let ret = self - .scope_chain_for(context_expr) - .flat_map(|scope| self.entries(scope).iter()) - .filter(|entry| shadowed.insert(entry.name())) - .find(|entry| entry.name() == &name); - ret - } - - fn root_scope(&mut self) -> ScopeId { - self.scopes.alloc(ScopeData { - parent: None, - entries: vec![], - }) - } - - fn new_scope(&mut self, parent: ScopeId) -> ScopeId { - self.scopes.alloc(ScopeData { - parent: Some(parent), - entries: vec![], - }) - } - - fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) { - match &body[pat] { - Pat::Bind { name, .. } => { - // bind can have a subpattern, but it's actually not allowed - // to bind to things in there - let entry = ScopeEntry { - name: name.clone(), - pat, - }; - self.scopes[scope].entries.push(entry) - } - p => p.walk_child_pats(|pat| self.add_bindings(body, scope, pat)), - } - } - - fn add_params_bindings(&mut self, scope: ScopeId, params: &[PatId]) { - let body = Arc::clone(&self.body); - params - .into_iter() - .for_each(|pat| self.add_bindings(&body, scope, *pat)); - } - - fn set_scope(&mut self, node: ExprId, scope: ScopeId) { - self.scope_for.insert(node, scope); - } - - fn scope_for(&self, expr: ExprId) -> Option { - self.scope_for.get(&expr).map(|&scope| scope) - } -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct ScopesWithSyntaxMapping { - pub syntax_mapping: Arc, - pub scopes: Arc, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct ScopeEntryWithSyntax { - name: Name, - ptr: SyntaxNodePtr, -} - -impl ScopeEntryWithSyntax { - pub fn name(&self) -> &Name { - &self.name - } - - pub fn ptr(&self) -> SyntaxNodePtr { - self.ptr - } -} - -impl ScopesWithSyntaxMapping { - pub fn scope_chain<'a>(&'a self, node: &SyntaxNode) -> impl Iterator + 'a { - generate(self.scope_for(node), move |&scope| { - self.scopes.scopes[scope].parent - }) - } - - pub fn scope_chain_for_offset<'a>( - &'a self, - offset: TextUnit, - ) -> impl Iterator + 'a { - let scope = self - .scopes - .scope_for - .iter() - .filter_map(|(id, scope)| Some((self.syntax_mapping.expr_syntax(*id)?, scope))) - // find containing scope - .min_by_key(|(ptr, _scope)| { - ( - !(ptr.range().start() <= offset && offset <= ptr.range().end()), - ptr.range().len(), - ) - }) - .map(|(ptr, scope)| self.adjust(ptr, *scope, offset)); - - generate(scope, move |&scope| self.scopes.scopes[scope].parent) - } - - // XXX: during completion, cursor might be outside of any particular - // expression. Try to figure out the correct scope... - fn adjust(&self, ptr: SyntaxNodePtr, original_scope: ScopeId, offset: TextUnit) -> ScopeId { - let r = ptr.range(); - let child_scopes = self - .scopes - .scope_for - .iter() - .filter_map(|(id, scope)| Some((self.syntax_mapping.expr_syntax(*id)?, scope))) - .map(|(ptr, scope)| (ptr.range(), scope)) - .filter(|(range, _)| range.start() <= offset && range.is_subrange(&r) && *range != r); - - child_scopes - .max_by(|(r1, _), (r2, _)| { - if r2.is_subrange(&r1) { - std::cmp::Ordering::Greater - } else if r1.is_subrange(&r2) { - std::cmp::Ordering::Less - } else { - r1.start().cmp(&r2.start()) - } - }) - .map(|(_ptr, scope)| *scope) - .unwrap_or(original_scope) - } - - pub fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option { - let mut shadowed = FxHashSet::default(); - let name = name_ref.as_name(); - let ret = self - .scope_chain(name_ref.syntax()) - .flat_map(|scope| self.scopes.entries(scope).iter()) - .filter(|entry| shadowed.insert(entry.name())) - .filter(|entry| entry.name() == &name) - .nth(0); - ret.and_then(|entry| { - Some(ScopeEntryWithSyntax { - name: entry.name().clone(), - ptr: self.syntax_mapping.pat_syntax(entry.pat())?, - }) - }) - } - - pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec { - let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); - let name_ptr = SyntaxNodePtr::new(pat.syntax()); - fn_def - .syntax() - .descendants() - .filter_map(ast::NameRef::cast) - .filter(|name_ref| match self.resolve_local_name(*name_ref) { - None => false, - Some(entry) => entry.ptr() == name_ptr, - }) - .map(|name_ref| ReferenceDescriptor { - name: name_ref.syntax().text().to_string(), - range: name_ref.syntax().range(), - }) - .collect() - } - - fn scope_for(&self, node: &SyntaxNode) -> Option { - node.ancestors() - .map(SyntaxNodePtr::new) - .filter_map(|ptr| self.syntax_mapping.syntax_expr(ptr)) - .find_map(|it| self.scopes.scope_for(it)) - } -} - -impl ScopeEntry { - pub fn name(&self) -> &Name { - &self.name - } - - pub fn pat(&self) -> PatId { - self.pat - } -} - -fn compute_block_scopes( - statements: &[Statement], - tail: Option, - body: &Body, - scopes: &mut FnScopes, - mut scope: ScopeId, -) { - for stmt in statements { - match stmt { - Statement::Let { - pat, initializer, .. - } => { - if let Some(expr) = initializer { - scopes.set_scope(*expr, scope); - compute_expr_scopes(*expr, body, scopes, scope); - } - scope = scopes.new_scope(scope); - scopes.add_bindings(body, scope, *pat); - } - Statement::Expr(expr) => { - scopes.set_scope(*expr, scope); - compute_expr_scopes(*expr, body, scopes, scope); - } - } - } - if let Some(expr) = tail { - compute_expr_scopes(expr, body, scopes, scope); - } -} - -fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut FnScopes, scope: ScopeId) { - scopes.set_scope(expr, scope); - match &body[expr] { - Expr::Block { statements, tail } => { - compute_block_scopes(&statements, *tail, body, scopes, scope); - } - Expr::For { - iterable, - pat, - body: body_expr, - } => { - compute_expr_scopes(*iterable, body, scopes, scope); - let scope = scopes.new_scope(scope); - scopes.add_bindings(body, scope, *pat); - compute_expr_scopes(*body_expr, body, scopes, scope); - } - Expr::Lambda { - args, - body: body_expr, - .. - } => { - let scope = scopes.new_scope(scope); - scopes.add_params_bindings(scope, &args); - compute_expr_scopes(*body_expr, body, scopes, scope); - } - Expr::Match { expr, arms } => { - compute_expr_scopes(*expr, body, scopes, scope); - for arm in arms { - let scope = scopes.new_scope(scope); - for pat in &arm.pats { - scopes.add_bindings(body, scope, *pat); - } - scopes.set_scope(arm.expr, scope); - compute_expr_scopes(arm.expr, body, scopes, scope); - } - } - e => e.walk_child_exprs(|e| compute_expr_scopes(e, body, scopes, scope)), - }; -} - -#[derive(Debug)] -pub struct ReferenceDescriptor { - pub range: TextRange, - pub name: String, -} - -#[cfg(test)] -mod tests { - use ra_syntax::{SourceFile, algo::find_node_at_offset}; - use test_utils::{extract_offset, assert_eq_text}; - - use crate::expr; - - use super::*; - - fn do_check(code: &str, expected: &[&str]) { - let (off, code) = extract_offset(code); - let code = { - let mut buf = String::new(); - let off = u32::from(off) as usize; - buf.push_str(&code[..off]); - buf.push_str("marker"); - buf.push_str(&code[off..]); - buf - }; - let file = SourceFile::parse(&code); - let marker: &ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); - let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); - let body_hir = expr::collect_fn_body_syntax(fn_def); - let scopes = FnScopes::new(Arc::clone(body_hir.body())); - let scopes = ScopesWithSyntaxMapping { - scopes: Arc::new(scopes), - syntax_mapping: Arc::new(body_hir), - }; - let actual = scopes - .scope_chain(marker.syntax()) - .flat_map(|scope| scopes.scopes.entries(scope)) - .map(|it| it.name().to_string()) - .collect::>() - .join("\n"); - let expected = expected.join("\n"); - assert_eq_text!(&expected, &actual); - } - - #[test] - fn test_lambda_scope() { - do_check( - r" - fn quux(foo: i32) { - let f = |bar, baz: i32| { - <|> - }; - }", - &["bar", "baz", "foo"], - ); - } - - #[test] - fn test_call_scope() { - do_check( - r" - fn quux() { - f(|x| <|> ); - }", - &["x"], - ); - } - - #[test] - fn test_method_call_scope() { - do_check( - r" - fn quux() { - z.f(|x| <|> ); - }", - &["x"], - ); - } - - #[test] - fn test_loop_scope() { - do_check( - r" - fn quux() { - loop { - let x = (); - <|> - }; - }", - &["x"], - ); - } - - #[test] - fn test_match() { - do_check( - r" - fn quux() { - match () { - Some(x) => { - <|> - } - }; - }", - &["x"], - ); - } - - #[test] - fn test_shadow_variable() { - do_check( - r" - fn foo(x: String) { - let x : &str = &x<|>; - }", - &["x"], - ); - } - - fn do_check_local_name(code: &str, expected_offset: u32) { - let (off, code) = extract_offset(code); - let file = SourceFile::parse(&code); - let expected_name = find_node_at_offset::(file.syntax(), expected_offset.into()) - .expect("failed to find a name at the target offset"); - - let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); - let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); - - let body_hir = expr::collect_fn_body_syntax(fn_def); - let scopes = FnScopes::new(Arc::clone(body_hir.body())); - let scopes = ScopesWithSyntaxMapping { - scopes: Arc::new(scopes), - syntax_mapping: Arc::new(body_hir), - }; - let local_name_entry = scopes.resolve_local_name(name_ref).unwrap(); - let local_name = local_name_entry.ptr(); - assert_eq!(local_name.range(), expected_name.syntax().range()); - } - - #[test] - fn test_resolve_local_name() { - do_check_local_name( - r#" - fn foo(x: i32, y: u32) { - { - let z = x * 2; - } - { - let t = x<|> * 3; - } - }"#, - 21, - ); - } - - #[test] - fn test_resolve_local_name_declaration() { - do_check_local_name( - r#" - fn foo(x: String) { - let x : &str = &x<|>; - }"#, - 21, - ); - } - - #[test] - fn test_resolve_local_name_shadow() { - do_check_local_name( - r" - fn foo(x: String) { - let x : &str = &x; - x<|> - } - ", - 53, - ); - } - - #[test] - fn ref_patterns_contribute_bindings() { - do_check_local_name( - r" - fn foo() { - if let Some(&from) = bar() { - from<|>; - } - } - ", - 53, - ); - } -} diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 16d5a7877..ddd4b2687 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -7,7 +7,7 @@ use crate::{ MacroCallId, HirFileId, SourceFileItems, SourceItemId, Crate, Module, HirInterner, query_definitions, - Function, FnSignature, FnScopes, + Function, FnSignature, ExprScopes, Struct, Enum, StructField, macros::MacroExpansion, module_tree::ModuleTree, @@ -27,8 +27,8 @@ pub trait HirDatabase: SourceDatabase + AsRef { #[salsa::invoke(crate::macros::expand_macro_invocation)] fn expand_macro_invocation(&self, invoc: MacroCallId) -> Option>; - #[salsa::invoke(query_definitions::fn_scopes)] - fn fn_scopes(&self, func: Function) -> Arc; + #[salsa::invoke(query_definitions::expr_scopes)] + fn expr_scopes(&self, func: Function) -> Arc; #[salsa::invoke(crate::adt::StructData::struct_data_query)] fn struct_data(&self, s: Struct) -> Arc; diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 60d997bbe..83e913e4a 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -16,6 +16,10 @@ use crate::{ }; use crate::ty::primitive::{UintTy, UncertainIntTy, UncertainFloatTy}; +pub use self::scope::{ExprScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax}; + +mod scope; + #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ExprId(RawId); impl_arena_id!(ExprId); diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs new file mode 100644 index 000000000..17a1efed2 --- /dev/null +++ b/crates/ra_hir/src/expr/scope.rs @@ -0,0 +1,510 @@ +use std::sync::Arc; + +use rustc_hash::{FxHashMap, FxHashSet}; + +use ra_syntax::{ + AstNode, SyntaxNode, TextUnit, TextRange, SyntaxNodePtr, + algo::generate, + ast, +}; +use ra_arena::{Arena, RawId, impl_arena_id}; + +use crate::{Name, AsName, expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySyntaxMapping}}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct ScopeId(RawId); +impl_arena_id!(ScopeId); + +#[derive(Debug, PartialEq, Eq)] +pub struct ExprScopes { + body: Arc, + scopes: Arena, + scope_for: FxHashMap, +} + +#[derive(Debug, PartialEq, Eq)] +pub struct ScopeEntry { + name: Name, + pat: PatId, +} + +#[derive(Debug, PartialEq, Eq)] +pub struct ScopeData { + parent: Option, + entries: Vec, +} + +impl ExprScopes { + pub(crate) fn new(body: Arc) -> ExprScopes { + let mut scopes = ExprScopes { + body: body.clone(), + scopes: Arena::default(), + scope_for: FxHashMap::default(), + }; + let root = scopes.root_scope(); + scopes.add_params_bindings(root, body.params()); + compute_expr_scopes(body.body_expr(), &body, &mut scopes, root); + scopes + } + + pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { + &self.scopes[scope].entries + } + + pub fn scope_chain_for<'a>(&'a self, expr: ExprId) -> impl Iterator + 'a { + generate(self.scope_for(expr), move |&scope| { + self.scopes[scope].parent + }) + } + + pub fn resolve_local_name<'a>( + &'a self, + context_expr: ExprId, + name: Name, + ) -> Option<&'a ScopeEntry> { + let mut shadowed = FxHashSet::default(); + let ret = self + .scope_chain_for(context_expr) + .flat_map(|scope| self.entries(scope).iter()) + .filter(|entry| shadowed.insert(entry.name())) + .find(|entry| entry.name() == &name); + ret + } + + fn root_scope(&mut self) -> ScopeId { + self.scopes.alloc(ScopeData { + parent: None, + entries: vec![], + }) + } + + fn new_scope(&mut self, parent: ScopeId) -> ScopeId { + self.scopes.alloc(ScopeData { + parent: Some(parent), + entries: vec![], + }) + } + + fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) { + match &body[pat] { + Pat::Bind { name, .. } => { + // bind can have a subpattern, but it's actually not allowed + // to bind to things in there + let entry = ScopeEntry { + name: name.clone(), + pat, + }; + self.scopes[scope].entries.push(entry) + } + p => p.walk_child_pats(|pat| self.add_bindings(body, scope, pat)), + } + } + + fn add_params_bindings(&mut self, scope: ScopeId, params: &[PatId]) { + let body = Arc::clone(&self.body); + params + .into_iter() + .for_each(|pat| self.add_bindings(&body, scope, *pat)); + } + + fn set_scope(&mut self, node: ExprId, scope: ScopeId) { + self.scope_for.insert(node, scope); + } + + fn scope_for(&self, expr: ExprId) -> Option { + self.scope_for.get(&expr).map(|&scope| scope) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ScopesWithSyntaxMapping { + pub syntax_mapping: Arc, + pub scopes: Arc, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ScopeEntryWithSyntax { + name: Name, + ptr: SyntaxNodePtr, +} + +impl ScopeEntryWithSyntax { + pub fn name(&self) -> &Name { + &self.name + } + + pub fn ptr(&self) -> SyntaxNodePtr { + self.ptr + } +} + +impl ScopesWithSyntaxMapping { + pub fn scope_chain<'a>(&'a self, node: &SyntaxNode) -> impl Iterator + 'a { + generate(self.scope_for(node), move |&scope| { + self.scopes.scopes[scope].parent + }) + } + + pub fn scope_chain_for_offset<'a>( + &'a self, + offset: TextUnit, + ) -> impl Iterator + 'a { + let scope = self + .scopes + .scope_for + .iter() + .filter_map(|(id, scope)| Some((self.syntax_mapping.expr_syntax(*id)?, scope))) + // find containing scope + .min_by_key(|(ptr, _scope)| { + ( + !(ptr.range().start() <= offset && offset <= ptr.range().end()), + ptr.range().len(), + ) + }) + .map(|(ptr, scope)| self.adjust(ptr, *scope, offset)); + + generate(scope, move |&scope| self.scopes.scopes[scope].parent) + } + + // XXX: during completion, cursor might be outside of any particular + // expression. Try to figure out the correct scope... + fn adjust(&self, ptr: SyntaxNodePtr, original_scope: ScopeId, offset: TextUnit) -> ScopeId { + let r = ptr.range(); + let child_scopes = self + .scopes + .scope_for + .iter() + .filter_map(|(id, scope)| Some((self.syntax_mapping.expr_syntax(*id)?, scope))) + .map(|(ptr, scope)| (ptr.range(), scope)) + .filter(|(range, _)| range.start() <= offset && range.is_subrange(&r) && *range != r); + + child_scopes + .max_by(|(r1, _), (r2, _)| { + if r2.is_subrange(&r1) { + std::cmp::Ordering::Greater + } else if r1.is_subrange(&r2) { + std::cmp::Ordering::Less + } else { + r1.start().cmp(&r2.start()) + } + }) + .map(|(_ptr, scope)| *scope) + .unwrap_or(original_scope) + } + + pub fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option { + let mut shadowed = FxHashSet::default(); + let name = name_ref.as_name(); + let ret = self + .scope_chain(name_ref.syntax()) + .flat_map(|scope| self.scopes.entries(scope).iter()) + .filter(|entry| shadowed.insert(entry.name())) + .filter(|entry| entry.name() == &name) + .nth(0); + ret.and_then(|entry| { + Some(ScopeEntryWithSyntax { + name: entry.name().clone(), + ptr: self.syntax_mapping.pat_syntax(entry.pat())?, + }) + }) + } + + pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec { + let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); + let name_ptr = SyntaxNodePtr::new(pat.syntax()); + fn_def + .syntax() + .descendants() + .filter_map(ast::NameRef::cast) + .filter(|name_ref| match self.resolve_local_name(*name_ref) { + None => false, + Some(entry) => entry.ptr() == name_ptr, + }) + .map(|name_ref| ReferenceDescriptor { + name: name_ref.syntax().text().to_string(), + range: name_ref.syntax().range(), + }) + .collect() + } + + fn scope_for(&self, node: &SyntaxNode) -> Option { + node.ancestors() + .map(SyntaxNodePtr::new) + .filter_map(|ptr| self.syntax_mapping.syntax_expr(ptr)) + .find_map(|it| self.scopes.scope_for(it)) + } +} + +impl ScopeEntry { + pub fn name(&self) -> &Name { + &self.name + } + + pub fn pat(&self) -> PatId { + self.pat + } +} + +fn compute_block_scopes( + statements: &[Statement], + tail: Option, + body: &Body, + scopes: &mut ExprScopes, + mut scope: ScopeId, +) { + for stmt in statements { + match stmt { + Statement::Let { + pat, initializer, .. + } => { + if let Some(expr) = initializer { + scopes.set_scope(*expr, scope); + compute_expr_scopes(*expr, body, scopes, scope); + } + scope = scopes.new_scope(scope); + scopes.add_bindings(body, scope, *pat); + } + Statement::Expr(expr) => { + scopes.set_scope(*expr, scope); + compute_expr_scopes(*expr, body, scopes, scope); + } + } + } + if let Some(expr) = tail { + compute_expr_scopes(expr, body, scopes, scope); + } +} + +fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope: ScopeId) { + scopes.set_scope(expr, scope); + match &body[expr] { + Expr::Block { statements, tail } => { + compute_block_scopes(&statements, *tail, body, scopes, scope); + } + Expr::For { + iterable, + pat, + body: body_expr, + } => { + compute_expr_scopes(*iterable, body, scopes, scope); + let scope = scopes.new_scope(scope); + scopes.add_bindings(body, scope, *pat); + compute_expr_scopes(*body_expr, body, scopes, scope); + } + Expr::Lambda { + args, + body: body_expr, + .. + } => { + let scope = scopes.new_scope(scope); + scopes.add_params_bindings(scope, &args); + compute_expr_scopes(*body_expr, body, scopes, scope); + } + Expr::Match { expr, arms } => { + compute_expr_scopes(*expr, body, scopes, scope); + for arm in arms { + let scope = scopes.new_scope(scope); + for pat in &arm.pats { + scopes.add_bindings(body, scope, *pat); + } + scopes.set_scope(arm.expr, scope); + compute_expr_scopes(arm.expr, body, scopes, scope); + } + } + e => e.walk_child_exprs(|e| compute_expr_scopes(e, body, scopes, scope)), + }; +} + +#[derive(Debug)] +pub struct ReferenceDescriptor { + pub range: TextRange, + pub name: String, +} + +#[cfg(test)] +mod tests { + use ra_syntax::{SourceFile, algo::find_node_at_offset}; + use test_utils::{extract_offset, assert_eq_text}; + + use crate::expr; + + use super::*; + + fn do_check(code: &str, expected: &[&str]) { + let (off, code) = extract_offset(code); + let code = { + let mut buf = String::new(); + let off = u32::from(off) as usize; + buf.push_str(&code[..off]); + buf.push_str("marker"); + buf.push_str(&code[off..]); + buf + }; + let file = SourceFile::parse(&code); + let marker: &ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); + let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); + let body_hir = expr::collect_fn_body_syntax(fn_def); + let scopes = ExprScopes::new(Arc::clone(body_hir.body())); + let scopes = ScopesWithSyntaxMapping { + scopes: Arc::new(scopes), + syntax_mapping: Arc::new(body_hir), + }; + let actual = scopes + .scope_chain(marker.syntax()) + .flat_map(|scope| scopes.scopes.entries(scope)) + .map(|it| it.name().to_string()) + .collect::>() + .join("\n"); + let expected = expected.join("\n"); + assert_eq_text!(&expected, &actual); + } + + #[test] + fn test_lambda_scope() { + do_check( + r" + fn quux(foo: i32) { + let f = |bar, baz: i32| { + <|> + }; + }", + &["bar", "baz", "foo"], + ); + } + + #[test] + fn test_call_scope() { + do_check( + r" + fn quux() { + f(|x| <|> ); + }", + &["x"], + ); + } + + #[test] + fn test_method_call_scope() { + do_check( + r" + fn quux() { + z.f(|x| <|> ); + }", + &["x"], + ); + } + + #[test] + fn test_loop_scope() { + do_check( + r" + fn quux() { + loop { + let x = (); + <|> + }; + }", + &["x"], + ); + } + + #[test] + fn test_match() { + do_check( + r" + fn quux() { + match () { + Some(x) => { + <|> + } + }; + }", + &["x"], + ); + } + + #[test] + fn test_shadow_variable() { + do_check( + r" + fn foo(x: String) { + let x : &str = &x<|>; + }", + &["x"], + ); + } + + fn do_check_local_name(code: &str, expected_offset: u32) { + let (off, code) = extract_offset(code); + let file = SourceFile::parse(&code); + let expected_name = find_node_at_offset::(file.syntax(), expected_offset.into()) + .expect("failed to find a name at the target offset"); + + let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); + let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); + + let body_hir = expr::collect_fn_body_syntax(fn_def); + let scopes = ExprScopes::new(Arc::clone(body_hir.body())); + let scopes = ScopesWithSyntaxMapping { + scopes: Arc::new(scopes), + syntax_mapping: Arc::new(body_hir), + }; + let local_name_entry = scopes.resolve_local_name(name_ref).unwrap(); + let local_name = local_name_entry.ptr(); + assert_eq!(local_name.range(), expected_name.syntax().range()); + } + + #[test] + fn test_resolve_local_name() { + do_check_local_name( + r#" + fn foo(x: i32, y: u32) { + { + let z = x * 2; + } + { + let t = x<|> * 3; + } + }"#, + 21, + ); + } + + #[test] + fn test_resolve_local_name_declaration() { + do_check_local_name( + r#" + fn foo(x: String) { + let x : &str = &x<|>; + }"#, + 21, + ); + } + + #[test] + fn test_resolve_local_name_shadow() { + do_check_local_name( + r" + fn foo(x: String) { + let x : &str = &x; + x<|> + } + ", + 53, + ); + } + + #[test] + fn ref_patterns_contribute_bindings() { + do_check_local_name( + r" + fn foo() { + if let Some(&from) = bar() { + from<|>; + } + } + ", + 53, + ); + } +} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index eaf8565ee..0b9ee63bf 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -57,9 +57,9 @@ pub use self::{ nameres::{ItemMap, PerNs, Namespace, Resolution}, ty::Ty, impl_block::{ImplBlock, ImplItem}, - code_model_impl::function::{FnScopes, ScopesWithSyntaxMapping}, docs::{Docs, Documentation}, adt::AdtDef, + expr::{ExprScopes, ScopesWithSyntaxMapping}, }; pub use self::code_model_api::{ diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index 6724649e1..1b61d449e 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs @@ -4,13 +4,13 @@ use ra_syntax::{SyntaxNode, TreeArc}; use crate::{ SourceFileItems, SourceItemId, HirFileId, - Function, FnScopes, + Function, ExprScopes, db::HirDatabase, }; -pub(super) fn fn_scopes(db: &impl HirDatabase, func: Function) -> Arc { +pub(super) fn expr_scopes(db: &impl HirDatabase, func: Function) -> Arc { let body = db.body_hir(func); - let res = FnScopes::new(body); + let res = ExprScopes::new(body); Arc::new(res) } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 7a5485698..0472414a6 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -34,7 +34,7 @@ use test_utils::tested_by; use crate::{ Module, Function, Struct, StructField, Enum, EnumVariant, Path, Name, ImplBlock, - FnSignature, FnScopes, ModuleDef, AdtDef, + FnSignature, ExprScopes, ModuleDef, AdtDef, db::HirDatabase, type_ref::{TypeRef, Mutability}, name::KnownName, @@ -814,7 +814,7 @@ impl Index for InferenceResult { struct InferenceContext<'a, D: HirDatabase> { db: &'a D, body: Arc, - scopes: Arc, + scopes: Arc, module: Module, impl_block: Option, var_unification_table: InPlaceUnificationTable, @@ -908,7 +908,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { fn new( db: &'a D, body: Arc, - scopes: Arc, + scopes: Arc, module: Module, impl_block: Option, ) -> Self { @@ -1720,7 +1720,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { pub fn infer(db: &impl HirDatabase, func: Function) -> Arc { db.check_canceled(); let body = func.body(db); - let scopes = db.fn_scopes(func); + let scopes = db.expr_scopes(func); let module = func.module(db); let impl_block = func.impl_block(db); let mut ctx = InferenceContext::new(db, body, scopes, module, impl_block); -- cgit v1.2.3 From 1acff307fe2e20f0c2291fd24b08fba6fa39e5ee Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 19 Jan 2019 19:57:43 +0100 Subject: Move expr_scopes query to its module --- crates/ra_hir/src/db.rs | 2 +- crates/ra_hir/src/expr/scope.rs | 15 +++++++++++++-- crates/ra_hir/src/query_definitions.rs | 11 +++-------- 3 files changed, 17 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index ddd4b2687..189649841 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -27,7 +27,7 @@ pub trait HirDatabase: SourceDatabase + AsRef { #[salsa::invoke(crate::macros::expand_macro_invocation)] fn expand_macro_invocation(&self, invoc: MacroCallId) -> Option>; - #[salsa::invoke(query_definitions::expr_scopes)] + #[salsa::invoke(ExprScopes::expr_scopes_query)] fn expr_scopes(&self, func: Function) -> Arc; #[salsa::invoke(crate::adt::StructData::struct_data_query)] diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index 17a1efed2..f8b5ba581 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -9,7 +9,11 @@ use ra_syntax::{ }; use ra_arena::{Arena, RawId, impl_arena_id}; -use crate::{Name, AsName, expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySyntaxMapping}}; +use crate::{ + Name, AsName, Function, + expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySyntaxMapping}, + db::HirDatabase, +}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ScopeId(RawId); @@ -35,7 +39,14 @@ pub struct ScopeData { } impl ExprScopes { - pub(crate) fn new(body: Arc) -> ExprScopes { + // TODO: This should take something more general than Function + pub(crate) fn expr_scopes_query(db: &impl HirDatabase, function: Function) -> Arc { + let body = db.body_hir(function); + let res = ExprScopes::new(body); + Arc::new(res) + } + + fn new(body: Arc) -> ExprScopes { let mut scopes = ExprScopes { body: body.clone(), scopes: Arena::default(), diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index 1b61d449e..734a98282 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs @@ -1,19 +1,14 @@ use std::sync::Arc; -use ra_syntax::{SyntaxNode, TreeArc}; +use ra_syntax::{ + SyntaxNode, TreeArc, +}; use crate::{ SourceFileItems, SourceItemId, HirFileId, - Function, ExprScopes, db::HirDatabase, }; -pub(super) fn expr_scopes(db: &impl HirDatabase, func: Function) -> Arc { - let body = db.body_hir(func); - let res = ExprScopes::new(body); - Arc::new(res) -} - pub(super) fn file_items(db: &impl HirDatabase, file_id: HirFileId) -> Arc { let source_file = db.hir_parse(file_id); let res = SourceFileItems::new(file_id, &source_file); -- cgit v1.2.3