Skip to content

Commit ce5ad1d

Browse files
committed
Allow non-inline modules in more places.
1 parent d0623cf commit ce5ad1d

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

src/libsyntax/ext/base.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,9 @@ pub struct ExpansionData {
696696
pub depth: usize,
697697
pub backtrace: ExpnId,
698698
pub module: Rc<ModuleData>,
699-
pub in_block: bool,
699+
700+
// True if non-inline modules without a `#[path]` are forbidden at the root of this expansion.
701+
pub no_noninline_mod: bool,
700702
}
701703

702704
/// One of these is made during expansion and incrementally updated as we go;
@@ -727,7 +729,7 @@ impl<'a> ExtCtxt<'a> {
727729
depth: 0,
728730
backtrace: NO_EXPANSION,
729731
module: Rc::new(ModuleData { mod_path: Vec::new(), directory: PathBuf::new() }),
730-
in_block: false,
732+
no_noninline_mod: false,
731733
},
732734
}
733735
}

src/libsyntax/ext/expand.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,9 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
601601
}
602602

603603
fn fold_block(&mut self, block: P<Block>) -> P<Block> {
604-
let orig_in_block = mem::replace(&mut self.cx.current_expansion.in_block, true);
604+
let no_noninline_mod = mem::replace(&mut self.cx.current_expansion.no_noninline_mod, true);
605605
let result = noop_fold_block(block, self);
606-
self.cx.current_expansion.in_block = orig_in_block;
606+
self.cx.current_expansion.no_noninline_mod = no_noninline_mod;
607607
result
608608
}
609609

@@ -642,6 +642,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
642642
return noop_fold_item(item, self);
643643
}
644644

645+
let orig_no_noninline_mod = self.cx.current_expansion.no_noninline_mod;
645646
let mut module = (*self.cx.current_expansion.module).clone();
646647
module.mod_path.push(item.ident);
647648

@@ -651,11 +652,14 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
651652
let inline_module = item.span.contains(inner) || inner == syntax_pos::DUMMY_SP;
652653

653654
if inline_module {
654-
module.directory.push(&*{
655-
::attr::first_attr_value_str_by_name(&item.attrs, "path")
656-
.unwrap_or(item.ident.name.as_str())
657-
});
655+
if let Some(path) = attr::first_attr_value_str_by_name(&item.attrs, "path") {
656+
self.cx.current_expansion.no_noninline_mod = false;
657+
module.directory.push(&*path);
658+
} else {
659+
module.directory.push(&*item.ident.name.as_str());
660+
}
658661
} else {
662+
self.cx.current_expansion.no_noninline_mod = false;
659663
module.directory =
660664
PathBuf::from(self.cx.parse_sess.codemap().span_to_filename(inner));
661665
module.directory.pop();
@@ -665,6 +669,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
665669
mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
666670
let result = noop_fold_item(item, self);
667671
self.cx.current_expansion.module = orig_module;
672+
self.cx.current_expansion.no_noninline_mod = orig_no_noninline_mod;
668673
return result;
669674
}
670675
_ => noop_fold_item(item, self),

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
211211
rhs);
212212
let mut p = Parser::new(cx.parse_sess(), cx.cfg(), Box::new(trncbr));
213213
p.directory = cx.current_expansion.module.directory.clone();
214-
p.restrictions = match cx.current_expansion.in_block {
214+
p.restrictions = match cx.current_expansion.no_noninline_mod {
215215
true => Restrictions::NO_NONINLINE_MOD,
216216
false => Restrictions::empty(),
217217
};

src/libsyntax/parse/parser.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5289,23 +5289,27 @@ impl<'a> Parser<'a> {
52895289
}
52905290
} else {
52915291
let directory = self.directory.clone();
5292-
self.push_directory(id, &outer_attrs);
5292+
let restrictions = self.push_directory(id, &outer_attrs);
52935293
self.expect(&token::OpenDelim(token::Brace))?;
52945294
let mod_inner_lo = self.span.lo;
52955295
let attrs = self.parse_inner_attributes()?;
5296-
let m = self.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo)?;
5296+
let m = self.with_res(restrictions, |this| {
5297+
this.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo)
5298+
})?;
52975299
self.directory = directory;
52985300
Ok((id, ItemKind::Mod(m), Some(attrs)))
52995301
}
53005302
}
53015303

5302-
fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) {
5303-
let default_path = self.id_to_interned_str(id);
5304-
let file_path = match ::attr::first_attr_value_str_by_name(attrs, "path") {
5305-
Some(d) => d,
5306-
None => default_path,
5307-
};
5308-
self.directory.push(&*file_path)
5304+
fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) -> Restrictions {
5305+
if let Some(path) = ::attr::first_attr_value_str_by_name(attrs, "path") {
5306+
self.directory.push(&*path);
5307+
self.restrictions - Restrictions::NO_NONINLINE_MOD
5308+
} else {
5309+
let default_path = self.id_to_interned_str(id);
5310+
self.directory.push(&*default_path);
5311+
self.restrictions
5312+
}
53095313
}
53105314

53115315
pub fn submod_path_from_attr(attrs: &[ast::Attribute], dir_path: &Path) -> Option<PathBuf> {

0 commit comments

Comments
 (0)