Skip to content

Commit

Permalink
Lower trait aliases to HIR
Browse files Browse the repository at this point in the history
  • Loading branch information
lowr committed Feb 21, 2023
1 parent dd1c11e commit aa2140e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
18 changes: 5 additions & 13 deletions crates/hir-def/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,11 @@ impl TraitData {
attrs.by_key("rustc_skip_array_during_method_dispatch").exists();
let rustc_has_incoherent_inherent_impls =
attrs.by_key("rustc_has_incoherent_inherent_impls").exists();
let (items, attribute_calls, diagnostics) = match &tr_def.items {
Some(items) => {
let mut collector = AssocItemCollector::new(
db,
module_id,
tree_id.file_id(),
ItemContainerId::TraitId(tr),
);
collector.collect(&item_tree, tree_id.tree_id(), items);
collector.finish()
}
None => Default::default(),
};
let mut collector =
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::TraitId(tr));
collector.collect(&item_tree, tree_id.tree_id(), &tr_def.items);
let (items, attribute_calls, diagnostics) = collector.finish();

(
Arc::new(TraitData {
name,
Expand Down
3 changes: 1 addition & 2 deletions crates/hir-def/src/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,7 @@ pub struct Trait {
pub generic_params: Interned<GenericParams>,
pub is_auto: bool,
pub is_unsafe: bool,
/// This is [`None`] if this Trait is a trait alias.
pub items: Option<Box<[AssocItem]>>,
pub items: Box<[AssocItem]>,
pub ast_id: FileAstId<ast::Trait>,
}

Expand Down
31 changes: 22 additions & 9 deletions crates/hir-def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<'a> Ctx<'a> {
ast::Item::Static(ast) => self.lower_static(ast)?.into(),
ast::Item::Const(ast) => self.lower_const(ast).into(),
ast::Item::Module(ast) => self.lower_module(ast)?.into(),
ast::Item::Trait(ast) => self.lower_trait(ast)?.into(),
ast::Item::Trait(ast) => self.lower_trait(ast)?,
ast::Item::Impl(ast) => self.lower_impl(ast)?.into(),
ast::Item::Use(ast) => self.lower_use(ast)?.into(),
ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast)?.into(),
Expand Down Expand Up @@ -439,26 +439,39 @@ impl<'a> Ctx<'a> {
Some(id(self.data().mods.alloc(res)))
}

fn lower_trait(&mut self, trait_def: &ast::Trait) -> Option<FileItemTreeId<Trait>> {
fn lower_trait(&mut self, trait_def: &ast::Trait) -> Option<ModItem> {
let name = trait_def.name()?.as_name();
let visibility = self.lower_visibility(trait_def);
let generic_params = self.lower_generic_params(GenericsOwner::Trait(trait_def), trait_def);
let is_auto = trait_def.auto_token().is_some();
let is_unsafe = trait_def.unsafe_token().is_some();
let items = trait_def.assoc_item_list().map(|list| {
list.assoc_items()
let ast_id = self.source_ast_id_map.ast_id(trait_def);

let item = if trait_def.eq_token().is_some() {
// trait aliases
let bounds = self.lower_type_bounds(trait_def).into_boxed_slice();
let alias = TraitAlias { name, visibility, generic_params, bounds, ast_id };
id(self.data().trait_aliases.alloc(alias)).into()
} else {
// trait definition
let items = trait_def
.assoc_item_list()
.into_iter()
.flat_map(|list| list.assoc_items())
.filter_map(|item| {
let attrs = RawAttrs::new(self.db.upcast(), &item, self.hygiene());
self.lower_assoc_item(&item).map(|item| {
self.add_attrs(ModItem::from(item).into(), attrs);
item
})
})
.collect()
});
let ast_id = self.source_ast_id_map.ast_id(trait_def);
let res = Trait { name, visibility, generic_params, is_auto, is_unsafe, items, ast_id };
Some(id(self.data().traits.alloc(res)))
.collect();

let def = Trait { name, visibility, generic_params, is_auto, is_unsafe, items, ast_id };
id(self.data().traits.alloc(def)).into()
};

Some(item)
}

fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> {
Expand Down

0 comments on commit aa2140e

Please sign in to comment.