1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
// A pass that checks to make sure private fields and methods aren't used
// outside their scopes.
use /*mod*/ syntax::ast;
use /*mod*/ syntax::visit;
use syntax::ast::{expr_field, expr_struct, ident, item_class, item_impl};
use syntax::ast::{item_trait, local_crate, node_id, pat_struct, private};
use syntax::ast::{provided, required};
use syntax::ast_map::{node_item, node_method};
use ty::ty_class;
use typeck::{method_map, method_origin, method_param, method_self};
use typeck::{method_static, method_trait};
use core::util::ignore;
use dvec::DVec;
fn check_crate(tcx: ty::ctxt, method_map: &method_map, crate: @ast::crate) {
let privileged_items = @DVec();
// Adds structs that are privileged to this scope.
let add_privileged_items = |items: &[@ast::item]| {
let mut count = 0;
for items.each |item| {
match item.node {
item_class(*) | item_trait(*) | item_impl(*) => {
privileged_items.push(item.id);
count += 1;
}
_ => {}
}
}
count
};
// Checks that a private field is in scope.
let check_field = |span, id, ident| {
let fields = ty::lookup_class_fields(tcx, id);
for fields.each |field| {
if field.ident != ident { loop; }
if field.vis == private {
tcx.sess.span_err(span, fmt!("field `%s` is private",
*tcx.sess.parse_sess.interner
.get(ident)));
}
break;
}
};
// Checks that a private method is in scope.
let check_method = |span, origin: &method_origin| {
match *origin {
method_static(method_id) => {
if method_id.crate == local_crate {
match tcx.items.find(method_id.node) {
Some(node_method(method, impl_id, _)) => {
if method.vis == private &&
(impl_id.crate != local_crate ||
!privileged_items
.contains(&(impl_id.node))) {
tcx.sess.span_err(span,
fmt!("method `%s` is \
private",
*tcx.sess
.parse_sess
.interner
.get(method
.ident)));
}
}
Some(_) => {
tcx.sess.span_bug(span, ~"method wasn't \
actually a method?!");
}
None => {
tcx.sess.span_bug(span, ~"method not found in \
AST map?!");
}
}
} else {
// XXX: External crates.
}
}
method_param({trait_id: trait_id, method_num: method_num, _}) |
method_trait(trait_id, method_num, _) |
method_self(trait_id, method_num) => {
if trait_id.crate == local_crate {
match tcx.items.find(trait_id.node) {
Some(node_item(item, _)) => {
match item.node {
item_trait(_, _, methods) => {
if method_num >= methods.len() {
tcx.sess.span_bug(span, ~"method \
number \
out of \
range?!");
}
match methods[method_num] {
provided(method)
if method.vis == private &&
!privileged_items
.contains(&(trait_id.node)) => {
tcx.sess.span_err(span,
fmt!("method
`%s` \
is \
private",
*tcx
.sess
.parse_sess
.interner
.get
(method
.ident)));
}
provided(_) | required(_) => {
// Required methods can't be
// private.
}
}
}
_ => {
tcx.sess.span_bug(span, ~"trait wasn't \
actually a \
trait?!");
}
}
}
Some(_) => {
tcx.sess.span_bug(span, ~"trait wasn't an \
item?!");
}
None => {
tcx.sess.span_bug(span, ~"trait item wasn't \
found in the AST \
map?!");
}
}
} else {
// XXX: External crates.
}
}
}
};
let visitor = visit::mk_vt(@{
visit_mod: |the_module, span, node_id, method_map, visitor| {
let n_added = add_privileged_items(the_module.items);
visit::visit_mod(the_module, span, node_id, method_map, visitor);
for n_added.times {
ignore(privileged_items.pop());
}
},
visit_expr: |expr, method_map: &method_map, visitor| {
match expr.node {
expr_field(base, ident, _) => {
match ty::get(ty::expr_ty(tcx, base)).sty {
ty_class(id, _)
if id.crate != local_crate ||
!privileged_items.contains(&(id.node)) => {
match method_map.find(expr.id) {
None => {
debug!("(privacy checking) checking \
field access");
check_field(expr.span, id, ident);
}
Some(entry) => {
debug!("(privacy checking) checking \
impl method");
check_method(expr.span, &entry.origin);
}
}
}
_ => {}
}
}
expr_struct(_, fields, _) => {
match ty::get(ty::expr_ty(tcx, expr)).sty {
ty_class(id, _) => {
if id.crate != local_crate ||
!privileged_items.contains(&(id.node)) {
for fields.each |field| {
debug!("(privacy checking) checking \
field in struct literal");
check_field(expr.span, id,
field.node.ident);
}
}
}
_ => {
tcx.sess.span_bug(expr.span, ~"struct expr \
didn't have \
struct type?!");
}
}
}
_ => {}
}
visit::visit_expr(expr, method_map, visitor);
},
visit_pat: |pattern, method_map, visitor| {
match pattern.node {
pat_struct(_, fields, _) => {
match ty::get(ty::pat_ty(tcx, pattern)).sty {
ty_class(id, _) => {
if id.crate != local_crate ||
!privileged_items.contains(&(id.node)) {
for fields.each |field| {
debug!("(privacy checking) checking \
struct pattern");
check_field(pattern.span, id,
field.ident);
}
}
}
_ => {
tcx.sess.span_bug(pattern.span,
~"struct pattern didn't have \
struct type?!");
}
}
}
_ => {}
}
visit::visit_pat(pattern, method_map, visitor);
},
.. *visit::default_visitor()
});
visit::visit_crate(*crate, method_map, visitor);
}
|