diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-14 08:56:48 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-14 08:56:48 +0000 |
commit | 5c3ccc55082838524f695ffe40138fd8e805db70 (patch) | |
tree | fe730c94b290f2004432908653941a4a8cd4506a /crates/ra_hir/src/expr | |
parent | 267f194c28940e2f8d8748e4708aa1c4a4a13e6f (diff) | |
parent | f924ae3b86dc5e978071b6f8308b9f357415780b (diff) |
Merge #2240
2240: Move scopes to hir_def r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/expr')
-rw-r--r-- | crates/ra_hir/src/expr/scope.rs | 353 |
1 files changed, 0 insertions, 353 deletions
diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs deleted file mode 100644 index afba66069..000000000 --- a/crates/ra_hir/src/expr/scope.rs +++ /dev/null | |||
@@ -1,353 +0,0 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use std::sync::Arc; | ||
4 | |||
5 | use ra_arena::{impl_arena_id, Arena, RawId}; | ||
6 | use rustc_hash::FxHashMap; | ||
7 | |||
8 | use crate::{ | ||
9 | db::HirDatabase, | ||
10 | expr::{Body, Expr, ExprId, Pat, PatId, Statement}, | ||
11 | DefWithBody, Name, | ||
12 | }; | ||
13 | |||
14 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
15 | pub struct ScopeId(RawId); | ||
16 | impl_arena_id!(ScopeId); | ||
17 | |||
18 | #[derive(Debug, PartialEq, Eq)] | ||
19 | pub struct ExprScopes { | ||
20 | scopes: Arena<ScopeId, ScopeData>, | ||
21 | scope_by_expr: FxHashMap<ExprId, ScopeId>, | ||
22 | } | ||
23 | |||
24 | #[derive(Debug, PartialEq, Eq)] | ||
25 | pub(crate) struct ScopeEntry { | ||
26 | name: Name, | ||
27 | pat: PatId, | ||
28 | } | ||
29 | |||
30 | impl ScopeEntry { | ||
31 | pub(crate) fn name(&self) -> &Name { | ||
32 | &self.name | ||
33 | } | ||
34 | |||
35 | pub(crate) fn pat(&self) -> PatId { | ||
36 | self.pat | ||
37 | } | ||
38 | } | ||
39 | |||
40 | #[derive(Debug, PartialEq, Eq)] | ||
41 | pub(crate) struct ScopeData { | ||
42 | parent: Option<ScopeId>, | ||
43 | entries: Vec<ScopeEntry>, | ||
44 | } | ||
45 | |||
46 | impl ExprScopes { | ||
47 | pub(crate) fn expr_scopes_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<ExprScopes> { | ||
48 | let body = db.body(def); | ||
49 | let res = ExprScopes::new(&*body); | ||
50 | Arc::new(res) | ||
51 | } | ||
52 | |||
53 | fn new(body: &Body) -> ExprScopes { | ||
54 | let mut scopes = | ||
55 | ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() }; | ||
56 | let root = scopes.root_scope(); | ||
57 | scopes.add_params_bindings(body, root, body.params()); | ||
58 | compute_expr_scopes(body.body_expr(), body, &mut scopes, root); | ||
59 | scopes | ||
60 | } | ||
61 | |||
62 | pub(crate) fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { | ||
63 | &self.scopes[scope].entries | ||
64 | } | ||
65 | |||
66 | pub(crate) fn scope_chain(&self, scope: Option<ScopeId>) -> impl Iterator<Item = ScopeId> + '_ { | ||
67 | std::iter::successors(scope, move |&scope| self.scopes[scope].parent) | ||
68 | } | ||
69 | |||
70 | pub(crate) fn scope_for(&self, expr: ExprId) -> Option<ScopeId> { | ||
71 | self.scope_by_expr.get(&expr).copied() | ||
72 | } | ||
73 | |||
74 | pub(crate) fn scope_by_expr(&self) -> &FxHashMap<ExprId, ScopeId> { | ||
75 | &self.scope_by_expr | ||
76 | } | ||
77 | |||
78 | fn root_scope(&mut self) -> ScopeId { | ||
79 | self.scopes.alloc(ScopeData { parent: None, entries: vec![] }) | ||
80 | } | ||
81 | |||
82 | fn new_scope(&mut self, parent: ScopeId) -> ScopeId { | ||
83 | self.scopes.alloc(ScopeData { parent: Some(parent), entries: vec![] }) | ||
84 | } | ||
85 | |||
86 | fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) { | ||
87 | match &body[pat] { | ||
88 | Pat::Bind { name, .. } => { | ||
89 | // bind can have a sub pattern, but it's actually not allowed | ||
90 | // to bind to things in there | ||
91 | let entry = ScopeEntry { name: name.clone(), pat }; | ||
92 | self.scopes[scope].entries.push(entry) | ||
93 | } | ||
94 | p => p.walk_child_pats(|pat| self.add_bindings(body, scope, pat)), | ||
95 | } | ||
96 | } | ||
97 | |||
98 | fn add_params_bindings(&mut self, body: &Body, scope: ScopeId, params: &[PatId]) { | ||
99 | params.iter().for_each(|pat| self.add_bindings(body, scope, *pat)); | ||
100 | } | ||
101 | |||
102 | fn set_scope(&mut self, node: ExprId, scope: ScopeId) { | ||
103 | self.scope_by_expr.insert(node, scope); | ||
104 | } | ||
105 | } | ||
106 | |||
107 | fn compute_block_scopes( | ||
108 | statements: &[Statement], | ||
109 | tail: Option<ExprId>, | ||
110 | body: &Body, | ||
111 | scopes: &mut ExprScopes, | ||
112 | mut scope: ScopeId, | ||
113 | ) { | ||
114 | for stmt in statements { | ||
115 | match stmt { | ||
116 | Statement::Let { pat, initializer, .. } => { | ||
117 | if let Some(expr) = initializer { | ||
118 | scopes.set_scope(*expr, scope); | ||
119 | compute_expr_scopes(*expr, body, scopes, scope); | ||
120 | } | ||
121 | scope = scopes.new_scope(scope); | ||
122 | scopes.add_bindings(body, scope, *pat); | ||
123 | } | ||
124 | Statement::Expr(expr) => { | ||
125 | scopes.set_scope(*expr, scope); | ||
126 | compute_expr_scopes(*expr, body, scopes, scope); | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | if let Some(expr) = tail { | ||
131 | compute_expr_scopes(expr, body, scopes, scope); | ||
132 | } | ||
133 | } | ||
134 | |||
135 | fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope: ScopeId) { | ||
136 | scopes.set_scope(expr, scope); | ||
137 | match &body[expr] { | ||
138 | Expr::Block { statements, tail } => { | ||
139 | compute_block_scopes(&statements, *tail, body, scopes, scope); | ||
140 | } | ||
141 | Expr::For { iterable, pat, body: body_expr } => { | ||
142 | compute_expr_scopes(*iterable, body, scopes, scope); | ||
143 | let scope = scopes.new_scope(scope); | ||
144 | scopes.add_bindings(body, scope, *pat); | ||
145 | compute_expr_scopes(*body_expr, body, scopes, scope); | ||
146 | } | ||
147 | Expr::Lambda { args, body: body_expr, .. } => { | ||
148 | let scope = scopes.new_scope(scope); | ||
149 | scopes.add_params_bindings(body, scope, &args); | ||
150 | compute_expr_scopes(*body_expr, body, scopes, scope); | ||
151 | } | ||
152 | Expr::Match { expr, arms } => { | ||
153 | compute_expr_scopes(*expr, body, scopes, scope); | ||
154 | for arm in arms { | ||
155 | let scope = scopes.new_scope(scope); | ||
156 | for pat in &arm.pats { | ||
157 | scopes.add_bindings(body, scope, *pat); | ||
158 | } | ||
159 | scopes.set_scope(arm.expr, scope); | ||
160 | compute_expr_scopes(arm.expr, body, scopes, scope); | ||
161 | } | ||
162 | } | ||
163 | e => e.walk_child_exprs(|e| compute_expr_scopes(e, body, scopes, scope)), | ||
164 | }; | ||
165 | } | ||
166 | |||
167 | #[cfg(test)] | ||
168 | mod tests { | ||
169 | use hir_expand::Source; | ||
170 | use ra_db::{fixture::WithFixture, SourceDatabase}; | ||
171 | use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; | ||
172 | use test_utils::{assert_eq_text, extract_offset}; | ||
173 | |||
174 | use crate::{source_binder::SourceAnalyzer, test_db::TestDB}; | ||
175 | |||
176 | fn do_check(code: &str, expected: &[&str]) { | ||
177 | let (off, code) = extract_offset(code); | ||
178 | let code = { | ||
179 | let mut buf = String::new(); | ||
180 | let off = u32::from(off) as usize; | ||
181 | buf.push_str(&code[..off]); | ||
182 | buf.push_str("marker"); | ||
183 | buf.push_str(&code[off..]); | ||
184 | buf | ||
185 | }; | ||
186 | |||
187 | let (db, file_id) = TestDB::with_single_file(&code); | ||
188 | let file = db.parse(file_id).ok().unwrap(); | ||
189 | let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); | ||
190 | let analyzer = SourceAnalyzer::new(&db, file_id, marker.syntax(), None); | ||
191 | |||
192 | let scopes = analyzer.scopes(); | ||
193 | let expr_id = analyzer | ||
194 | .body_source_map() | ||
195 | .node_expr(Source { file_id: file_id.into(), ast: &marker.into() }) | ||
196 | .unwrap(); | ||
197 | let scope = scopes.scope_for(expr_id); | ||
198 | |||
199 | let actual = scopes | ||
200 | .scope_chain(scope) | ||
201 | .flat_map(|scope| scopes.entries(scope)) | ||
202 | .map(|it| it.name().to_string()) | ||
203 | .collect::<Vec<_>>() | ||
204 | .join("\n"); | ||
205 | let expected = expected.join("\n"); | ||
206 | assert_eq_text!(&expected, &actual); | ||
207 | } | ||
208 | |||
209 | #[test] | ||
210 | fn test_lambda_scope() { | ||
211 | do_check( | ||
212 | r" | ||
213 | fn quux(foo: i32) { | ||
214 | let f = |bar, baz: i32| { | ||
215 | <|> | ||
216 | }; | ||
217 | }", | ||
218 | &["bar", "baz", "foo"], | ||
219 | ); | ||
220 | } | ||
221 | |||
222 | #[test] | ||
223 | fn test_call_scope() { | ||
224 | do_check( | ||
225 | r" | ||
226 | fn quux() { | ||
227 | f(|x| <|> ); | ||
228 | }", | ||
229 | &["x"], | ||
230 | ); | ||
231 | } | ||
232 | |||
233 | #[test] | ||
234 | fn test_method_call_scope() { | ||
235 | do_check( | ||
236 | r" | ||
237 | fn quux() { | ||
238 | z.f(|x| <|> ); | ||
239 | }", | ||
240 | &["x"], | ||
241 | ); | ||
242 | } | ||
243 | |||
244 | #[test] | ||
245 | fn test_loop_scope() { | ||
246 | do_check( | ||
247 | r" | ||
248 | fn quux() { | ||
249 | loop { | ||
250 | let x = (); | ||
251 | <|> | ||
252 | }; | ||
253 | }", | ||
254 | &["x"], | ||
255 | ); | ||
256 | } | ||
257 | |||
258 | #[test] | ||
259 | fn test_match() { | ||
260 | do_check( | ||
261 | r" | ||
262 | fn quux() { | ||
263 | match () { | ||
264 | Some(x) => { | ||
265 | <|> | ||
266 | } | ||
267 | }; | ||
268 | }", | ||
269 | &["x"], | ||
270 | ); | ||
271 | } | ||
272 | |||
273 | #[test] | ||
274 | fn test_shadow_variable() { | ||
275 | do_check( | ||
276 | r" | ||
277 | fn foo(x: String) { | ||
278 | let x : &str = &x<|>; | ||
279 | }", | ||
280 | &["x"], | ||
281 | ); | ||
282 | } | ||
283 | |||
284 | fn do_check_local_name(code: &str, expected_offset: u32) { | ||
285 | let (off, code) = extract_offset(code); | ||
286 | |||
287 | let (db, file_id) = TestDB::with_single_file(&code); | ||
288 | let file = db.parse(file_id).ok().unwrap(); | ||
289 | let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()) | ||
290 | .expect("failed to find a name at the target offset"); | ||
291 | let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); | ||
292 | let analyzer = SourceAnalyzer::new(&db, file_id, name_ref.syntax(), None); | ||
293 | |||
294 | let local_name_entry = analyzer.resolve_local_name(&name_ref).unwrap(); | ||
295 | let local_name = | ||
296 | local_name_entry.ptr().either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr()); | ||
297 | assert_eq!(local_name.range(), expected_name.syntax().text_range()); | ||
298 | } | ||
299 | |||
300 | #[test] | ||
301 | fn test_resolve_local_name() { | ||
302 | do_check_local_name( | ||
303 | r#" | ||
304 | fn foo(x: i32, y: u32) { | ||
305 | { | ||
306 | let z = x * 2; | ||
307 | } | ||
308 | { | ||
309 | let t = x<|> * 3; | ||
310 | } | ||
311 | }"#, | ||
312 | 21, | ||
313 | ); | ||
314 | } | ||
315 | |||
316 | #[test] | ||
317 | fn test_resolve_local_name_declaration() { | ||
318 | do_check_local_name( | ||
319 | r#" | ||
320 | fn foo(x: String) { | ||
321 | let x : &str = &x<|>; | ||
322 | }"#, | ||
323 | 21, | ||
324 | ); | ||
325 | } | ||
326 | |||
327 | #[test] | ||
328 | fn test_resolve_local_name_shadow() { | ||
329 | do_check_local_name( | ||
330 | r" | ||
331 | fn foo(x: String) { | ||
332 | let x : &str = &x; | ||
333 | x<|> | ||
334 | } | ||
335 | ", | ||
336 | 53, | ||
337 | ); | ||
338 | } | ||
339 | |||
340 | #[test] | ||
341 | fn ref_patterns_contribute_bindings() { | ||
342 | do_check_local_name( | ||
343 | r" | ||
344 | fn foo() { | ||
345 | if let Some(&from) = bar() { | ||
346 | from<|>; | ||
347 | } | ||
348 | } | ||
349 | ", | ||
350 | 53, | ||
351 | ); | ||
352 | } | ||
353 | } | ||