forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprivacy.rs
More file actions
606 lines (581 loc) · 26.9 KB
/
privacy.rs
File metadata and controls
606 lines (581 loc) · 26.9 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// A pass that checks to make sure private fields and methods aren't used
// outside their scopes.
use metadata::csearch;
use middle::ty::{ty_struct, ty_enum};
use middle::ty;
use middle::typeck::{method_map, method_origin, method_param, method_self};
use middle::typeck::{method_super};
use middle::typeck::{method_static, method_trait};
use core::util::ignore;
use syntax::ast::{decl_item, def, def_fn, def_id, def_static_method};
use syntax::ast::{def_variant, expr_field, expr_method_call, expr_path};
use syntax::ast::{expr_struct, expr_unary, ident, inherited, item_enum};
use syntax::ast::{item_foreign_mod, item_fn, item_impl, item_struct};
use syntax::ast::{item_trait, local_crate, node_id, pat_struct, Path};
use syntax::ast::{private, provided, public, required, stmt_decl, visibility};
use syntax::ast;
use syntax::ast_map::{node_foreign_item, node_item, node_method};
use syntax::ast_map::{node_trait_method};
use syntax::ast_map;
use syntax::ast_util::{Private, Public, is_local};
use syntax::ast_util::{variant_visibility_to_privacy, visibility_to_privacy};
use syntax::attr;
use syntax::codemap::span;
use syntax::visit;
pub fn check_crate(tcx: ty::ctxt,
method_map: &method_map,
crate: @ast::crate) {
let privileged_items = @mut ~[];
// Adds an item to its scope.
let add_privileged_item: @fn(@ast::item, &mut uint) = |item, count| {
match item.node {
item_struct(*) | item_trait(*) | item_enum(*) |
item_fn(*) => {
privileged_items.push(item.id);
*count += 1;
}
item_impl(_, _, _, ref methods) => {
for methods.each |method| {
privileged_items.push(method.id);
*count += 1;
}
privileged_items.push(item.id);
*count += 1;
}
item_foreign_mod(ref foreign_mod) => {
for foreign_mod.items.each |foreign_item| {
privileged_items.push(foreign_item.id);
*count += 1;
}
}
_ => {}
}
};
// Adds items that are privileged to this scope.
let add_privileged_items: @fn(&[@ast::item]) -> uint = |items| {
let mut count = 0;
for items.each |&item| {
add_privileged_item(item, &mut count);
}
count
};
// Checks that an enum variant is in scope
let check_variant: @fn(span: span, enum_id: ast::def_id) =
|span, enum_id| {
let variant_info = ty::enum_variants(tcx, enum_id)[0];
let parental_privacy = if is_local(enum_id) {
let parent_vis = ast_map::node_item_query(tcx.items, enum_id.node,
|it| { it.vis },
~"unbound enum parent when checking \
dereference of enum type");
visibility_to_privacy(parent_vis)
}
else {
// WRONG
Public
};
debug!("parental_privacy = %?", parental_privacy);
debug!("vis = %?, priv = %?",
variant_info.vis,
visibility_to_privacy(variant_info.vis))
// inherited => privacy of the enum item
if variant_visibility_to_privacy(variant_info.vis,
parental_privacy == Public)
== Private {
tcx.sess.span_err(span,
~"can only dereference enums \
with a single, public variant");
}
};
// Returns the ID of the container (impl or trait) that a crate-local
// method belongs to.
let local_method_container_id:
@fn(span: span, method_id: node_id) -> def_id =
|span, method_id| {
match tcx.items.find(&method_id) {
Some(&node_method(_, impl_id, _)) => impl_id,
Some(&node_trait_method(_, trait_id, _)) => trait_id,
Some(_) => {
tcx.sess.span_bug(span,
fmt!("method was a %s?!",
ast_map::node_id_to_str(
tcx.items,
method_id,
tcx.sess.parse_sess.interner)));
}
None => {
tcx.sess.span_bug(span, ~"method not found in \
AST map?!");
}
}
};
// Returns true if a crate-local method is private and false otherwise.
let method_is_private: @fn(span: span, method_id: node_id) -> bool =
|span, method_id| {
let check = |vis: visibility, container_id: def_id| {
let mut is_private = false;
if vis == private {
is_private = true;
} else if vis == public {
is_private = false;
} else {
// Look up the enclosing impl.
if container_id.crate != local_crate {
tcx.sess.span_bug(span,
~"local method isn't in local \
impl?!");
}
match tcx.items.find(&container_id.node) {
Some(&node_item(item, _)) => {
match item.node {
item_impl(_, None, _, _)
if item.vis != public => {
is_private = true;
}
_ => {}
}
}
Some(_) => {
tcx.sess.span_bug(span, ~"impl wasn't an item?!");
}
None => {
tcx.sess.span_bug(span, ~"impl wasn't in AST map?!");
}
}
}
is_private
};
match tcx.items.find(&method_id) {
Some(&node_method(method, impl_id, _)) => {
check(method.vis, impl_id)
}
Some(&node_trait_method(trait_method, trait_id, _)) => {
match *trait_method {
required(_) => check(public, trait_id),
provided(method) => check(method.vis, trait_id),
}
}
Some(_) => {
tcx.sess.span_bug(span,
fmt!("method_is_private: method was a %s?!",
ast_map::node_id_to_str(
tcx.items,
method_id,
tcx.sess.parse_sess.interner)));
}
None => {
tcx.sess.span_bug(span, ~"method not found in \
AST map?!");
}
}
};
// Returns true if the given local item is private and false otherwise.
let local_item_is_private: @fn(span: span, item_id: node_id) -> bool =
|span, item_id| {
let mut f: &fn(node_id) -> bool = |_| false;
f = |item_id| {
match tcx.items.find(&item_id) {
Some(&node_item(item, _)) => item.vis != public,
Some(&node_foreign_item(_, _, vis, _)) => vis != public,
Some(&node_method(method, impl_did, _)) => {
match method.vis {
private => true,
public => false,
inherited => f(impl_did.node)
}
}
Some(&node_trait_method(_, trait_did, _)) => f(trait_did.node),
Some(_) => {
tcx.sess.span_bug(span,
fmt!("local_item_is_private: item was \
a %s?!",
ast_map::node_id_to_str(
tcx.items,
item_id,
tcx.sess
.parse_sess
.interner)));
}
None => {
tcx.sess.span_bug(span, ~"item not found in AST map?!");
}
}
};
f(item_id)
};
// Checks that a private field is in scope.
let check_field: @fn(span: span, id: ast::def_id, ident: ast::ident) =
|span, id, ident| {
let fields = ty::lookup_struct_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;
}
};
// Given the ID of a method, checks to ensure it's in scope.
let check_method_common: @fn(span: span,
method_id: def_id,
name: &ident) =
|span, method_id, name| {
if method_id.crate == local_crate {
let is_private = method_is_private(span, method_id.node);
let container_id = local_method_container_id(span,
method_id.node);
if is_private &&
(container_id.crate != local_crate ||
!privileged_items.contains(&(container_id.node))) {
tcx.sess.span_err(span,
fmt!("method `%s` is private",
*tcx.sess
.parse_sess
.interner
.get(*name)));
}
} else {
let visibility =
csearch::get_item_visibility(tcx.sess.cstore, method_id);
if visibility != public {
tcx.sess.span_err(span,
fmt!("method `%s` is private",
*tcx.sess.parse_sess.interner
.get(*name)));
}
}
};
// Checks that a private path is in scope.
let check_path: @fn(span: span, def: def, path: @Path) =
|span, def, path| {
debug!("checking path");
match def {
def_static_method(method_id, _, _) => {
debug!("found static method def, checking it");
check_method_common(span, method_id, path.idents.last())
}
def_fn(def_id, _) => {
if def_id.crate == local_crate {
if local_item_is_private(span, def_id.node) &&
!privileged_items.contains(&def_id.node) {
tcx.sess.span_err(span,
fmt!("function `%s` is private",
*tcx.sess
.parse_sess
.interner
.get(copy *path
.idents
.last())));
}
} else if csearch::get_item_visibility(tcx.sess.cstore,
def_id) != public {
tcx.sess.span_err(span,
fmt!("function `%s` is private",
*tcx.sess
.parse_sess
.interner
.get(copy *path
.idents
.last())));
}
}
_ => {}
}
};
// Checks that a private method is in scope.
let check_method: @fn(span: span,
origin: &method_origin,
ident: ast::ident) =
|span, origin, ident| {
match *origin {
method_static(method_id) => {
check_method_common(span, method_id, &ident)
}
method_param(method_param {
trait_id: trait_id,
method_num: method_num,
_
}) |
method_trait(trait_id, method_num, _) |
method_self(trait_id, method_num) |
method_super(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(_, _, ref 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 {
// FIXME #4732: External crates.
}
}
}
};
let visitor = visit::mk_vt(@visit::Visitor {
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_item: |item, method_map, visitor| {
// Do not check privacy inside items with the resolve_unexported
// attribute. This is used for the test runner.
if !attr::contains_name(attr::attr_metas(/*bad*/copy item.attrs),
~"!resolve_unexported") {
visit::visit_item(item, method_map, visitor);
}
},
visit_block: |block, method_map, visitor| {
// Gather up all the privileged items.
let mut n_added = 0;
for block.node.stmts.each |stmt| {
match stmt.node {
stmt_decl(decl, _) => {
match decl.node {
decl_item(item) => {
add_privileged_item(item, &mut n_added);
}
_ => {}
}
}
_ => {}
}
}
visit::visit_block(block, 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, _) => {
// With type_autoderef, make sure we don't
// allow pointers to violate privacy
match ty::get(ty::type_autoderef(tcx, ty::expr_ty(tcx,
base))).sty {
ty_struct(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(ref entry) => {
debug!("(privacy checking) checking \
impl method");
check_method(expr.span,
&entry.origin,
ident);
}
}
}
_ => {}
}
}
expr_method_call(base, ident, _, _, _) => {
// Ditto
match ty::get(ty::type_autoderef(tcx, ty::expr_ty(tcx,
base))).sty {
ty_struct(id, _)
if id.crate != local_crate ||
!privileged_items.contains(&(id.node)) => {
match method_map.find(&expr.id) {
None => {
tcx.sess.span_bug(expr.span,
~"method call not in \
method map");
}
Some(ref entry) => {
debug!("(privacy checking) checking \
impl method");
check_method(expr.span,
&entry.origin,
ident);
}
}
}
_ => {}
}
}
expr_path(path) => {
check_path(expr.span, *tcx.def_map.get(&expr.id), path);
}
expr_struct(_, ref fields, _) => {
match ty::get(ty::expr_ty(tcx, expr)).sty {
ty_struct(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);
}
}
}
ty_enum(id, _) => {
if id.crate != local_crate ||
!privileged_items.contains(&(id.node)) {
match *tcx.def_map.get(&expr.id) {
def_variant(_, variant_id) => {
for (*fields).each |field| {
debug!("(privacy checking) \
checking field in \
struct variant \
literal");
check_field(expr.span, variant_id,
field.node.ident);
}
}
_ => {
tcx.sess.span_bug(expr.span,
~"resolve didn't \
map enum struct \
constructor to a \
variant def");
}
}
}
}
_ => {
tcx.sess.span_bug(expr.span, ~"struct expr \
didn't have \
struct type?!");
}
}
}
expr_unary(ast::deref, operand) => {
// In *e, we need to check that if e's type is an
// enum type t, then t's first variant is public or
// privileged. (We can assume it has only one variant
// since typeck already happened.)
match ty::get(ty::expr_ty(tcx, operand)).sty {
ty_enum(id, _) => {
if id.crate != local_crate ||
!privileged_items.contains(&(id.node)) {
check_variant(expr.span, id);
}
}
_ => { /* No check needed */ }
}
}
_ => {}
}
visit::visit_expr(expr, method_map, visitor);
},
visit_pat: |pattern, method_map, visitor| {
match pattern.node {
pat_struct(_, ref fields, _) => {
match ty::get(ty::pat_ty(tcx, pattern)).sty {
ty_struct(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);
}
}
}
ty_enum(enum_id, _) => {
if enum_id.crate != local_crate ||
!privileged_items.contains(
&enum_id.node) {
match tcx.def_map.find(&pattern.id) {
Some(&def_variant(_, variant_id)) => {
for fields.each |field| {
debug!("(privacy checking) \
checking field in \
struct variant pattern");
check_field(pattern.span,
variant_id,
field.ident);
}
}
_ => {
tcx.sess.span_bug(pattern.span,
~"resolve didn't \
map enum struct \
pattern to a \
variant def");
}
}
}
}
_ => {
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);
}