aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def')
-rw-r--r--crates/hir_def/src/attr.rs289
-rw-r--r--crates/hir_def/src/body/scope.rs57
-rw-r--r--crates/hir_def/src/builtin_attr.rs7
-rw-r--r--crates/hir_def/src/db.rs6
-rw-r--r--crates/hir_def/src/import_map.rs18
-rw-r--r--crates/hir_def/src/item_scope.rs2
-rw-r--r--crates/hir_def/src/item_tree.rs4
-rw-r--r--crates/hir_def/src/item_tree/lower.rs8
-rw-r--r--crates/hir_def/src/lib.rs12
-rw-r--r--crates/hir_def/src/nameres.rs16
-rw-r--r--crates/hir_def/src/nameres/collector.rs118
-rw-r--r--crates/hir_def/src/nameres/mod_resolution.rs13
-rw-r--r--crates/hir_def/src/nameres/proc_macro.rs71
-rw-r--r--crates/hir_def/src/nameres/tests/macros.rs47
-rw-r--r--crates/hir_def/src/nameres/tests/mod_resolution.rs19
-rw-r--r--crates/hir_def/src/resolver.rs6
16 files changed, 498 insertions, 195 deletions
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs
index e4c84afbf..52a2bce9b 100644
--- a/crates/hir_def/src/attr.rs
+++ b/crates/hir_def/src/attr.rs
@@ -21,7 +21,7 @@ use crate::{
21 item_tree::{ItemTreeId, ItemTreeNode}, 21 item_tree::{ItemTreeId, ItemTreeNode},
22 nameres::ModuleSource, 22 nameres::ModuleSource,
23 path::{ModPath, PathKind}, 23 path::{ModPath, PathKind},
24 src::HasChildSource, 24 src::{HasChildSource, HasSource},
25 AdtId, AttrDefId, EnumId, GenericParamId, HasModule, LocalEnumVariantId, LocalFieldId, Lookup, 25 AdtId, AttrDefId, EnumId, GenericParamId, HasModule, LocalEnumVariantId, LocalFieldId, Lookup,
26 VariantId, 26 VariantId,
27}; 27};
@@ -51,6 +51,12 @@ pub(crate) struct RawAttrs {
51#[derive(Default, Debug, Clone, PartialEq, Eq)] 51#[derive(Default, Debug, Clone, PartialEq, Eq)]
52pub struct Attrs(RawAttrs); 52pub struct Attrs(RawAttrs);
53 53
54#[derive(Debug, Clone, PartialEq, Eq)]
55pub struct AttrsWithOwner {
56 attrs: Attrs,
57 owner: AttrDefId,
58}
59
54impl ops::Deref for RawAttrs { 60impl ops::Deref for RawAttrs {
55 type Target = [Attr]; 61 type Target = [Attr];
56 62
@@ -73,6 +79,14 @@ impl ops::Deref for Attrs {
73 } 79 }
74} 80}
75 81
82impl ops::Deref for AttrsWithOwner {
83 type Target = Attrs;
84
85 fn deref(&self) -> &Attrs {
86 &self.attrs
87 }
88}
89
76impl RawAttrs { 90impl RawAttrs {
77 pub(crate) const EMPTY: Self = Self { entries: None }; 91 pub(crate) const EMPTY: Self = Self { entries: None };
78 92
@@ -169,77 +183,6 @@ impl RawAttrs {
169impl Attrs { 183impl Attrs {
170 pub const EMPTY: Self = Self(RawAttrs::EMPTY); 184 pub const EMPTY: Self = Self(RawAttrs::EMPTY);
171 185
172 pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs {
173 let raw_attrs = match def {
174 AttrDefId::ModuleId(module) => {
175 let def_map = module.def_map(db);
176 let mod_data = &def_map[module.local_id];
177 match mod_data.declaration_source(db) {
178 Some(it) => {
179 let raw_attrs = RawAttrs::from_attrs_owner(
180 db,
181 it.as_ref().map(|it| it as &dyn ast::AttrsOwner),
182 );
183 match mod_data.definition_source(db) {
184 InFile { file_id, value: ModuleSource::SourceFile(file) } => raw_attrs
185 .merge(RawAttrs::from_attrs_owner(db, InFile::new(file_id, &file))),
186 _ => raw_attrs,
187 }
188 }
189 None => RawAttrs::from_attrs_owner(
190 db,
191 mod_data.definition_source(db).as_ref().map(|src| match src {
192 ModuleSource::SourceFile(file) => file as &dyn ast::AttrsOwner,
193 ModuleSource::Module(module) => module as &dyn ast::AttrsOwner,
194 ModuleSource::BlockExpr(block) => block as &dyn ast::AttrsOwner,
195 }),
196 ),
197 }
198 }
199 AttrDefId::FieldId(it) => {
200 return db.fields_attrs(it.parent)[it.local_id].clone();
201 }
202 AttrDefId::EnumVariantId(it) => {
203 return db.variants_attrs(it.parent)[it.local_id].clone();
204 }
205 AttrDefId::AdtId(it) => match it {
206 AdtId::StructId(it) => attrs_from_item_tree(it.lookup(db).id, db),
207 AdtId::EnumId(it) => attrs_from_item_tree(it.lookup(db).id, db),
208 AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
209 },
210 AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db),
211 AttrDefId::MacroDefId(it) => {
212 it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db))
213 }
214 AttrDefId::ImplId(it) => attrs_from_item_tree(it.lookup(db).id, db),
215 AttrDefId::ConstId(it) => attrs_from_item_tree(it.lookup(db).id, db),
216 AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db),
217 AttrDefId::FunctionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
218 AttrDefId::TypeAliasId(it) => attrs_from_item_tree(it.lookup(db).id, db),
219 AttrDefId::GenericParamId(it) => match it {
220 GenericParamId::TypeParamId(it) => {
221 let src = it.parent.child_source(db);
222 RawAttrs::from_attrs_owner(
223 db,
224 src.with_value(
225 src.value[it.local_id].as_ref().either(|it| it as _, |it| it as _),
226 ),
227 )
228 }
229 GenericParamId::LifetimeParamId(it) => {
230 let src = it.parent.child_source(db);
231 RawAttrs::from_attrs_owner(db, src.with_value(&src.value[it.local_id]))
232 }
233 GenericParamId::ConstParamId(it) => {
234 let src = it.parent.child_source(db);
235 RawAttrs::from_attrs_owner(db, src.with_value(&src.value[it.local_id]))
236 }
237 },
238 };
239
240 raw_attrs.filter(db, def.krate(db))
241 }
242
243 pub(crate) fn variants_attrs_query( 186 pub(crate) fn variants_attrs_query(
244 db: &dyn DefDatabase, 187 db: &dyn DefDatabase,
245 e: EnumId, 188 e: EnumId,
@@ -280,13 +223,6 @@ impl Attrs {
280 Arc::new(res) 223 Arc::new(res)
281 } 224 }
282 225
283 /// Constructs a map that maps the lowered `Attr`s in this `Attrs` back to its original syntax nodes.
284 ///
285 /// `owner` must be the original owner of the attributes.
286 pub fn source_map(&self, owner: &dyn ast::AttrsOwner) -> AttrSourceMap {
287 AttrSourceMap { attrs: collect_attrs(owner).collect() }
288 }
289
290 pub fn by_key(&self, key: &'static str) -> AttrQuery<'_> { 226 pub fn by_key(&self, key: &'static str) -> AttrQuery<'_> {
291 AttrQuery { attrs: self, key } 227 AttrQuery { attrs: self, key }
292 } 228 }
@@ -343,6 +279,180 @@ impl Attrs {
343 } 279 }
344} 280}
345 281
282impl AttrsWithOwner {
283 pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Self {
284 // FIXME: this should use `Trace` to avoid duplication in `source_map` below
285 let raw_attrs = match def {
286 AttrDefId::ModuleId(module) => {
287 let def_map = module.def_map(db);
288 let mod_data = &def_map[module.local_id];
289 match mod_data.declaration_source(db) {
290 Some(it) => {
291 let raw_attrs = RawAttrs::from_attrs_owner(
292 db,
293 it.as_ref().map(|it| it as &dyn ast::AttrsOwner),
294 );
295 match mod_data.definition_source(db) {
296 InFile { file_id, value: ModuleSource::SourceFile(file) } => raw_attrs
297 .merge(RawAttrs::from_attrs_owner(db, InFile::new(file_id, &file))),
298 _ => raw_attrs,
299 }
300 }
301 None => RawAttrs::from_attrs_owner(
302 db,
303 mod_data.definition_source(db).as_ref().map(|src| match src {
304 ModuleSource::SourceFile(file) => file as &dyn ast::AttrsOwner,
305 ModuleSource::Module(module) => module as &dyn ast::AttrsOwner,
306 ModuleSource::BlockExpr(block) => block as &dyn ast::AttrsOwner,
307 }),
308 ),
309 }
310 }
311 AttrDefId::FieldId(it) => {
312 return Self { attrs: db.fields_attrs(it.parent)[it.local_id].clone(), owner: def };
313 }
314 AttrDefId::EnumVariantId(it) => {
315 return Self {
316 attrs: db.variants_attrs(it.parent)[it.local_id].clone(),
317 owner: def,
318 };
319 }
320 AttrDefId::AdtId(it) => match it {
321 AdtId::StructId(it) => attrs_from_item_tree(it.lookup(db).id, db),
322 AdtId::EnumId(it) => attrs_from_item_tree(it.lookup(db).id, db),
323 AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
324 },
325 AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db),
326 AttrDefId::MacroDefId(it) => it
327 .ast_id()
328 .left()
329 .map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)),
330 AttrDefId::ImplId(it) => attrs_from_item_tree(it.lookup(db).id, db),
331 AttrDefId::ConstId(it) => attrs_from_item_tree(it.lookup(db).id, db),
332 AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db),
333 AttrDefId::FunctionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
334 AttrDefId::TypeAliasId(it) => attrs_from_item_tree(it.lookup(db).id, db),
335 AttrDefId::GenericParamId(it) => match it {
336 GenericParamId::TypeParamId(it) => {
337 let src = it.parent.child_source(db);
338 RawAttrs::from_attrs_owner(
339 db,
340 src.with_value(
341 src.value[it.local_id].as_ref().either(|it| it as _, |it| it as _),
342 ),
343 )
344 }
345 GenericParamId::LifetimeParamId(it) => {
346 let src = it.parent.child_source(db);
347 RawAttrs::from_attrs_owner(db, src.with_value(&src.value[it.local_id]))
348 }
349 GenericParamId::ConstParamId(it) => {
350 let src = it.parent.child_source(db);
351 RawAttrs::from_attrs_owner(db, src.with_value(&src.value[it.local_id]))
352 }
353 },
354 };
355
356 let attrs = raw_attrs.filter(db, def.krate(db));
357 Self { attrs, owner: def }
358 }
359
360 pub fn source_map(&self, db: &dyn DefDatabase) -> AttrSourceMap {
361 let owner = match self.owner {
362 AttrDefId::ModuleId(module) => {
363 // Modules can have 2 attribute owners (the `mod x;` item, and the module file itself).
364
365 let def_map = module.def_map(db);
366 let mod_data = &def_map[module.local_id];
367 let attrs = match mod_data.declaration_source(db) {
368 Some(it) => {
369 let mut attrs: Vec<_> = collect_attrs(&it.value as &dyn ast::AttrsOwner)
370 .map(|attr| InFile::new(it.file_id, attr))
371 .collect();
372 if let InFile { file_id, value: ModuleSource::SourceFile(file) } =
373 mod_data.definition_source(db)
374 {
375 attrs.extend(
376 collect_attrs(&file as &dyn ast::AttrsOwner)
377 .map(|attr| InFile::new(file_id, attr)),
378 )
379 }
380 attrs
381 }
382 None => {
383 let InFile { file_id, value } = mod_data.definition_source(db);
384 match &value {
385 ModuleSource::SourceFile(file) => {
386 collect_attrs(file as &dyn ast::AttrsOwner)
387 }
388 ModuleSource::Module(module) => {
389 collect_attrs(module as &dyn ast::AttrsOwner)
390 }
391 ModuleSource::BlockExpr(block) => {
392 collect_attrs(block as &dyn ast::AttrsOwner)
393 }
394 }
395 .map(|attr| InFile::new(file_id, attr))
396 .collect()
397 }
398 };
399 return AttrSourceMap { attrs };
400 }
401 AttrDefId::FieldId(id) => {
402 id.parent.child_source(db).map(|source| match &source[id.local_id] {
403 Either::Left(field) => ast::AttrsOwnerNode::new(field.clone()),
404 Either::Right(field) => ast::AttrsOwnerNode::new(field.clone()),
405 })
406 }
407 AttrDefId::AdtId(adt) => match adt {
408 AdtId::StructId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new),
409 AdtId::UnionId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new),
410 AdtId::EnumId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new),
411 },
412 AttrDefId::FunctionId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new),
413 AttrDefId::EnumVariantId(id) => id
414 .parent
415 .child_source(db)
416 .map(|source| ast::AttrsOwnerNode::new(source[id.local_id].clone())),
417 AttrDefId::StaticId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new),
418 AttrDefId::ConstId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new),
419 AttrDefId::TraitId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new),
420 AttrDefId::TypeAliasId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new),
421 AttrDefId::MacroDefId(id) => match id.ast_id() {
422 Either::Left(it) => {
423 it.with_value(ast::AttrsOwnerNode::new(it.to_node(db.upcast())))
424 }
425 Either::Right(it) => {
426 it.with_value(ast::AttrsOwnerNode::new(it.to_node(db.upcast())))
427 }
428 },
429 AttrDefId::ImplId(id) => id.lookup(db).source(db).map(ast::AttrsOwnerNode::new),
430 AttrDefId::GenericParamId(id) => match id {
431 GenericParamId::TypeParamId(id) => {
432 id.parent.child_source(db).map(|source| match &source[id.local_id] {
433 Either::Left(id) => ast::AttrsOwnerNode::new(id.clone()),
434 Either::Right(id) => ast::AttrsOwnerNode::new(id.clone()),
435 })
436 }
437 GenericParamId::LifetimeParamId(id) => id
438 .parent
439 .child_source(db)
440 .map(|source| ast::AttrsOwnerNode::new(source[id.local_id].clone())),
441 GenericParamId::ConstParamId(id) => id
442 .parent
443 .child_source(db)
444 .map(|source| ast::AttrsOwnerNode::new(source[id.local_id].clone())),
445 },
446 };
447
448 AttrSourceMap {
449 attrs: collect_attrs(&owner.value)
450 .map(|attr| InFile::new(owner.file_id, attr))
451 .collect(),
452 }
453 }
454}
455
346fn inner_attributes( 456fn inner_attributes(
347 syntax: &SyntaxNode, 457 syntax: &SyntaxNode,
348) -> Option<(impl Iterator<Item = ast::Attr>, impl Iterator<Item = ast::Comment>)> { 458) -> Option<(impl Iterator<Item = ast::Attr>, impl Iterator<Item = ast::Comment>)> {
@@ -379,7 +489,7 @@ fn inner_attributes(
379} 489}
380 490
381pub struct AttrSourceMap { 491pub struct AttrSourceMap {
382 attrs: Vec<Either<ast::Attr, ast::Comment>>, 492 attrs: Vec<InFile<Either<ast::Attr, ast::Comment>>>,
383} 493}
384 494
385impl AttrSourceMap { 495impl AttrSourceMap {
@@ -389,10 +499,11 @@ impl AttrSourceMap {
389 /// 499 ///
390 /// Note that the returned syntax node might be a `#[cfg_attr]`, or a doc comment, instead of 500 /// Note that the returned syntax node might be a `#[cfg_attr]`, or a doc comment, instead of
391 /// the attribute represented by `Attr`. 501 /// the attribute represented by `Attr`.
392 pub fn source_of(&self, attr: &Attr) -> &Either<ast::Attr, ast::Comment> { 502 pub fn source_of(&self, attr: &Attr) -> InFile<&Either<ast::Attr, ast::Comment>> {
393 self.attrs 503 self.attrs
394 .get(attr.index as usize) 504 .get(attr.index as usize)
395 .unwrap_or_else(|| panic!("cannot find `Attr` at index {}", attr.index)) 505 .unwrap_or_else(|| panic!("cannot find `Attr` at index {}", attr.index))
506 .as_ref()
396 } 507 }
397} 508}
398 509
@@ -414,7 +525,7 @@ pub enum AttrInput {
414impl Attr { 525impl Attr {
415 fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: u32) -> Option<Attr> { 526 fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: u32) -> Option<Attr> {
416 let path = ModPath::from_src(ast.path()?, hygiene)?; 527 let path = ModPath::from_src(ast.path()?, hygiene)?;
417 let input = if let Some(lit) = ast.literal() { 528 let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
418 let value = match lit.kind() { 529 let value = match lit.kind() {
419 ast::LiteralKind::String(string) => string.value()?.into(), 530 ast::LiteralKind::String(string) => string.value()?.into(),
420 _ => lit.syntax().first_token()?.text().trim_matches('"').into(), 531 _ => lit.syntax().first_token()?.text().trim_matches('"').into(),
@@ -428,18 +539,6 @@ impl Attr {
428 Some(Attr { index, path, input }) 539 Some(Attr { index, path, input })
429 } 540 }
430 541
431 /// Maps this lowered `Attr` back to its original syntax node.
432 ///
433 /// `owner` must be the original owner of the attribute.
434 ///
435 /// Note that the returned syntax node might be a `#[cfg_attr]`, or a doc comment, instead of
436 /// the attribute represented by `Attr`.
437 pub fn to_src(&self, owner: &dyn ast::AttrsOwner) -> Either<ast::Attr, ast::Comment> {
438 collect_attrs(owner).nth(self.index as usize).unwrap_or_else(|| {
439 panic!("cannot find `Attr` at index {} in {}", self.index, owner.syntax())
440 })
441 }
442
443 /// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths 542 /// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths
444 /// to derive macros. 543 /// to derive macros.
445 /// 544 ///
@@ -539,7 +638,7 @@ fn collect_attrs(
539 owner: &dyn ast::AttrsOwner, 638 owner: &dyn ast::AttrsOwner,
540) -> impl Iterator<Item = Either<ast::Attr, ast::Comment>> { 639) -> impl Iterator<Item = Either<ast::Attr, ast::Comment>> {
541 let (inner_attrs, inner_docs) = inner_attributes(owner.syntax()) 640 let (inner_attrs, inner_docs) = inner_attributes(owner.syntax())
542 .map_or((None, None), |(attrs, docs)| ((Some(attrs), Some(docs)))); 641 .map_or((None, None), |(attrs, docs)| (Some(attrs), Some(docs)));
543 642
544 let outer_attrs = owner.attrs().filter(|attr| attr.excl_token().is_none()); 643 let outer_attrs = owner.attrs().filter(|attr| attr.excl_token().is_none());
545 let attrs = outer_attrs 644 let attrs = outer_attrs
diff --git a/crates/hir_def/src/body/scope.rs b/crates/hir_def/src/body/scope.rs
index 1bbb54fc6..bd7005ca6 100644
--- a/crates/hir_def/src/body/scope.rs
+++ b/crates/hir_def/src/body/scope.rs
@@ -8,7 +8,7 @@ use rustc_hash::FxHashMap;
8use crate::{ 8use crate::{
9 body::Body, 9 body::Body,
10 db::DefDatabase, 10 db::DefDatabase,
11 expr::{Expr, ExprId, Pat, PatId, Statement}, 11 expr::{Expr, ExprId, LabelId, Pat, PatId, Statement},
12 BlockId, DefWithBodyId, 12 BlockId, DefWithBodyId,
13}; 13};
14 14
@@ -40,6 +40,7 @@ impl ScopeEntry {
40pub struct ScopeData { 40pub struct ScopeData {
41 parent: Option<ScopeId>, 41 parent: Option<ScopeId>,
42 block: Option<BlockId>, 42 block: Option<BlockId>,
43 label: Option<(LabelId, Name)>,
43 entries: Vec<ScopeEntry>, 44 entries: Vec<ScopeEntry>,
44} 45}
45 46
@@ -67,6 +68,11 @@ impl ExprScopes {
67 self.scopes[scope].block 68 self.scopes[scope].block
68 } 69 }
69 70
71 /// If `scope` refers to a labeled expression scope, returns the corresponding `Label`.
72 pub fn label(&self, scope: ScopeId) -> Option<(LabelId, Name)> {
73 self.scopes[scope].label.clone()
74 }
75
70 pub fn scope_chain(&self, scope: Option<ScopeId>) -> impl Iterator<Item = ScopeId> + '_ { 76 pub fn scope_chain(&self, scope: Option<ScopeId>) -> impl Iterator<Item = ScopeId> + '_ {
71 std::iter::successors(scope, move |&scope| self.scopes[scope].parent) 77 std::iter::successors(scope, move |&scope| self.scopes[scope].parent)
72 } 78 }
@@ -85,15 +91,34 @@ impl ExprScopes {
85 } 91 }
86 92
87 fn root_scope(&mut self) -> ScopeId { 93 fn root_scope(&mut self) -> ScopeId {
88 self.scopes.alloc(ScopeData { parent: None, block: None, entries: vec![] }) 94 self.scopes.alloc(ScopeData { parent: None, block: None, label: None, entries: vec![] })
89 } 95 }
90 96
91 fn new_scope(&mut self, parent: ScopeId) -> ScopeId { 97 fn new_scope(&mut self, parent: ScopeId) -> ScopeId {
92 self.scopes.alloc(ScopeData { parent: Some(parent), block: None, entries: vec![] }) 98 self.scopes.alloc(ScopeData {
99 parent: Some(parent),
100 block: None,
101 label: None,
102 entries: vec![],
103 })
93 } 104 }
94 105
95 fn new_block_scope(&mut self, parent: ScopeId, block: BlockId) -> ScopeId { 106 fn new_labeled_scope(&mut self, parent: ScopeId, label: Option<(LabelId, Name)>) -> ScopeId {
96 self.scopes.alloc(ScopeData { parent: Some(parent), block: Some(block), entries: vec![] }) 107 self.scopes.alloc(ScopeData { parent: Some(parent), block: None, label, entries: vec![] })
108 }
109
110 fn new_block_scope(
111 &mut self,
112 parent: ScopeId,
113 block: BlockId,
114 label: Option<(LabelId, Name)>,
115 ) -> ScopeId {
116 self.scopes.alloc(ScopeData {
117 parent: Some(parent),
118 block: Some(block),
119 label,
120 entries: vec![],
121 })
97 } 122 }
98 123
99 fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) { 124 fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) {
@@ -144,21 +169,33 @@ fn compute_block_scopes(
144} 169}
145 170
146fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope: ScopeId) { 171fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope: ScopeId) {
172 let make_label =
173 |label: &Option<_>| label.map(|label| (label, body.labels[label].name.clone()));
174
147 scopes.set_scope(expr, scope); 175 scopes.set_scope(expr, scope);
148 match &body[expr] { 176 match &body[expr] {
149 Expr::Block { statements, tail, id, .. } => { 177 Expr::Block { statements, tail, id, label } => {
150 let scope = scopes.new_block_scope(scope, *id); 178 let scope = scopes.new_block_scope(scope, *id, make_label(label));
151 // Overwrite the old scope for the block expr, so that every block scope can be found 179 // 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). 180 // via the block itself (important for blocks that only contain items, no expressions).
153 scopes.set_scope(expr, scope); 181 scopes.set_scope(expr, scope);
154 compute_block_scopes(&statements, *tail, body, scopes, scope); 182 compute_block_scopes(statements, *tail, body, scopes, scope);
155 } 183 }
156 Expr::For { iterable, pat, body: body_expr, .. } => { 184 Expr::For { iterable, pat, body: body_expr, label } => {
157 compute_expr_scopes(*iterable, body, scopes, scope); 185 compute_expr_scopes(*iterable, body, scopes, scope);
158 let scope = scopes.new_scope(scope); 186 let scope = scopes.new_labeled_scope(scope, make_label(label));
159 scopes.add_bindings(body, scope, *pat); 187 scopes.add_bindings(body, scope, *pat);
160 compute_expr_scopes(*body_expr, body, scopes, scope); 188 compute_expr_scopes(*body_expr, body, scopes, scope);
161 } 189 }
190 Expr::While { condition, body: body_expr, label } => {
191 let scope = scopes.new_labeled_scope(scope, make_label(label));
192 compute_expr_scopes(*condition, body, scopes, scope);
193 compute_expr_scopes(*body_expr, body, scopes, scope);
194 }
195 Expr::Loop { body: body_expr, label } => {
196 let scope = scopes.new_labeled_scope(scope, make_label(label));
197 compute_expr_scopes(*body_expr, body, scopes, scope);
198 }
162 Expr::Lambda { args, body: body_expr, .. } => { 199 Expr::Lambda { args, body: body_expr, .. } => {
163 let scope = scopes.new_scope(scope); 200 let scope = scopes.new_scope(scope);
164 scopes.add_params_bindings(body, scope, &args); 201 scopes.add_params_bindings(body, scope, &args);
diff --git a/crates/hir_def/src/builtin_attr.rs b/crates/hir_def/src/builtin_attr.rs
index 2e989c504..d5d7f0f47 100644
--- a/crates/hir_def/src/builtin_attr.rs
+++ b/crates/hir_def/src/builtin_attr.rs
@@ -34,9 +34,12 @@ macro_rules! rustc_attr {
34 }; 34 };
35} 35}
36 36
37/// Attributes that have a special meaning to rustc or rustdoc. 37/// Built-in macro-like attributes.
38pub const EXTRA_ATTRIBUTES: &[BuiltinAttribute] = &["test", "bench"];
39
40/// "Inert" built-in attributes that have a special meaning to rustc or rustdoc.
38#[rustfmt::skip] 41#[rustfmt::skip]
39pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ 42pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[
40 // ========================================================================== 43 // ==========================================================================
41 // Stable attributes: 44 // Stable attributes:
42 // ========================================================================== 45 // ==========================================================================
diff --git a/crates/hir_def/src/db.rs b/crates/hir_def/src/db.rs
index 276caf5b3..53df85089 100644
--- a/crates/hir_def/src/db.rs
+++ b/crates/hir_def/src/db.rs
@@ -8,7 +8,7 @@ use syntax::SmolStr;
8 8
9use crate::{ 9use crate::{
10 adt::{EnumData, StructData}, 10 adt::{EnumData, StructData},
11 attr::Attrs, 11 attr::{Attrs, AttrsWithOwner},
12 body::{scope::ExprScopes, Body, BodySourceMap}, 12 body::{scope::ExprScopes, Body, BodySourceMap},
13 data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData}, 13 data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData},
14 generics::GenericParams, 14 generics::GenericParams,
@@ -120,8 +120,8 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
120 #[salsa::invoke(Attrs::fields_attrs_query)] 120 #[salsa::invoke(Attrs::fields_attrs_query)]
121 fn fields_attrs(&self, def: VariantId) -> Arc<ArenaMap<LocalFieldId, Attrs>>; 121 fn fields_attrs(&self, def: VariantId) -> Arc<ArenaMap<LocalFieldId, Attrs>>;
122 122
123 #[salsa::invoke(Attrs::attrs_query)] 123 #[salsa::invoke(AttrsWithOwner::attrs_query)]
124 fn attrs(&self, def: AttrDefId) -> Attrs; 124 fn attrs(&self, def: AttrDefId) -> AttrsWithOwner;
125 125
126 #[salsa::invoke(LangItems::crate_lang_items_query)] 126 #[salsa::invoke(LangItems::crate_lang_items_query)]
127 fn crate_lang_items(&self, krate: CrateId) -> Arc<LangItems>; 127 fn crate_lang_items(&self, krate: CrateId) -> Arc<LangItems>;
diff --git a/crates/hir_def/src/import_map.rs b/crates/hir_def/src/import_map.rs
index 369bc3350..960cabb5f 100644
--- a/crates/hir_def/src/import_map.rs
+++ b/crates/hir_def/src/import_map.rs
@@ -912,10 +912,10 @@ mod tests {
912 dep::fmt (t) 912 dep::fmt (t)
913 dep::format (f) 913 dep::format (f)
914 dep::Fmt (v) 914 dep::Fmt (v)
915 dep::fmt::Display (t) 915 dep::Fmt (m)
916 dep::Fmt (t) 916 dep::Fmt (t)
917 dep::fmt::Display::fmt (a) 917 dep::fmt::Display::fmt (a)
918 dep::Fmt (m) 918 dep::fmt::Display (t)
919 "#]], 919 "#]],
920 ); 920 );
921 921
@@ -926,9 +926,9 @@ mod tests {
926 expect![[r#" 926 expect![[r#"
927 dep::fmt (t) 927 dep::fmt (t)
928 dep::Fmt (v) 928 dep::Fmt (v)
929 dep::Fmt (m)
929 dep::Fmt (t) 930 dep::Fmt (t)
930 dep::fmt::Display::fmt (a) 931 dep::fmt::Display::fmt (a)
931 dep::Fmt (m)
932 "#]], 932 "#]],
933 ); 933 );
934 934
@@ -939,10 +939,10 @@ mod tests {
939 expect![[r#" 939 expect![[r#"
940 dep::fmt (t) 940 dep::fmt (t)
941 dep::Fmt (v) 941 dep::Fmt (v)
942 dep::fmt::Display (t) 942 dep::Fmt (m)
943 dep::Fmt (t) 943 dep::Fmt (t)
944 dep::fmt::Display::fmt (a) 944 dep::fmt::Display::fmt (a)
945 dep::Fmt (m) 945 dep::fmt::Display (t)
946 "#]], 946 "#]],
947 ); 947 );
948 } 948 }
@@ -980,10 +980,10 @@ mod tests {
980 expect![[r#" 980 expect![[r#"
981 dep::fmt (t) 981 dep::fmt (t)
982 dep::Fmt (v) 982 dep::Fmt (v)
983 dep::fmt::Display (t) 983 dep::Fmt (m)
984 dep::Fmt (t) 984 dep::Fmt (t)
985 dep::fmt::Display::fmt (a) 985 dep::fmt::Display::fmt (a)
986 dep::Fmt (m) 986 dep::fmt::Display (t)
987 "#]], 987 "#]],
988 ); 988 );
989 989
@@ -994,9 +994,9 @@ mod tests {
994 expect![[r#" 994 expect![[r#"
995 dep::fmt (t) 995 dep::fmt (t)
996 dep::Fmt (v) 996 dep::Fmt (v)
997 dep::Fmt (m)
997 dep::Fmt (t) 998 dep::Fmt (t)
998 dep::fmt::Display::fmt (a) 999 dep::fmt::Display::fmt (a)
999 dep::Fmt (m)
1000 "#]], 1000 "#]],
1001 ); 1001 );
1002 } 1002 }
@@ -1058,8 +1058,8 @@ mod tests {
1058 Query::new("".to_string()).limit(2), 1058 Query::new("".to_string()).limit(2),
1059 expect![[r#" 1059 expect![[r#"
1060 dep::fmt (t) 1060 dep::fmt (t)
1061 dep::Fmt (t)
1062 dep::Fmt (m) 1061 dep::Fmt (m)
1062 dep::Fmt (t)
1063 dep::Fmt (v) 1063 dep::Fmt (v)
1064 "#]], 1064 "#]],
1065 ); 1065 );
diff --git a/crates/hir_def/src/item_scope.rs b/crates/hir_def/src/item_scope.rs
index aafd73b60..f3ebe7c72 100644
--- a/crates/hir_def/src/item_scope.rs
+++ b/crates/hir_def/src/item_scope.rs
@@ -252,7 +252,7 @@ impl ItemScope {
252 .for_each(|vis| *vis = Visibility::Module(this_module)); 252 .for_each(|vis| *vis = Visibility::Module(this_module));
253 253
254 for (mac, vis) in self.macros.values_mut() { 254 for (mac, vis) in self.macros.values_mut() {
255 if let MacroDefKind::ProcMacro(_) = mac.kind { 255 if let MacroDefKind::ProcMacro(..) = mac.kind {
256 // FIXME: Technically this is insufficient since reexports of proc macros are also 256 // FIXME: Technically this is insufficient since reexports of proc macros are also
257 // forbidden. Practically nobody does that. 257 // forbidden. Practically nobody does that.
258 continue; 258 continue;
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs
index 5f5b7151a..ae2475b4e 100644
--- a/crates/hir_def/src/item_tree.rs
+++ b/crates/hir_def/src/item_tree.rs
@@ -115,6 +115,10 @@ impl ItemTree {
115 // still need to collect inner items. 115 // still need to collect inner items.
116 ctx.lower_inner_items(stmt.syntax()) 116 ctx.lower_inner_items(stmt.syntax())
117 }, 117 },
118 ast::Item(item) => {
119 // Macros can expand to stmt and other item, and we add it as top level item
120 ctx.lower_single_item(item)
121 },
118 _ => { 122 _ => {
119 panic!("cannot create item tree from {:?} {}", syntax, syntax); 123 panic!("cannot create item tree from {:?} {}", syntax, syntax);
120 }, 124 },
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs
index 3f558edd8..d3fe1ce1e 100644
--- a/crates/hir_def/src/item_tree/lower.rs
+++ b/crates/hir_def/src/item_tree/lower.rs
@@ -87,6 +87,14 @@ impl Ctx {
87 self.tree 87 self.tree
88 } 88 }
89 89
90 pub(super) fn lower_single_item(mut self, item: ast::Item) -> ItemTree {
91 self.tree.top_level = self
92 .lower_mod_item(&item, false)
93 .map(|item| item.0)
94 .unwrap_or_else(|| Default::default());
95 self.tree
96 }
97
90 pub(super) fn lower_inner_items(mut self, within: &SyntaxNode) -> ItemTree { 98 pub(super) fn lower_inner_items(mut self, within: &SyntaxNode) -> ItemTree {
91 self.collect_inner_items(within); 99 self.collect_inner_items(within);
92 self.tree 100 self.tree
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs
index 6758411a0..c9e07de86 100644
--- a/crates/hir_def/src/lib.rs
+++ b/crates/hir_def/src/lib.rs
@@ -76,7 +76,11 @@ use stdx::impl_from;
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 76#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
77pub struct ModuleId { 77pub struct ModuleId {
78 krate: CrateId, 78 krate: CrateId,
79 /// If this `ModuleId` was derived from a `DefMap` for a block expression, this stores the
80 /// `BlockId` of that block expression. If `None`, this module is part of the crate-level
81 /// `DefMap` of `krate`.
79 block: Option<BlockId>, 82 block: Option<BlockId>,
83 /// The module's ID in its originating `DefMap`.
80 pub local_id: LocalModuleId, 84 pub local_id: LocalModuleId,
81} 85}
82 86
@@ -87,7 +91,7 @@ impl ModuleId {
87 db.block_def_map(block).unwrap_or_else(|| { 91 db.block_def_map(block).unwrap_or_else(|| {
88 // NOTE: This should be unreachable - all `ModuleId`s come from their `DefMap`s, 92 // NOTE: This should be unreachable - all `ModuleId`s come from their `DefMap`s,
89 // so the `DefMap` here must exist. 93 // so the `DefMap` here must exist.
90 panic!("no `block_def_map` for `ModuleId` {:?}", self); 94 unreachable!("no `block_def_map` for `ModuleId` {:?}", self);
91 }) 95 })
92 } 96 }
93 None => db.crate_def_map(self.krate), 97 None => db.crate_def_map(self.krate),
@@ -650,7 +654,7 @@ fn macro_call_as_call_id(
650) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> { 654) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
651 let def: MacroDefId = resolver(call.path.clone()).ok_or(UnresolvedMacro)?; 655 let def: MacroDefId = resolver(call.path.clone()).ok_or(UnresolvedMacro)?;
652 656
653 let res = if let MacroDefKind::BuiltInEager(_) = def.kind { 657 let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
654 let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast())); 658 let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast()));
655 let hygiene = Hygiene::new(db.upcast(), call.ast_id.file_id); 659 let hygiene = Hygiene::new(db.upcast(), call.ast_id.file_id);
656 660
@@ -669,7 +673,7 @@ fn macro_call_as_call_id(
669 Ok(res) 673 Ok(res)
670} 674}
671 675
672fn item_attr_as_call_id( 676fn derive_macro_as_call_id(
673 item_attr: &AstIdWithPath<ast::Item>, 677 item_attr: &AstIdWithPath<ast::Item>,
674 db: &dyn db::DefDatabase, 678 db: &dyn db::DefDatabase,
675 krate: CrateId, 679 krate: CrateId,
@@ -681,7 +685,7 @@ fn item_attr_as_call_id(
681 .as_lazy_macro( 685 .as_lazy_macro(
682 db.upcast(), 686 db.upcast(),
683 krate, 687 krate,
684 MacroCallKind::Attr(item_attr.ast_id, last_segment.to_string()), 688 MacroCallKind::Derive(item_attr.ast_id, last_segment.to_string()),
685 ) 689 )
686 .into(); 690 .into();
687 Ok(res) 691 Ok(res)
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs
index c97be584e..0d3a0b54f 100644
--- a/crates/hir_def/src/nameres.rs
+++ b/crates/hir_def/src/nameres.rs
@@ -53,11 +53,12 @@ mod path_resolution;
53 53
54#[cfg(test)] 54#[cfg(test)]
55mod tests; 55mod tests;
56mod proc_macro;
56 57
57use std::sync::Arc; 58use std::sync::Arc;
58 59
59use base_db::{CrateId, Edition, FileId}; 60use base_db::{CrateId, Edition, FileId};
60use hir_expand::{diagnostics::DiagnosticSink, name::Name, InFile}; 61use hir_expand::{diagnostics::DiagnosticSink, name::Name, InFile, MacroDefId};
61use la_arena::Arena; 62use la_arena::Arena;
62use profile::Count; 63use profile::Count;
63use rustc_hash::FxHashMap; 64use rustc_hash::FxHashMap;
@@ -73,6 +74,8 @@ use crate::{
73 AstId, BlockId, BlockLoc, LocalModuleId, ModuleDefId, ModuleId, 74 AstId, BlockId, BlockLoc, LocalModuleId, ModuleDefId, ModuleId,
74}; 75};
75 76
77use self::proc_macro::ProcMacroDef;
78
76/// Contains the results of (early) name resolution. 79/// Contains the results of (early) name resolution.
77/// 80///
78/// A `DefMap` stores the module tree and the definitions that are in scope in every module after 81/// A `DefMap` stores the module tree and the definitions that are in scope in every module after
@@ -95,6 +98,12 @@ pub struct DefMap {
95 prelude: Option<ModuleId>, 98 prelude: Option<ModuleId>,
96 extern_prelude: FxHashMap<Name, ModuleDefId>, 99 extern_prelude: FxHashMap<Name, ModuleDefId>,
97 100
101 /// Side table with additional proc. macro info, for use by name resolution in downstream
102 /// crates.
103 ///
104 /// (the primary purpose is to resolve derive helpers)
105 exported_proc_macros: FxHashMap<MacroDefId, ProcMacroDef>,
106
98 edition: Edition, 107 edition: Edition,
99 diagnostics: Vec<DefDiagnostic>, 108 diagnostics: Vec<DefDiagnostic>,
100} 109}
@@ -237,6 +246,7 @@ impl DefMap {
237 krate, 246 krate,
238 edition, 247 edition,
239 extern_prelude: FxHashMap::default(), 248 extern_prelude: FxHashMap::default(),
249 exported_proc_macros: FxHashMap::default(),
240 prelude: None, 250 prelude: None,
241 root, 251 root,
242 modules, 252 modules,
@@ -565,7 +575,7 @@ mod diagnostics {
565 let node = ast.to_node(db.upcast()); 575 let node = ast.to_node(db.upcast());
566 (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) 576 (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None)
567 } 577 }
568 MacroCallKind::Attr(ast, name) => { 578 MacroCallKind::Derive(ast, name) => {
569 let node = ast.to_node(db.upcast()); 579 let node = ast.to_node(db.upcast());
570 580
571 // Compute the precise location of the macro name's token in the derive 581 // Compute the precise location of the macro name's token in the derive
@@ -621,7 +631,7 @@ mod diagnostics {
621 let node = ast.to_node(db.upcast()); 631 let node = ast.to_node(db.upcast());
622 (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) 632 (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node)))
623 } 633 }
624 MacroCallKind::Attr(ast, _) => { 634 MacroCallKind::Derive(ast, _) => {
625 let node = ast.to_node(db.upcast()); 635 let node = ast.to_node(db.upcast());
626 (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) 636 (ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node)))
627 } 637 }
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs
index d0fefb5af..28b73c3a1 100644
--- a/crates/hir_def/src/nameres/collector.rs
+++ b/crates/hir_def/src/nameres/collector.rs
@@ -18,12 +18,11 @@ use hir_expand::{
18use hir_expand::{InFile, MacroCallLoc}; 18use hir_expand::{InFile, MacroCallLoc};
19use rustc_hash::{FxHashMap, FxHashSet}; 19use rustc_hash::{FxHashMap, FxHashSet};
20use syntax::ast; 20use syntax::ast;
21use tt::{Leaf, TokenTree};
22 21
23use crate::{ 22use crate::{
24 attr::Attrs, 23 attr::Attrs,
25 db::DefDatabase, 24 db::DefDatabase,
26 item_attr_as_call_id, 25 derive_macro_as_call_id,
27 item_scope::{ImportType, PerNsGlobImports}, 26 item_scope::{ImportType, PerNsGlobImports},
28 item_tree::{ 27 item_tree::{
29 self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, MacroRules, Mod, ModItem, ModKind, 28 self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, MacroRules, Mod, ModItem, ModKind,
@@ -42,6 +41,8 @@ use crate::{
42 UnresolvedMacro, 41 UnresolvedMacro,
43}; 42};
44 43
44use super::proc_macro::ProcMacroDef;
45
45const GLOB_RECURSION_LIMIT: usize = 100; 46const GLOB_RECURSION_LIMIT: usize = 100;
46const EXPANSION_DEPTH_LIMIT: usize = 128; 47const EXPANSION_DEPTH_LIMIT: usize = 128;
47const FIXED_POINT_LIMIT: usize = 8192; 48const FIXED_POINT_LIMIT: usize = 8192;
@@ -353,24 +354,23 @@ impl DefCollector<'_> {
353 /// use a dummy expander that always errors. This comes with the drawback of macros potentially 354 /// use a dummy expander that always errors. This comes with the drawback of macros potentially
354 /// going out of sync with what the build system sees (since we resolve using VFS state, but 355 /// going out of sync with what the build system sees (since we resolve using VFS state, but
355 /// Cargo builds only on-disk files). We could and probably should add diagnostics for that. 356 /// Cargo builds only on-disk files). We could and probably should add diagnostics for that.
356 fn resolve_proc_macro(&mut self, name: &Name) { 357 fn export_proc_macro(&mut self, def: ProcMacroDef, ast_id: AstId<ast::Fn>) {
357 self.exports_proc_macros = true; 358 self.exports_proc_macros = true;
358 let macro_def = match self.proc_macros.iter().find(|(n, _)| n == name) { 359 let macro_def = match self.proc_macros.iter().find(|(n, _)| n == &def.name) {
359 Some((_, expander)) => MacroDefId { 360 Some((_, expander)) => MacroDefId {
360 ast_id: None,
361 krate: self.def_map.krate, 361 krate: self.def_map.krate,
362 kind: MacroDefKind::ProcMacro(*expander), 362 kind: MacroDefKind::ProcMacro(*expander, ast_id),
363 local_inner: false, 363 local_inner: false,
364 }, 364 },
365 None => MacroDefId { 365 None => MacroDefId {
366 ast_id: None,
367 krate: self.def_map.krate, 366 krate: self.def_map.krate,
368 kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate)), 367 kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate), ast_id),
369 local_inner: false, 368 local_inner: false,
370 }, 369 },
371 }; 370 };
372 371
373 self.define_proc_macro(name.clone(), macro_def); 372 self.define_proc_macro(def.name.clone(), macro_def);
373 self.def_map.exported_proc_macros.insert(macro_def, def);
374 } 374 }
375 375
376 /// Define a macro with `macro_rules`. 376 /// Define a macro with `macro_rules`.
@@ -820,8 +820,8 @@ impl DefCollector<'_> {
820 true 820 true
821 }); 821 });
822 attribute_macros.retain(|directive| { 822 attribute_macros.retain(|directive| {
823 match item_attr_as_call_id(&directive.ast_id, self.db, self.def_map.krate, |path| { 823 match derive_macro_as_call_id(&directive.ast_id, self.db, self.def_map.krate, |path| {
824 self.resolve_attribute_macro(&directive, &path) 824 self.resolve_derive_macro(&directive, &path)
825 }) { 825 }) {
826 Ok(call_id) => { 826 Ok(call_id) => {
827 resolved.push((directive.module_id, call_id, 0)); 827 resolved.push((directive.module_id, call_id, 0));
@@ -844,7 +844,7 @@ impl DefCollector<'_> {
844 res 844 res
845 } 845 }
846 846
847 fn resolve_attribute_macro( 847 fn resolve_derive_macro(
848 &self, 848 &self,
849 directive: &DeriveDirective, 849 directive: &DeriveDirective,
850 path: &ModPath, 850 path: &ModPath,
@@ -1118,7 +1118,8 @@ impl ModCollector<'_, '_> {
1118 ModItem::Function(id) => { 1118 ModItem::Function(id) => {
1119 let func = &self.item_tree[id]; 1119 let func = &self.item_tree[id];
1120 1120
1121 self.collect_proc_macro_def(&func.name, &attrs); 1121 let ast_id = InFile::new(self.file_id, func.ast_id);
1122 self.collect_proc_macro_def(&func.name, ast_id, &attrs);
1122 1123
1123 def = Some(DefData { 1124 def = Some(DefData {
1124 id: FunctionLoc { 1125 id: FunctionLoc {
@@ -1293,29 +1294,37 @@ impl ModCollector<'_, '_> {
1293 let db = self.def_collector.db; 1294 let db = self.def_collector.db;
1294 match self.mod_dir.resolve_declaration(db, self.file_id, &module.name, path_attr) { 1295 match self.mod_dir.resolve_declaration(db, self.file_id, &module.name, path_attr) {
1295 Ok((file_id, is_mod_rs, mod_dir)) => { 1296 Ok((file_id, is_mod_rs, mod_dir)) => {
1296 let module_id = self.push_child_module(
1297 module.name.clone(),
1298 ast_id,
1299 Some((file_id, is_mod_rs)),
1300 &self.item_tree[module.visibility],
1301 );
1302 let item_tree = db.file_item_tree(file_id.into()); 1297 let item_tree = db.file_item_tree(file_id.into());
1303 ModCollector { 1298 if item_tree
1304 def_collector: &mut *self.def_collector, 1299 .top_level_attrs(db, self.def_collector.def_map.krate)
1305 macro_depth: self.macro_depth, 1300 .cfg()
1306 module_id, 1301 .map_or(true, |cfg| {
1307 file_id: file_id.into(), 1302 self.def_collector.cfg_options.check(&cfg) != Some(false)
1308 item_tree: &item_tree, 1303 })
1309 mod_dir,
1310 }
1311 .collect(item_tree.top_level_items());
1312 if is_macro_use
1313 || item_tree
1314 .top_level_attrs(db, self.def_collector.def_map.krate)
1315 .by_key("macro_use")
1316 .exists()
1317 { 1304 {
1318 self.import_all_legacy_macros(module_id); 1305 let module_id = self.push_child_module(
1306 module.name.clone(),
1307 ast_id,
1308 Some((file_id, is_mod_rs)),
1309 &self.item_tree[module.visibility],
1310 );
1311 ModCollector {
1312 def_collector: &mut *self.def_collector,
1313 macro_depth: self.macro_depth,
1314 module_id,
1315 file_id: file_id.into(),
1316 item_tree: &item_tree,
1317 mod_dir,
1318 }
1319 .collect(item_tree.top_level_items());
1320 if is_macro_use
1321 || item_tree
1322 .top_level_attrs(db, self.def_collector.def_map.krate)
1323 .by_key("macro_use")
1324 .exists()
1325 {
1326 self.import_all_legacy_macros(module_id);
1327 }
1319 } 1328 }
1320 } 1329 }
1321 Err(candidate) => { 1330 Err(candidate) => {
@@ -1385,28 +1394,11 @@ impl ModCollector<'_, '_> {
1385 } 1394 }
1386 1395
1387 /// If `attrs` registers a procedural macro, collects its definition. 1396 /// If `attrs` registers a procedural macro, collects its definition.
1388 fn collect_proc_macro_def(&mut self, func_name: &Name, attrs: &Attrs) { 1397 fn collect_proc_macro_def(&mut self, func_name: &Name, ast_id: AstId<ast::Fn>, attrs: &Attrs) {
1389 // FIXME: this should only be done in the root module of `proc-macro` crates, not everywhere 1398 // FIXME: this should only be done in the root module of `proc-macro` crates, not everywhere
1390 // FIXME: distinguish the type of macro 1399 if let Some(proc_macro) = attrs.parse_proc_macro_decl(func_name) {
1391 let macro_name = if attrs.by_key("proc_macro").exists() 1400 self.def_collector.export_proc_macro(proc_macro, ast_id);
1392 || attrs.by_key("proc_macro_attribute").exists() 1401 }
1393 {
1394 func_name.clone()
1395 } else {
1396 let derive = attrs.by_key("proc_macro_derive");
1397 if let Some(arg) = derive.tt_values().next() {
1398 if let [TokenTree::Leaf(Leaf::Ident(trait_name)), ..] = &*arg.token_trees {
1399 trait_name.as_name()
1400 } else {
1401 log::trace!("malformed `#[proc_macro_derive]`: {}", arg);
1402 return;
1403 }
1404 } else {
1405 return;
1406 }
1407 };
1408
1409 self.def_collector.resolve_proc_macro(&macro_name);
1410 } 1402 }
1411 1403
1412 fn collect_macro_rules(&mut self, id: FileItemTreeId<MacroRules>) { 1404 fn collect_macro_rules(&mut self, id: FileItemTreeId<MacroRules>) {
@@ -1445,9 +1437,8 @@ impl ModCollector<'_, '_> {
1445 1437
1446 // Case 2: normal `macro_rules!` macro 1438 // Case 2: normal `macro_rules!` macro
1447 let macro_id = MacroDefId { 1439 let macro_id = MacroDefId {
1448 ast_id: Some(ast_id),
1449 krate: self.def_collector.def_map.krate, 1440 krate: self.def_collector.def_map.krate,
1450 kind: MacroDefKind::Declarative, 1441 kind: MacroDefKind::Declarative(ast_id),
1451 local_inner: is_local_inner, 1442 local_inner: is_local_inner,
1452 }; 1443 };
1453 self.def_collector.define_macro(self.module_id, mac.name.clone(), macro_id, is_export); 1444 self.def_collector.define_macro(self.module_id, mac.name.clone(), macro_id, is_export);
@@ -1476,12 +1467,13 @@ impl ModCollector<'_, '_> {
1476 }, 1467 },
1477 ) { 1468 ) {
1478 Ok(Ok(macro_call_id)) => { 1469 Ok(Ok(macro_call_id)) => {
1479 self.def_collector.unexpanded_macros.push(MacroDirective { 1470 // Legacy macros need to be expanded immediately, so that any macros they produce
1480 module_id: self.module_id, 1471 // are in scope.
1481 ast_id, 1472 self.def_collector.collect_macro_expansion(
1482 legacy: Some(macro_call_id), 1473 self.module_id,
1483 depth: self.macro_depth + 1, 1474 macro_call_id,
1484 }); 1475 self.macro_depth + 1,
1476 );
1485 1477
1486 return; 1478 return;
1487 } 1479 }
diff --git a/crates/hir_def/src/nameres/mod_resolution.rs b/crates/hir_def/src/nameres/mod_resolution.rs
index d5de9899c..d9cec0e27 100644
--- a/crates/hir_def/src/nameres/mod_resolution.rs
+++ b/crates/hir_def/src/nameres/mod_resolution.rs
@@ -62,7 +62,7 @@ impl ModDir {
62 name: &Name, 62 name: &Name,
63 attr_path: Option<&SmolStr>, 63 attr_path: Option<&SmolStr>,
64 ) -> Result<(FileId, bool, ModDir), String> { 64 ) -> Result<(FileId, bool, ModDir), String> {
65 let file_id = file_id.original_file(db.upcast()); 65 let orig_file_id = file_id.original_file(db.upcast());
66 66
67 let mut candidate_files = Vec::new(); 67 let mut candidate_files = Vec::new();
68 match attr_path { 68 match attr_path {
@@ -70,13 +70,18 @@ impl ModDir {
70 candidate_files.push(self.dir_path.join_attr(attr_path, self.root_non_dir_owner)) 70 candidate_files.push(self.dir_path.join_attr(attr_path, self.root_non_dir_owner))
71 } 71 }
72 None => { 72 None => {
73 candidate_files.push(format!("{}{}.rs", self.dir_path.0, name)); 73 if file_id.is_include_macro(db.upcast()) {
74 candidate_files.push(format!("{}{}/mod.rs", self.dir_path.0, name)); 74 candidate_files.push(format!("{}.rs", name));
75 candidate_files.push(format!("{}/mod.rs", name));
76 } else {
77 candidate_files.push(format!("{}{}.rs", self.dir_path.0, name));
78 candidate_files.push(format!("{}{}/mod.rs", self.dir_path.0, name));
79 }
75 } 80 }
76 }; 81 };
77 82
78 for candidate in candidate_files.iter() { 83 for candidate in candidate_files.iter() {
79 let path = AnchoredPath { anchor: file_id, path: candidate.as_str() }; 84 let path = AnchoredPath { anchor: orig_file_id, path: candidate.as_str() };
80 if let Some(file_id) = db.resolve_path(path) { 85 if let Some(file_id) = db.resolve_path(path) {
81 let is_mod_rs = candidate.ends_with("/mod.rs"); 86 let is_mod_rs = candidate.ends_with("/mod.rs");
82 87
diff --git a/crates/hir_def/src/nameres/proc_macro.rs b/crates/hir_def/src/nameres/proc_macro.rs
new file mode 100644
index 000000000..156598f19
--- /dev/null
+++ b/crates/hir_def/src/nameres/proc_macro.rs
@@ -0,0 +1,71 @@
1//! Nameres-specific procedural macro data and helpers.
2
3use hir_expand::name::{AsName, Name};
4use tt::{Leaf, TokenTree};
5
6use crate::attr::Attrs;
7
8#[derive(Debug, PartialEq, Eq)]
9pub(super) struct ProcMacroDef {
10 pub(super) name: Name,
11 pub(super) kind: ProcMacroKind,
12}
13
14#[derive(Debug, PartialEq, Eq)]
15pub(super) enum ProcMacroKind {
16 CustomDerive { helpers: Box<[Name]> },
17 FnLike,
18 Attr,
19}
20
21impl Attrs {
22 #[rustfmt::skip]
23 pub(super) fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> {
24 if self.by_key("proc_macro").exists() {
25 Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::FnLike })
26 } else if self.by_key("proc_macro_attribute").exists() {
27 Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::Attr })
28 } else if self.by_key("proc_macro_derive").exists() {
29 let derive = self.by_key("proc_macro_derive").tt_values().next().unwrap();
30
31 match &*derive.token_trees {
32 // `#[proc_macro_derive(Trait)]`
33 [TokenTree::Leaf(Leaf::Ident(trait_name))] => Some(ProcMacroDef {
34 name: trait_name.as_name(),
35 kind: ProcMacroKind::CustomDerive { helpers: Box::new([]) },
36 }),
37
38 // `#[proc_macro_derive(Trait, attibutes(helper1, helper2, ...))]`
39 [
40 TokenTree::Leaf(Leaf::Ident(trait_name)),
41 TokenTree::Leaf(Leaf::Punct(comma)),
42 TokenTree::Leaf(Leaf::Ident(attributes)),
43 TokenTree::Subtree(helpers)
44 ] if comma.char == ',' && attributes.text == "attributes" =>
45 {
46 let helpers = helpers.token_trees.iter()
47 .filter(|tt| !matches!(tt, TokenTree::Leaf(Leaf::Punct(comma)) if comma.char == ','))
48 .map(|tt| {
49 match tt {
50 TokenTree::Leaf(Leaf::Ident(helper)) => Some(helper.as_name()),
51 _ => None
52 }
53 })
54 .collect::<Option<Box<[_]>>>()?;
55
56 Some(ProcMacroDef {
57 name: trait_name.as_name(),
58 kind: ProcMacroKind::CustomDerive { helpers },
59 })
60 }
61
62 _ => {
63 log::trace!("malformed `#[proc_macro_derive]`: {}", derive);
64 None
65 }
66 }
67 } else {
68 None
69 }
70 }
71}
diff --git a/crates/hir_def/src/nameres/tests/macros.rs b/crates/hir_def/src/nameres/tests/macros.rs
index f65a655bf..6d3cb8d7a 100644
--- a/crates/hir_def/src/nameres/tests/macros.rs
+++ b/crates/hir_def/src/nameres/tests/macros.rs
@@ -1,4 +1,5 @@
1use super::*; 1use super::*;
2use crate::nameres::proc_macro::{ProcMacroDef, ProcMacroKind};
2 3
3#[test] 4#[test]
4fn macro_rules_are_globally_visible() { 5fn macro_rules_are_globally_visible() {
@@ -712,6 +713,27 @@ b! { static = #[] ();}
712} 713}
713 714
714#[test] 715#[test]
716fn macros_defining_macros() {
717 check(
718 r#"
719macro_rules! item {
720 ($item:item) => { $item }
721}
722
723item! {
724 macro_rules! indirect_macro { () => { struct S {} } }
725}
726
727indirect_macro!();
728 "#,
729 expect![[r#"
730 crate
731 S: t
732 "#]],
733 );
734}
735
736#[test]
715fn resolves_proc_macros() { 737fn resolves_proc_macros() {
716 check( 738 check(
717 r" 739 r"
@@ -790,3 +812,28 @@ fn proc_macro_censoring() {
790 "#]], 812 "#]],
791 ); 813 );
792} 814}
815
816#[test]
817fn collects_derive_helpers() {
818 let def_map = compute_crate_def_map(
819 r"
820 struct TokenStream;
821
822 #[proc_macro_derive(AnotherTrait, attributes(helper_attr))]
823 pub fn derive_macro_2(_item: TokenStream) -> TokenStream {
824 TokenStream
825 }
826 ",
827 );
828
829 assert_eq!(def_map.exported_proc_macros.len(), 1);
830 match def_map.exported_proc_macros.values().next() {
831 Some(ProcMacroDef { kind: ProcMacroKind::CustomDerive { helpers }, .. }) => {
832 match &**helpers {
833 [attr] => assert_eq!(attr.to_string(), "helper_attr"),
834 _ => unreachable!(),
835 }
836 }
837 _ => unreachable!(),
838 }
839}
diff --git a/crates/hir_def/src/nameres/tests/mod_resolution.rs b/crates/hir_def/src/nameres/tests/mod_resolution.rs
index dfbbad1f9..16a2cd27a 100644
--- a/crates/hir_def/src/nameres/tests/mod_resolution.rs
+++ b/crates/hir_def/src/nameres/tests/mod_resolution.rs
@@ -819,3 +819,22 @@ pub mod hash { pub trait Hash {} }
819 "#]], 819 "#]],
820 ); 820 );
821} 821}
822
823#[test]
824fn cfg_in_module_file() {
825 // Inner `#![cfg]` in a module file makes the whole module disappear.
826 check(
827 r#"
828//- /main.rs
829mod module;
830
831//- /module.rs
832#![cfg(NEVER)]
833
834struct AlsoShoulntAppear;
835 "#,
836 expect![[r#"
837 crate
838 "#]],
839 )
840}
diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs
index 42736171e..04ea9c5d7 100644
--- a/crates/hir_def/src/resolver.rs
+++ b/crates/hir_def/src/resolver.rs
@@ -12,7 +12,7 @@ use crate::{
12 body::scope::{ExprScopes, ScopeId}, 12 body::scope::{ExprScopes, ScopeId},
13 builtin_type::BuiltinType, 13 builtin_type::BuiltinType,
14 db::DefDatabase, 14 db::DefDatabase,
15 expr::{ExprId, PatId}, 15 expr::{ExprId, LabelId, PatId},
16 generics::GenericParams, 16 generics::GenericParams,
17 item_scope::{BuiltinShadowMode, BUILTIN_SCOPE}, 17 item_scope::{BuiltinShadowMode, BUILTIN_SCOPE},
18 nameres::DefMap, 18 nameres::DefMap,
@@ -409,6 +409,7 @@ pub enum ScopeDef {
409 AdtSelfType(AdtId), 409 AdtSelfType(AdtId),
410 GenericParam(GenericParamId), 410 GenericParam(GenericParamId),
411 Local(PatId), 411 Local(PatId),
412 Label(LabelId),
412} 413}
413 414
414impl Scope { 415impl Scope {
@@ -470,6 +471,9 @@ impl Scope {
470 f(name![Self], ScopeDef::AdtSelfType(*i)); 471 f(name![Self], ScopeDef::AdtSelfType(*i));
471 } 472 }
472 Scope::ExprScope(scope) => { 473 Scope::ExprScope(scope) => {
474 if let Some((label, name)) = scope.expr_scopes.label(scope.scope_id) {
475 f(name, ScopeDef::Label(label))
476 }
473 scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| { 477 scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| {
474 f(e.name().clone(), ScopeDef::Local(e.pat())); 478 f(e.name().clone(), ScopeDef::Local(e.pat()));
475 }); 479 });