]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/item_tree/lower.rs
Merge #9428
[rust.git] / crates / hir_def / src / item_tree / lower.rs
1 //! AST -> `ItemTree` lowering code.
2
3 use std::{collections::hash_map::Entry, mem, sync::Arc};
4
5 use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, name::known, HirFileId};
6 use syntax::{
7     ast::{self, ModuleItemOwner},
8     SyntaxNode, WalkEvent,
9 };
10
11 use crate::{
12     generics::{GenericParams, TypeParamData, TypeParamProvenance},
13     type_ref::{LifetimeRef, TraitRef},
14 };
15
16 use super::*;
17
18 fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
19     FileItemTreeId { index, _p: PhantomData }
20 }
21
22 pub(super) struct Ctx<'a> {
23     db: &'a dyn DefDatabase,
24     tree: ItemTree,
25     hygiene: Hygiene,
26     source_ast_id_map: Arc<AstIdMap>,
27     body_ctx: crate::body::LowerCtx<'a>,
28     forced_visibility: Option<RawVisibilityId>,
29 }
30
31 impl<'a> Ctx<'a> {
32     pub(super) fn new(db: &'a dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self {
33         Self {
34             db,
35             tree: ItemTree::default(),
36             hygiene,
37             source_ast_id_map: db.ast_id_map(file),
38             body_ctx: crate::body::LowerCtx::new(db, file),
39             forced_visibility: None,
40         }
41     }
42
43     pub(super) fn lower_module_items(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree {
44         self.tree.top_level =
45             item_owner.items().flat_map(|item| self.lower_mod_item(&item, false)).collect();
46         self.tree
47     }
48
49     pub(super) fn lower_macro_stmts(mut self, stmts: ast::MacroStmts) -> ItemTree {
50         self.tree.top_level = stmts
51             .statements()
52             .filter_map(|stmt| match stmt {
53                 ast::Stmt::Item(item) => Some(item),
54                 // Macro calls can be both items and expressions. The syntax library always treats
55                 // them as expressions here, so we undo that.
56                 ast::Stmt::ExprStmt(es) => match es.expr()? {
57                     ast::Expr::MacroCall(call) => {
58                         cov_mark::hit!(macro_call_in_macro_stmts_is_added_to_item_tree);
59                         Some(call.into())
60                     }
61                     _ => None,
62                 },
63                 _ => None,
64             })
65             .flat_map(|item| self.lower_mod_item(&item, false))
66             .collect();
67
68         // Non-items need to have their inner items collected.
69         for stmt in stmts.statements() {
70             match stmt {
71                 ast::Stmt::ExprStmt(_) | ast::Stmt::LetStmt(_) => {
72                     self.collect_inner_items(stmt.syntax())
73                 }
74                 _ => {}
75             }
76         }
77         if let Some(expr) = stmts.expr() {
78             self.collect_inner_items(expr.syntax());
79         }
80         self.tree
81     }
82
83     pub(super) fn lower_inner_items(mut self, within: &SyntaxNode) -> ItemTree {
84         self.collect_inner_items(within);
85         self.tree
86     }
87
88     fn data(&mut self) -> &mut ItemTreeData {
89         self.tree.data_mut()
90     }
91
92     fn lower_mod_item(&mut self, item: &ast::Item, inner: bool) -> Option<ModItem> {
93         // Collect inner items for 1-to-1-lowered items.
94         match item {
95             ast::Item::Struct(_)
96             | ast::Item::Union(_)
97             | ast::Item::Enum(_)
98             | ast::Item::Fn(_)
99             | ast::Item::TypeAlias(_)
100             | ast::Item::Const(_)
101             | ast::Item::Static(_) => {
102                 // Skip this if we're already collecting inner items. We'll descend into all nodes
103                 // already.
104                 if !inner {
105                     self.collect_inner_items(item.syntax());
106                 }
107             }
108
109             // These are handled in their respective `lower_X` method (since we can't just blindly
110             // walk them).
111             ast::Item::Trait(_) | ast::Item::Impl(_) | ast::Item::ExternBlock(_) => {}
112
113             // These don't have inner items.
114             ast::Item::Module(_)
115             | ast::Item::ExternCrate(_)
116             | ast::Item::Use(_)
117             | ast::Item::MacroCall(_)
118             | ast::Item::MacroRules(_)
119             | ast::Item::MacroDef(_) => {}
120         };
121
122         let attrs = RawAttrs::new(self.db, item, &self.hygiene);
123         let item: ModItem = match item {
124             ast::Item::Struct(ast) => self.lower_struct(ast)?.into(),
125             ast::Item::Union(ast) => self.lower_union(ast)?.into(),
126             ast::Item::Enum(ast) => self.lower_enum(ast)?.into(),
127             ast::Item::Fn(ast) => self.lower_function(ast)?.into(),
128             ast::Item::TypeAlias(ast) => self.lower_type_alias(ast)?.into(),
129             ast::Item::Static(ast) => self.lower_static(ast)?.into(),
130             ast::Item::Const(ast) => self.lower_const(ast).into(),
131             ast::Item::Module(ast) => self.lower_module(ast)?.into(),
132             ast::Item::Trait(ast) => self.lower_trait(ast)?.into(),
133             ast::Item::Impl(ast) => self.lower_impl(ast)?.into(),
134             ast::Item::Use(ast) => self.lower_use(ast)?.into(),
135             ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast)?.into(),
136             ast::Item::MacroCall(ast) => self.lower_macro_call(ast)?.into(),
137             ast::Item::MacroRules(ast) => self.lower_macro_rules(ast)?.into(),
138             ast::Item::MacroDef(ast) => self.lower_macro_def(ast)?.into(),
139             ast::Item::ExternBlock(ast) => self.lower_extern_block(ast).into(),
140         };
141
142         self.add_attrs(item.into(), attrs);
143
144         Some(item)
145     }
146
147     fn add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs) {
148         match self.tree.attrs.entry(item) {
149             Entry::Occupied(mut entry) => {
150                 *entry.get_mut() = entry.get().merge(attrs);
151             }
152             Entry::Vacant(entry) => {
153                 entry.insert(attrs);
154             }
155         }
156     }
157
158     fn collect_inner_items(&mut self, container: &SyntaxNode) {
159         let forced_vis = self.forced_visibility.take();
160
161         let mut block_stack = Vec::new();
162
163         // if container itself is block, add it to the stack
164         if let Some(block) = ast::BlockExpr::cast(container.clone()) {
165             block_stack.push(self.source_ast_id_map.ast_id(&block));
166         }
167
168         for event in container.preorder().skip(1) {
169             match event {
170                 WalkEvent::Enter(node) => {
171                     match_ast! {
172                         match node {
173                             ast::BlockExpr(block) => {
174                                 block_stack.push(self.source_ast_id_map.ast_id(&block));
175                             },
176                             ast::Item(item) => {
177                                 // FIXME: This triggers for macro calls in expression/pattern/type position
178                                 let mod_item = self.lower_mod_item(&item, true);
179                                 let current_block = block_stack.last();
180                                 if let (Some(mod_item), Some(block)) = (mod_item, current_block) {
181                                         self.data().inner_items.entry(*block).or_default().push(mod_item);
182                                 }
183                             },
184                             _ => {}
185                         }
186                     }
187                 }
188                 WalkEvent::Leave(node) => {
189                     if ast::BlockExpr::cast(node).is_some() {
190                         block_stack.pop();
191                     }
192                 }
193             }
194         }
195
196         self.forced_visibility = forced_vis;
197     }
198
199     fn lower_assoc_item(&mut self, item: &ast::AssocItem) -> Option<AssocItem> {
200         match item {
201             ast::AssocItem::Fn(ast) => self.lower_function(ast).map(Into::into),
202             ast::AssocItem::TypeAlias(ast) => self.lower_type_alias(ast).map(Into::into),
203             ast::AssocItem::Const(ast) => Some(self.lower_const(ast).into()),
204             ast::AssocItem::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
205         }
206     }
207
208     fn lower_struct(&mut self, strukt: &ast::Struct) -> Option<FileItemTreeId<Struct>> {
209         let visibility = self.lower_visibility(strukt);
210         let name = strukt.name()?.as_name();
211         let generic_params = self.lower_generic_params(GenericsOwner::Struct, strukt);
212         let fields = self.lower_fields(&strukt.kind());
213         let ast_id = self.source_ast_id_map.ast_id(strukt);
214         let res = Struct { name, visibility, generic_params, fields, ast_id };
215         Some(id(self.data().structs.alloc(res)))
216     }
217
218     fn lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields {
219         match strukt_kind {
220             ast::StructKind::Record(it) => {
221                 let range = self.lower_record_fields(it);
222                 Fields::Record(range)
223             }
224             ast::StructKind::Tuple(it) => {
225                 let range = self.lower_tuple_fields(it);
226                 Fields::Tuple(range)
227             }
228             ast::StructKind::Unit => Fields::Unit,
229         }
230     }
231
232     fn lower_record_fields(&mut self, fields: &ast::RecordFieldList) -> IdRange<Field> {
233         let start = self.next_field_idx();
234         for field in fields.fields() {
235             if let Some(data) = self.lower_record_field(&field) {
236                 let idx = self.data().fields.alloc(data);
237                 self.add_attrs(idx.into(), RawAttrs::new(self.db, &field, &self.hygiene));
238             }
239         }
240         let end = self.next_field_idx();
241         IdRange::new(start..end)
242     }
243
244     fn lower_record_field(&mut self, field: &ast::RecordField) -> Option<Field> {
245         let name = field.name()?.as_name();
246         let visibility = self.lower_visibility(field);
247         let type_ref = self.lower_type_ref_opt(field.ty());
248         let res = Field { name, type_ref, visibility };
249         Some(res)
250     }
251
252     fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldList) -> IdRange<Field> {
253         let start = self.next_field_idx();
254         for (i, field) in fields.fields().enumerate() {
255             let data = self.lower_tuple_field(i, &field);
256             let idx = self.data().fields.alloc(data);
257             self.add_attrs(idx.into(), RawAttrs::new(self.db, &field, &self.hygiene));
258         }
259         let end = self.next_field_idx();
260         IdRange::new(start..end)
261     }
262
263     fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleField) -> Field {
264         let name = Name::new_tuple_field(idx);
265         let visibility = self.lower_visibility(field);
266         let type_ref = self.lower_type_ref_opt(field.ty());
267         let res = Field { name, type_ref, visibility };
268         res
269     }
270
271     fn lower_union(&mut self, union: &ast::Union) -> Option<FileItemTreeId<Union>> {
272         let visibility = self.lower_visibility(union);
273         let name = union.name()?.as_name();
274         let generic_params = self.lower_generic_params(GenericsOwner::Union, union);
275         let fields = match union.record_field_list() {
276             Some(record_field_list) => self.lower_fields(&StructKind::Record(record_field_list)),
277             None => Fields::Record(IdRange::new(self.next_field_idx()..self.next_field_idx())),
278         };
279         let ast_id = self.source_ast_id_map.ast_id(union);
280         let res = Union { name, visibility, generic_params, fields, ast_id };
281         Some(id(self.data().unions.alloc(res)))
282     }
283
284     fn lower_enum(&mut self, enum_: &ast::Enum) -> Option<FileItemTreeId<Enum>> {
285         let visibility = self.lower_visibility(enum_);
286         let name = enum_.name()?.as_name();
287         let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_);
288         let variants =
289             self.with_inherited_visibility(visibility, |this| match &enum_.variant_list() {
290                 Some(variant_list) => this.lower_variants(variant_list),
291                 None => IdRange::new(this.next_variant_idx()..this.next_variant_idx()),
292             });
293         let ast_id = self.source_ast_id_map.ast_id(enum_);
294         let res = Enum { name, visibility, generic_params, variants, ast_id };
295         Some(id(self.data().enums.alloc(res)))
296     }
297
298     fn lower_variants(&mut self, variants: &ast::VariantList) -> IdRange<Variant> {
299         let start = self.next_variant_idx();
300         for variant in variants.variants() {
301             if let Some(data) = self.lower_variant(&variant) {
302                 let idx = self.data().variants.alloc(data);
303                 self.add_attrs(idx.into(), RawAttrs::new(self.db, &variant, &self.hygiene));
304             }
305         }
306         let end = self.next_variant_idx();
307         IdRange::new(start..end)
308     }
309
310     fn lower_variant(&mut self, variant: &ast::Variant) -> Option<Variant> {
311         let name = variant.name()?.as_name();
312         let fields = self.lower_fields(&variant.kind());
313         let res = Variant { name, fields };
314         Some(res)
315     }
316
317     fn lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>> {
318         let visibility = self.lower_visibility(func);
319         let name = func.name()?.as_name();
320
321         let mut has_self_param = false;
322         let start_param = self.next_param_idx();
323         if let Some(param_list) = func.param_list() {
324             if let Some(self_param) = param_list.self_param() {
325                 let self_type = match self_param.ty() {
326                     Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
327                     None => {
328                         let self_type = TypeRef::Path(name![Self].into());
329                         match self_param.kind() {
330                             ast::SelfParamKind::Owned => self_type,
331                             ast::SelfParamKind::Ref => TypeRef::Reference(
332                                 Box::new(self_type),
333                                 self_param.lifetime().as_ref().map(LifetimeRef::new),
334                                 Mutability::Shared,
335                             ),
336                             ast::SelfParamKind::MutRef => TypeRef::Reference(
337                                 Box::new(self_type),
338                                 self_param.lifetime().as_ref().map(LifetimeRef::new),
339                                 Mutability::Mut,
340                             ),
341                         }
342                     }
343                 };
344                 let ty = Interned::new(self_type);
345                 let idx = self.data().params.alloc(Param::Normal(ty));
346                 self.add_attrs(idx.into(), RawAttrs::new(self.db, &self_param, &self.hygiene));
347                 has_self_param = true;
348             }
349             for param in param_list.params() {
350                 let idx = match param.dotdotdot_token() {
351                     Some(_) => self.data().params.alloc(Param::Varargs),
352                     None => {
353                         let type_ref = TypeRef::from_ast_opt(&self.body_ctx, param.ty());
354                         let ty = Interned::new(type_ref);
355                         self.data().params.alloc(Param::Normal(ty))
356                     }
357                 };
358                 self.add_attrs(idx.into(), RawAttrs::new(self.db, &param, &self.hygiene));
359             }
360         }
361         let end_param = self.next_param_idx();
362         let params = IdRange::new(start_param..end_param);
363
364         let ret_type = match func.ret_type().and_then(|rt| rt.ty()) {
365             Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
366             _ => TypeRef::unit(),
367         };
368
369         let (ret_type, async_ret_type) = if func.async_token().is_some() {
370             let async_ret_type = ret_type.clone();
371             let future_impl = desugar_future_path(ret_type);
372             let ty_bound = Interned::new(TypeBound::Path(future_impl));
373             (TypeRef::ImplTrait(vec![ty_bound]), Some(async_ret_type))
374         } else {
375             (ret_type, None)
376         };
377
378         let abi = func.abi().map(lower_abi);
379
380         let ast_id = self.source_ast_id_map.ast_id(func);
381
382         let mut flags = FnFlags::default();
383         if func.body().is_some() {
384             flags.bits |= FnFlags::HAS_BODY;
385         }
386         if has_self_param {
387             flags.bits |= FnFlags::HAS_SELF_PARAM;
388         }
389         if func.default_token().is_some() {
390             flags.bits |= FnFlags::IS_DEFAULT;
391         }
392         if func.const_token().is_some() {
393             flags.bits |= FnFlags::IS_CONST;
394         }
395         if func.async_token().is_some() {
396             flags.bits |= FnFlags::IS_ASYNC;
397         }
398         if func.unsafe_token().is_some() {
399             flags.bits |= FnFlags::IS_UNSAFE;
400         }
401
402         let mut res = Function {
403             name,
404             visibility,
405             generic_params: Interned::new(GenericParams::default()),
406             abi,
407             params,
408             ret_type: Interned::new(ret_type),
409             async_ret_type: async_ret_type.map(Interned::new),
410             ast_id,
411             flags,
412         };
413         res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func);
414
415         Some(id(self.data().functions.alloc(res)))
416     }
417
418     fn lower_type_alias(
419         &mut self,
420         type_alias: &ast::TypeAlias,
421     ) -> Option<FileItemTreeId<TypeAlias>> {
422         let name = type_alias.name()?.as_name();
423         let type_ref = type_alias.ty().map(|it| self.lower_type_ref(&it));
424         let visibility = self.lower_visibility(type_alias);
425         let bounds = self.lower_type_bounds(type_alias);
426         let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias);
427         let ast_id = self.source_ast_id_map.ast_id(type_alias);
428         let res = TypeAlias {
429             name,
430             visibility,
431             bounds: bounds.into_boxed_slice(),
432             generic_params,
433             type_ref,
434             ast_id,
435             is_extern: false,
436         };
437         Some(id(self.data().type_aliases.alloc(res)))
438     }
439
440     fn lower_static(&mut self, static_: &ast::Static) -> Option<FileItemTreeId<Static>> {
441         let name = static_.name()?.as_name();
442         let type_ref = self.lower_type_ref_opt(static_.ty());
443         let visibility = self.lower_visibility(static_);
444         let mutable = static_.mut_token().is_some();
445         let ast_id = self.source_ast_id_map.ast_id(static_);
446         let res = Static { name, visibility, mutable, type_ref, ast_id, is_extern: false };
447         Some(id(self.data().statics.alloc(res)))
448     }
449
450     fn lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const> {
451         let name = konst.name().map(|it| it.as_name());
452         let type_ref = self.lower_type_ref_opt(konst.ty());
453         let visibility = self.lower_visibility(konst);
454         let ast_id = self.source_ast_id_map.ast_id(konst);
455         let res = Const { name, visibility, type_ref, ast_id };
456         id(self.data().consts.alloc(res))
457     }
458
459     fn lower_module(&mut self, module: &ast::Module) -> Option<FileItemTreeId<Mod>> {
460         let name = module.name()?.as_name();
461         let visibility = self.lower_visibility(module);
462         let kind = if module.semicolon_token().is_some() {
463             ModKind::Outline {}
464         } else {
465             ModKind::Inline {
466                 items: module
467                     .item_list()
468                     .map(|list| {
469                         list.items().flat_map(|item| self.lower_mod_item(&item, false)).collect()
470                     })
471                     .unwrap_or_else(|| {
472                         cov_mark::hit!(name_res_works_for_broken_modules);
473                         Box::new([]) as Box<[_]>
474                     }),
475             }
476         };
477         let ast_id = self.source_ast_id_map.ast_id(module);
478         let res = Mod { name, visibility, kind, ast_id };
479         Some(id(self.data().mods.alloc(res)))
480     }
481
482     fn lower_trait(&mut self, trait_def: &ast::Trait) -> Option<FileItemTreeId<Trait>> {
483         let name = trait_def.name()?.as_name();
484         let visibility = self.lower_visibility(trait_def);
485         let generic_params =
486             self.lower_generic_params_and_inner_items(GenericsOwner::Trait(trait_def), trait_def);
487         let is_auto = trait_def.auto_token().is_some();
488         let is_unsafe = trait_def.unsafe_token().is_some();
489         let items = trait_def.assoc_item_list().map(|list| {
490             let db = self.db;
491             self.with_inherited_visibility(visibility, |this| {
492                 list.assoc_items()
493                     .filter_map(|item| {
494                         let attrs = RawAttrs::new(db, &item, &this.hygiene);
495                         this.collect_inner_items(item.syntax());
496                         this.lower_assoc_item(&item).map(|item| {
497                             this.add_attrs(ModItem::from(item).into(), attrs);
498                             item
499                         })
500                     })
501                     .collect()
502             })
503         });
504         let ast_id = self.source_ast_id_map.ast_id(trait_def);
505         let res = Trait {
506             name,
507             visibility,
508             generic_params,
509             is_auto,
510             is_unsafe,
511             items: items.unwrap_or_default(),
512             ast_id,
513         };
514         Some(id(self.data().traits.alloc(res)))
515     }
516
517     fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> {
518         let generic_params =
519             self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def);
520         // FIXME: If trait lowering fails, due to a non PathType for example, we treat this impl
521         // as if it was an non-trait impl. Ideally we want to create a unique missing ref that only
522         // equals itself.
523         let target_trait = impl_def.trait_().and_then(|tr| self.lower_trait_ref(&tr));
524         let self_ty = self.lower_type_ref(&impl_def.self_ty()?);
525         let is_negative = impl_def.excl_token().is_some();
526
527         // We cannot use `assoc_items()` here as that does not include macro calls.
528         let items = impl_def
529             .assoc_item_list()
530             .into_iter()
531             .flat_map(|it| it.assoc_items())
532             .filter_map(|item| {
533                 self.collect_inner_items(item.syntax());
534                 let assoc = self.lower_assoc_item(&item)?;
535                 let attrs = RawAttrs::new(self.db, &item, &self.hygiene);
536                 self.add_attrs(ModItem::from(assoc).into(), attrs);
537                 Some(assoc)
538             })
539             .collect();
540         let ast_id = self.source_ast_id_map.ast_id(impl_def);
541         let res = Impl { generic_params, target_trait, self_ty, is_negative, items, ast_id };
542         Some(id(self.data().impls.alloc(res)))
543     }
544
545     fn lower_use(&mut self, use_item: &ast::Use) -> Option<FileItemTreeId<Import>> {
546         let visibility = self.lower_visibility(use_item);
547         let ast_id = self.source_ast_id_map.ast_id(use_item);
548         let (use_tree, _) = lower_use_tree(self.db, &self.hygiene, use_item.use_tree()?)?;
549
550         let res = Import { visibility, ast_id, use_tree };
551         Some(id(self.data().imports.alloc(res)))
552     }
553
554     fn lower_extern_crate(
555         &mut self,
556         extern_crate: &ast::ExternCrate,
557     ) -> Option<FileItemTreeId<ExternCrate>> {
558         let name = extern_crate.name_ref()?.as_name();
559         let alias = extern_crate.rename().map(|a| {
560             a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
561         });
562         let visibility = self.lower_visibility(extern_crate);
563         let ast_id = self.source_ast_id_map.ast_id(extern_crate);
564
565         let res = ExternCrate { name, alias, visibility, ast_id };
566         Some(id(self.data().extern_crates.alloc(res)))
567     }
568
569     fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> {
570         let path = Interned::new(ModPath::from_src(self.db, m.path()?, &self.hygiene)?);
571         let ast_id = self.source_ast_id_map.ast_id(m);
572         let fragment = hir_expand::to_fragment_kind(m);
573         let res = MacroCall { path, ast_id, fragment };
574         Some(id(self.data().macro_calls.alloc(res)))
575     }
576
577     fn lower_macro_rules(&mut self, m: &ast::MacroRules) -> Option<FileItemTreeId<MacroRules>> {
578         let name = m.name().map(|it| it.as_name())?;
579         let ast_id = self.source_ast_id_map.ast_id(m);
580
581         let res = MacroRules { name, ast_id };
582         Some(id(self.data().macro_rules.alloc(res)))
583     }
584
585     fn lower_macro_def(&mut self, m: &ast::MacroDef) -> Option<FileItemTreeId<MacroDef>> {
586         let name = m.name().map(|it| it.as_name())?;
587
588         let ast_id = self.source_ast_id_map.ast_id(m);
589         let visibility = self.lower_visibility(m);
590
591         let res = MacroDef { name, ast_id, visibility };
592         Some(id(self.data().macro_defs.alloc(res)))
593     }
594
595     fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> FileItemTreeId<ExternBlock> {
596         let ast_id = self.source_ast_id_map.ast_id(block);
597         let abi = block.abi().map(lower_abi);
598         let children: Box<[_]> = block.extern_item_list().map_or(Box::new([]), |list| {
599             list.extern_items()
600                 .filter_map(|item| {
601                     self.collect_inner_items(item.syntax());
602                     let attrs = RawAttrs::new(self.db, &item, &self.hygiene);
603                     let id: ModItem = match item {
604                         ast::ExternItem::Fn(ast) => {
605                             let func_id = self.lower_function(&ast)?;
606                             let func = &mut self.data().functions[func_id.index];
607                             if is_intrinsic_fn_unsafe(&func.name) {
608                                 func.flags.bits |= FnFlags::IS_UNSAFE;
609                             }
610                             func.flags.bits |= FnFlags::IS_IN_EXTERN_BLOCK;
611                             func_id.into()
612                         }
613                         ast::ExternItem::Static(ast) => {
614                             let statik = self.lower_static(&ast)?;
615                             self.data().statics[statik.index].is_extern = true;
616                             statik.into()
617                         }
618                         ast::ExternItem::TypeAlias(ty) => {
619                             let foreign_ty = self.lower_type_alias(&ty)?;
620                             self.data().type_aliases[foreign_ty.index].is_extern = true;
621                             foreign_ty.into()
622                         }
623                         ast::ExternItem::MacroCall(call) => {
624                             // FIXME: we need some way of tracking that the macro call is in an
625                             // extern block
626                             self.lower_macro_call(&call)?.into()
627                         }
628                     };
629                     self.add_attrs(id.into(), attrs);
630                     Some(id)
631                 })
632                 .collect()
633         });
634
635         let res = ExternBlock { abi, ast_id, children };
636         id(self.data().extern_blocks.alloc(res))
637     }
638
639     /// Lowers generics defined on `node` and collects inner items defined within.
640     fn lower_generic_params_and_inner_items(
641         &mut self,
642         owner: GenericsOwner<'_>,
643         node: &impl ast::GenericParamsOwner,
644     ) -> Interned<GenericParams> {
645         // Generics are part of item headers and may contain inner items we need to collect.
646         if let Some(params) = node.generic_param_list() {
647             self.collect_inner_items(params.syntax());
648         }
649         if let Some(clause) = node.where_clause() {
650             self.collect_inner_items(clause.syntax());
651         }
652
653         self.lower_generic_params(owner, node)
654     }
655
656     fn lower_generic_params(
657         &mut self,
658         owner: GenericsOwner<'_>,
659         node: &impl ast::GenericParamsOwner,
660     ) -> Interned<GenericParams> {
661         let mut sm = &mut Default::default();
662         let mut generics = GenericParams::default();
663         match owner {
664             GenericsOwner::Function(func) => {
665                 generics.fill(&self.body_ctx, sm, node);
666                 // lower `impl Trait` in arguments
667                 for id in func.params.clone() {
668                     if let Param::Normal(ty) = &self.data().params[id] {
669                         generics.fill_implicit_impl_trait_args(ty);
670                     }
671                 }
672             }
673             GenericsOwner::Struct
674             | GenericsOwner::Enum
675             | GenericsOwner::Union
676             | GenericsOwner::TypeAlias => {
677                 generics.fill(&self.body_ctx, sm, node);
678             }
679             GenericsOwner::Trait(trait_def) => {
680                 // traits get the Self type as an implicit first type parameter
681                 let self_param_id = generics.types.alloc(TypeParamData {
682                     name: Some(name![Self]),
683                     default: None,
684                     provenance: TypeParamProvenance::TraitSelf,
685                 });
686                 sm.type_params.insert(self_param_id, Either::Right(trait_def.clone()));
687                 // add super traits as bounds on Self
688                 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
689                 let self_param = TypeRef::Path(name![Self].into());
690                 generics.fill_bounds(&self.body_ctx, trait_def, Either::Left(self_param));
691                 generics.fill(&self.body_ctx, &mut sm, node);
692             }
693             GenericsOwner::Impl => {
694                 // Note that we don't add `Self` here: in `impl`s, `Self` is not a
695                 // type-parameter, but rather is a type-alias for impl's target
696                 // type, so this is handled by the resolver.
697                 generics.fill(&self.body_ctx, &mut sm, node);
698             }
699         }
700
701         generics.shrink_to_fit();
702         Interned::new(generics)
703     }
704
705     fn lower_type_bounds(&mut self, node: &impl ast::TypeBoundsOwner) -> Vec<Interned<TypeBound>> {
706         match node.type_bound_list() {
707             Some(bound_list) => bound_list
708                 .bounds()
709                 .map(|it| Interned::new(TypeBound::from_ast(&self.body_ctx, it)))
710                 .collect(),
711             None => Vec::new(),
712         }
713     }
714
715     fn lower_visibility(&mut self, item: &impl ast::VisibilityOwner) -> RawVisibilityId {
716         let vis = match self.forced_visibility {
717             Some(vis) => return vis,
718             None => RawVisibility::from_ast_with_hygiene(self.db, item.visibility(), &self.hygiene),
719         };
720
721         self.data().vis.alloc(vis)
722     }
723
724     fn lower_trait_ref(&mut self, trait_ref: &ast::Type) -> Option<Interned<TraitRef>> {
725         let trait_ref = TraitRef::from_ast(&self.body_ctx, trait_ref.clone())?;
726         Some(Interned::new(trait_ref))
727     }
728
729     fn lower_type_ref(&mut self, type_ref: &ast::Type) -> Interned<TypeRef> {
730         let tyref = TypeRef::from_ast(&self.body_ctx, type_ref.clone());
731         Interned::new(tyref)
732     }
733
734     fn lower_type_ref_opt(&mut self, type_ref: Option<ast::Type>) -> Interned<TypeRef> {
735         match type_ref.map(|ty| self.lower_type_ref(&ty)) {
736             Some(it) => it,
737             None => Interned::new(TypeRef::Error),
738         }
739     }
740
741     /// Forces the visibility `vis` to be used for all items lowered during execution of `f`.
742     fn with_inherited_visibility<R>(
743         &mut self,
744         vis: RawVisibilityId,
745         f: impl FnOnce(&mut Self) -> R,
746     ) -> R {
747         let old = mem::replace(&mut self.forced_visibility, Some(vis));
748         let res = f(self);
749         self.forced_visibility = old;
750         res
751     }
752
753     fn next_field_idx(&self) -> Idx<Field> {
754         Idx::from_raw(RawIdx::from(
755             self.tree.data.as_ref().map_or(0, |data| data.fields.len() as u32),
756         ))
757     }
758     fn next_variant_idx(&self) -> Idx<Variant> {
759         Idx::from_raw(RawIdx::from(
760             self.tree.data.as_ref().map_or(0, |data| data.variants.len() as u32),
761         ))
762     }
763     fn next_param_idx(&self) -> Idx<Param> {
764         Idx::from_raw(RawIdx::from(
765             self.tree.data.as_ref().map_or(0, |data| data.params.len() as u32),
766         ))
767     }
768 }
769
770 fn desugar_future_path(orig: TypeRef) -> Path {
771     let path = path![core::future::Future];
772     let mut generic_args: Vec<_> =
773         std::iter::repeat(None).take(path.segments().len() - 1).collect();
774     let mut last = GenericArgs::empty();
775     let binding =
776         AssociatedTypeBinding { name: name![Output], type_ref: Some(orig), bounds: Vec::new() };
777     last.bindings.push(binding);
778     generic_args.push(Some(Interned::new(last)));
779
780     Path::from_known_path(path, generic_args)
781 }
782
783 enum GenericsOwner<'a> {
784     /// We need access to the partially-lowered `Function` for lowering `impl Trait` in argument
785     /// position.
786     Function(&'a Function),
787     Struct,
788     Enum,
789     Union,
790     /// The `TraitDef` is needed to fill the source map for the implicit `Self` parameter.
791     Trait(&'a ast::Trait),
792     TypeAlias,
793     Impl,
794 }
795
796 /// Returns `true` if the given intrinsic is unsafe to call, or false otherwise.
797 fn is_intrinsic_fn_unsafe(name: &Name) -> bool {
798     // Should be kept in sync with https://github.com/rust-lang/rust/blob/c6e4db620a7d2f569f11dcab627430921ea8aacf/compiler/rustc_typeck/src/check/intrinsic.rs#L68
799     ![
800         known::abort,
801         known::min_align_of,
802         known::needs_drop,
803         known::caller_location,
804         known::size_of_val,
805         known::min_align_of_val,
806         known::add_with_overflow,
807         known::sub_with_overflow,
808         known::mul_with_overflow,
809         known::wrapping_add,
810         known::wrapping_sub,
811         known::wrapping_mul,
812         known::saturating_add,
813         known::saturating_sub,
814         known::rotate_left,
815         known::rotate_right,
816         known::ctpop,
817         known::ctlz,
818         known::cttz,
819         known::bswap,
820         known::bitreverse,
821         known::discriminant_value,
822         known::type_id,
823         known::likely,
824         known::unlikely,
825         known::ptr_guaranteed_eq,
826         known::ptr_guaranteed_ne,
827         known::minnumf32,
828         known::minnumf64,
829         known::maxnumf32,
830         known::rustc_peek,
831         known::maxnumf64,
832         known::type_name,
833         known::variant_count,
834     ]
835     .contains(name)
836 }
837
838 fn lower_abi(abi: ast::Abi) -> Interned<str> {
839     // FIXME: Abi::abi() -> Option<SyntaxToken>?
840     match abi.syntax().last_token() {
841         Some(tok) if tok.kind() == SyntaxKind::STRING => {
842             // FIXME: Better way to unescape?
843             Interned::new_str(tok.text().trim_matches('"'))
844         }
845         _ => {
846             // `extern` default to be `extern "C"`.
847             Interned::new_str("C")
848         }
849     }
850 }
851
852 struct UseTreeLowering<'a> {
853     db: &'a dyn DefDatabase,
854     hygiene: &'a Hygiene,
855     mapping: Arena<ast::UseTree>,
856 }
857
858 impl UseTreeLowering<'_> {
859     fn lower_use_tree(&mut self, tree: ast::UseTree) -> Option<UseTree> {
860         if let Some(use_tree_list) = tree.use_tree_list() {
861             let prefix = match tree.path() {
862                 // E.g. use something::{{{inner}}};
863                 None => None,
864                 // E.g. `use something::{inner}` (prefix is `None`, path is `something`)
865                 // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
866                 Some(path) => {
867                     match ModPath::from_src(self.db, path, self.hygiene) {
868                         Some(it) => Some(it),
869                         None => return None, // FIXME: report errors somewhere
870                     }
871                 }
872             };
873
874             let list =
875                 use_tree_list.use_trees().filter_map(|tree| self.lower_use_tree(tree)).collect();
876
877             Some(
878                 self.use_tree(
879                     UseTreeKind::Prefixed { prefix: prefix.map(Interned::new), list },
880                     tree,
881                 ),
882             )
883         } else {
884             let is_glob = tree.star_token().is_some();
885             let path = match tree.path() {
886                 Some(path) => Some(ModPath::from_src(self.db, path, self.hygiene)?),
887                 None => None,
888             };
889             let alias = tree.rename().map(|a| {
890                 a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
891             });
892             if alias.is_some() && is_glob {
893                 return None;
894             }
895
896             match (path, alias, is_glob) {
897                 (path, None, true) => {
898                     if path.is_none() {
899                         cov_mark::hit!(glob_enum_group);
900                     }
901                     Some(self.use_tree(UseTreeKind::Glob { path: path.map(Interned::new) }, tree))
902                 }
903                 // Globs can't be renamed
904                 (_, Some(_), true) | (None, None, false) => None,
905                 // `bla::{ as Name}` is invalid
906                 (None, Some(_), false) => None,
907                 (Some(path), alias, false) => Some(
908                     self.use_tree(UseTreeKind::Single { path: Interned::new(path), alias }, tree),
909                 ),
910             }
911         }
912     }
913
914     fn use_tree(&mut self, kind: UseTreeKind, ast: ast::UseTree) -> UseTree {
915         let index = self.mapping.alloc(ast);
916         UseTree { index, kind }
917     }
918 }
919
920 pub(super) fn lower_use_tree(
921     db: &dyn DefDatabase,
922     hygiene: &Hygiene,
923     tree: ast::UseTree,
924 ) -> Option<(UseTree, Arena<ast::UseTree>)> {
925     let mut lowering = UseTreeLowering { db, hygiene, mapping: Arena::new() };
926     let tree = lowering.lower_use_tree(tree)?;
927     Some((tree, lowering.mapping))
928 }