aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lattimore <[email protected]>2020-08-01 08:41:42 +0100
committerDavid Lattimore <[email protected]>2020-08-01 08:41:42 +0100
commit6bbeffc8c56893548f5667844f59ce5a76f9fd98 (patch)
tree446162072916f99a3c9b4c5e1734fbcc56cbd3f1
parent5af32aeb2b83c7ae8adf3e088bf4f3691aa45eb1 (diff)
SSR: Allow `self` in patterns.
It's now consistent with other variables in that if the pattern references self, only the `self` in scope where the rule is invoked will be accepted. Since `self` doesn't work the same as other paths, this is implemented by restricting the search to just the current function. Prior to this change (since path resolution was implemented), having self in a pattern would just result in no matches.
-rw-r--r--crates/ra_ssr/src/lib.rs7
-rw-r--r--crates/ra_ssr/src/resolving.rs29
-rw-r--r--crates/ra_ssr/src/search.rs9
-rw-r--r--crates/ra_ssr/src/tests.rs35
4 files changed, 74 insertions, 6 deletions
diff --git a/crates/ra_ssr/src/lib.rs b/crates/ra_ssr/src/lib.rs
index 73abfecb2..c780b460a 100644
--- a/crates/ra_ssr/src/lib.rs
+++ b/crates/ra_ssr/src/lib.rs
@@ -66,12 +66,7 @@ impl<'db> MatchFinder<'db> {
66 restrict_ranges.retain(|range| !range.range.is_empty()); 66 restrict_ranges.retain(|range| !range.range.is_empty());
67 let sema = Semantics::new(db); 67 let sema = Semantics::new(db);
68 let resolution_scope = resolving::ResolutionScope::new(&sema, lookup_context); 68 let resolution_scope = resolving::ResolutionScope::new(&sema, lookup_context);
69 MatchFinder { 69 MatchFinder { sema, rules: Vec::new(), resolution_scope, restrict_ranges }
70 sema: Semantics::new(db),
71 rules: Vec::new(),
72 resolution_scope,
73 restrict_ranges,
74 }
75 } 70 }
76 71
77 /// Constructs an instance using the start of the first file in `db` as the lookup context. 72 /// Constructs an instance using the start of the first file in `db` as the lookup context.
diff --git a/crates/ra_ssr/src/resolving.rs b/crates/ra_ssr/src/resolving.rs
index 6f62000f4..0ac929bd5 100644
--- a/crates/ra_ssr/src/resolving.rs
+++ b/crates/ra_ssr/src/resolving.rs
@@ -11,6 +11,7 @@ use test_utils::mark;
11pub(crate) struct ResolutionScope<'db> { 11pub(crate) struct ResolutionScope<'db> {
12 scope: hir::SemanticsScope<'db>, 12 scope: hir::SemanticsScope<'db>,
13 hygiene: hir::Hygiene, 13 hygiene: hir::Hygiene,
14 node: SyntaxNode,
14} 15}
15 16
16pub(crate) struct ResolvedRule { 17pub(crate) struct ResolvedRule {
@@ -25,6 +26,7 @@ pub(crate) struct ResolvedPattern {
25 // Paths in `node` that we've resolved. 26 // Paths in `node` that we've resolved.
26 pub(crate) resolved_paths: FxHashMap<SyntaxNode, ResolvedPath>, 27 pub(crate) resolved_paths: FxHashMap<SyntaxNode, ResolvedPath>,
27 pub(crate) ufcs_function_calls: FxHashMap<SyntaxNode, hir::Function>, 28 pub(crate) ufcs_function_calls: FxHashMap<SyntaxNode, hir::Function>,
29 pub(crate) contains_self: bool,
28} 30}
29 31
30pub(crate) struct ResolvedPath { 32pub(crate) struct ResolvedPath {
@@ -68,6 +70,7 @@ struct Resolver<'a, 'db> {
68 70
69impl Resolver<'_, '_> { 71impl Resolver<'_, '_> {
70 fn resolve_pattern_tree(&self, pattern: SyntaxNode) -> Result<ResolvedPattern, SsrError> { 72 fn resolve_pattern_tree(&self, pattern: SyntaxNode) -> Result<ResolvedPattern, SsrError> {
73 use ra_syntax::{SyntaxElement, T};
71 let mut resolved_paths = FxHashMap::default(); 74 let mut resolved_paths = FxHashMap::default();
72 self.resolve(pattern.clone(), 0, &mut resolved_paths)?; 75 self.resolve(pattern.clone(), 0, &mut resolved_paths)?;
73 let ufcs_function_calls = resolved_paths 76 let ufcs_function_calls = resolved_paths
@@ -85,11 +88,17 @@ impl Resolver<'_, '_> {
85 None 88 None
86 }) 89 })
87 .collect(); 90 .collect();
91 let contains_self =
92 pattern.descendants_with_tokens().any(|node_or_token| match node_or_token {
93 SyntaxElement::Token(t) => t.kind() == T![self],
94 _ => false,
95 });
88 Ok(ResolvedPattern { 96 Ok(ResolvedPattern {
89 node: pattern, 97 node: pattern,
90 resolved_paths, 98 resolved_paths,
91 placeholders_by_stand_in: self.placeholders_by_stand_in.clone(), 99 placeholders_by_stand_in: self.placeholders_by_stand_in.clone(),
92 ufcs_function_calls, 100 ufcs_function_calls,
101 contains_self,
93 }) 102 })
94 } 103 }
95 104
@@ -101,6 +110,10 @@ impl Resolver<'_, '_> {
101 ) -> Result<(), SsrError> { 110 ) -> Result<(), SsrError> {
102 use ra_syntax::ast::AstNode; 111 use ra_syntax::ast::AstNode;
103 if let Some(path) = ast::Path::cast(node.clone()) { 112 if let Some(path) = ast::Path::cast(node.clone()) {
113 if is_self(&path) {
114 // Self cannot be resolved like other paths.
115 return Ok(());
116 }
104 // Check if this is an appropriate place in the path to resolve. If the path is 117 // Check if this is an appropriate place in the path to resolve. If the path is
105 // something like `a::B::<i32>::c` then we want to resolve `a::B`. If the path contains 118 // something like `a::B::<i32>::c` then we want to resolve `a::B`. If the path contains
106 // a placeholder. e.g. `a::$b::c` then we want to resolve `a`. 119 // a placeholder. e.g. `a::$b::c` then we want to resolve `a`.
@@ -157,6 +170,18 @@ impl<'db> ResolutionScope<'db> {
157 ResolutionScope { 170 ResolutionScope {
158 scope, 171 scope,
159 hygiene: hir::Hygiene::new(sema.db, resolve_context.file_id.into()), 172 hygiene: hir::Hygiene::new(sema.db, resolve_context.file_id.into()),
173 node,
174 }
175 }
176
177 /// Returns the function in which SSR was invoked, if any.
178 pub(crate) fn current_function(&self) -> Option<SyntaxNode> {
179 let mut node = self.node.clone();
180 loop {
181 if node.kind() == SyntaxKind::FN {
182 return Some(node);
183 }
184 node = node.parent()?;
160 } 185 }
161 } 186 }
162 187
@@ -186,6 +211,10 @@ impl<'db> ResolutionScope<'db> {
186 } 211 }
187} 212}
188 213
214fn is_self(path: &ast::Path) -> bool {
215 path.segment().map(|segment| segment.self_token().is_some()).unwrap_or(false)
216}
217
189/// Returns a suitable node for resolving paths in the current scope. If we create a scope based on 218/// Returns a suitable node for resolving paths in the current scope. If we create a scope based on
190/// a statement node, then we can't resolve local variables that were defined in the current scope 219/// a statement node, then we can't resolve local variables that were defined in the current scope
191/// (only in parent scopes). So we find another node, ideally a child of the statement where local 220/// (only in parent scopes). So we find another node, ideally a child of the statement where local
diff --git a/crates/ra_ssr/src/search.rs b/crates/ra_ssr/src/search.rs
index 213dc494f..85ffa2ac2 100644
--- a/crates/ra_ssr/src/search.rs
+++ b/crates/ra_ssr/src/search.rs
@@ -33,6 +33,15 @@ impl<'db> MatchFinder<'db> {
33 usage_cache: &mut UsageCache, 33 usage_cache: &mut UsageCache,
34 matches_out: &mut Vec<Match>, 34 matches_out: &mut Vec<Match>,
35 ) { 35 ) {
36 if rule.pattern.contains_self {
37 // If the pattern contains `self` we restrict the scope of the search to just the
38 // current method. No other method can reference the same `self`. This makes the
39 // behavior of `self` consistent with other variables.
40 if let Some(current_function) = self.resolution_scope.current_function() {
41 self.slow_scan_node(&current_function, rule, &None, matches_out);
42 }
43 return;
44 }
36 if pick_path_for_usages(&rule.pattern).is_none() { 45 if pick_path_for_usages(&rule.pattern).is_none() {
37 self.slow_scan(rule, matches_out); 46 self.slow_scan(rule, matches_out);
38 return; 47 return;
diff --git a/crates/ra_ssr/src/tests.rs b/crates/ra_ssr/src/tests.rs
index 2ae03c64c..d483640df 100644
--- a/crates/ra_ssr/src/tests.rs
+++ b/crates/ra_ssr/src/tests.rs
@@ -1044,3 +1044,38 @@ fn replace_nonpath_within_selection() {
1044 }"#]], 1044 }"#]],
1045 ); 1045 );
1046} 1046}
1047
1048#[test]
1049fn replace_self() {
1050 // `foo(self)` occurs twice in the code, however only the first occurrence is the `self` that's
1051 // in scope where the rule is invoked.
1052 assert_ssr_transform(
1053 "foo(self) ==>> bar(self)",
1054 r#"
1055 struct S1 {}
1056 fn foo(_: &S1) {}
1057 fn bar(_: &S1) {}
1058 impl S1 {
1059 fn f1(&self) {
1060 foo(self)<|>
1061 }
1062 fn f2(&self) {
1063 foo(self)
1064 }
1065 }
1066 "#,
1067 expect![[r#"
1068 struct S1 {}
1069 fn foo(_: &S1) {}
1070 fn bar(_: &S1) {}
1071 impl S1 {
1072 fn f1(&self) {
1073 bar(self)
1074 }
1075 fn f2(&self) {
1076 foo(self)
1077 }
1078 }
1079 "#]],
1080 );
1081}