diff options
Diffstat (limited to 'crates/ra_ssr/src/resolving.rs')
-rw-r--r-- | crates/ra_ssr/src/resolving.rs | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/crates/ra_ssr/src/resolving.rs b/crates/ra_ssr/src/resolving.rs new file mode 100644 index 000000000..e9d052111 --- /dev/null +++ b/crates/ra_ssr/src/resolving.rs | |||
@@ -0,0 +1,153 @@ | |||
1 | //! This module is responsible for resolving paths within rules. | ||
2 | |||
3 | use crate::errors::error; | ||
4 | use crate::{parsing, SsrError}; | ||
5 | use parsing::Placeholder; | ||
6 | use ra_syntax::{ast, SmolStr, SyntaxKind, SyntaxNode, SyntaxToken}; | ||
7 | use rustc_hash::{FxHashMap, FxHashSet}; | ||
8 | use test_utils::mark; | ||
9 | |||
10 | pub(crate) struct ResolvedRule { | ||
11 | pub(crate) pattern: ResolvedPattern, | ||
12 | pub(crate) template: Option<ResolvedPattern>, | ||
13 | pub(crate) index: usize, | ||
14 | } | ||
15 | |||
16 | pub(crate) struct ResolvedPattern { | ||
17 | pub(crate) placeholders_by_stand_in: FxHashMap<SmolStr, parsing::Placeholder>, | ||
18 | pub(crate) node: SyntaxNode, | ||
19 | // Paths in `node` that we've resolved. | ||
20 | pub(crate) resolved_paths: FxHashMap<SyntaxNode, ResolvedPath>, | ||
21 | } | ||
22 | |||
23 | pub(crate) struct ResolvedPath { | ||
24 | pub(crate) resolution: hir::PathResolution, | ||
25 | } | ||
26 | |||
27 | impl ResolvedRule { | ||
28 | pub(crate) fn new( | ||
29 | rule: parsing::ParsedRule, | ||
30 | scope: &hir::SemanticsScope, | ||
31 | hygiene: &hir::Hygiene, | ||
32 | index: usize, | ||
33 | ) -> Result<ResolvedRule, SsrError> { | ||
34 | let resolver = | ||
35 | Resolver { scope, hygiene, placeholders_by_stand_in: rule.placeholders_by_stand_in }; | ||
36 | let resolved_template = if let Some(template) = rule.template { | ||
37 | Some(resolver.resolve_pattern_tree(template)?) | ||
38 | } else { | ||
39 | None | ||
40 | }; | ||
41 | Ok(ResolvedRule { | ||
42 | pattern: resolver.resolve_pattern_tree(rule.pattern)?, | ||
43 | template: resolved_template, | ||
44 | index, | ||
45 | }) | ||
46 | } | ||
47 | |||
48 | pub(crate) fn get_placeholder(&self, token: &SyntaxToken) -> Option<&Placeholder> { | ||
49 | if token.kind() != SyntaxKind::IDENT { | ||
50 | return None; | ||
51 | } | ||
52 | self.pattern.placeholders_by_stand_in.get(token.text()) | ||
53 | } | ||
54 | } | ||
55 | |||
56 | struct Resolver<'a, 'db> { | ||
57 | scope: &'a hir::SemanticsScope<'db>, | ||
58 | hygiene: &'a hir::Hygiene, | ||
59 | placeholders_by_stand_in: FxHashMap<SmolStr, parsing::Placeholder>, | ||
60 | } | ||
61 | |||
62 | impl Resolver<'_, '_> { | ||
63 | fn resolve_pattern_tree(&self, pattern: SyntaxNode) -> Result<ResolvedPattern, SsrError> { | ||
64 | let mut resolved_paths = FxHashMap::default(); | ||
65 | self.resolve(pattern.clone(), &mut resolved_paths)?; | ||
66 | Ok(ResolvedPattern { | ||
67 | node: pattern, | ||
68 | resolved_paths, | ||
69 | placeholders_by_stand_in: self.placeholders_by_stand_in.clone(), | ||
70 | }) | ||
71 | } | ||
72 | |||
73 | fn resolve( | ||
74 | &self, | ||
75 | node: SyntaxNode, | ||
76 | resolved_paths: &mut FxHashMap<SyntaxNode, ResolvedPath>, | ||
77 | ) -> Result<(), SsrError> { | ||
78 | use ra_syntax::ast::AstNode; | ||
79 | if let Some(path) = ast::Path::cast(node.clone()) { | ||
80 | // Check if this is an appropriate place in the path to resolve. If the path is | ||
81 | // something like `a::B::<i32>::c` then we want to resolve `a::B`. If the path contains | ||
82 | // a placeholder. e.g. `a::$b::c` then we want to resolve `a`. | ||
83 | if !path_contains_type_arguments(path.qualifier()) | ||
84 | && !self.path_contains_placeholder(&path) | ||
85 | { | ||
86 | let resolution = self | ||
87 | .resolve_path(&path) | ||
88 | .ok_or_else(|| error!("Failed to resolve path `{}`", node.text()))?; | ||
89 | resolved_paths.insert(node, ResolvedPath { resolution }); | ||
90 | return Ok(()); | ||
91 | } | ||
92 | } | ||
93 | for node in node.children() { | ||
94 | self.resolve(node, resolved_paths)?; | ||
95 | } | ||
96 | Ok(()) | ||
97 | } | ||
98 | |||
99 | /// Returns whether `path` contains a placeholder, but ignores any placeholders within type | ||
100 | /// arguments. | ||
101 | fn path_contains_placeholder(&self, path: &ast::Path) -> bool { | ||
102 | if let Some(segment) = path.segment() { | ||
103 | if let Some(name_ref) = segment.name_ref() { | ||
104 | if self.placeholders_by_stand_in.contains_key(name_ref.text()) { | ||
105 | return true; | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | if let Some(qualifier) = path.qualifier() { | ||
110 | return self.path_contains_placeholder(&qualifier); | ||
111 | } | ||
112 | false | ||
113 | } | ||
114 | |||
115 | fn resolve_path(&self, path: &ast::Path) -> Option<hir::PathResolution> { | ||
116 | let hir_path = hir::Path::from_src(path.clone(), self.hygiene)?; | ||
117 | // First try resolving the whole path. This will work for things like | ||
118 | // `std::collections::HashMap`, but will fail for things like | ||
119 | // `std::collections::HashMap::new`. | ||
120 | if let Some(resolution) = self.scope.resolve_hir_path(&hir_path) { | ||
121 | return Some(resolution); | ||
122 | } | ||
123 | // Resolution failed, try resolving the qualifier (e.g. `std::collections::HashMap` and if | ||
124 | // that succeeds, then iterate through the candidates on the resolved type with the provided | ||
125 | // name. | ||
126 | let resolved_qualifier = self.scope.resolve_hir_path_qualifier(&hir_path.qualifier()?)?; | ||
127 | if let hir::PathResolution::Def(hir::ModuleDef::Adt(adt)) = resolved_qualifier { | ||
128 | adt.ty(self.scope.db).iterate_path_candidates( | ||
129 | self.scope.db, | ||
130 | self.scope.module()?.krate(), | ||
131 | &FxHashSet::default(), | ||
132 | Some(hir_path.segments().last()?.name), | ||
133 | |_ty, assoc_item| Some(hir::PathResolution::AssocItem(assoc_item)), | ||
134 | ) | ||
135 | } else { | ||
136 | None | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | |||
141 | /// Returns whether `path` or any of its qualifiers contains type arguments. | ||
142 | fn path_contains_type_arguments(path: Option<ast::Path>) -> bool { | ||
143 | if let Some(path) = path { | ||
144 | if let Some(segment) = path.segment() { | ||
145 | if segment.type_arg_list().is_some() { | ||
146 | mark::hit!(type_arguments_within_path); | ||
147 | return true; | ||
148 | } | ||
149 | } | ||
150 | return path_contains_type_arguments(path.qualifier()); | ||
151 | } | ||
152 | false | ||
153 | } | ||