Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/hir_def/src/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ impl ItemTree {
let ItemTreeData {
imports,
extern_crates,
extern_blocks,
functions,
params,
structs,
Expand All @@ -154,6 +155,7 @@ impl ItemTree {

imports.shrink_to_fit();
extern_crates.shrink_to_fit();
extern_blocks.shrink_to_fit();
functions.shrink_to_fit();
params.shrink_to_fit();
structs.shrink_to_fit();
Expand Down Expand Up @@ -239,6 +241,7 @@ static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(P
struct ItemTreeData {
imports: Arena<Import>,
extern_crates: Arena<ExternCrate>,
extern_blocks: Arena<ExternBlock>,
functions: Arena<Function>,
params: Arena<Param>,
structs: Arena<Struct>,
Expand Down Expand Up @@ -432,6 +435,7 @@ macro_rules! mod_items {
mod_items! {
Import in imports -> ast::Use,
ExternCrate in extern_crates -> ast::ExternCrate,
ExternBlock in extern_blocks -> ast::ExternBlock,
Function in functions -> ast::Fn,
Struct in structs -> ast::Struct,
Union in unions -> ast::Union,
Expand Down Expand Up @@ -507,6 +511,13 @@ pub struct ExternCrate {
pub ast_id: FileAstId<ast::ExternCrate>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ExternBlock {
pub abi: Option<Interned<str>>,
pub ast_id: FileAstId<ast::ExternBlock>,
pub children: Box<[ModItem]>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Function {
pub name: Name,
Expand Down Expand Up @@ -691,6 +702,7 @@ impl ModItem {
match self {
ModItem::Import(_)
| ModItem::ExternCrate(_)
| ModItem::ExternBlock(_)
| ModItem::Struct(_)
| ModItem::Union(_)
| ModItem::Enum(_)
Expand All @@ -715,6 +727,7 @@ impl ModItem {
match self {
ModItem::Import(it) => tree[it.index].ast_id().upcast(),
ModItem::ExternCrate(it) => tree[it.index].ast_id().upcast(),
ModItem::ExternBlock(it) => tree[it.index].ast_id().upcast(),
ModItem::Function(it) => tree[it.index].ast_id().upcast(),
ModItem::Struct(it) => tree[it.index].ast_id().upcast(),
ModItem::Union(it) => tree[it.index].ast_id().upcast(),
Expand Down
49 changes: 29 additions & 20 deletions crates/hir_def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ impl<'a> Ctx<'a> {
ast::Item::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
ast::Item::MacroRules(ast) => self.lower_macro_rules(ast).map(Into::into),
ast::Item::MacroDef(ast) => self.lower_macro_def(ast).map(Into::into),
ast::Item::ExternBlock(ast) => {
Some(ModItems(self.lower_extern_block(ast).into_iter().collect::<SmallVec<_>>()))
}
ast::Item::ExternBlock(ast) => Some(self.lower_extern_block(ast).into()),
};

if !attrs.is_empty() {
Expand Down Expand Up @@ -397,19 +395,7 @@ impl<'a> Ctx<'a> {
ret_type
};

let abi = func.abi().map(|abi| {
// FIXME: Abi::abi() -> Option<SyntaxToken>?
match abi.syntax().last_token() {
Some(tok) if tok.kind() == SyntaxKind::STRING => {
// FIXME: Better way to unescape?
Interned::new_str(tok.text().trim_matches('"'))
}
_ => {
// `extern` default to be `extern "C"`.
Interned::new_str("C")
}
}
});
let abi = func.abi().map(lower_abi);

let ast_id = self.source_ast_id_map.ast_id(func);

Expand Down Expand Up @@ -647,8 +633,10 @@ impl<'a> Ctx<'a> {
Some(id(self.data().macro_defs.alloc(res)))
}

fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> Vec<ModItem> {
block.extern_item_list().map_or(Vec::new(), |list| {
fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> FileItemTreeId<ExternBlock> {
let ast_id = self.source_ast_id_map.ast_id(block);
let abi = block.abi().map(lower_abi);
let children: Box<[_]> = block.extern_item_list().map_or(Box::new([]), |list| {
list.extern_items()
.filter_map(|item| {
self.collect_inner_items(item.syntax());
Expand All @@ -673,13 +661,20 @@ impl<'a> Ctx<'a> {
self.data().type_aliases[foreign_ty.index].is_extern = true;
foreign_ty.into()
}
ast::ExternItem::MacroCall(_) => return None,
ast::ExternItem::MacroCall(call) => {
// FIXME: we need some way of tracking that the macro call is in an
// extern block
self.lower_macro_call(&call)?.into()
}
};
self.add_attrs(id.into(), attrs);
Some(id)
})
.collect()
})
});

let res = ExternBlock { abi, ast_id, children };
id(self.data().extern_blocks.alloc(res))
}

/// Lowers generics defined on `node` and collects inner items defined within.
Expand Down Expand Up @@ -879,3 +874,17 @@ fn is_intrinsic_fn_unsafe(name: &Name) -> bool {
]
.contains(&name)
}

fn lower_abi(abi: ast::Abi) -> Interned<str> {
// FIXME: Abi::abi() -> Option<SyntaxToken>?
match abi.syntax().last_token() {
Some(tok) if tok.kind() == SyntaxKind::STRING => {
// FIXME: Better way to unescape?
Interned::new_str(tok.text().trim_matches('"'))
}
_ => {
// `extern` default to be `extern "C"`.
Interned::new_str("C")
}
}
}
1 change: 1 addition & 0 deletions crates/hir_def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,7 @@ impl ModCollector<'_, '_> {
status: PartialResolvedImport::Unresolved,
})
}
ModItem::ExternBlock(block) => self.collect(&self.item_tree[block].children),
ModItem::MacroCall(mac) => self.collect_macro_call(&self.item_tree[mac]),
ModItem::MacroRules(id) => self.collect_macro_rules(id),
ModItem::MacroDef(id) => self.collect_macro_def(id),
Expand Down
36 changes: 36 additions & 0 deletions crates/hir_def/src/nameres/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,42 @@ fn unresolved_attributes_fall_back_track_per_file_moditems() {
);
}

#[test]
fn unresolved_attrs_extern_block_hang() {
check(
r#"
#[unresolved]
extern "C" {
#[unresolved]
fn f();
}
"#,
expect![[r#"
crate
f: v
"#]],
);
}

#[test]
fn macros_in_extern_block() {
check(
r#"
macro_rules! m {
() => { static S: u8; };
}

extern {
m!();
}
"#,
expect![[r#"
crate
S: v
"#]],
);
}

#[test]
fn resolves_derive_helper() {
cov_mark::check!(resolved_derive_helper);
Expand Down