Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid loading and parsing unconfigured non-inline modules. #36482

Merged
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
2 changes: 1 addition & 1 deletion src/libsyntax/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'a> StripUnconfigured<'a> {
}

// Determine if a node with the given attributes should be included in this configuation.
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
pub fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
attrs.iter().all(|attr| {
// When not compiling with --test we should not compile the #[test] functions
if !self.should_test && is_test_or_bench(attr) {
Expand Down
27 changes: 18 additions & 9 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5291,20 +5291,29 @@ impl<'a> Parser<'a> {

/// Parse a `mod <foo> { ... }` or `mod <foo>;` item
fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> {
let outer_attrs = ::config::StripUnconfigured {
config: &self.cfg,
sess: self.sess,
should_test: false, // irrelevant
features: None, // don't perform gated feature checking
}.process_cfg_attrs(outer_attrs.to_owned());
let (in_cfg, outer_attrs) = {
let mut strip_unconfigured = ::config::StripUnconfigured {
config: &self.cfg,
sess: self.sess,
should_test: false, // irrelevant
features: None, // don't perform gated feature checking
};
let outer_attrs = strip_unconfigured.process_cfg_attrs(outer_attrs.to_owned());
(strip_unconfigured.in_cfg(&outer_attrs), outer_attrs)
};

let id_span = self.span;
let id = self.parse_ident()?;
if self.check(&token::Semi) {
self.bump();
// This mod is in an external file. Let's go get it!
let (m, attrs) = self.eval_src_mod(id, &outer_attrs, id_span)?;
Ok((id, m, Some(attrs)))
if in_cfg {
// This mod is in an external file. Let's go get it!
let (m, attrs) = self.eval_src_mod(id, &outer_attrs, id_span)?;
Ok((id, m, Some(attrs)))
} else {
let placeholder = ast::Mod { inner: syntax_pos::DUMMY_SP, items: Vec::new() };
Ok((id, ItemKind::Mod(placeholder), None))
}
} else {
let directory = self.directory.clone();
self.push_directory(id, &outer_attrs);
Expand Down
3 changes: 3 additions & 0 deletions src/test/run-pass/conditional-compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,6 @@ mod test_methods {
fn the(&self);
}
}

#[cfg(any())]
mod nonexistent_file; // Check that unconfigured non-inline modules are not loaded or parsed.