]> git.lizzy.rs Git - rust.git/commitdiff
Warn instead of error when using an inaccessable extern crate
authorJeffrey Seyfried <jeffrey.seyfried@gmail.com>
Fri, 12 Feb 2016 06:42:44 +0000 (06:42 +0000)
committerJeffrey Seyfried <jeffrey.seyfried@gmail.com>
Wed, 24 Feb 2016 01:34:39 +0000 (01:34 +0000)
src/librustc/lint/builtin.rs
src/librustc_lint/lib.rs
src/librustc_privacy/lib.rs
src/test/compile-fail/extern-crate-visibility.rs

index 99fea59e28d94185e85bd44d72b86f6c8b47ecb2..4bb69a2688a41ded0c12a090929b61d422f093e5 100644 (file)
     "detect private items in public interfaces not caught by the old implementation"
 }
 
+declare_lint! {
+    pub INACCESSIBLE_EXTERN_CRATE,
+    Warn,
+    "use of inaccessible extern crate erroneously allowed"
+}
+
 declare_lint! {
     pub INVALID_TYPE_PARAM_DEFAULT,
     Warn,
@@ -167,6 +173,7 @@ fn get_lints(&self) -> LintArray {
             TRIVIAL_CASTS,
             TRIVIAL_NUMERIC_CASTS,
             PRIVATE_IN_PUBLIC,
+            INACCESSIBLE_EXTERN_CRATE,
             INVALID_TYPE_PARAM_DEFAULT,
             MATCH_OF_UNIT_VARIANT_VIA_PAREN_DOTDOT,
             CONST_ERR,
index 6868b4f2ab768a054bc70c487e12dcedcec98d24..1cf0339c086e5137269a141fe784d630e033f1b9 100644 (file)
@@ -158,6 +158,10 @@ macro_rules! add_lint_group {
             id: LintId::of(PRIVATE_IN_PUBLIC),
             reference: "the explanation for E0446 (`--explain E0446`)",
         },
+        FutureIncompatibleInfo {
+            id: LintId::of(INACCESSIBLE_EXTERN_CRATE),
+            reference: "PR 31362 <https://github.com/rust-lang/rust/pull/31362>",
+        },
         FutureIncompatibleInfo {
             id: LintId::of(INVALID_TYPE_PARAM_DEFAULT),
             reference: "PR 30742 <https://github.com/rust-lang/rust/pull/30724>",
index f0786b9b1fadd283397e631668ea0815280e80b1..8908dac7a36dd72c9cd355cd6e94e3da09c9a13a 100644 (file)
@@ -743,6 +743,7 @@ fn ensure_public(&self,
                      source_did: Option<DefId>,
                      msg: &str)
                      -> CheckResult {
+        use rustc_front::hir::Item_::ItemExternCrate;
         debug!("ensure_public(span={:?}, to_check={:?}, source_did={:?}, msg={:?})",
                span, to_check, source_did, msg);
         let def_privacy = self.def_privacy(to_check);
@@ -763,6 +764,21 @@ fn ensure_public(&self,
         // be local.)
         let def_id = source_did.unwrap_or(to_check);
         let node_id = self.tcx.map.as_local_node_id(def_id);
+
+        // Warn when using a inaccessible extern crate.
+        if let Some(node_id) = self.tcx.map.as_local_node_id(to_check) {
+            match self.tcx.map.get(node_id) {
+                ast_map::Node::NodeItem(&hir::Item { node: ItemExternCrate(_), name, .. }) => {
+                    self.tcx.sess.add_lint(lint::builtin::INACCESSIBLE_EXTERN_CRATE,
+                                           node_id,
+                                           span,
+                                           format!("extern crate `{}` is private", name));
+                    return None;
+                }
+                _ => {}
+            }
+        }
+
         let (err_span, err_msg) = if Some(id) == node_id {
             return Some((span, format!("{} is private", msg), None));
         } else {
index 95eeb60c1ea7e5f38595653ac2443f5500cd56ca..56a41a15ab3c0b63317895c62c3295fd3c884779 100644 (file)
@@ -8,16 +8,27 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(rustc_attrs)]
+#![allow(dead_code)]
+#![allow(unused_imports)]
+
 mod foo {
     extern crate core;
-    pub use self::core as reexported_core; // Check that private extern crates can be reexported
 }
 
-// Check that private crates cannot be used from outside their modules
-use foo::core; //~ ERROR module `core` is inaccessible
-use foo::core::cell; //~ ERROR
+// Check that private crates can be used from outside their modules, albeit with warnings
+use foo::core; //~ WARN extern crate `core` is private
+//~^ WARN this was previously accepted by the compiler but is being phased out
+use foo::core::cell; //~ WARN extern crate `core` is private
+//~^ WARN this was previously accepted by the compiler but is being phased out
+
+fn f() {
+    foo::core::cell::Cell::new(0); //~ WARN extern crate `core` is private
+    //~^ WARN this was previously accepted by the compiler but is being phased out
 
-fn main() {
     use foo::*;
     mod core {} // Check that private crates are not glob imported
 }
+
+#[rustc_error]
+fn main() {} //~ ERROR compilation successful