Skip to content

Commit 3b91784

Browse files
committed
Privacy: tweak macros + more tests
1 parent ddc1a64 commit 3b91784

13 files changed

Lines changed: 175 additions & 15 deletions

compiler/rustc_resolve/src/effective_visibilities.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,8 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
278278
// all the parents in the loop below are also guaranteed to be modules.
279279
let mut module_def_id = macro_module_def_id;
280280
loop {
281-
let changed_reachability =
282-
self.update_macro_reachable(module_def_id, macro_module_def_id, macro_ev);
283-
if changed_reachability || module_def_id == CRATE_DEF_ID {
281+
self.update_macro_reachable(module_def_id, macro_module_def_id, macro_ev);
282+
if module_def_id == CRATE_DEF_ID {
284283
break;
285284
}
286285
module_def_id = self.r.tcx.local_parent(module_def_id);
@@ -294,7 +293,7 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
294293
module_def_id: LocalDefId,
295294
defining_mod: LocalDefId,
296295
macro_ev: EffectiveVisibility,
297-
) -> bool {
296+
) {
298297
if self.macro_reachable.insert((module_def_id, defining_mod)) {
299298
let module = self.r.expect_module(module_def_id.to_def_id());
300299
for (_, name_resolution) in self.r.resolutions(module).borrow().iter() {
@@ -311,9 +310,6 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
311310
self.update_macro_reachable_def(def_id, def_kind, vis, defining_mod, macro_ev);
312311
}
313312
}
314-
true
315-
} else {
316-
false
317313
}
318314
}
319315

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(decl_macro)]
2+
3+
mod n {
4+
pub struct Struct(i32);
5+
pub fn get_struct() -> Struct { Struct(0) }
6+
7+
pub macro allow_field_access($x:expr) {
8+
&mut $x.0
9+
}
10+
}
11+
12+
pub use n::{allow_field_access, get_struct};
13+
14+
pub macro deny_field_access($x:expr) {
15+
&mut $x.0
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(decl_macro)]
2+
3+
mod mod1 {
4+
mod mod2 {
5+
pub fn foo() {}
6+
}
7+
8+
pub(crate) macro m1() {
9+
mod2::foo()
10+
}
11+
}
12+
13+
pub macro m() {
14+
mod1::m1!()
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ aux-build:field-access-macro.rs
2+
3+
extern crate field_access_macro;
4+
5+
fn main() {
6+
let mut s = field_access_macro::get_struct();
7+
8+
let try_field_access = field_access_macro::allow_field_access!(s); // Ok
9+
let try_field_access = field_access_macro::deny_field_access!(s);
10+
//~^ ERROR field `0` of struct `field_access_macro::n::Struct` is private
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0616]: field `0` of struct `field_access_macro::n::Struct` is private
2+
--> $DIR/field-access.rs:9:28
3+
|
4+
LL | let try_field_access = field_access_macro::deny_field_access!(s);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private field
6+
|
7+
= note: this error originates in the macro `field_access_macro::deny_field_access` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0616`.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// Check that functions accessible through a field visible to a macro are
1+
// Check that functions visible to macros through paths with >2 segments are
22
// considered reachable
33

4-
//@ aux-build:nested-fn-macro.rs
4+
//@ aux-build:field-method-macro.rs
55
//@ run-pass
66

7-
extern crate nested_fn_macro;
7+
extern crate field_method_macro;
88

99
fn main() {
10-
assert_eq!(nested_fn_macro::m!(), 12);
10+
assert_eq!(field_method_macro::m!(), 33);
1111
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// Check that functions visible to macros through paths with >2 segments are
1+
// Check that functions accessible through a field visible to a macro are
22
// considered reachable
33

4-
//@ aux-build:field-method-macro.rs
4+
//@ aux-build:nested-fn-macro.rs
55
//@ run-pass
66

7-
extern crate field_method_macro;
7+
extern crate nested_fn_macro;
88

99
fn main() {
10-
assert_eq!(field_method_macro::m!(), 33);
10+
assert_eq!(nested_fn_macro::m!(), 12);
1111
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ aux-build:transitive-macro.rs
2+
//@ build-fail
3+
4+
extern crate transitive_macro;
5+
6+
fn main() {
7+
transitive_macro::m!();
8+
}
9+
10+
//~? ERROR missing optimized MIR for `transitive_macro::mod1::mod2::foo` in the crate `transitive_macro`
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: missing optimized MIR for `transitive_macro::mod1::mod2::foo` in the crate `transitive_macro`
2+
|
3+
note: missing optimized MIR for this item (was the crate `transitive_macro` compiled with `--emit=metadata`?)
4+
--> $DIR/auxiliary/transitive-macro.rs:5:9
5+
|
6+
LL | pub fn foo() {}
7+
| ^^^^^^^^^^^^
8+
9+
error: aborting due to 1 previous error
10+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Make sure that `Placeholder` will be marked as reachable without `use crate::ty` like in
2+
// `effective-visibility-macro.rs` test.
3+
4+
#![feature(rustc_attrs, decl_macro)]
5+
6+
pub mod ty {
7+
pub mod print {
8+
mod pretty {
9+
#[rustc_effective_visibility]
10+
pub macro with_no_queries() {}
11+
//~^ ERROR Direct: pub(in crate::ty::print), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub
12+
}
13+
14+
pub use self::pretty::with_no_queries;
15+
}
16+
17+
#[rustc_effective_visibility]
18+
mod sty {
19+
//~^ ERROR Direct: pub(self), Reexported: pub(self), Reachable: pub(self), ReachableThroughImplTrait: pub(self)
20+
#[rustc_effective_visibility]
21+
pub type Placeholder = ();
22+
//~^ ERROR Direct: pub(in crate::ty), Reexported: pub(in crate::ty), Reachable: pub, ReachableThroughImplTrait: pub
23+
}
24+
}
25+
26+
fn main() {}

0 commit comments

Comments
 (0)