about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-09-16 23:29:15 -0700
committerGitHub <noreply@github.com>2016-09-16 23:29:15 -0700
commit141012dd523e6c13924e7c092e1f7f1407430e8c (patch)
tree6bb0f18a53a235873d4538a204f85ee402aafda4
parent9dc9f340ccf37bccdd6aec0e0f44238d22fa6d7f (diff)
parent6f0ee455023fe24cade7a8ebb0af31c2ac98548e (diff)
downloadrust-141012dd523e6c13924e7c092e1f7f1407430e8c.tar.gz
rust-141012dd523e6c13924e7c092e1f7f1407430e8c.zip
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
-rw-r--r--src/libsyntax/config.rs2
-rw-r--r--src/libsyntax/parse/parser.rs27
-rw-r--r--src/test/run-pass/conditional-compile.rs3
3 files changed, 22 insertions, 10 deletions
diff --git a/src/libsyntax/config.rs b/src/libsyntax/config.rs
index 3f5b294cc04..abbbbe1e3d1 100644
--- a/src/libsyntax/config.rs
+++ b/src/libsyntax/config.rs
@@ -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) {
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index d0936fd981b..98ceb4fbd6c 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -5260,20 +5260,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);
diff --git a/src/test/run-pass/conditional-compile.rs b/src/test/run-pass/conditional-compile.rs
index 5891d9f1aa0..c8e9cbdae1e 100644
--- a/src/test/run-pass/conditional-compile.rs
+++ b/src/test/run-pass/conditional-compile.rs
@@ -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.