Auto merge of #36482 - jseyfried:dont_load_unconfigured_noninline_modules, r=nrc
Avoid loading and parsing unconfigured non-inline modules. For example, `#[cfg(any())] mod foo;` will always compile after this PR, even if `foo.rs` and `foo/mod.rs` do not exist or do not contain valid Rust. Fixes #36478 and fixes #27873. r? @nrc
This commit is contained in:
commit
141012dd52
3 changed files with 22 additions and 10 deletions
|
@ -126,7 +126,7 @@ impl<'a> StripUnconfigured<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine if a node with the given attributes should be included in this configuation.
|
// 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| {
|
attrs.iter().all(|attr| {
|
||||||
// When not compiling with --test we should not compile the #[test] functions
|
// When not compiling with --test we should not compile the #[test] functions
|
||||||
if !self.should_test && is_test_or_bench(attr) {
|
if !self.should_test && is_test_or_bench(attr) {
|
||||||
|
|
|
@ -5260,20 +5260,29 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
/// Parse a `mod <foo> { ... }` or `mod <foo>;` item
|
/// Parse a `mod <foo> { ... }` or `mod <foo>;` item
|
||||||
fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> {
|
fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> {
|
||||||
let outer_attrs = ::config::StripUnconfigured {
|
let (in_cfg, outer_attrs) = {
|
||||||
|
let mut strip_unconfigured = ::config::StripUnconfigured {
|
||||||
config: &self.cfg,
|
config: &self.cfg,
|
||||||
sess: self.sess,
|
sess: self.sess,
|
||||||
should_test: false, // irrelevant
|
should_test: false, // irrelevant
|
||||||
features: None, // don't perform gated feature checking
|
features: None, // don't perform gated feature checking
|
||||||
}.process_cfg_attrs(outer_attrs.to_owned());
|
};
|
||||||
|
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_span = self.span;
|
||||||
let id = self.parse_ident()?;
|
let id = self.parse_ident()?;
|
||||||
if self.check(&token::Semi) {
|
if self.check(&token::Semi) {
|
||||||
self.bump();
|
self.bump();
|
||||||
|
if in_cfg {
|
||||||
// This mod is in an external file. Let's go get it!
|
// This mod is in an external file. Let's go get it!
|
||||||
let (m, attrs) = self.eval_src_mod(id, &outer_attrs, id_span)?;
|
let (m, attrs) = self.eval_src_mod(id, &outer_attrs, id_span)?;
|
||||||
Ok((id, m, Some(attrs)))
|
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 {
|
} else {
|
||||||
let directory = self.directory.clone();
|
let directory = self.directory.clone();
|
||||||
self.push_directory(id, &outer_attrs);
|
self.push_directory(id, &outer_attrs);
|
||||||
|
|
|
@ -148,3 +148,6 @@ mod test_methods {
|
||||||
fn the(&self);
|
fn the(&self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any())]
|
||||||
|
mod nonexistent_file; // Check that unconfigured non-inline modules are not loaded or parsed.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue