aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/body
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src/body')
-rw-r--r--crates/hir_def/src/body/scope.rs22
-rw-r--r--crates/hir_def/src/body/tests.rs104
-rw-r--r--crates/hir_def/src/body/tests/block.rs27
3 files changed, 49 insertions, 104 deletions
diff --git a/crates/hir_def/src/body/scope.rs b/crates/hir_def/src/body/scope.rs
index 49f1427b4..210b4a617 100644
--- a/crates/hir_def/src/body/scope.rs
+++ b/crates/hir_def/src/body/scope.rs
@@ -9,7 +9,7 @@ use crate::{
9 body::Body, 9 body::Body,
10 db::DefDatabase, 10 db::DefDatabase,
11 expr::{Expr, ExprId, Pat, PatId, Statement}, 11 expr::{Expr, ExprId, Pat, PatId, Statement},
12 DefWithBodyId, 12 BlockId, DefWithBodyId,
13}; 13};
14 14
15pub type ScopeId = Idx<ScopeData>; 15pub type ScopeId = Idx<ScopeData>;
@@ -39,6 +39,7 @@ impl ScopeEntry {
39#[derive(Debug, PartialEq, Eq)] 39#[derive(Debug, PartialEq, Eq)]
40pub struct ScopeData { 40pub struct ScopeData {
41 parent: Option<ScopeId>, 41 parent: Option<ScopeId>,
42 block: Option<BlockId>,
42 entries: Vec<ScopeEntry>, 43 entries: Vec<ScopeEntry>,
43} 44}
44 45
@@ -61,6 +62,11 @@ impl ExprScopes {
61 &self.scopes[scope].entries 62 &self.scopes[scope].entries
62 } 63 }
63 64
65 /// If `scope` refers to a block expression scope, returns the corresponding `BlockId`.
66 pub fn block(&self, scope: ScopeId) -> Option<BlockId> {
67 self.scopes[scope].block
68 }
69
64 pub fn scope_chain(&self, scope: Option<ScopeId>) -> impl Iterator<Item = ScopeId> + '_ { 70 pub fn scope_chain(&self, scope: Option<ScopeId>) -> impl Iterator<Item = ScopeId> + '_ {
65 std::iter::successors(scope, move |&scope| self.scopes[scope].parent) 71 std::iter::successors(scope, move |&scope| self.scopes[scope].parent)
66 } 72 }
@@ -79,11 +85,15 @@ impl ExprScopes {
79 } 85 }
80 86
81 fn root_scope(&mut self) -> ScopeId { 87 fn root_scope(&mut self) -> ScopeId {
82 self.scopes.alloc(ScopeData { parent: None, entries: vec![] }) 88 self.scopes.alloc(ScopeData { parent: None, block: None, entries: vec![] })
83 } 89 }
84 90
85 fn new_scope(&mut self, parent: ScopeId) -> ScopeId { 91 fn new_scope(&mut self, parent: ScopeId) -> ScopeId {
86 self.scopes.alloc(ScopeData { parent: Some(parent), entries: vec![] }) 92 self.scopes.alloc(ScopeData { parent: Some(parent), block: None, entries: vec![] })
93 }
94
95 fn new_block_scope(&mut self, parent: ScopeId, block: BlockId) -> ScopeId {
96 self.scopes.alloc(ScopeData { parent: Some(parent), block: Some(block), entries: vec![] })
87 } 97 }
88 98
89 fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) { 99 fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) {
@@ -136,7 +146,11 @@ fn compute_block_scopes(
136fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope: ScopeId) { 146fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope: ScopeId) {
137 scopes.set_scope(expr, scope); 147 scopes.set_scope(expr, scope);
138 match &body[expr] { 148 match &body[expr] {
139 Expr::Block { statements, tail, .. } => { 149 Expr::Block { statements, tail, id, .. } => {
150 let scope = scopes.new_block_scope(scope, *id);
151 // Overwrite the old scope for the block expr, so that every block scope can be found
152 // via the block itself (important for blocks that only contain items, no expressions).
153 scopes.set_scope(expr, scope);
140 compute_block_scopes(&statements, *tail, body, scopes, scope); 154 compute_block_scopes(&statements, *tail, body, scopes, scope);
141 } 155 }
142 Expr::For { iterable, pat, body: body_expr, .. } => { 156 Expr::For { iterable, pat, body: body_expr, .. } => {
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs
index a92134ba7..bb43569d7 100644
--- a/crates/hir_def/src/body/tests.rs
+++ b/crates/hir_def/src/body/tests.rs
@@ -1,10 +1,10 @@
1mod block; 1mod block;
2 2
3use base_db::{fixture::WithFixture, FilePosition, SourceDatabase}; 3use base_db::{fixture::WithFixture, SourceDatabase};
4use expect_test::Expect; 4use expect_test::Expect;
5use test_utils::mark; 5use test_utils::mark;
6 6
7use crate::{test_db::TestDB, BlockId, ModuleDefId}; 7use crate::{test_db::TestDB, ModuleDefId};
8 8
9use super::*; 9use super::*;
10 10
@@ -37,104 +37,8 @@ fn check_diagnostics(ra_fixture: &str) {
37fn block_def_map_at(ra_fixture: &str) -> String { 37fn block_def_map_at(ra_fixture: &str) -> String {
38 let (db, position) = crate::test_db::TestDB::with_position(ra_fixture); 38 let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
39 39
40 let krate = db.crate_graph().iter().next().unwrap(); 40 let module = db.module_at_position(position);
41 let def_map = db.crate_def_map(krate); 41 module.def_map(&db).dump(&db)
42
43 let mut block =
44 block_at_pos(&db, &def_map, position).expect("couldn't find enclosing function or block");
45 loop {
46 let def_map = db.block_def_map(block).unwrap_or_else(|| def_map.clone());
47 let new_block = block_at_pos(&db, &def_map, position);
48 match new_block {
49 Some(new_block) => {
50 assert_ne!(block, new_block);
51 block = new_block;
52 }
53 None => {
54 return def_map.dump(&db);
55 }
56 }
57 }
58}
59
60fn block_at_pos(db: &dyn DefDatabase, def_map: &DefMap, position: FilePosition) -> Option<BlockId> {
61 // Find the smallest (innermost) function containing the cursor.
62 let mut size = None;
63 let mut fn_def = None;
64 for (_, module) in def_map.modules() {
65 let file_id = module.definition_source(db).file_id;
66 if file_id != position.file_id.into() {
67 continue;
68 }
69 let root = db.parse_or_expand(file_id).unwrap();
70 let ast_map = db.ast_id_map(file_id);
71 let item_tree = db.item_tree(file_id);
72 for decl in module.scope.declarations() {
73 if let ModuleDefId::FunctionId(it) = decl {
74 let ast = ast_map.get(item_tree[it.lookup(db).id.value].ast_id).to_node(&root);
75 let range = ast.syntax().text_range();
76
77 if !range.contains(position.offset) {
78 continue;
79 }
80
81 let new_size = match size {
82 None => range.len(),
83 Some(size) => {
84 if range.len() < size {
85 range.len()
86 } else {
87 size
88 }
89 }
90 };
91 if size != Some(new_size) {
92 size = Some(new_size);
93 fn_def = Some(it);
94 }
95 }
96 }
97 }
98
99 let (body, source_map) = db.body_with_source_map(fn_def?.into());
100
101 // Now find the smallest encompassing block expression in the function body.
102 let mut size = None;
103 let mut block_id = None;
104 for (expr_id, expr) in body.exprs.iter() {
105 if let Expr::Block { id, .. } = expr {
106 if let Ok(ast) = source_map.expr_syntax(expr_id) {
107 if ast.file_id != position.file_id.into() {
108 continue;
109 }
110
111 let root = db.parse_or_expand(ast.file_id).unwrap();
112 let ast = ast.value.to_node(&root);
113 let range = ast.syntax().text_range();
114
115 if !range.contains(position.offset) {
116 continue;
117 }
118
119 let new_size = match size {
120 None => range.len(),
121 Some(size) => {
122 if range.len() < size {
123 range.len()
124 } else {
125 size
126 }
127 }
128 };
129 if size != Some(new_size) {
130 size = Some(new_size);
131 block_id = Some(*id);
132 }
133 }
134 }
135 }
136
137 Some(block_id.expect("can't find block containing cursor"))
138} 42}
139 43
140fn check_at(ra_fixture: &str, expect: Expect) { 44fn check_at(ra_fixture: &str, expect: Expect) {
diff --git a/crates/hir_def/src/body/tests/block.rs b/crates/hir_def/src/body/tests/block.rs
index b599c6269..a5ec0883f 100644
--- a/crates/hir_def/src/body/tests/block.rs
+++ b/crates/hir_def/src/body/tests/block.rs
@@ -232,3 +232,30 @@ fn f() {
232 "#]], 232 "#]],
233 ) 233 )
234} 234}
235
236#[test]
237fn super_does_not_resolve_to_block_module() {
238 check_at(
239 r#"
240fn main() {
241 struct Struct {}
242 mod module {
243 use super::Struct;
244
245 $0
246 }
247}
248 "#,
249 expect![[r#"
250 block scope
251 Struct: t
252 module: t
253
254 block scope::module
255 Struct: _
256
257 crate
258 main: v
259 "#]],
260 );
261}