2012-09-04 16:48:32 -05:00
|
|
|
// 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;
|
2012-11-30 13:24:16 -06:00
|
|
|
use syntax::ast_map;
|
|
|
|
use syntax::ast::{def_variant, expr_field, expr_struct, expr_unary, ident,
|
|
|
|
item_class};
|
|
|
|
use syntax::ast::{item_impl, item_trait, item_enum, local_crate, node_id,
|
|
|
|
pat_struct};
|
2012-10-23 17:56:40 -05:00
|
|
|
use syntax::ast::{private, provided, required};
|
2012-09-04 20:28:22 -05:00
|
|
|
use syntax::ast_map::{node_item, node_method};
|
2012-11-30 13:24:16 -06:00
|
|
|
use syntax::ast_util::{has_legacy_export_attr, is_local,
|
|
|
|
visibility_to_privacy, Private, Public};
|
2012-10-23 17:56:40 -05:00
|
|
|
use ty::{ty_class, ty_enum};
|
2012-10-05 20:51:36 -05:00
|
|
|
use typeck::{method_map, method_origin, method_param, method_self};
|
|
|
|
use typeck::{method_static, method_trait};
|
2012-09-04 16:48:32 -05:00
|
|
|
|
|
|
|
use core::util::ignore;
|
|
|
|
use dvec::DVec;
|
|
|
|
|
2012-09-04 20:28:22 -05:00
|
|
|
fn check_crate(tcx: ty::ctxt, method_map: &method_map, crate: @ast::crate) {
|
|
|
|
let privileged_items = @DVec();
|
2012-11-30 13:24:16 -06:00
|
|
|
let legacy_exports = has_legacy_export_attr(crate.node.attrs);
|
2012-09-04 16:48:32 -05:00
|
|
|
|
2012-09-04 20:28:22 -05:00
|
|
|
// Adds structs that are privileged to this scope.
|
|
|
|
let add_privileged_items = |items: &[@ast::item]| {
|
2012-09-04 16:48:32 -05:00
|
|
|
let mut count = 0;
|
|
|
|
for items.each |item| {
|
|
|
|
match item.node {
|
2012-11-30 13:24:16 -06:00
|
|
|
item_class(*) | item_trait(*) | item_impl(*)
|
|
|
|
| item_enum(*) => {
|
2012-09-04 20:28:22 -05:00
|
|
|
privileged_items.push(item.id);
|
2012-09-04 16:48:32 -05:00
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count
|
|
|
|
};
|
|
|
|
|
2012-11-30 13:24:16 -06:00
|
|
|
// Checks that an enum variant is in scope
|
|
|
|
let check_variant = |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, legacy_exports)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// WRONG
|
|
|
|
Public
|
|
|
|
};
|
|
|
|
debug!("parental_privacy = %?", parental_privacy);
|
|
|
|
debug!("vis = %?, priv = %?, legacy_exports = %?",
|
|
|
|
variant_info.vis,
|
|
|
|
visibility_to_privacy(variant_info.vis, legacy_exports),
|
|
|
|
legacy_exports);
|
|
|
|
// inherited => privacy of the enum item
|
|
|
|
if visibility_to_privacy(variant_info.vis,
|
|
|
|
parental_privacy == Public) == Private {
|
|
|
|
tcx.sess.span_err(span,
|
|
|
|
~"can only dereference enums \
|
|
|
|
with a single, public variant");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-04 20:28:22 -05:00
|
|
|
// 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| {
|
2012-09-07 17:32:04 -05:00
|
|
|
if field.ident != ident { loop; }
|
2012-09-04 20:28:22 -05:00
|
|
|
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
|
2012-09-25 19:39:22 -05:00
|
|
|
.contains(&(impl_id.node))) {
|
2012-09-04 20:28:22 -05:00
|
|
|
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, _}) |
|
2012-10-05 20:51:36 -05:00
|
|
|
method_trait(trait_id, method_num, _) |
|
|
|
|
method_self(trait_id, method_num) => {
|
2012-09-04 20:28:22 -05:00
|
|
|
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)
|
2012-09-25 19:39:22 -05:00
|
|
|
if method.vis == private &&
|
|
|
|
!privileged_items
|
|
|
|
.contains(&(trait_id.node)) => {
|
2012-09-04 20:28:22 -05:00
|
|
|
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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-04 16:48:32 -05:00
|
|
|
let visitor = visit::mk_vt(@{
|
2012-09-04 20:28:22 -05:00
|
|
|
visit_mod: |the_module, span, node_id, method_map, visitor| {
|
|
|
|
let n_added = add_privileged_items(the_module.items);
|
2012-09-04 16:48:32 -05:00
|
|
|
|
2012-09-04 20:28:22 -05:00
|
|
|
visit::visit_mod(the_module, span, node_id, method_map, visitor);
|
2012-09-04 16:48:32 -05:00
|
|
|
|
2012-11-28 14:33:00 -06:00
|
|
|
// FIXME #4054: n_added gets corrupted without this log statement
|
|
|
|
debug!("%i", n_added);
|
|
|
|
|
2012-09-04 16:48:32 -05:00
|
|
|
for n_added.times {
|
2012-09-04 20:28:22 -05:00
|
|
|
ignore(privileged_items.pop());
|
2012-09-04 16:48:32 -05:00
|
|
|
}
|
|
|
|
},
|
2012-09-04 20:28:22 -05:00
|
|
|
visit_expr: |expr, method_map: &method_map, visitor| {
|
2012-09-04 16:48:32 -05:00
|
|
|
match expr.node {
|
|
|
|
expr_field(base, ident, _) => {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(ty::expr_ty(tcx, base)).sty {
|
2012-09-04 16:48:32 -05:00
|
|
|
ty_class(id, _)
|
|
|
|
if id.crate != local_crate ||
|
2012-09-25 19:39:22 -05:00
|
|
|
!privileged_items.contains(&(id.node)) => {
|
2012-09-04 20:28:22 -05:00
|
|
|
match method_map.find(expr.id) {
|
|
|
|
None => {
|
|
|
|
debug!("(privacy checking) checking \
|
2012-09-04 21:07:23 -05:00
|
|
|
field access");
|
2012-09-04 20:28:22 -05:00
|
|
|
check_field(expr.span, id, ident);
|
|
|
|
}
|
|
|
|
Some(entry) => {
|
|
|
|
debug!("(privacy checking) checking \
|
|
|
|
impl method");
|
|
|
|
check_method(expr.span, &entry.origin);
|
2012-09-04 16:48:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 21:07:23 -05:00
|
|
|
expr_struct(_, fields, _) => {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(ty::expr_ty(tcx, expr)).sty {
|
2012-09-04 21:07:23 -05:00
|
|
|
ty_class(id, _) => {
|
|
|
|
if id.crate != local_crate ||
|
2012-09-25 19:39:22 -05:00
|
|
|
!privileged_items.contains(&(id.node)) {
|
2012-09-04 21:07:23 -05:00
|
|
|
for fields.each |field| {
|
|
|
|
debug!("(privacy checking) checking \
|
|
|
|
field in struct literal");
|
|
|
|
check_field(expr.span, id,
|
|
|
|
field.node.ident);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-23 17:56:40 -05:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 21:07:23 -05:00
|
|
|
_ => {
|
|
|
|
tcx.sess.span_bug(expr.span, ~"struct expr \
|
|
|
|
didn't have \
|
|
|
|
struct type?!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-30 13:24:16 -06:00
|
|
|
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 */ }
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 16:48:32 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2012-09-04 20:28:22 -05:00
|
|
|
visit::visit_expr(expr, method_map, visitor);
|
2012-09-04 15:29:32 -05:00
|
|
|
},
|
2012-09-04 21:07:23 -05:00
|
|
|
visit_pat: |pattern, method_map, visitor| {
|
|
|
|
match pattern.node {
|
|
|
|
pat_struct(_, fields, _) => {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(ty::pat_ty(tcx, pattern)).sty {
|
2012-09-04 21:07:23 -05:00
|
|
|
ty_class(id, _) => {
|
|
|
|
if id.crate != local_crate ||
|
2012-09-25 19:39:22 -05:00
|
|
|
!privileged_items.contains(&(id.node)) {
|
2012-09-04 21:07:23 -05:00
|
|
|
for fields.each |field| {
|
|
|
|
debug!("(privacy checking) checking \
|
|
|
|
struct pattern");
|
|
|
|
check_field(pattern.span, id,
|
|
|
|
field.ident);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-24 19:04:00 -05:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 21:07:23 -05:00
|
|
|
_ => {
|
|
|
|
tcx.sess.span_bug(pattern.span,
|
|
|
|
~"struct pattern didn't have \
|
|
|
|
struct type?!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::visit_pat(pattern, method_map, visitor);
|
|
|
|
},
|
2012-09-04 15:29:32 -05:00
|
|
|
.. *visit::default_visitor()
|
2012-09-04 16:48:32 -05:00
|
|
|
});
|
2012-09-04 20:28:22 -05:00
|
|
|
visit::visit_crate(*crate, method_map, visitor);
|
2012-09-04 16:48:32 -05:00
|
|
|
}
|
|
|
|
|