]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/item_tree/lower.rs
Remove previously added parameter names from the function data
[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 arena::map::ArenaMap;
6 use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, HirFileId};
7 use smallvec::SmallVec;
8 use syntax::{
9     ast::{self, ModuleItemOwner},
10     SyntaxNode,
11 };
12
13 use crate::{
14     attr::Attrs,
15     generics::{GenericParams, TypeParamData, TypeParamProvenance},
16 };
17
18 use super::*;
19
20 fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
21     FileItemTreeId { index, _p: PhantomData }
22 }
23
24 struct ModItems(SmallVec<[ModItem; 1]>);
25
26 impl<T> From<T> for ModItems
27 where
28     T: Into<ModItem>,
29 {
30     fn from(t: T) -> Self {
31         ModItems(SmallVec::from_buf([t.into(); 1]))
32     }
33 }
34
35 pub(super) struct Ctx {
36     tree: ItemTree,
37     hygiene: Hygiene,
38     file: HirFileId,
39     source_ast_id_map: Arc<AstIdMap>,
40     body_ctx: crate::body::LowerCtx,
41     inner_items: Vec<ModItem>,
42     forced_visibility: Option<RawVisibilityId>,
43 }
44
45 impl Ctx {
46     pub(super) fn new(db: &dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self {
47         Self {
48             tree: ItemTree::empty(),
49             hygiene,
50             file,
51             source_ast_id_map: db.ast_id_map(file),
52             body_ctx: crate::body::LowerCtx::new(db, file),
53             inner_items: Vec::new(),
54             forced_visibility: None,
55         }
56     }
57
58     pub(super) fn lower_module_items(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree {
59         self.tree.top_level = item_owner
60             .items()
61             .flat_map(|item| self.lower_mod_item(&item, false))
62             .flat_map(|items| items.0)
63             .collect();
64         self.tree
65     }
66
67     pub(super) fn lower_inner_items(mut self, within: &SyntaxNode) -> ItemTree {
68         self.collect_inner_items(within);
69         self.tree
70     }
71
72     fn data(&mut self) -> &mut ItemTreeData {
73         self.tree.data_mut()
74     }
75
76     fn lower_mod_item(&mut self, item: &ast::Item, inner: bool) -> Option<ModItems> {
77         assert!(inner || self.inner_items.is_empty());
78
79         // Collect inner items for 1-to-1-lowered items.
80         match item {
81             ast::Item::Struct(_)
82             | ast::Item::Union(_)
83             | ast::Item::Enum(_)
84             | ast::Item::Fn(_)
85             | ast::Item::TypeAlias(_)
86             | ast::Item::Const(_)
87             | ast::Item::Static(_)
88             | ast::Item::MacroCall(_) => {
89                 // Skip this if we're already collecting inner items. We'll descend into all nodes
90                 // already.
91                 if !inner {
92                     self.collect_inner_items(item.syntax());
93                 }
94             }
95
96             // These are handled in their respective `lower_X` method (since we can't just blindly
97             // walk them).
98             ast::Item::Trait(_) | ast::Item::Impl(_) | ast::Item::ExternBlock(_) => {}
99
100             // These don't have inner items.
101             ast::Item::Module(_) | ast::Item::ExternCrate(_) | ast::Item::Use(_) => {}
102         };
103
104         let attrs = Attrs::new(item, &self.hygiene);
105         let items = match item {
106             ast::Item::Struct(ast) => self.lower_struct(ast).map(Into::into),
107             ast::Item::Union(ast) => self.lower_union(ast).map(Into::into),
108             ast::Item::Enum(ast) => self.lower_enum(ast).map(Into::into),
109             ast::Item::Fn(ast) => self.lower_function(ast).map(Into::into),
110             ast::Item::TypeAlias(ast) => self.lower_type_alias(ast).map(Into::into),
111             ast::Item::Static(ast) => self.lower_static(ast).map(Into::into),
112             ast::Item::Const(ast) => Some(self.lower_const(ast).into()),
113             ast::Item::Module(ast) => self.lower_module(ast).map(Into::into),
114             ast::Item::Trait(ast) => self.lower_trait(ast).map(Into::into),
115             ast::Item::Impl(ast) => self.lower_impl(ast).map(Into::into),
116             ast::Item::Use(ast) => Some(ModItems(
117                 self.lower_use(ast).into_iter().map(Into::into).collect::<SmallVec<_>>(),
118             )),
119             ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast).map(Into::into),
120             ast::Item::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
121             ast::Item::ExternBlock(ast) => {
122                 Some(ModItems(self.lower_extern_block(ast).into_iter().collect::<SmallVec<_>>()))
123             }
124         };
125
126         if !attrs.is_empty() {
127             for item in items.iter().flat_map(|items| &items.0) {
128                 self.add_attrs((*item).into(), attrs.clone());
129             }
130         }
131
132         items
133     }
134
135     fn add_attrs(&mut self, item: AttrOwner, attrs: Attrs) {
136         match self.tree.attrs.entry(item) {
137             Entry::Occupied(mut entry) => {
138                 *entry.get_mut() = entry.get().merge(attrs);
139             }
140             Entry::Vacant(entry) => {
141                 entry.insert(attrs);
142             }
143         }
144     }
145
146     fn collect_inner_items(&mut self, container: &SyntaxNode) {
147         let forced_vis = self.forced_visibility.take();
148         let mut inner_items = mem::take(&mut self.tree.inner_items);
149         inner_items.extend(container.descendants().skip(1).filter_map(ast::Item::cast).filter_map(
150             |item| {
151                 let ast_id = self.source_ast_id_map.ast_id(&item);
152                 Some((ast_id, self.lower_mod_item(&item, true)?.0))
153             },
154         ));
155         self.tree.inner_items = inner_items;
156         self.forced_visibility = forced_vis;
157     }
158
159     fn lower_assoc_item(&mut self, item: &ast::AssocItem) -> Option<AssocItem> {
160         match item {
161             ast::AssocItem::Fn(ast) => self.lower_function(ast).map(Into::into),
162             ast::AssocItem::TypeAlias(ast) => self.lower_type_alias(ast).map(Into::into),
163             ast::AssocItem::Const(ast) => Some(self.lower_const(ast).into()),
164             ast::AssocItem::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
165         }
166     }
167
168     fn lower_struct(&mut self, strukt: &ast::Struct) -> Option<FileItemTreeId<Struct>> {
169         let visibility = self.lower_visibility(strukt);
170         let name = strukt.name()?.as_name();
171         let generic_params = self.lower_generic_params(GenericsOwner::Struct, strukt);
172         let fields = self.lower_fields(&strukt.kind());
173         let ast_id = self.source_ast_id_map.ast_id(strukt);
174         let kind = match strukt.kind() {
175             ast::StructKind::Record(_) => StructDefKind::Record,
176             ast::StructKind::Tuple(_) => StructDefKind::Tuple,
177             ast::StructKind::Unit => StructDefKind::Unit,
178         };
179         let res = Struct { name, visibility, generic_params, fields, ast_id, kind };
180         Some(id(self.data().structs.alloc(res)))
181     }
182
183     fn lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields {
184         match strukt_kind {
185             ast::StructKind::Record(it) => {
186                 let range = self.lower_record_fields(it);
187                 Fields::Record(range)
188             }
189             ast::StructKind::Tuple(it) => {
190                 let range = self.lower_tuple_fields(it);
191                 Fields::Tuple(range)
192             }
193             ast::StructKind::Unit => Fields::Unit,
194         }
195     }
196
197     fn lower_record_fields(&mut self, fields: &ast::RecordFieldList) -> IdRange<Field> {
198         let start = self.next_field_idx();
199         for field in fields.fields() {
200             if let Some(data) = self.lower_record_field(&field) {
201                 let idx = self.data().fields.alloc(data);
202                 self.add_attrs(idx.into(), Attrs::new(&field, &self.hygiene));
203             }
204         }
205         let end = self.next_field_idx();
206         IdRange::new(start..end)
207     }
208
209     fn lower_record_field(&mut self, field: &ast::RecordField) -> Option<Field> {
210         let name = field.name()?.as_name();
211         let visibility = self.lower_visibility(field);
212         let type_ref = self.lower_type_ref_opt(field.ty());
213         let res = Field { name, type_ref, visibility };
214         Some(res)
215     }
216
217     fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldList) -> IdRange<Field> {
218         let start = self.next_field_idx();
219         for (i, field) in fields.fields().enumerate() {
220             let data = self.lower_tuple_field(i, &field);
221             let idx = self.data().fields.alloc(data);
222             self.add_attrs(idx.into(), Attrs::new(&field, &self.hygiene));
223         }
224         let end = self.next_field_idx();
225         IdRange::new(start..end)
226     }
227
228     fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleField) -> Field {
229         let name = Name::new_tuple_field(idx);
230         let visibility = self.lower_visibility(field);
231         let type_ref = self.lower_type_ref_opt(field.ty());
232         let res = Field { name, type_ref, visibility };
233         res
234     }
235
236     fn lower_union(&mut self, union: &ast::Union) -> Option<FileItemTreeId<Union>> {
237         let visibility = self.lower_visibility(union);
238         let name = union.name()?.as_name();
239         let generic_params = self.lower_generic_params(GenericsOwner::Union, union);
240         let fields = match union.record_field_list() {
241             Some(record_field_list) => self.lower_fields(&StructKind::Record(record_field_list)),
242             None => Fields::Record(IdRange::new(self.next_field_idx()..self.next_field_idx())),
243         };
244         let ast_id = self.source_ast_id_map.ast_id(union);
245         let res = Union { name, visibility, generic_params, fields, ast_id };
246         Some(id(self.data().unions.alloc(res)))
247     }
248
249     fn lower_enum(&mut self, enum_: &ast::Enum) -> Option<FileItemTreeId<Enum>> {
250         let visibility = self.lower_visibility(enum_);
251         let name = enum_.name()?.as_name();
252         let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_);
253         let variants = match &enum_.variant_list() {
254             Some(variant_list) => self.lower_variants(variant_list),
255             None => IdRange::new(self.next_variant_idx()..self.next_variant_idx()),
256         };
257         let ast_id = self.source_ast_id_map.ast_id(enum_);
258         let res = Enum { name, visibility, generic_params, variants, ast_id };
259         Some(id(self.data().enums.alloc(res)))
260     }
261
262     fn lower_variants(&mut self, variants: &ast::VariantList) -> IdRange<Variant> {
263         let start = self.next_variant_idx();
264         for variant in variants.variants() {
265             if let Some(data) = self.lower_variant(&variant) {
266                 let idx = self.data().variants.alloc(data);
267                 self.add_attrs(idx.into(), Attrs::new(&variant, &self.hygiene));
268             }
269         }
270         let end = self.next_variant_idx();
271         IdRange::new(start..end)
272     }
273
274     fn lower_variant(&mut self, variant: &ast::Variant) -> Option<Variant> {
275         let name = variant.name()?.as_name();
276         let fields = self.lower_fields(&variant.kind());
277         let res = Variant { name, fields };
278         Some(res)
279     }
280
281     fn lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>> {
282         let visibility = self.lower_visibility(func);
283         let name = func.name()?.as_name();
284
285         let mut params = Vec::new();
286         let mut has_self_param = false;
287         if let Some(param_list) = func.param_list() {
288             if let Some(self_param) = param_list.self_param() {
289                 let self_type = match self_param.ty() {
290                     Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
291                     None => {
292                         let self_type = TypeRef::Path(name![Self].into());
293                         match self_param.kind() {
294                             ast::SelfParamKind::Owned => self_type,
295                             ast::SelfParamKind::Ref => {
296                                 TypeRef::Reference(Box::new(self_type), Mutability::Shared)
297                             }
298                             ast::SelfParamKind::MutRef => {
299                                 TypeRef::Reference(Box::new(self_type), Mutability::Mut)
300                             }
301                         }
302                     }
303                 };
304                 params.push(self_type);
305                 has_self_param = true;
306             }
307             for param in param_list.params() {
308                 let type_ref = TypeRef::from_ast_opt(&self.body_ctx, param.ty());
309                 params.push(type_ref);
310             }
311         }
312
313         let mut is_varargs = false;
314         if let Some(params) = func.param_list() {
315             if let Some(last) = params.params().last() {
316                 is_varargs = last.dotdotdot_token().is_some();
317             }
318         }
319
320         let ret_type = match func.ret_type().and_then(|rt| rt.ty()) {
321             Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
322             _ => TypeRef::unit(),
323         };
324
325         let ret_type = if func.async_token().is_some() {
326             let future_impl = desugar_future_path(ret_type);
327             let ty_bound = TypeBound::Path(future_impl);
328             TypeRef::ImplTrait(vec![ty_bound])
329         } else {
330             ret_type
331         };
332
333         let has_body = func.body().is_some();
334
335         let ast_id = self.source_ast_id_map.ast_id(func);
336         let mut res = Function {
337             name,
338             visibility,
339             generic_params: GenericParamsId::EMPTY,
340             has_self_param,
341             has_body,
342             is_unsafe: func.unsafe_token().is_some(),
343             params: params.into_boxed_slice(),
344             is_varargs,
345             ret_type,
346             ast_id,
347         };
348         res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func);
349
350         Some(id(self.data().functions.alloc(res)))
351     }
352
353     fn lower_type_alias(
354         &mut self,
355         type_alias: &ast::TypeAlias,
356     ) -> Option<FileItemTreeId<TypeAlias>> {
357         let name = type_alias.name()?.as_name();
358         let type_ref = type_alias.ty().map(|it| self.lower_type_ref(&it));
359         let visibility = self.lower_visibility(type_alias);
360         let bounds = self.lower_type_bounds(type_alias);
361         let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias);
362         let ast_id = self.source_ast_id_map.ast_id(type_alias);
363         let res = TypeAlias {
364             name,
365             visibility,
366             bounds: bounds.into_boxed_slice(),
367             generic_params,
368             type_ref,
369             ast_id,
370             is_extern: false,
371         };
372         Some(id(self.data().type_aliases.alloc(res)))
373     }
374
375     fn lower_static(&mut self, static_: &ast::Static) -> Option<FileItemTreeId<Static>> {
376         let name = static_.name()?.as_name();
377         let type_ref = self.lower_type_ref_opt(static_.ty());
378         let visibility = self.lower_visibility(static_);
379         let mutable = static_.mut_token().is_some();
380         let ast_id = self.source_ast_id_map.ast_id(static_);
381         let res = Static { name, visibility, mutable, type_ref, ast_id };
382         Some(id(self.data().statics.alloc(res)))
383     }
384
385     fn lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const> {
386         let name = konst.name().map(|it| it.as_name());
387         let type_ref = self.lower_type_ref_opt(konst.ty());
388         let visibility = self.lower_visibility(konst);
389         let ast_id = self.source_ast_id_map.ast_id(konst);
390         let res = Const { name, visibility, type_ref, ast_id };
391         id(self.data().consts.alloc(res))
392     }
393
394     fn lower_module(&mut self, module: &ast::Module) -> Option<FileItemTreeId<Mod>> {
395         let name = module.name()?.as_name();
396         let visibility = self.lower_visibility(module);
397         let kind = if module.semicolon_token().is_some() {
398             ModKind::Outline {}
399         } else {
400             ModKind::Inline {
401                 items: module
402                     .item_list()
403                     .map(|list| {
404                         list.items()
405                             .flat_map(|item| self.lower_mod_item(&item, false))
406                             .flat_map(|items| items.0)
407                             .collect()
408                     })
409                     .unwrap_or_else(|| {
410                         mark::hit!(name_res_works_for_broken_modules);
411                         Box::new([]) as Box<[_]>
412                     }),
413             }
414         };
415         let ast_id = self.source_ast_id_map.ast_id(module);
416         let res = Mod { name, visibility, kind, ast_id };
417         Some(id(self.data().mods.alloc(res)))
418     }
419
420     fn lower_trait(&mut self, trait_def: &ast::Trait) -> Option<FileItemTreeId<Trait>> {
421         let name = trait_def.name()?.as_name();
422         let visibility = self.lower_visibility(trait_def);
423         let generic_params =
424             self.lower_generic_params_and_inner_items(GenericsOwner::Trait(trait_def), trait_def);
425         let auto = trait_def.auto_token().is_some();
426         let items = trait_def.assoc_item_list().map(|list| {
427             self.with_inherited_visibility(visibility, |this| {
428                 list.assoc_items()
429                     .filter_map(|item| {
430                         let attrs = Attrs::new(&item, &this.hygiene);
431                         this.collect_inner_items(item.syntax());
432                         this.lower_assoc_item(&item).map(|item| {
433                             this.add_attrs(ModItem::from(item).into(), attrs);
434                             item
435                         })
436                     })
437                     .collect()
438             })
439         });
440         let ast_id = self.source_ast_id_map.ast_id(trait_def);
441         let res = Trait {
442             name,
443             visibility,
444             generic_params,
445             auto,
446             items: items.unwrap_or_default(),
447             ast_id,
448         };
449         Some(id(self.data().traits.alloc(res)))
450     }
451
452     fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> {
453         let generic_params =
454             self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def);
455         let target_trait = impl_def.trait_().map(|tr| self.lower_type_ref(&tr));
456         let target_type = self.lower_type_ref(&impl_def.self_ty()?);
457         let is_negative = impl_def.excl_token().is_some();
458
459         // We cannot use `assoc_items()` here as that does not include macro calls.
460         let items = impl_def
461             .assoc_item_list()
462             .into_iter()
463             .flat_map(|it| it.assoc_items())
464             .filter_map(|item| {
465                 self.collect_inner_items(item.syntax());
466                 let assoc = self.lower_assoc_item(&item)?;
467                 let attrs = Attrs::new(&item, &self.hygiene);
468                 self.add_attrs(ModItem::from(assoc).into(), attrs);
469                 Some(assoc)
470             })
471             .collect();
472         let ast_id = self.source_ast_id_map.ast_id(impl_def);
473         let res = Impl { generic_params, target_trait, target_type, is_negative, items, ast_id };
474         Some(id(self.data().impls.alloc(res)))
475     }
476
477     fn lower_use(&mut self, use_item: &ast::Use) -> Vec<FileItemTreeId<Import>> {
478         // FIXME: cfg_attr
479         let is_prelude = use_item.has_atom_attr("prelude_import");
480         let visibility = self.lower_visibility(use_item);
481         let ast_id = self.source_ast_id_map.ast_id(use_item);
482
483         // Every use item can expand to many `Import`s.
484         let mut imports = Vec::new();
485         let tree = self.tree.data_mut();
486         ModPath::expand_use_item(
487             InFile::new(self.file, use_item.clone()),
488             &self.hygiene,
489             |path, _use_tree, is_glob, alias| {
490                 imports.push(id(tree.imports.alloc(Import {
491                     path,
492                     alias,
493                     visibility,
494                     is_glob,
495                     is_prelude,
496                     ast_id,
497                     index: imports.len(),
498                 })));
499             },
500         );
501
502         imports
503     }
504
505     fn lower_extern_crate(
506         &mut self,
507         extern_crate: &ast::ExternCrate,
508     ) -> Option<FileItemTreeId<ExternCrate>> {
509         let name = extern_crate.name_ref()?.as_name();
510         let alias = extern_crate.rename().map(|a| {
511             a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
512         });
513         let visibility = self.lower_visibility(extern_crate);
514         let ast_id = self.source_ast_id_map.ast_id(extern_crate);
515         // FIXME: cfg_attr
516         let is_macro_use = extern_crate.has_atom_attr("macro_use");
517
518         let res = ExternCrate { name, alias, visibility, is_macro_use, ast_id };
519         Some(id(self.data().extern_crates.alloc(res)))
520     }
521
522     fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> {
523         let name = m.name().map(|it| it.as_name());
524         let attrs = Attrs::new(m, &self.hygiene);
525         let path = ModPath::from_src(m.path()?, &self.hygiene)?;
526
527         let ast_id = self.source_ast_id_map.ast_id(m);
528
529         // FIXME: cfg_attr
530         let export_attr = attrs.by_key("macro_export");
531
532         let is_export = export_attr.exists();
533         let is_local_inner = if is_export {
534             export_attr.tt_values().map(|it| &it.token_trees).flatten().any(|it| match it {
535                 tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
536                     ident.text.contains("local_inner_macros")
537                 }
538                 _ => false,
539             })
540         } else {
541             false
542         };
543
544         let is_builtin = attrs.by_key("rustc_builtin_macro").exists();
545         let res = MacroCall { name, path, is_export, is_builtin, is_local_inner, ast_id };
546         Some(id(self.data().macro_calls.alloc(res)))
547     }
548
549     fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> Vec<ModItem> {
550         block.extern_item_list().map_or(Vec::new(), |list| {
551             list.extern_items()
552                 .filter_map(|item| {
553                     self.collect_inner_items(item.syntax());
554                     let attrs = Attrs::new(&item, &self.hygiene);
555                     let id: ModItem = match item {
556                         ast::ExternItem::Fn(ast) => {
557                             let func = self.lower_function(&ast)?;
558                             self.data().functions[func.index].is_unsafe = true;
559                             func.into()
560                         }
561                         ast::ExternItem::Static(ast) => {
562                             let statik = self.lower_static(&ast)?;
563                             statik.into()
564                         }
565                         ast::ExternItem::TypeAlias(ty) => {
566                             let foreign_ty = self.lower_type_alias(&ty)?;
567                             self.data().type_aliases[foreign_ty.index].is_extern = true;
568                             foreign_ty.into()
569                         }
570                         ast::ExternItem::MacroCall(_) => return None,
571                     };
572                     self.add_attrs(id.into(), attrs);
573                     Some(id)
574                 })
575                 .collect()
576         })
577     }
578
579     /// Lowers generics defined on `node` and collects inner items defined within.
580     fn lower_generic_params_and_inner_items(
581         &mut self,
582         owner: GenericsOwner<'_>,
583         node: &impl ast::GenericParamsOwner,
584     ) -> GenericParamsId {
585         // Generics are part of item headers and may contain inner items we need to collect.
586         if let Some(params) = node.generic_param_list() {
587             self.collect_inner_items(params.syntax());
588         }
589         if let Some(clause) = node.where_clause() {
590             self.collect_inner_items(clause.syntax());
591         }
592
593         self.lower_generic_params(owner, node)
594     }
595
596     fn lower_generic_params(
597         &mut self,
598         owner: GenericsOwner<'_>,
599         node: &impl ast::GenericParamsOwner,
600     ) -> GenericParamsId {
601         let mut sm = &mut ArenaMap::default();
602         let mut generics = GenericParams::default();
603         match owner {
604             GenericsOwner::Function(func) => {
605                 generics.fill(&self.body_ctx, sm, node);
606                 // lower `impl Trait` in arguments
607                 for param in &*func.params {
608                     generics.fill_implicit_impl_trait_args(param);
609                 }
610             }
611             GenericsOwner::Struct
612             | GenericsOwner::Enum
613             | GenericsOwner::Union
614             | GenericsOwner::TypeAlias => {
615                 generics.fill(&self.body_ctx, sm, node);
616             }
617             GenericsOwner::Trait(trait_def) => {
618                 // traits get the Self type as an implicit first type parameter
619                 let self_param_id = generics.types.alloc(TypeParamData {
620                     name: Some(name![Self]),
621                     default: None,
622                     provenance: TypeParamProvenance::TraitSelf,
623                 });
624                 sm.insert(self_param_id, Either::Left(trait_def.clone()));
625                 // add super traits as bounds on Self
626                 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
627                 let self_param = TypeRef::Path(name![Self].into());
628                 generics.fill_bounds(&self.body_ctx, trait_def, self_param);
629
630                 generics.fill(&self.body_ctx, &mut sm, node);
631             }
632             GenericsOwner::Impl => {
633                 // Note that we don't add `Self` here: in `impl`s, `Self` is not a
634                 // type-parameter, but rather is a type-alias for impl's target
635                 // type, so this is handled by the resolver.
636                 generics.fill(&self.body_ctx, &mut sm, node);
637             }
638         }
639
640         self.data().generics.alloc(generics)
641     }
642
643     fn lower_type_bounds(&mut self, node: &impl ast::TypeBoundsOwner) -> Vec<TypeBound> {
644         match node.type_bound_list() {
645             Some(bound_list) => {
646                 bound_list.bounds().map(|it| TypeBound::from_ast(&self.body_ctx, it)).collect()
647             }
648             None => Vec::new(),
649         }
650     }
651
652     fn lower_visibility(&mut self, item: &impl ast::VisibilityOwner) -> RawVisibilityId {
653         let vis = match self.forced_visibility {
654             Some(vis) => return vis,
655             None => RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene),
656         };
657
658         self.data().vis.alloc(vis)
659     }
660
661     fn lower_type_ref(&self, type_ref: &ast::Type) -> TypeRef {
662         TypeRef::from_ast(&self.body_ctx, type_ref.clone())
663     }
664     fn lower_type_ref_opt(&self, type_ref: Option<ast::Type>) -> TypeRef {
665         type_ref.map(|ty| self.lower_type_ref(&ty)).unwrap_or(TypeRef::Error)
666     }
667
668     /// Forces the visibility `vis` to be used for all items lowered during execution of `f`.
669     fn with_inherited_visibility<R>(
670         &mut self,
671         vis: RawVisibilityId,
672         f: impl FnOnce(&mut Self) -> R,
673     ) -> R {
674         let old = mem::replace(&mut self.forced_visibility, Some(vis));
675         let res = f(self);
676         self.forced_visibility = old;
677         res
678     }
679
680     fn next_field_idx(&self) -> Idx<Field> {
681         Idx::from_raw(RawId::from(
682             self.tree.data.as_ref().map_or(0, |data| data.fields.len() as u32),
683         ))
684     }
685     fn next_variant_idx(&self) -> Idx<Variant> {
686         Idx::from_raw(RawId::from(
687             self.tree.data.as_ref().map_or(0, |data| data.variants.len() as u32),
688         ))
689     }
690 }
691
692 fn desugar_future_path(orig: TypeRef) -> Path {
693     let path = path![core::future::Future];
694     let mut generic_args: Vec<_> = std::iter::repeat(None).take(path.segments.len() - 1).collect();
695     let mut last = GenericArgs::empty();
696     let binding =
697         AssociatedTypeBinding { name: name![Output], type_ref: Some(orig), bounds: Vec::new() };
698     last.bindings.push(binding);
699     generic_args.push(Some(Arc::new(last)));
700
701     Path::from_known_path(path, generic_args)
702 }
703
704 enum GenericsOwner<'a> {
705     /// We need access to the partially-lowered `Function` for lowering `impl Trait` in argument
706     /// position.
707     Function(&'a Function),
708     Struct,
709     Enum,
710     Union,
711     /// The `TraitDef` is needed to fill the source map for the implicit `Self` parameter.
712     Trait(&'a ast::Trait),
713     TypeAlias,
714     Impl,
715 }