Skip to content

Commit a9e21a3

Browse files
committed
update tests
1 parent b0551a4 commit a9e21a3

File tree

3 files changed

+57
-56
lines changed

3 files changed

+57
-56
lines changed

compiler/rustc_resolve/src/access_levels.rs

+24-23
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_ast::visit;
77
use rustc_ast::visit::Visitor;
88
use rustc_ast::Crate;
99
use rustc_ast::EnumDef;
10-
use rustc_ast::NodeId;
1110
use rustc_hir::def_id::LocalDefId;
1211
use rustc_hir::def_id::CRATE_DEF_ID;
1312
use rustc_middle::middle::privacy::AccessLevel;
@@ -45,35 +44,47 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
4544
/// This will also follow `use` chains (see PrivacyVisitor::set_import_binding_access_level).
4645
fn set_bindings_access_level(&mut self, module_id: LocalDefId) {
4746
assert!(self.r.module_map.contains_key(&&module_id.to_def_id()));
48-
let module_level = self.r.access_levels.get_access_level(module_id);
47+
4948
// Set the given binding access level to `AccessLevel::Public` and
5049
// sets the rest of the `use` chain to `AccessLevel::Exported` until
5150
// we hit the actual exported item.
5251
let set_import_binding_access_level =
53-
|this: &mut Self, mut binding: &NameBinding<'a>, mut access_level| {
52+
|this: &mut Self, mut binding: &NameBinding<'a>, mut parent_id| {
5453
while let NameBindingKind::Import { binding: nested_binding, import, .. } =
5554
binding.kind
5655
{
57-
this.set_access_level(import.id, access_level);
58-
if let ImportKind::Single { additional_ids, .. } = import.kind {
59-
this.set_access_level(additional_ids.0, access_level);
60-
this.set_access_level(additional_ids.1, access_level);
61-
}
56+
if this.r.opt_local_def_id(import.id).is_some() {
57+
let vis = match binding.vis {
58+
Visibility::Public => Visibility::Public,
59+
Visibility::Restricted(id) => Visibility::Restricted(id.expect_local())
60+
};
61+
this.update_effective_vis(this.r.local_def_id(import.id), vis, parent_id, AccessLevel::Exported);
62+
if let ImportKind::Single { additional_ids, .. } = import.kind {
63+
64+
if let Some(id) = this.r.opt_local_def_id(additional_ids.0) {
65+
this.update_effective_vis(id, vis, parent_id, AccessLevel::Exported);
66+
}
67+
68+
if let Some(id) = this.r.opt_local_def_id(additional_ids.1) {
69+
this.update_effective_vis(id, vis, parent_id, AccessLevel::Exported);
70+
}
71+
}
6272

63-
access_level = Some(AccessLevel::Exported);
73+
parent_id = this.r.local_def_id(import.id);
74+
}
6475
binding = nested_binding;
6576
}
6677
};
6778

68-
let module = self.r.get_module(module_id.to_def_id()).unwrap();
69-
let resolutions = self.r.resolutions(module);
79+
let module = self.r.get_module(module_id.to_def_id()).unwrap();
80+
let resolutions = self.r.resolutions(module);
7081

