Record definitions in extern blocks

This commit is contained in:
Jonas Schievink 2020-04-23 23:10:14 +02:00
parent 189ac4abbf
commit 5cc9546ca0
2 changed files with 38 additions and 3 deletions

View File

@ -266,8 +266,8 @@ impl RawItemsCollector {
self.add_macro(current_module, it);
return;
}
ast::ModuleItem::ExternBlock(_) => {
// FIXME: add extern block
ast::ModuleItem::ExternBlock(it) => {
self.add_extern_block(current_module, it);
return;
}
};
@ -278,6 +278,34 @@ impl RawItemsCollector {
}
}
fn add_extern_block(
&mut self,
current_module: Option<Idx<ModuleData>>,
block: ast::ExternBlock,
) {
if let Some(items) = block.extern_item_list() {
for item in items.extern_items() {
let attrs = self.parse_attrs(&item);
let visibility =
RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene);
let (kind, name) = match item {
ast::ExternItem::FnDef(it) => {
(DefKind::Function(self.source_ast_id_map.ast_id(&it)), it.name())
}
ast::ExternItem::StaticDef(it) => {
(DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name())
}
};
if let Some(name) = name {
let name = name.as_name();
let def = self.raw_items.defs.alloc(DefData { name, kind, visibility });
self.push_item(current_module, attrs, RawItemKind::Def(def));
}
}
}
}
fn add_module(&mut self, current_module: Option<Idx<ModuleData>>, module: ast::Module) {
let name = match module.name() {
Some(it) => it.as_name(),

View File

@ -25,7 +25,7 @@ fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
#[test]
fn crate_def_map_smoke_test() {
let map = def_map(
"
r"
//- /lib.rs
mod foo;
struct S;
@ -45,6 +45,11 @@ fn crate_def_map_smoke_test() {
}
enum E { V }
extern {
static EXT: u8;
fn ext();
}
",
);
assert_snapshot!(map, @r###"
@ -61,7 +66,9 @@ fn crate_def_map_smoke_test() {
crate::foo::bar
Baz: t v
E: t
EXT: v
U: t v
ext: v
"###)
}