]> git.lizzy.rs Git - rust.git/commitdiff
Prohibit access to private statics from other crates through macros 2.0
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sat, 18 Nov 2017 16:15:16 +0000 (19:15 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Thu, 21 Dec 2017 00:17:19 +0000 (03:17 +0300)
src/librustc_privacy/lib.rs
src/test/compile-fail/auxiliary/private-inferred-type.rs
src/test/compile-fail/private-inferred-type-3.rs
src/test/compile-fail/private-inferred-type.rs

index 906e584e0ecbcc5cb5020df9d226bd31e28167f1..fc2e2ca662031a7f792b0cbdc53774b247979434 100644 (file)
@@ -783,11 +783,16 @@ fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
     }
 
     // Prohibit access to associated items with insufficient nominal visibility.
+    //
+    // Additionally, until better reachability analysis for macros 2.0 is available,
+    // we prohibit access to private statics from other crates, this allows to give
+    // more code internal visibility at link time. (Access to private functions
+    // is already prohibited by type privacy for funciton types.)
     fn visit_qpath(&mut self, qpath: &'tcx hir::QPath, id: ast::NodeId, span: Span) {
         let def = match *qpath {
             hir::QPath::Resolved(_, ref path) => match path.def {
                 Def::Method(..) | Def::AssociatedConst(..) |
-                Def::AssociatedTy(..) => Some(path.def),
+                Def::AssociatedTy(..) | Def::Static(..) => Some(path.def),
                 _ => None,
             }
             hir::QPath::TypeRelative(..) => {
@@ -797,7 +802,8 @@ fn visit_qpath(&mut self, qpath: &'tcx hir::QPath, id: ast::NodeId, span: Span)
         };
         if let Some(def) = def {
             let def_id = def.def_id();
-            if !self.item_is_accessible(def_id) {
+            let is_local_static = if let Def::Static(..) = def { def_id.is_local() } else { false };
+            if !self.item_is_accessible(def_id) && !is_local_static {
                 let name = match *qpath {
                     hir::QPath::Resolved(_, ref path) => format!("{}", path),
                     hir::QPath::TypeRelative(_, ref segment) => segment.name.to_string(),
index 7627f5dc0cd09e70b7163062ab5e3b597303d725..fc43765f63ce826ef34a0aebe8de703694253ca3 100644 (file)
@@ -11,6 +11,7 @@
 #![feature(decl_macro)]
 
 fn priv_fn() {}
+static PRIV_STATIC: u8 = 0;
 enum PrivEnum { Variant }
 pub enum PubEnum { Variant }
 trait PrivTrait { fn method() {} }
@@ -34,6 +35,7 @@ fn priv_method(&self) {}
 
 pub macro m() {
     priv_fn;
+    PRIV_STATIC;
     PrivEnum::Variant;
     PubEnum::Variant;
     <u8 as PrivTrait>::method;
index c0ba38b240202ece9a41e16605b858f6cd9ef7f1..0c393f02323ec04a3757f16aa641798c773227bb 100644 (file)
@@ -11,6 +11,7 @@
 // aux-build:private-inferred-type.rs
 
 // error-pattern:type `fn() {ext::priv_fn}` is private
+// error-pattern:static `PRIV_STATIC` is private
 // error-pattern:type `ext::PrivEnum` is private
 // error-pattern:type `fn() {<u8 as ext::PrivTrait>::method}` is private
 // error-pattern:type `fn(u8) -> ext::PrivTupleStruct {ext::PrivTupleStruct::{{constructor}}}` is pr
index 95e3732d6134278e980b0b2f44c45fea2af88e14..c50d7f59b8e0a2392e6b9a188b405bfe6833df85 100644 (file)
@@ -15,6 +15,7 @@
 
 mod m {
     fn priv_fn() {}
+    static PRIV_STATIC: u8 = 0;
     enum PrivEnum { Variant }
     pub enum PubEnum { Variant }
     trait PrivTrait { fn method() {} }
@@ -47,6 +48,7 @@ impl TraitWithAssocConst for Priv {}
 
     pub macro m() {
         priv_fn; //~ ERROR type `fn() {m::priv_fn}` is private
+        PRIV_STATIC; // OK, not cross-crate
         PrivEnum::Variant; //~ ERROR type `m::PrivEnum` is private
         PubEnum::Variant; // OK
         <u8 as PrivTrait>::method; //~ ERROR type `fn() {<u8 as m::PrivTrait>::method}` is private