7182
for (.., name_resolution) in resolutions.borrow().iter() {
7283
if let Some(binding) = name_resolution.borrow().binding() {
7384
let tag = match binding.is_import() {
7485
true => {
75-
if binding.vis.is_public() && !binding.is_ambiguity() && module_level.is_some() {
76-
set_import_binding_access_level(self, binding, module_level);
86+
if !binding.is_ambiguity() {
87+
set_import_binding_access_level(self, binding, module_id);
7788
}
7889
AccessLevel::Exported
7990
},
@@ -125,16 +136,6 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
125136
}
126137
}
127138

128-
/// Sets the access level of the `LocalDefId` corresponding to the given `NodeId`.
129-
/// This function will panic if the `NodeId` does not have a `LocalDefId`
130-
fn set_access_level(
131-
&mut self,
132-
node_id: NodeId,
133-
access_level: Option<AccessLevel>,
134-
) -> Option<AccessLevel> {
135-
self.set_access_level_def_id(self.r.local_def_id(node_id), access_level)
136-
}
137-
138139
fn set_access_level_def_id(
139140
&mut self,
140141
def_id: LocalDefId,

src/test/ui/privacy/access_levels.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
11
#![feature(rustc_attrs)]
22

33
#[rustc_effective_visibility]
4-
mod outer { //~ ERROR Public: pub(self), Exported: pub(self), Reachable: pub(self), ReachableFromImplTrait: pub(self)
4+
mod outer { //~ ERROR Public: pub(access_levels), Exported: pub(access_levels), Reachable: pub(access_levels), ReachableFromImplTrait: pub(access_levels)
55
#[rustc_effective_visibility]
6-
pub mod inner1 { //~ ERROR Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
6+
pub mod inner1 { //~ ERROR Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
77

88
#[rustc_effective_visibility]
9-
extern "C" {} //~ ERROR Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
9+
extern "C" {} //~ ERROR Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
1010

1111
#[rustc_effective_visibility]
12-
pub trait PubTrait { //~ ERROR Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
12+
pub trait PubTrait { //~ ERROR Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
1313
#[rustc_effective_visibility]
14-
const A: i32; //~ ERROR Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
14+
const A: i32; //~ ERROR Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
1515
#[rustc_effective_visibility]
16-
type B; //~ ERROR Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
16+
type B; //~ ERROR Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
1717
}
1818

1919
#[rustc_effective_visibility]
20-
struct PrivStruct; //~ ERROR Public: pub(self), Exported: pub(self), Reachable: pub(self), ReachableFromImplTrait: pub(self)
20+
struct PrivStruct; //~ ERROR Public: pub(inner1), Exported: pub(inner1), Reachable: pub(inner1), ReachableFromImplTrait: pub(inner1)
2121

2222
#[rustc_effective_visibility]
23-
pub union PubUnion { //~ ERROR Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
23+
pub union PubUnion { //~ ERROR Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
2424
#[rustc_effective_visibility]
25-
a: u8, //~ ERROR Public: pub(self), Exported: pub(self), Reachable: pub(self), ReachableFromImplTrait: pub(self)
25+
a: u8, //~ ERROR Public: pub(inner1), Exported: pub(inner1), Reachable: pub(inner1), ReachableFromImplTrait: pub(inner1)
2626
#[rustc_effective_visibility]
27-
pub b: u8, //~ ERROR Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
27+
pub b: u8, //~ ERROR Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
2828
}
2929

3030
#[rustc_effective_visibility]
31-
pub enum Enum { //~ ERROR Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
31+
pub enum Enum { //~ ERROR Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
3232
#[rustc_effective_visibility]
33-
A( //~ ERROR Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
33+
A( //~ ERROR Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
3434
#[rustc_effective_visibility]
35-
PubUnion, //~ ERROR Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
35+
PubUnion, //~ ERROR Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
3636
),
3737
}
3838
}
3939

4040
#[rustc_effective_visibility]
41-
macro_rules! none_macro { //~ Public: pub(self), Exported: pub(self), Reachable: pub(self), ReachableFromImplTrait: pub(self)
41+
macro_rules! none_macro { //~ Public: pub(access_levels), Exported: pub(access_levels), Reachable: pub(access_levels), ReachableFromImplTrait: pub(access_levels)
4242
() => {};
4343
}
4444

4545
#[macro_export]
4646
#[rustc_effective_visibility]
47-
macro_rules! public_macro { //~ Public: pub, Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
47+
macro_rules! public_macro { //~ Public: pub, Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
4848
() => {};
4949
}
5050

5151
#[rustc_effective_visibility]
52-
pub struct ReachableStruct { //~ ERROR Public: pub(self), Exported: pub(self), Reachable: pub, ReachableFromImplTrait: pub
52+
pub struct ReachableStruct { //~ ERROR Public: pub(access_levels), Exported: pub(access_levels), Reachable: pub, ReachableFromImplTrait: pub
5353
#[rustc_effective_visibility]
54-
pub a: u8, //~ ERROR Public: pub(self), Exported: pub(self), Reachable: pub, ReachableFromImplTrait: pub
54+
pub a: u8, //~ ERROR Public: pub(access_levels), Exported: pub(access_levels), Reachable: pub, ReachableFromImplTrait: pub
5555
}
5656
}
5757

src/test/ui/privacy/access_levels.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
1-
error: Public: pub(self), Exported: pub(self), Reachable: pub(self), ReachableFromImplTrait: pub(self)
1+
error: Public: pub(access_levels), Exported: pub(access_levels), Reachable: pub(access_levels), ReachableFromImplTrait: pub(access_levels)
22
--> $DIR/access_levels.rs:4:1
33
|
44
LL | mod outer {
55
| ^^^^^^^^^
66

7-
error: Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
7+
error: Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
88
--> $DIR/access_levels.rs:6:5
99
|
1010
LL | pub mod inner1 {
1111
| ^^^^^^^^^^^^^^
1212

13-
error: Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
13+
error: Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
1414
--> $DIR/access_levels.rs:9:9
1515
|
1616
LL | extern "C" {}
1717
| ^^^^^^^^^^^^^
1818

19-
error: Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
19+
error: Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
2020
--> $DIR/access_levels.rs:12:9
2121
|
2222
LL | pub trait PubTrait {
2323
| ^^^^^^^^^^^^^^^^^^
2424

25-
error: Public: pub(self), Exported: pub(self), Reachable: pub(self), ReachableFromImplTrait: pub(self)
25+
error: Public: pub(inner1), Exported: pub(inner1), Reachable: pub(inner1), ReachableFromImplTrait: pub(inner1)
2626
--> $DIR/access_levels.rs:20:9
2727
|
2828
LL | struct PrivStruct;
2929
| ^^^^^^^^^^^^^^^^^
3030

31-
error: Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
31+
error: Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
3232
--> $DIR/access_levels.rs:23:9
3333
|
3434
LL | pub union PubUnion {
3535
| ^^^^^^^^^^^^^^^^^^
3636

37-
error: Public: pub(self), Exported: pub(self), Reachable: pub(self), ReachableFromImplTrait: pub(self)
37+
error: Public: pub(inner1), Exported: pub(inner1), Reachable: pub(inner1), ReachableFromImplTrait: pub(inner1)
3838
--> $DIR/access_levels.rs:25:13
3939
|
4040
LL | a: u8,
4141
| ^^^^^
4242

43-
error: Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
43+
error: Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
4444
--> $DIR/access_levels.rs:27:13
4545
|
4646
LL | pub b: u8,
4747
| ^^^^^^^^^
4848

49-
error: Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
49+
error: Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
5050
--> $DIR/access_levels.rs:31:9
5151
|
5252
LL | pub enum Enum {
5353
| ^^^^^^^^^^^^^
5454

55-
error: Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
55+
error: Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
5656
--> $DIR/access_levels.rs:33:13
5757
|
5858
LL | A(
5959
| ^
6060

61-
error: Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
61+
error: Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
6262
--> $DIR/access_levels.rs:35:17
6363
|
6464
LL | PubUnion,
6565
| ^^^^^^^^
6666

67-
error: Public: pub(self), Exported: pub(self), Reachable: pub(self), ReachableFromImplTrait: pub(self)
67+
error: Public: pub(access_levels), Exported: pub(access_levels), Reachable: pub(access_levels), ReachableFromImplTrait: pub(access_levels)
6868
--> $DIR/access_levels.rs:41:5
6969
|
7070
LL | macro_rules! none_macro {
@@ -76,25 +76,25 @@ error: Public: pub, Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
7676
LL | macro_rules! public_macro {
7777
| ^^^^^^^^^^^^^^^^^^^^^^^^^
7878

79-
error: Public: pub(self), Exported: pub(self), Reachable: pub, ReachableFromImplTrait: pub
79+
error: Public: pub(access_levels), Exported: pub(access_levels), Reachable: pub, ReachableFromImplTrait: pub
8080
--> $DIR/access_levels.rs:52:5
8181
|
8282
LL | pub struct ReachableStruct {
8383
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
8484

85-
error: Public: pub(self), Exported: pub(self), Reachable: pub, ReachableFromImplTrait: pub
85+
error: Public: pub(access_levels), Exported: pub(access_levels), Reachable: pub, ReachableFromImplTrait: pub
8686
--> $DIR/access_levels.rs:54:9
8787
|
8888
LL | pub a: u8,
8989
| ^^^^^^^^^
9090

91-
error: Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
91+
error: Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
9292
--> $DIR/access_levels.rs:14:13
9393
|
9494
LL | const A: i32;
9595
| ^^^^^^^^^^^^
9696

97-
error: Public: pub(self), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
97+
error: Public: pub(access_levels), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
9898
--> $DIR/access_levels.rs:16:13
9999
|
100100
LL | type B;

0 commit comments

Comments
 (0)