Skip to content

Commit 020961d

Browse files
committed
Prohibit access to private statics from other crates through macros 2.0
1 parent 190adc0 commit 020961d

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

src/librustc_privacy/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -783,11 +783,16 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
783783
}
784784

785785
// Prohibit access to associated items with insufficient nominal visibility.
786+
//
787+
// Additionally, until better reachability analysis for macros 2.0 is available,
788+
// we prohibit access to private statics from other crates, this allows to give
789+
// more code internal visibility at link time. (Access to private functions
790+
// is already prohibited by type privacy for funciton types.)
786791
fn visit_qpath(&mut self, qpath: &'tcx hir::QPath, id: ast::NodeId, span: Span) {
787792
let def = match *qpath {
788793
hir::QPath::Resolved(_, ref path) => match path.def {
789794
Def::Method(..) | Def::AssociatedConst(..) |
790-
Def::AssociatedTy(..) => Some(path.def),
795+
Def::AssociatedTy(..) | Def::Static(..) => Some(path.def),
791796
_ => None,
792797
}
793798
hir::QPath::TypeRelative(..) => {
@@ -797,7 +802,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
797802
};
798803
if let Some(def) = def {
799804
let def_id = def.def_id();
800-
if !self.item_is_accessible(def_id) {
805+
let is_local_static = if let Def::Static(..) = def { def_id.is_local() } else { false };
806+
if !self.item_is_accessible(def_id) && !is_local_static {
801807
let name = match *qpath {
802808
hir::QPath::Resolved(_, ref path) => format!("{}", path),
803809
hir::QPath::TypeRelative(_, ref segment) => segment.name.to_string(),

src/test/compile-fail/auxiliary/private-inferred-type.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(decl_macro)]
1212

1313
fn priv_fn() {}
14+
static PRIV_STATIC: u8 = 0;
1415
enum PrivEnum { Variant }
1516
pub enum PubEnum { Variant }
1617
trait PrivTrait { fn method() {} }
@@ -34,6 +35,7 @@ impl Pub<u8> {
3435

3536
pub macro m() {
3637
priv_fn;
38+
PRIV_STATIC;
3739
PrivEnum::Variant;
3840
PubEnum::Variant;
3941
<u8 as PrivTrait>::method;

src/test/compile-fail/private-inferred-type-3.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// aux-build:private-inferred-type.rs
1212

1313
// error-pattern:type `fn() {ext::priv_fn}` is private
14+
// error-pattern:static `PRIV_STATIC` is private
1415
// error-pattern:type `ext::PrivEnum` is private
1516
// error-pattern:type `fn() {<u8 as ext::PrivTrait>::method}` is private
1617
// error-pattern:type `fn(u8) -> ext::PrivTupleStruct {ext::PrivTupleStruct::{{constructor}}}` is pr

src/test/compile-fail/private-inferred-type.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
mod m {
1717
fn priv_fn() {}
18+
static PRIV_STATIC: u8 = 0;
1819
enum PrivEnum { Variant }
1920
pub enum PubEnum { Variant }
2021
trait PrivTrait { fn method() {} }
@@ -47,6 +48,7 @@ mod m {
4748

4849
pub macro m() {
4950
priv_fn; //~ ERROR type `fn() {m::priv_fn}` is private
51+
PRIV_STATIC; // OK, not cross-crate
5052
PrivEnum::Variant; //~ ERROR type `m::PrivEnum` is private
5153
PubEnum::Variant; // OK
5254
<u8 as PrivTrait>::method; //~ ERROR type `fn() {<u8 as m::PrivTrait>::method}` is private

0 commit comments

Comments
 (0)