1 //! AST -> `ItemTree` lowering code.
3 use std::{collections::hash_map::Entry, mem, sync::Arc};
5 use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, name::known, HirFileId};
6 use smallvec::SmallVec;
8 ast::{self, ModuleItemOwner},
13 generics::{GenericParams, TypeParamData, TypeParamProvenance},
14 type_ref::{LifetimeRef, TraitRef},
19 fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
20 FileItemTreeId { index, _p: PhantomData }
23 struct ModItems(SmallVec<[ModItem; 1]>);
25 impl<T> From<T> for ModItems
29 fn from(t: T) -> Self {
30 ModItems(SmallVec::from_buf([t.into(); 1]))
34 pub(super) struct Ctx<'a> {
35 db: &'a dyn DefDatabase,
39 source_ast_id_map: Arc<AstIdMap>,
40 body_ctx: crate::body::LowerCtx<'a>,
41 forced_visibility: Option<RawVisibilityId>,
45 pub(super) fn new(db: &'a dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self {
48 tree: ItemTree::default(),
51 source_ast_id_map: db.ast_id_map(file),
52 body_ctx: crate::body::LowerCtx::new(db, file),
53 forced_visibility: None,
57 pub(super) fn lower_module_items(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree {
58 self.tree.top_level = item_owner
60 .flat_map(|item| self.lower_mod_item(&item, false))
61 .flat_map(|items| items.0)
66 pub(super) fn lower_macro_stmts(mut self, stmts: ast::MacroStmts) -> ItemTree {
67 self.tree.top_level = stmts
69 .filter_map(|stmt| match stmt {
70 ast::Stmt::Item(item) => Some(item),
73 .flat_map(|item| self.lower_mod_item(&item, false))
74 .flat_map(|items| items.0)
77 // Non-items need to have their inner items collected.
78 for stmt in stmts.statements() {
80 ast::Stmt::ExprStmt(_) | ast::Stmt::LetStmt(_) => {
81 self.collect_inner_items(stmt.syntax())
86 if let Some(expr) = stmts.expr() {
87 self.collect_inner_items(expr.syntax());
92 pub(super) fn lower_inner_items(mut self, within: &SyntaxNode) -> ItemTree {
93 self.collect_inner_items(within);
97 fn data(&mut self) -> &mut ItemTreeData {
101 fn lower_mod_item(&mut self, item: &ast::Item, inner: bool) -> Option<ModItems> {
102 // Collect inner items for 1-to-1-lowered items.
105 | ast::Item::Union(_)
108 | ast::Item::TypeAlias(_)
109 | ast::Item::Const(_)
110 | ast::Item::Static(_) => {
111 // Skip this if we're already collecting inner items. We'll descend into all nodes
114 self.collect_inner_items(item.syntax());
118 // These are handled in their respective `lower_X` method (since we can't just blindly
120 ast::Item::Trait(_) | ast::Item::Impl(_) | ast::Item::ExternBlock(_) => {}
122 // These don't have inner items.
124 | ast::Item::ExternCrate(_)
126 | ast::Item::MacroCall(_)
127 | ast::Item::MacroRules(_)
128 | ast::Item::MacroDef(_) => {}
131 let attrs = RawAttrs::new(self.db, item, &self.hygiene);
132 let items = match item {
133 ast::Item::Struct(ast) => self.lower_struct(ast).map(Into::into),
134 ast::Item::Union(ast) => self.lower_union(ast).map(Into::into),
135 ast::Item::Enum(ast) => self.lower_enum(ast).map(Into::into),
136 ast::Item::Fn(ast) => self.lower_function(ast).map(Into::into),
137 ast::Item::TypeAlias(ast) => self.lower_type_alias(ast).map(Into::into),
138 ast::Item::Static(ast) => self.lower_static(ast).map(Into::into),
139 ast::Item::Const(ast) => Some(self.lower_const(ast).into()),
140 ast::Item::Module(ast) => self.lower_module(ast).map(Into::into),
141 ast::Item::Trait(ast) => self.lower_trait(ast).map(Into::into),
142 ast::Item::Impl(ast) => self.lower_impl(ast).map(Into::into),
143 ast::Item::Use(ast) => Some(ModItems(
144 self.lower_use(ast).into_iter().map(Into::into).collect::<SmallVec<_>>(),
146 ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast).map(Into::into),
147 ast::Item::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
148 ast::Item::MacroRules(ast) => self.lower_macro_rules(ast).map(Into::into),
149 ast::Item::MacroDef(ast) => self.lower_macro_def(ast).map(Into::into),
150 ast::Item::ExternBlock(ast) => Some(self.lower_extern_block(ast).into()),
153 if !attrs.is_empty() {
154 for item in items.iter().flat_map(|items| &items.0) {
155 self.add_attrs((*item).into(), attrs.clone());
162 fn add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs) {
163 match self.tree.attrs.entry(item) {
164 Entry::Occupied(mut entry) => {
165 *entry.get_mut() = entry.get().merge(attrs);
167 Entry::Vacant(entry) => {
173 fn collect_inner_items(&mut self, container: &SyntaxNode) {
174 let forced_vis = self.forced_visibility.take();
176 let mut block_stack = Vec::new();
178 // if container itself is block, add it to the stack
179 if let Some(block) = ast::BlockExpr::cast(container.clone()) {
180 block_stack.push(self.source_ast_id_map.ast_id(&block));
183 for event in container.preorder().skip(1) {
185 WalkEvent::Enter(node) => {
188 ast::BlockExpr(block) => {
189 block_stack.push(self.source_ast_id_map.ast_id(&block));
192 // FIXME: This triggers for macro calls in expression/pattern/type position
193 let mod_items = self.lower_mod_item(&item, true);
194 let current_block = block_stack.last();
195 if let (Some(mod_items), Some(block)) = (mod_items, current_block) {
196 if !mod_items.0.is_empty() {
197 self.data().inner_items.entry(*block).or_default().extend(mod_items.0.iter().copied());
205 WalkEvent::Leave(node) => {
206 if ast::BlockExpr::cast(node).is_some() {
213 self.forced_visibility = forced_vis;
216 fn lower_assoc_item(&mut self, item: &ast::AssocItem) -> Option<AssocItem> {
218 ast::AssocItem::Fn(ast) => self.lower_function(ast).map(Into::into),
219 ast::AssocItem::TypeAlias(ast) => self.lower_type_alias(ast).map(Into::into),
220 ast::AssocItem::Const(ast) => Some(self.lower_const(ast).into()),
221 ast::AssocItem::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
225 fn lower_struct(&mut self, strukt: &ast::Struct) -> Option<FileItemTreeId<Struct>> {
226 let visibility = self.lower_visibility(strukt);
227 let name = strukt.name()?.as_name();
228 let generic_params = self.lower_generic_params(GenericsOwner::Struct, strukt);
229 let fields = self.lower_fields(&strukt.kind());
230 let ast_id = self.source_ast_id_map.ast_id(strukt);
231 let kind = match strukt.kind() {
232 ast::StructKind::Record(_) => StructDefKind::Record,
233 ast::StructKind::Tuple(_) => StructDefKind::Tuple,
234 ast::StructKind::Unit => StructDefKind::Unit,
236 let res = Struct { name, visibility, generic_params, fields, ast_id, kind };
237 Some(id(self.data().structs.alloc(res)))
240 fn lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields {
242 ast::StructKind::Record(it) => {
243 let range = self.lower_record_fields(it);
244 Fields::Record(range)
246 ast::StructKind::Tuple(it) => {
247 let range = self.lower_tuple_fields(it);
250 ast::StructKind::Unit => Fields::Unit,
254 fn lower_record_fields(&mut self, fields: &ast::RecordFieldList) -> IdRange<Field> {
255 let start = self.next_field_idx();
256 for field in fields.fields() {
257 if let Some(data) = self.lower_record_field(&field) {
258 let idx = self.data().fields.alloc(data);
259 self.add_attrs(idx.into(), RawAttrs::new(self.db, &field, &self.hygiene));
262 let end = self.next_field_idx();
263 IdRange::new(start..end)
266 fn lower_record_field(&mut self, field: &ast::RecordField) -> Option<Field> {
267 let name = field.name()?.as_name();
268 let visibility = self.lower_visibility(field);
269 let type_ref = self.lower_type_ref_opt(field.ty());
270 let res = Field { name, type_ref, visibility };
274 fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldList) -> IdRange<Field> {
275 let start = self.next_field_idx();
276 for (i, field) in fields.fields().enumerate() {
277 let data = self.lower_tuple_field(i, &field);
278 let idx = self.data().fields.alloc(data);
279 self.add_attrs(idx.into(), RawAttrs::new(self.db, &field, &self.hygiene));
281 let end = self.next_field_idx();
282 IdRange::new(start..end)
285 fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleField) -> Field {
286 let name = Name::new_tuple_field(idx);
287 let visibility = self.lower_visibility(field);
288 let type_ref = self.lower_type_ref_opt(field.ty());
289 let res = Field { name, type_ref, visibility };
293 fn lower_union(&mut self, union: &ast::Union) -> Option<FileItemTreeId<Union>> {
294 let visibility = self.lower_visibility(union);
295 let name = union.name()?.as_name();
296 let generic_params = self.lower_generic_params(GenericsOwner::Union, union);
297 let fields = match union.record_field_list() {
298 Some(record_field_list) => self.lower_fields(&StructKind::Record(record_field_list)),
299 None => Fields::Record(IdRange::new(self.next_field_idx()..self.next_field_idx())),
301 let ast_id = self.source_ast_id_map.ast_id(union);
302 let res = Union { name, visibility, generic_params, fields, ast_id };
303 Some(id(self.data().unions.alloc(res)))
306 fn lower_enum(&mut self, enum_: &ast::Enum) -> Option<FileItemTreeId<Enum>> {
307 let visibility = self.lower_visibility(enum_);
308 let name = enum_.name()?.as_name();
309 let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_);
310 let variants = match &enum_.variant_list() {
311 Some(variant_list) => self.lower_variants(variant_list),
312 None => IdRange::new(self.next_variant_idx()..self.next_variant_idx()),
314 let ast_id = self.source_ast_id_map.ast_id(enum_);
315 let res = Enum { name, visibility, generic_params, variants, ast_id };
316 Some(id(self.data().enums.alloc(res)))
319 fn lower_variants(&mut self, variants: &ast::VariantList) -> IdRange<Variant> {
320 let start = self.next_variant_idx();
321 for variant in variants.variants() {
322 if let Some(data) = self.lower_variant(&variant) {
323 let idx = self.data().variants.alloc(data);
324 self.add_attrs(idx.into(), RawAttrs::new(self.db, &variant, &self.hygiene));
327 let end = self.next_variant_idx();
328 IdRange::new(start..end)
331 fn lower_variant(&mut self, variant: &ast::Variant) -> Option<Variant> {
332 let name = variant.name()?.as_name();
333 let fields = self.lower_fields(&variant.kind());
334 let res = Variant { name, fields };
338 fn lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>> {
339 let visibility = self.lower_visibility(func);
340 let name = func.name()?.as_name();
342 let mut has_self_param = false;
343 let start_param = self.next_param_idx();
344 if let Some(param_list) = func.param_list() {
345 if let Some(self_param) = param_list.self_param() {
346 let self_type = match self_param.ty() {
347 Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
349 let self_type = TypeRef::Path(name![Self].into());
350 match self_param.kind() {
351 ast::SelfParamKind::Owned => self_type,
352 ast::SelfParamKind::Ref => TypeRef::Reference(
354 self_param.lifetime().as_ref().map(LifetimeRef::new),
357 ast::SelfParamKind::MutRef => TypeRef::Reference(
359 self_param.lifetime().as_ref().map(LifetimeRef::new),
365 let ty = Interned::new(self_type);
366 let idx = self.data().params.alloc(Param::Normal(ty));
367 self.add_attrs(idx.into(), RawAttrs::new(self.db, &self_param, &self.hygiene));
368 has_self_param = true;
370 for param in param_list.params() {
371 let idx = match param.dotdotdot_token() {
372 Some(_) => self.data().params.alloc(Param::Varargs),
374 let type_ref = TypeRef::from_ast_opt(&self.body_ctx, param.ty());
375 let ty = Interned::new(type_ref);
376 self.data().params.alloc(Param::Normal(ty))
379 self.add_attrs(idx.into(), RawAttrs::new(self.db, ¶m, &self.hygiene));
382 let end_param = self.next_param_idx();
383 let params = IdRange::new(start_param..end_param);
385 let ret_type = match func.ret_type().and_then(|rt| rt.ty()) {
386 Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
387 _ => TypeRef::unit(),
390 let ret_type = if func.async_token().is_some() {
391 let future_impl = desugar_future_path(ret_type);
392 let ty_bound = TypeBound::Path(future_impl);
393 TypeRef::ImplTrait(vec![ty_bound])
398 let abi = func.abi().map(lower_abi);
400 let ast_id = self.source_ast_id_map.ast_id(func);
402 let mut flags = FnFlags::default();
403 if func.body().is_some() {
404 flags.bits |= FnFlags::HAS_BODY;
407 flags.bits |= FnFlags::HAS_SELF_PARAM;
409 if func.default_token().is_some() {
410 flags.bits |= FnFlags::IS_DEFAULT;
412 if func.const_token().is_some() {
413 flags.bits |= FnFlags::IS_CONST;
415 if func.async_token().is_some() {
416 flags.bits |= FnFlags::IS_ASYNC;
418 if func.unsafe_token().is_some() {
419 flags.bits |= FnFlags::IS_UNSAFE;
422 let mut res = Function {
425 generic_params: Interned::new(GenericParams::default()),
428 ret_type: Interned::new(ret_type),
432 res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func);
434 Some(id(self.data().functions.alloc(res)))
439 type_alias: &ast::TypeAlias,
440 ) -> Option<FileItemTreeId<TypeAlias>> {
441 let name = type_alias.name()?.as_name();
442 let type_ref = type_alias.ty().map(|it| self.lower_type_ref(&it));
443 let visibility = self.lower_visibility(type_alias);
444 let bounds = self.lower_type_bounds(type_alias);
445 let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias);
446 let ast_id = self.source_ast_id_map.ast_id(type_alias);
447 let res = TypeAlias {
450 bounds: bounds.into_boxed_slice(),
456 Some(id(self.data().type_aliases.alloc(res)))
459 fn lower_static(&mut self, static_: &ast::Static) -> Option<FileItemTreeId<Static>> {
460 let name = static_.name()?.as_name();
461 let type_ref = self.lower_type_ref_opt(static_.ty());
462 let visibility = self.lower_visibility(static_);
463 let mutable = static_.mut_token().is_some();
464 let ast_id = self.source_ast_id_map.ast_id(static_);
465 let res = Static { name, visibility, mutable, type_ref, ast_id, is_extern: false };
466 Some(id(self.data().statics.alloc(res)))
469 fn lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const> {
470 let name = konst.name().map(|it| it.as_name());
471 let type_ref = self.lower_type_ref_opt(konst.ty());
472 let visibility = self.lower_visibility(konst);
473 let ast_id = self.source_ast_id_map.ast_id(konst);
474 let res = Const { name, visibility, type_ref, ast_id };
475 id(self.data().consts.alloc(res))
478 fn lower_module(&mut self, module: &ast::Module) -> Option<FileItemTreeId<Mod>> {
479 let name = module.name()?.as_name();
480 let visibility = self.lower_visibility(module);
481 let kind = if module.semicolon_token().is_some() {
489 .flat_map(|item| self.lower_mod_item(&item, false))
490 .flat_map(|items| items.0)
494 cov_mark::hit!(name_res_works_for_broken_modules);
495 Box::new([]) as Box<[_]>
499 let ast_id = self.source_ast_id_map.ast_id(module);
500 let res = Mod { name, visibility, kind, ast_id };
501 Some(id(self.data().mods.alloc(res)))
504 fn lower_trait(&mut self, trait_def: &ast::Trait) -> Option<FileItemTreeId<Trait>> {
505 let name = trait_def.name()?.as_name();
506 let visibility = self.lower_visibility(trait_def);
508 self.lower_generic_params_and_inner_items(GenericsOwner::Trait(trait_def), trait_def);
509 let is_auto = trait_def.auto_token().is_some();
510 let is_unsafe = trait_def.unsafe_token().is_some();
511 let bounds = self.lower_type_bounds(trait_def);
512 let items = trait_def.assoc_item_list().map(|list| {
514 self.with_inherited_visibility(visibility, |this| {
517 let attrs = RawAttrs::new(db, &item, &this.hygiene);
518 this.collect_inner_items(item.syntax());
519 this.lower_assoc_item(&item).map(|item| {
520 this.add_attrs(ModItem::from(item).into(), attrs);
527 let ast_id = self.source_ast_id_map.ast_id(trait_def);
534 bounds: bounds.into(),
535 items: items.unwrap_or_default(),
538 Some(id(self.data().traits.alloc(res)))
541 fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> {
543 self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def);
544 // FIXME: If trait lowering fails, due to a non PathType for example, we treat this impl
545 // as if it was an non-trait impl. Ideally we want to create a unique missing ref that only
547 let target_trait = impl_def.trait_().and_then(|tr| self.lower_trait_ref(&tr));
548 let self_ty = self.lower_type_ref(&impl_def.self_ty()?);
549 let is_negative = impl_def.excl_token().is_some();
551 // We cannot use `assoc_items()` here as that does not include macro calls.
555 .flat_map(|it| it.assoc_items())
557 self.collect_inner_items(item.syntax());
558 let assoc = self.lower_assoc_item(&item)?;
559 let attrs = RawAttrs::new(self.db, &item, &self.hygiene);
560 self.add_attrs(ModItem::from(assoc).into(), attrs);
564 let ast_id = self.source_ast_id_map.ast_id(impl_def);
565 let res = Impl { generic_params, target_trait, self_ty, is_negative, items, ast_id };
566 Some(id(self.data().impls.alloc(res)))
569 fn lower_use(&mut self, use_item: &ast::Use) -> Vec<FileItemTreeId<Import>> {
570 let visibility = self.lower_visibility(use_item);
571 let ast_id = self.source_ast_id_map.ast_id(use_item);
573 // Every use item can expand to many `Import`s.
574 let mut imports = Vec::new();
575 let tree = self.tree.data_mut();
576 ModPath::expand_use_item(
578 InFile::new(self.file, use_item.clone()),
580 |path, _use_tree, is_glob, alias| {
581 imports.push(id(tree.imports.alloc(Import {
582 path: Interned::new(path),
587 index: imports.len(),
595 fn lower_extern_crate(
597 extern_crate: &ast::ExternCrate,
598 ) -> Option<FileItemTreeId<ExternCrate>> {
599 let name = extern_crate.name_ref()?.as_name();
600 let alias = extern_crate.rename().map(|a| {
601 a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
603 let visibility = self.lower_visibility(extern_crate);
604 let ast_id = self.source_ast_id_map.ast_id(extern_crate);
606 let res = ExternCrate { name, alias, visibility, ast_id };
607 Some(id(self.data().extern_crates.alloc(res)))
610 fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> {
611 let path = Interned::new(ModPath::from_src(self.db, m.path()?, &self.hygiene)?);
612 let ast_id = self.source_ast_id_map.ast_id(m);
613 let fragment = hir_expand::to_fragment_kind(m);
614 let res = MacroCall { path, ast_id, fragment };
615 Some(id(self.data().macro_calls.alloc(res)))
618 fn lower_macro_rules(&mut self, m: &ast::MacroRules) -> Option<FileItemTreeId<MacroRules>> {
619 let name = m.name().map(|it| it.as_name())?;
620 let ast_id = self.source_ast_id_map.ast_id(m);
622 let res = MacroRules { name, ast_id };
623 Some(id(self.data().macro_rules.alloc(res)))
626 fn lower_macro_def(&mut self, m: &ast::MacroDef) -> Option<FileItemTreeId<MacroDef>> {
627 let name = m.name().map(|it| it.as_name())?;
629 let ast_id = self.source_ast_id_map.ast_id(m);
630 let visibility = self.lower_visibility(m);
632 let res = MacroDef { name, ast_id, visibility };
633 Some(id(self.data().macro_defs.alloc(res)))
636 fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> FileItemTreeId<ExternBlock> {
637 let ast_id = self.source_ast_id_map.ast_id(block);
638 let abi = block.abi().map(lower_abi);
639 let children: Box<[_]> = block.extern_item_list().map_or(Box::new([]), |list| {
642 self.collect_inner_items(item.syntax());
643 let attrs = RawAttrs::new(self.db, &item, &self.hygiene);
644 let id: ModItem = match item {
645 ast::ExternItem::Fn(ast) => {
646 let func_id = self.lower_function(&ast)?;
647 let func = &mut self.data().functions[func_id.index];
648 if is_intrinsic_fn_unsafe(&func.name) {
649 func.flags.bits |= FnFlags::IS_UNSAFE;
651 func.flags.bits |= FnFlags::IS_IN_EXTERN_BLOCK;
654 ast::ExternItem::Static(ast) => {
655 let statik = self.lower_static(&ast)?;
656 self.data().statics[statik.index].is_extern = true;
659 ast::ExternItem::TypeAlias(ty) => {
660 let foreign_ty = self.lower_type_alias(&ty)?;
661 self.data().type_aliases[foreign_ty.index].is_extern = true;
664 ast::ExternItem::MacroCall(call) => {
665 // FIXME: we need some way of tracking that the macro call is in an
667 self.lower_macro_call(&call)?.into()
670 self.add_attrs(id.into(), attrs);
676 let res = ExternBlock { abi, ast_id, children };
677 id(self.data().extern_blocks.alloc(res))
680 /// Lowers generics defined on `node` and collects inner items defined within.
681 fn lower_generic_params_and_inner_items(
683 owner: GenericsOwner<'_>,
684 node: &impl ast::GenericParamsOwner,
685 ) -> Interned<GenericParams> {
686 // Generics are part of item headers and may contain inner items we need to collect.
687 if let Some(params) = node.generic_param_list() {
688 self.collect_inner_items(params.syntax());
690 if let Some(clause) = node.where_clause() {
691 self.collect_inner_items(clause.syntax());
694 self.lower_generic_params(owner, node)
697 fn lower_generic_params(
699 owner: GenericsOwner<'_>,
700 node: &impl ast::GenericParamsOwner,
701 ) -> Interned<GenericParams> {
702 let mut sm = &mut Default::default();
703 let mut generics = GenericParams::default();
705 GenericsOwner::Function(func) => {
706 generics.fill(&self.body_ctx, sm, node);
707 // lower `impl Trait` in arguments
708 for id in func.params.clone() {
709 if let Param::Normal(ty) = &self.data().params[id] {
710 generics.fill_implicit_impl_trait_args(ty);
714 GenericsOwner::Struct
715 | GenericsOwner::Enum
716 | GenericsOwner::Union
717 | GenericsOwner::TypeAlias => {
718 generics.fill(&self.body_ctx, sm, node);
720 GenericsOwner::Trait(trait_def) => {
721 // traits get the Self type as an implicit first type parameter
722 let self_param_id = generics.types.alloc(TypeParamData {
723 name: Some(name![Self]),
725 provenance: TypeParamProvenance::TraitSelf,
727 sm.type_params.insert(self_param_id, Either::Left(trait_def.clone()));
728 // add super traits as bounds on Self
729 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
730 let self_param = TypeRef::Path(name![Self].into());
731 generics.fill_bounds(&self.body_ctx, trait_def, Either::Left(self_param));
732 generics.fill(&self.body_ctx, &mut sm, node);
734 GenericsOwner::Impl => {
735 // Note that we don't add `Self` here: in `impl`s, `Self` is not a
736 // type-parameter, but rather is a type-alias for impl's target
737 // type, so this is handled by the resolver.
738 generics.fill(&self.body_ctx, &mut sm, node);
742 generics.shrink_to_fit();
743 Interned::new(generics)
746 fn lower_type_bounds(&mut self, node: &impl ast::TypeBoundsOwner) -> Vec<TypeBound> {
747 match node.type_bound_list() {
748 Some(bound_list) => {
749 bound_list.bounds().map(|it| TypeBound::from_ast(&self.body_ctx, it)).collect()
755 fn lower_visibility(&mut self, item: &impl ast::VisibilityOwner) -> RawVisibilityId {
756 let vis = match self.forced_visibility {
757 Some(vis) => return vis,
758 None => RawVisibility::from_ast_with_hygiene(self.db, item.visibility(), &self.hygiene),
761 self.data().vis.alloc(vis)
764 fn lower_trait_ref(&mut self, trait_ref: &ast::Type) -> Option<Interned<TraitRef>> {
765 let trait_ref = TraitRef::from_ast(&self.body_ctx, trait_ref.clone())?;
766 Some(Interned::new(trait_ref))
769 fn lower_type_ref(&mut self, type_ref: &ast::Type) -> Interned<TypeRef> {
770 let tyref = TypeRef::from_ast(&self.body_ctx, type_ref.clone());
774 fn lower_type_ref_opt(&mut self, type_ref: Option<ast::Type>) -> Interned<TypeRef> {
775 match type_ref.map(|ty| self.lower_type_ref(&ty)) {
777 None => Interned::new(TypeRef::Error),
781 /// Forces the visibility `vis` to be used for all items lowered during execution of `f`.
782 fn with_inherited_visibility<R>(
784 vis: RawVisibilityId,
785 f: impl FnOnce(&mut Self) -> R,
787 let old = mem::replace(&mut self.forced_visibility, Some(vis));
789 self.forced_visibility = old;
793 fn next_field_idx(&self) -> Idx<Field> {
794 Idx::from_raw(RawIdx::from(
795 self.tree.data.as_ref().map_or(0, |data| data.fields.len() as u32),
798 fn next_variant_idx(&self) -> Idx<Variant> {
799 Idx::from_raw(RawIdx::from(
800 self.tree.data.as_ref().map_or(0, |data| data.variants.len() as u32),
803 fn next_param_idx(&self) -> Idx<Param> {
804 Idx::from_raw(RawIdx::from(
805 self.tree.data.as_ref().map_or(0, |data| data.params.len() as u32),
810 fn desugar_future_path(orig: TypeRef) -> Path {
811 let path = path![core::future::Future];
812 let mut generic_args: Vec<_> =
813 std::iter::repeat(None).take(path.segments().len() - 1).collect();
814 let mut last = GenericArgs::empty();
816 AssociatedTypeBinding { name: name![Output], type_ref: Some(orig), bounds: Vec::new() };
817 last.bindings.push(binding);
818 generic_args.push(Some(Arc::new(last)));
820 Path::from_known_path(path, generic_args)
823 enum GenericsOwner<'a> {
824 /// We need access to the partially-lowered `Function` for lowering `impl Trait` in argument
826 Function(&'a Function),
830 /// The `TraitDef` is needed to fill the source map for the implicit `Self` parameter.
831 Trait(&'a ast::Trait),
836 /// Returns `true` if the given intrinsic is unsafe to call, or false otherwise.
837 fn is_intrinsic_fn_unsafe(name: &Name) -> bool {
838 // Should be kept in sync with https://github.com/rust-lang/rust/blob/c6e4db620a7d2f569f11dcab627430921ea8aacf/compiler/rustc_typeck/src/check/intrinsic.rs#L68
843 known::caller_location,
845 known::min_align_of_val,
846 known::add_with_overflow,
847 known::sub_with_overflow,
848 known::mul_with_overflow,
852 known::saturating_add,
853 known::saturating_sub,
861 known::discriminant_value,
865 known::ptr_guaranteed_eq,
866 known::ptr_guaranteed_ne,
873 known::variant_count,
878 fn lower_abi(abi: ast::Abi) -> Interned<str> {
879 // FIXME: Abi::abi() -> Option<SyntaxToken>?
880 match abi.syntax().last_token() {
881 Some(tok) if tok.kind() == SyntaxKind::STRING => {
882 // FIXME: Better way to unescape?
883 Interned::new_str(tok.text().trim_matches('"'))
886 // `extern` default to be `extern "C"`.
887 Interned::new_str("C")