2011-11-15 11:15:35 -06:00
|
|
|
import syntax::{visit, ast_util};
|
|
|
|
import syntax::ast::*;
|
|
|
|
import syntax::codemap::span;
|
2012-08-07 20:10:06 -05:00
|
|
|
import ty::{kind, kind_copyable, kind_noncopyable, kind_const};
|
2012-01-12 10:59:49 -06:00
|
|
|
import driver::session::session;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2012-05-07 17:14:56 -05:00
|
|
|
import util::ppaux::{ty_to_str, tys_to_str};
|
2012-04-30 12:37:58 -05:00
|
|
|
import syntax::print::pprust::expr_to_str;
|
2012-05-07 13:31:57 -05:00
|
|
|
import freevars::freevar_entry;
|
2012-06-04 22:44:58 -05:00
|
|
|
import lint::{non_implicitly_copyable_typarams,implicit_copies};
|
2011-11-15 11:15:35 -06:00
|
|
|
|
2012-05-24 16:33:21 -05:00
|
|
|
// Kind analysis pass.
|
2011-11-18 10:09:14 -06:00
|
|
|
//
|
2012-05-24 16:33:21 -05:00
|
|
|
// There are several kinds defined by various operations. The most restrictive
|
|
|
|
// kind is noncopyable. The noncopyable kind can be extended with any number
|
|
|
|
// of the following attributes.
|
|
|
|
//
|
|
|
|
// send: Things that can be sent on channels or included in spawned closures.
|
|
|
|
// copy: Things that can be copied.
|
|
|
|
// const: Things thare are deeply immutable. They are guaranteed never to
|
|
|
|
// change, and can be safely shared without copying between tasks.
|
2012-07-16 22:17:57 -05:00
|
|
|
// owned: Things that do not contain borrowed pointers.
|
2012-05-24 16:33:21 -05:00
|
|
|
//
|
2012-06-24 17:09:57 -05:00
|
|
|
// Send includes scalar types as well as classes and unique types containing
|
|
|
|
// only sendable types.
|
2012-05-24 16:33:21 -05:00
|
|
|
//
|
|
|
|
// Copy includes boxes, closure and unique types containing copyable types.
|
|
|
|
//
|
|
|
|
// Const include scalar types, things without non-const fields, and pointers
|
|
|
|
// to const things.
|
2011-11-18 10:09:14 -06:00
|
|
|
//
|
|
|
|
// This pass ensures that type parameters are only instantiated with types
|
|
|
|
// whose kinds are equal or less general than the way the type parameter was
|
2012-05-24 16:33:21 -05:00
|
|
|
// annotated (with the `send`, `copy` or `const` keyword).
|
2011-11-18 10:09:14 -06:00
|
|
|
//
|
|
|
|
// It also verifies that noncopyable kinds are not copied. Sendability is not
|
|
|
|
// applied, since none of our language primitives send. Instead, the sending
|
|
|
|
// primitives in the stdlib are explicitly annotated to only take sendable
|
|
|
|
// types.
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn kind_to_str(k: kind) -> ~str {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut kinds = ~[];
|
2012-07-16 22:17:57 -05:00
|
|
|
|
2012-05-24 17:04:59 -05:00
|
|
|
if ty::kind_lteq(kind_const(), k) {
|
2012-07-14 00:57:48 -05:00
|
|
|
vec::push(kinds, ~"const");
|
2012-05-22 16:10:32 -05:00
|
|
|
}
|
2012-07-16 22:17:57 -05:00
|
|
|
|
2012-05-24 16:24:09 -05:00
|
|
|
if ty::kind_can_be_copied(k) {
|
2012-07-14 00:57:48 -05:00
|
|
|
vec::push(kinds, ~"copy");
|
2012-05-24 16:24:09 -05:00
|
|
|
}
|
2012-07-16 22:17:57 -05:00
|
|
|
|
2012-05-24 16:24:09 -05:00
|
|
|
if ty::kind_can_be_sent(k) {
|
2012-07-14 00:57:48 -05:00
|
|
|
vec::push(kinds, ~"send");
|
2012-07-16 22:17:57 -05:00
|
|
|
} else if ty::kind_is_owned(k) {
|
|
|
|
vec::push(kinds, ~"owned");
|
2012-05-24 16:24:09 -05:00
|
|
|
}
|
2012-07-16 22:17:57 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
str::connect(kinds, ~" ")
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type rval_map = std::map::hashmap<node_id, ()>;
|
|
|
|
|
|
|
|
type ctx = {tcx: ty::ctxt,
|
2011-12-14 07:38:25 -06:00
|
|
|
method_map: typeck::method_map,
|
2012-05-29 18:22:22 -05:00
|
|
|
last_use_map: liveness::last_use_map,
|
|
|
|
current_item: node_id};
|
2011-11-15 11:15:35 -06:00
|
|
|
|
2012-05-29 18:22:22 -05:00
|
|
|
fn check_crate(tcx: ty::ctxt,
|
|
|
|
method_map: typeck::method_map,
|
|
|
|
last_use_map: liveness::last_use_map,
|
|
|
|
crate: @crate) {
|
2011-11-15 11:15:35 -06:00
|
|
|
let ctx = {tcx: tcx,
|
2011-12-14 07:38:25 -06:00
|
|
|
method_map: method_map,
|
2012-05-29 18:22:22 -05:00
|
|
|
last_use_map: last_use_map,
|
|
|
|
current_item: -1};
|
2011-11-15 11:15:35 -06:00
|
|
|
let visit = visit::mk_vt(@{
|
|
|
|
visit_expr: check_expr,
|
2011-12-20 21:39:33 -06:00
|
|
|
visit_stmt: check_stmt,
|
2012-01-02 08:23:11 -06:00
|
|
|
visit_block: check_block,
|
2012-03-19 04:45:29 -05:00
|
|
|
visit_fn: check_fn,
|
2012-05-29 18:22:22 -05:00
|
|
|
visit_ty: check_ty,
|
|
|
|
visit_item: fn@(i: @item, cx: ctx, v: visit::vt<ctx>) {
|
|
|
|
visit::visit_item(i, {current_item: i.id with cx}, v);
|
|
|
|
}
|
2011-11-15 11:15:35 -06:00
|
|
|
with *visit::default_visitor()
|
|
|
|
});
|
|
|
|
visit::visit_crate(*crate, ctx, visit);
|
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:44:58 -05:00
|
|
|
type check_fn = fn@(ctx, node_id, option<@freevar_entry>,
|
|
|
|
bool, ty::t, sp: span);
|
2012-05-07 13:31:57 -05:00
|
|
|
|
2011-12-20 21:39:33 -06:00
|
|
|
// Yields the appropriate function to check the kind of closed over
|
|
|
|
// variables. `id` is the node_id for some expression that creates the
|
|
|
|
// closure.
|
2012-05-07 13:31:57 -05:00
|
|
|
fn with_appropriate_checker(cx: ctx, id: node_id, b: fn(check_fn)) {
|
2012-06-04 22:44:58 -05:00
|
|
|
fn check_for_uniq(cx: ctx, id: node_id, fv: option<@freevar_entry>,
|
|
|
|
is_move: bool, var_t: ty::t, sp: span) {
|
2012-05-07 13:31:57 -05:00
|
|
|
// all captured data must be sendable, regardless of whether it is
|
2012-07-16 22:17:57 -05:00
|
|
|
// moved in or copied in. Note that send implies owned.
|
2012-08-01 19:30:05 -05:00
|
|
|
if !check_send(cx, var_t, sp) { return; }
|
2012-05-07 13:31:57 -05:00
|
|
|
|
2012-05-23 17:46:43 -05:00
|
|
|
// copied in data must be copyable, but moved in data can be anything
|
2012-05-23 20:57:56 -05:00
|
|
|
let is_implicit = fv.is_some();
|
2012-06-04 22:44:58 -05:00
|
|
|
if !is_move { check_copy(cx, id, var_t, sp, is_implicit); }
|
2012-05-23 17:46:43 -05:00
|
|
|
|
2012-05-07 13:31:57 -05:00
|
|
|
// check that only immutable variables are implicitly copied in
|
2012-06-30 18:19:07 -05:00
|
|
|
for fv.each |fv| {
|
2012-05-30 12:46:22 -05:00
|
|
|
check_imm_free_var(cx, fv.def, fv.span);
|
2012-05-07 13:31:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:44:58 -05:00
|
|
|
fn check_for_box(cx: ctx, id: node_id, fv: option<@freevar_entry>,
|
|
|
|
is_move: bool, var_t: ty::t, sp: span) {
|
2012-07-16 22:17:57 -05:00
|
|
|
// all captured data must be owned
|
2012-08-01 19:30:05 -05:00
|
|
|
if !check_owned(cx.tcx, var_t, sp) { return; }
|
2012-07-16 22:17:57 -05:00
|
|
|
|
2012-05-07 13:31:57 -05:00
|
|
|
// copied in data must be copyable, but moved in data can be anything
|
2012-05-23 20:57:56 -05:00
|
|
|
let is_implicit = fv.is_some();
|
2012-06-04 22:44:58 -05:00
|
|
|
if !is_move { check_copy(cx, id, var_t, sp, is_implicit); }
|
2012-05-07 13:31:57 -05:00
|
|
|
|
|
|
|
// check that only immutable variables are implicitly copied in
|
2012-06-30 18:19:07 -05:00
|
|
|
for fv.each |fv| {
|
2012-05-30 12:46:22 -05:00
|
|
|
check_imm_free_var(cx, fv.def, fv.span);
|
2012-05-07 13:31:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:44:58 -05:00
|
|
|
fn check_for_block(cx: ctx, _id: node_id, fv: option<@freevar_entry>,
|
|
|
|
_is_move: bool, _var_t: ty::t, sp: span) {
|
2012-05-07 13:31:57 -05:00
|
|
|
// only restriction: no capture clauses (we would have to take
|
|
|
|
// ownership of the moved/copied in data).
|
|
|
|
if fv.is_none() {
|
|
|
|
cx.tcx.sess.span_err(
|
|
|
|
sp,
|
2012-07-14 00:57:48 -05:00
|
|
|
~"cannot capture values explicitly with a block closure");
|
2012-05-07 13:31:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:44:58 -05:00
|
|
|
fn check_for_bare(cx: ctx, _id: node_id, _fv: option<@freevar_entry>,
|
|
|
|
_is_move: bool,_var_t: ty::t, sp: span) {
|
2012-07-14 00:57:48 -05:00
|
|
|
cx.tcx.sess.span_err(sp, ~"attempted dynamic environment capture");
|
2012-05-07 13:31:57 -05:00
|
|
|
}
|
|
|
|
|
2012-01-30 10:28:30 -06:00
|
|
|
let fty = ty::node_id_to_type(cx.tcx, id);
|
2012-08-06 14:34:08 -05:00
|
|
|
match ty::ty_fn_proto(fty) {
|
2012-08-10 20:14:27 -05:00
|
|
|
proto_uniq => b(check_for_uniq),
|
|
|
|
proto_box => b(check_for_box),
|
|
|
|
proto_bare => b(check_for_bare),
|
|
|
|
proto_block => b(check_for_block)
|
2011-12-20 21:39:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the free variables used in a shared/sendable closure conform
|
|
|
|
// to the copy/move kind bounds. Then recursively check the function body.
|
2011-12-29 22:07:55 -06:00
|
|
|
fn check_fn(fk: visit::fn_kind, decl: fn_decl, body: blk, sp: span,
|
2012-02-29 11:53:30 -06:00
|
|
|
fn_id: node_id, cx: ctx, v: visit::vt<ctx>) {
|
2011-12-09 18:56:48 -06:00
|
|
|
|
2012-05-07 13:31:57 -05:00
|
|
|
// Find the check function that enforces the appropriate bounds for this
|
|
|
|
// kind of function:
|
2012-06-30 18:19:07 -05:00
|
|
|
do with_appropriate_checker(cx, fn_id) |chk| {
|
2012-05-07 13:31:57 -05:00
|
|
|
|
|
|
|
// Begin by checking the variables in the capture clause, if any.
|
|
|
|
// Here we slightly abuse the map function to both check and report
|
|
|
|
// errors and produce a list of the def id's for all capture
|
|
|
|
// variables. This list is used below to avoid checking and reporting
|
|
|
|
// on a given variable twice.
|
2012-08-06 14:34:08 -05:00
|
|
|
let cap_clause = match fk {
|
2012-08-03 21:59:04 -05:00
|
|
|
visit::fk_anon(_, cc) | visit::fk_fn_block(cc) => cc,
|
2012-05-07 13:31:57 -05:00
|
|
|
visit::fk_item_fn(*) | visit::fk_method(*) |
|
2012-08-03 21:59:04 -05:00
|
|
|
visit::fk_ctor(*) | visit::fk_dtor(*) => @~[]
|
2012-05-07 13:31:57 -05:00
|
|
|
};
|
2012-06-30 18:19:07 -05:00
|
|
|
let captured_vars = do (*cap_clause).map |cap_item| {
|
2012-05-07 13:31:57 -05:00
|
|
|
let cap_def = cx.tcx.def_map.get(cap_item.id);
|
|
|
|
let cap_def_id = ast_util::def_id_of_def(cap_def).node;
|
|
|
|
let ty = ty::node_id_to_type(cx.tcx, cap_def_id);
|
2012-06-04 22:44:58 -05:00
|
|
|
chk(cx, fn_id, none, cap_item.is_move, ty, cap_item.span);
|
2012-05-07 13:31:57 -05:00
|
|
|
cap_def_id
|
|
|
|
};
|
|
|
|
|
|
|
|
// Iterate over any free variables that may not have appeared in the
|
|
|
|
// capture list. Ensure that they too are of the appropriate kind.
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(*freevars::get_freevars(cx.tcx, fn_id)) |fv| {
|
2012-04-06 13:01:43 -05:00
|
|
|
let id = ast_util::def_id_of_def(fv.def).node;
|
2012-05-07 13:31:57 -05:00
|
|
|
|
|
|
|
// skip over free variables that appear in the cap clause
|
2012-07-09 16:37:48 -05:00
|
|
|
if captured_vars.contains(id) { again; }
|
2012-05-07 13:31:57 -05:00
|
|
|
|
|
|
|
// if this is the last use of the variable, then it will be
|
|
|
|
// a move and not a copy
|
|
|
|
let is_move = {
|
2012-08-06 14:34:08 -05:00
|
|
|
match check cx.last_use_map.find(fn_id) {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(vars) => (*vars).contains(id),
|
|
|
|
none => false
|
2012-05-25 02:14:40 -05:00
|
|
|
}
|
2012-05-07 13:31:57 -05:00
|
|
|
};
|
|
|
|
|
2011-12-09 18:56:48 -06:00
|
|
|
let ty = ty::node_id_to_type(cx.tcx, id);
|
2012-06-04 22:44:58 -05:00
|
|
|
chk(cx, fn_id, some(fv), is_move, ty, fv.span);
|
2011-12-09 18:56:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-29 11:53:30 -06:00
|
|
|
visit::visit_fn(fk, decl, body, sp, fn_id, cx, v);
|
2011-12-20 21:39:33 -06:00
|
|
|
}
|
|
|
|
|
2012-01-02 08:23:11 -06:00
|
|
|
fn check_block(b: blk, cx: ctx, v: visit::vt<ctx>) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match b.node.expr {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(ex) => maybe_copy(cx, ex),
|
|
|
|
_ => ()
|
2012-01-02 08:23:11 -06:00
|
|
|
}
|
|
|
|
visit::visit_block(b, cx, v);
|
|
|
|
}
|
2011-12-20 21:39:33 -06:00
|
|
|
|
2012-01-02 08:23:11 -06:00
|
|
|
fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
|
2012-07-30 18:01:07 -05:00
|
|
|
debug!{"kind::check_expr(%s)", expr_to_str(e)};
|
2012-08-06 14:34:08 -05:00
|
|
|
match e.node {
|
2012-05-23 20:57:56 -05:00
|
|
|
expr_assign(_, ex) |
|
2012-01-02 08:23:11 -06:00
|
|
|
expr_unary(box(_), ex) | expr_unary(uniq(_), ex) |
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_ret(some(ex)) => {
|
2012-07-18 13:01:54 -05:00
|
|
|
maybe_copy(cx, ex);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_cast(source, _) => {
|
2012-07-18 13:01:54 -05:00
|
|
|
maybe_copy(cx, source);
|
|
|
|
check_cast_for_escaping_regions(cx, source, e);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_copy(expr) => check_copy_ex(cx, expr, false),
|
2012-05-23 20:57:56 -05:00
|
|
|
// Vector add copies, but not "implicitly"
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_assign_op(_, _, ex) => check_copy_ex(cx, ex, false),
|
|
|
|
expr_binary(add, ls, rs) => {
|
2012-05-23 20:57:56 -05:00
|
|
|
check_copy_ex(cx, ls, false);
|
|
|
|
check_copy_ex(cx, rs, false);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_rec(fields, def) => {
|
2012-06-30 18:19:07 -05:00
|
|
|
for fields.each |field| { maybe_copy(cx, field.node.expr); }
|
2012-08-06 14:34:08 -05:00
|
|
|
match def {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(ex) => {
|
2011-11-22 06:27:40 -06:00
|
|
|
// All noncopyable fields must be overridden
|
|
|
|
let t = ty::expr_ty(cx.tcx, ex);
|
2012-08-06 14:34:08 -05:00
|
|
|
let ty_fields = match ty::get(t).struct {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_rec(f) => f,
|
|
|
|
_ => cx.tcx.sess.span_bug(ex.span, ~"bad expr type in record")
|
2012-02-03 08:15:28 -06:00
|
|
|
};
|
2012-06-30 18:19:07 -05:00
|
|
|
for ty_fields.each |tf| {
|
|
|
|
if !vec::any(fields, |f| f.node.ident == tf.ident ) &&
|
2011-12-28 11:11:33 -06:00
|
|
|
!ty::kind_can_be_copied(ty::type_kind(cx.tcx, tf.mt.ty)) {
|
2011-11-22 06:27:40 -06:00
|
|
|
cx.tcx.sess.span_err(ex.span,
|
2012-07-14 00:57:48 -05:00
|
|
|
~"copying a noncopyable value");
|
2011-11-22 06:27:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {}
|
2011-11-22 06:27:40 -06:00
|
|
|
}
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_tup(exprs) | expr_vec(exprs, _) => {
|
2012-06-30 18:19:07 -05:00
|
|
|
for exprs.each |expr| { maybe_copy(cx, expr); }
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_call(f, args, _) => {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = 0u;
|
2012-06-30 18:19:07 -05:00
|
|
|
for ty::ty_fn_args(ty::expr_ty(cx.tcx, f)).each |arg_t| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match ty::arg_mode(cx.tcx, arg_t) {
|
2012-08-03 21:59:04 -05:00
|
|
|
by_copy => maybe_copy(cx, args[i]),
|
|
|
|
by_ref | by_val | by_mutbl_ref | by_move => ()
|
2012-02-02 18:50:17 -06:00
|
|
|
}
|
2011-11-18 03:20:51 -06:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_path(_) | expr_field(_, _, _) => {
|
2012-06-30 18:19:07 -05:00
|
|
|
do option::iter(cx.tcx.node_type_substs.find(e.id)) |ts| {
|
2012-08-06 14:34:08 -05:00
|
|
|
let bounds = match check e.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_path(_) => {
|
2012-03-16 10:25:36 -05:00
|
|
|
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(e.id));
|
|
|
|
ty::lookup_item_type(cx.tcx, did).bounds
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_field(base, _, _) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match cx.method_map.get(e.id).origin {
|
2012-08-03 21:59:04 -05:00
|
|
|
typeck::method_static(did) => {
|
2012-06-04 13:21:11 -05:00
|
|
|
// n.b.: When we encode class/impl methods, the bounds
|
|
|
|
// that we encode include both the class/impl bounds
|
|
|
|
// and then the method bounds themselves...
|
|
|
|
ty::lookup_item_type(cx.tcx, did).bounds
|
2012-03-16 10:25:36 -05:00
|
|
|
}
|
2012-07-03 18:30:42 -05:00
|
|
|
typeck::method_param({trait_id:trt_id,
|
2012-06-07 12:51:21 -05:00
|
|
|
method_num:n_mth, _}) |
|
2012-08-03 21:59:04 -05:00
|
|
|
typeck::method_trait(trt_id, n_mth) => {
|
2012-07-03 18:30:42 -05:00
|
|
|
// ...trait methods bounds, in contrast, include only the
|
2012-06-04 13:21:11 -05:00
|
|
|
// method bounds, so we must preprend the tps from the
|
2012-07-03 18:30:42 -05:00
|
|
|
// trait itself. This ought to be harmonized.
|
|
|
|
let trt_bounds =
|
|
|
|
ty::lookup_item_type(cx.tcx, trt_id).bounds;
|
|
|
|
let mth = ty::trait_methods(cx.tcx, trt_id)[n_mth];
|
|
|
|
@(vec::append(*trt_bounds, *mth.tps))
|
2012-03-16 10:25:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-05-03 11:17:58 -05:00
|
|
|
if vec::len(ts) != vec::len(*bounds) {
|
|
|
|
// Fail earlier to make debugging easier
|
2012-07-30 18:01:07 -05:00
|
|
|
fail fmt!{"Internal error: in kind::check_expr, length \
|
2012-05-03 11:17:58 -05:00
|
|
|
mismatch between actual and declared bounds: actual = \
|
2012-05-07 17:14:56 -05:00
|
|
|
%s (%u tys), declared = %? (%u tys)",
|
2012-07-30 18:01:07 -05:00
|
|
|
tys_to_str(cx.tcx, ts), ts.len(), *bounds, (*bounds).len()};
|
2012-05-03 11:17:58 -05:00
|
|
|
}
|
2012-06-30 18:19:07 -05:00
|
|
|
do vec::iter2(ts, *bounds) |ty, bound| {
|
2012-05-29 18:22:22 -05:00
|
|
|
check_bounds(cx, e.id, e.span, ty, bound)
|
2011-11-18 03:20:51 -06:00
|
|
|
}
|
|
|
|
}
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => { }
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
|
|
|
visit::visit_expr(e, cx, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_stmt(stmt: @stmt, cx: ctx, v: visit::vt<ctx>) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match stmt.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
stmt_decl(@{node: decl_local(locals), _}, _) => {
|
2012-06-30 18:19:07 -05:00
|
|
|
for locals.each |local| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match local.node.init {
|
2012-08-03 21:59:04 -05:00
|
|
|
some({op: init_assign, expr}) => maybe_copy(cx, expr),
|
|
|
|
_ => {}
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {}
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
|
|
|
visit::visit_stmt(stmt, cx, v);
|
|
|
|
}
|
|
|
|
|
2012-03-19 04:45:29 -05:00
|
|
|
fn check_ty(aty: @ty, cx: ctx, v: visit::vt<ctx>) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match aty.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty_path(_, id) => {
|
2012-06-30 18:19:07 -05:00
|
|
|
do option::iter(cx.tcx.node_type_substs.find(id)) |ts| {
|
2012-03-19 04:45:29 -05:00
|
|
|
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(id));
|
|
|
|
let bounds = ty::lookup_item_type(cx.tcx, did).bounds;
|
2012-06-30 18:19:07 -05:00
|
|
|
do vec::iter2(ts, *bounds) |ty, bound| {
|
2012-05-29 18:22:22 -05:00
|
|
|
check_bounds(cx, aty.id, aty.span, ty, bound)
|
2012-03-19 04:45:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {}
|
2012-03-19 04:45:29 -05:00
|
|
|
}
|
|
|
|
visit::visit_ty(aty, cx, v);
|
|
|
|
}
|
|
|
|
|
2012-05-29 18:22:22 -05:00
|
|
|
fn check_bounds(cx: ctx, id: node_id, sp: span,
|
|
|
|
ty: ty::t, bounds: ty::param_bounds) {
|
2012-03-19 04:45:29 -05:00
|
|
|
let kind = ty::type_kind(cx.tcx, ty);
|
|
|
|
let p_kind = ty::param_bounds_to_kind(bounds);
|
|
|
|
if !ty::kind_lteq(p_kind, kind) {
|
2012-05-29 18:22:22 -05:00
|
|
|
// If the only reason the kind check fails is because the
|
|
|
|
// argument type isn't implicitly copyable, consult the warning
|
|
|
|
// settings to figure out what to do.
|
|
|
|
let implicit = ty::kind_implicitly_copyable() - ty::kind_copyable();
|
|
|
|
if ty::kind_lteq(p_kind, kind | implicit) {
|
|
|
|
cx.tcx.sess.span_lint(
|
|
|
|
non_implicitly_copyable_typarams,
|
|
|
|
id, cx.current_item, sp,
|
2012-07-14 00:57:48 -05:00
|
|
|
~"instantiating copy type parameter with a \
|
2012-05-29 18:22:22 -05:00
|
|
|
not implicitly copyable type");
|
|
|
|
} else {
|
|
|
|
cx.tcx.sess.span_err(
|
|
|
|
sp,
|
2012-07-14 00:57:48 -05:00
|
|
|
~"instantiating a type parameter with an incompatible type " +
|
|
|
|
~"(needs `" + kind_to_str(p_kind) +
|
|
|
|
~"`, got `" + kind_to_str(kind) +
|
|
|
|
~"`, missing `" + kind_to_str(p_kind - kind) + ~"`)");
|
2012-05-29 18:22:22 -05:00
|
|
|
}
|
2012-03-19 04:45:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-15 11:15:35 -06:00
|
|
|
fn maybe_copy(cx: ctx, ex: @expr) {
|
|
|
|
check_copy_ex(cx, ex, true);
|
|
|
|
}
|
|
|
|
|
2012-01-31 05:34:22 -06:00
|
|
|
fn is_nullary_variant(cx: ctx, ex: @expr) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match ex.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_path(_) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match cx.tcx.def_map.get(ex.id) {
|
2012-08-03 21:59:04 -05:00
|
|
|
def_variant(edid, vdid) => {
|
2012-01-31 05:34:22 -06:00
|
|
|
vec::len(ty::enum_variant_with_id(cx.tcx, edid, vdid).args) == 0u
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => false
|
2012-01-31 05:34:22 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => false
|
2012-01-31 05:34:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 20:57:56 -05:00
|
|
|
fn check_copy_ex(cx: ctx, ex: @expr, implicit_copy: bool) {
|
2012-01-13 03:58:31 -06:00
|
|
|
if ty::expr_is_lval(cx.method_map, ex) &&
|
2012-05-25 02:14:40 -05:00
|
|
|
!cx.last_use_map.contains_key(ex.id) &&
|
2012-01-31 05:34:22 -06:00
|
|
|
!is_nullary_variant(cx, ex) {
|
2011-11-15 11:15:35 -06:00
|
|
|
let ty = ty::expr_ty(cx.tcx, ex);
|
2012-06-04 22:44:58 -05:00
|
|
|
check_copy(cx, ex.id, ty, ex.span, implicit_copy);
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-07 13:31:57 -05:00
|
|
|
fn check_imm_free_var(cx: ctx, def: def, sp: span) {
|
2012-07-14 00:57:48 -05:00
|
|
|
let msg = ~"mutable variables cannot be implicitly captured; \
|
2012-05-07 13:31:57 -05:00
|
|
|
use a capture clause";
|
2012-08-06 14:34:08 -05:00
|
|
|
match def {
|
2012-08-03 21:59:04 -05:00
|
|
|
def_local(_, is_mutbl) => {
|
2012-05-07 13:31:57 -05:00
|
|
|
if is_mutbl {
|
|
|
|
cx.tcx.sess.span_err(sp, msg);
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
def_arg(_, mode) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match ty::resolved_mode(cx.tcx, mode) {
|
2012-08-03 21:59:04 -05:00
|
|
|
by_ref | by_val | by_move | by_copy => { /* ok */ }
|
|
|
|
by_mutbl_ref => {
|
2012-05-07 13:31:57 -05:00
|
|
|
cx.tcx.sess.span_err(sp, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
def_upvar(_, def1, _) => {
|
2012-05-07 13:31:57 -05:00
|
|
|
check_imm_free_var(cx, *def1, sp);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
def_binding(*) | def_self(*) => { /*ok*/ }
|
|
|
|
_ => {
|
2012-05-07 13:31:57 -05:00
|
|
|
cx.tcx.sess.span_bug(
|
|
|
|
sp,
|
2012-07-30 18:01:07 -05:00
|
|
|
fmt!{"unknown def for free variable: %?", def});
|
2012-05-07 13:31:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:44:58 -05:00
|
|
|
fn check_copy(cx: ctx, id: node_id, ty: ty::t, sp: span,
|
|
|
|
implicit_copy: bool) {
|
2012-05-23 20:57:56 -05:00
|
|
|
let k = ty::type_kind(cx.tcx, ty);
|
|
|
|
if !ty::kind_can_be_copied(k) {
|
2012-07-14 00:57:48 -05:00
|
|
|
cx.tcx.sess.span_err(sp, ~"copying a noncopyable value");
|
2012-05-23 20:57:56 -05:00
|
|
|
} else if implicit_copy && !ty::kind_can_be_implicitly_copied(k) {
|
2012-06-04 22:44:58 -05:00
|
|
|
cx.tcx.sess.span_lint(
|
|
|
|
implicit_copies, id, cx.current_item,
|
2012-05-23 20:57:56 -05:00
|
|
|
sp,
|
2012-07-14 00:57:48 -05:00
|
|
|
~"implicitly copying a non-implicitly-copyable value");
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-16 22:17:57 -05:00
|
|
|
fn check_send(cx: ctx, ty: ty::t, sp: span) -> bool {
|
2011-12-28 11:11:33 -06:00
|
|
|
if !ty::kind_can_be_sent(ty::type_kind(cx.tcx, ty)) {
|
2012-07-14 00:57:48 -05:00
|
|
|
cx.tcx.sess.span_err(sp, ~"not a sendable value");
|
2012-07-16 22:17:57 -05:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-18 13:01:54 -05:00
|
|
|
// note: also used from middle::typeck::regionck!
|
|
|
|
fn check_owned(tcx: ty::ctxt, ty: ty::t, sp: span) -> bool {
|
|
|
|
if !ty::kind_is_owned(ty::type_kind(tcx, ty)) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match ty::get(ty).struct {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_param(*) => {
|
2012-07-18 13:01:54 -05:00
|
|
|
tcx.sess.span_err(sp, ~"value may contain borrowed \
|
|
|
|
pointers; use `owned` bound");
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-07-18 13:01:54 -05:00
|
|
|
tcx.sess.span_err(sp, ~"value may contain borrowed \
|
|
|
|
pointers");
|
|
|
|
}
|
|
|
|
}
|
2012-07-16 22:17:57 -05:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
2011-12-09 18:56:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-18 13:01:54 -05:00
|
|
|
/// This is rather subtle. When we are casting a value to a
|
2012-07-30 16:51:21 -05:00
|
|
|
/// instantiated trait like `a as trait/&r`, regionck already ensures
|
2012-07-18 13:01:54 -05:00
|
|
|
/// that any borrowed pointers that appear in the type of `a` are
|
|
|
|
/// bounded by `&r`. However, it is possible that there are *type
|
|
|
|
/// parameters* in the type of `a`, and those *type parameters* may
|
|
|
|
/// have borrowed pointers within them. We have to guarantee that the
|
|
|
|
/// regions which appear in those type parameters are not obscured.
|
|
|
|
///
|
|
|
|
/// Therefore, we ensure that one of three conditions holds:
|
|
|
|
///
|
2012-07-30 16:51:21 -05:00
|
|
|
/// (1) The trait instance cannot escape the current fn. This is
|
2012-07-18 13:01:54 -05:00
|
|
|
/// guaranteed if the region bound `&r` is some scope within the fn
|
|
|
|
/// itself. This case is safe because whatever borrowed pointers are
|
|
|
|
/// found within the type parameter, they must enclose the fn body
|
|
|
|
/// itself.
|
|
|
|
///
|
2012-07-30 16:51:21 -05:00
|
|
|
/// (2) The type parameter appears in the type of the trait. For
|
|
|
|
/// example, if the type parameter is `T` and the trait type is
|
2012-07-18 13:01:54 -05:00
|
|
|
/// `deque<T>`, then whatever borrowed ptrs may appear in `T` also
|
|
|
|
/// appear in `deque<T>`.
|
|
|
|
///
|
|
|
|
/// (3) The type parameter is owned (and therefore does not contain
|
|
|
|
/// borrowed ptrs).
|
|
|
|
fn check_cast_for_escaping_regions(
|
|
|
|
cx: ctx,
|
|
|
|
source: @expr,
|
|
|
|
target: @expr) {
|
|
|
|
|
2012-07-30 16:51:21 -05:00
|
|
|
// Determine what type we are casting to; if it is not an trait, then no
|
2012-07-18 13:01:54 -05:00
|
|
|
// worries.
|
|
|
|
let target_ty = ty::expr_ty(cx.tcx, target);
|
2012-08-06 14:34:08 -05:00
|
|
|
let target_substs = match ty::get(target_ty).struct {
|
2012-07-18 13:01:54 -05:00
|
|
|
ty::ty_trait(_, substs) => {substs}
|
2012-08-01 19:30:05 -05:00
|
|
|
_ => { return; /* not a cast to a trait */ }
|
2012-07-18 13:01:54 -05:00
|
|
|
};
|
|
|
|
|
2012-07-30 16:51:21 -05:00
|
|
|
// Check, based on the region associated with the trait, whether it can
|
2012-07-18 13:01:54 -05:00
|
|
|
// possibly escape the enclosing fn item (note that all type parameters
|
|
|
|
// must have been declared on the enclosing fn item):
|
2012-08-06 14:34:08 -05:00
|
|
|
match target_substs.self_r {
|
2012-08-01 19:30:05 -05:00
|
|
|
some(ty::re_scope(*)) => { return; /* case (1) */ }
|
2012-07-18 13:01:54 -05:00
|
|
|
none | some(ty::re_static) | some(ty::re_free(*)) => {}
|
|
|
|
some(ty::re_bound(*)) | some(ty::re_var(*)) => {
|
|
|
|
cx.tcx.sess.span_bug(
|
|
|
|
source.span,
|
2012-07-30 18:01:07 -05:00
|
|
|
fmt!{"bad region found in kind: %?", target_substs.self_r});
|
2012-07-18 13:01:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 16:51:21 -05:00
|
|
|
// Assuming the trait instance can escape, then ensure that each parameter
|
|
|
|
// either appears in the trait type or is owned:
|
2012-07-18 13:01:54 -05:00
|
|
|
let target_params = ty::param_tys_in_type(target_ty);
|
|
|
|
let source_ty = ty::expr_ty(cx.tcx, source);
|
|
|
|
do ty::walk_ty(source_ty) |ty| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match ty::get(ty).struct {
|
2012-07-18 13:01:54 -05:00
|
|
|
ty::ty_param(source_param) => {
|
|
|
|
if target_params.contains(source_param) {
|
|
|
|
/* case (2) */
|
|
|
|
} else {
|
|
|
|
check_owned(cx.tcx, ty, source.span); /* case (3) */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 19:49:00 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|