Skip to content

Commit 871c475

Browse files
feat: support mods inside cfg_if macro
1 parent f800ce4 commit 871c475

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/modules.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
105105
visitor.visit_item(&item);
106106
for module_item in visitor.mods() {
107107
if let ast::ItemKind::Mod(ref sub_mod) = module_item.item.node {
108-
self.visit_sub_mod(&item, Cow::Owned(sub_mod.clone()))?;
108+
self.visit_sub_mod(&module_item.item, Cow::Owned(sub_mod.clone()))?;
109109
}
110110
}
111111
Ok(())
@@ -477,7 +477,14 @@ fn parse_mod_items<'a>(parser: &mut parser::Parser<'a>, inner_lo: Span) -> PResu
477477

478478
fn is_cfg_if(item: &ast::Item) -> bool {
479479
match item.node {
480-
ast::ItemKind::Mac(..) if item.ident.name == Symbol::intern("cfg_if") => true,
480+
ast::ItemKind::Mac(ref mac) => {
481+
if let Some(first_segment) = mac.node.path.segments.first() {
482+
if first_segment.ident.name == Symbol::intern("cfg_if") {
483+
return true;
484+
}
485+
}
486+
false
487+
}
481488
_ => false,
482489
}
483490
}

src/modules/visitor.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,21 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CfgIfVisitor<'a> {
4343

4444
impl<'a, 'ast: 'a> CfgIfVisitor<'a> {
4545
fn visit_mac_inner(&mut self, mac: &'ast ast::Mac) -> Result<(), &'static str> {
46-
if mac.node.path != Symbol::intern("cfg_if") {
47-
return Err("Expected cfg_if");
46+
// Support both:
47+
// ```
48+
// extern crate cfg_if;
49+
// cfg_if::cfg_if! {..}
50+
// ```
51+
// And:
52+
// ```
53+
// #[macro_use]
54+
// extern crate cfg_if;
55+
// cfg_if! {..}
56+
// ```
57+
if let Some(first_segment) = mac.node.path.segments.first() {
58+
if first_segment.ident.name != Symbol::intern("cfg_if") {
59+
return Err("Expected cfg_if");
60+
}
4861
}
4962

5063
let mut parser = stream_to_parser_with_base_dir(

0 commit comments

Comments
 (0)