2011-07-27 19:49:00 -05:00
|
|
|
/*
|
|
|
|
* Kinds are types of type.
|
|
|
|
*
|
|
|
|
* Every type has a kind. Every type parameter has a set of kind-capabilities
|
|
|
|
* saying which kind of type may be passed as the parameter.
|
|
|
|
*
|
2011-07-28 01:43:17 -05:00
|
|
|
* The kinds are based on two capabilities: move and send. These may each be
|
2011-07-27 19:49:00 -05:00
|
|
|
* present or absent, though only three of the four combinations can actually
|
|
|
|
* occur:
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
2011-07-27 23:23:40 -05:00
|
|
|
* MOVE + SEND = "Unique": no shared substructures or pins, only
|
2011-07-27 19:49:00 -05:00
|
|
|
* interiors and ~ boxes.
|
|
|
|
*
|
2011-07-27 23:23:40 -05:00
|
|
|
* MOVE + NOSEND = "Shared": structures containing @, fixed to the local
|
|
|
|
* task heap/pool; or ~ structures pointing to
|
|
|
|
* pinned values.
|
2011-07-27 19:49:00 -05:00
|
|
|
*
|
2011-07-27 23:23:40 -05:00
|
|
|
* NOMOVE + NOSEND = "Pinned": structures directly containing resources, or
|
2011-07-27 19:49:00 -05:00
|
|
|
* by-alias closures as interior or
|
|
|
|
* uniquely-boxed members.
|
|
|
|
*
|
2011-07-27 23:23:40 -05:00
|
|
|
* NOMOVE + SEND = -- : no types are like this.
|
2011-07-27 19:49:00 -05:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Since this forms a lattice, we denote the capabilites in terms of a
|
2011-10-25 08:56:55 -05:00
|
|
|
* worst-case requirement. That is, if your function needs to move-and-send (or
|
2011-10-28 08:09:12 -05:00
|
|
|
* copy) your T, you write fn<uniq T>(...). If you need to move but not send,
|
2011-10-25 08:56:55 -05:00
|
|
|
* you write fn<T>(...). And if you need neither -- can work with any sort of
|
2011-10-28 08:09:12 -05:00
|
|
|
* pinned data at all -- then you write fn<pin T>(...).
|
2011-07-27 19:49:00 -05:00
|
|
|
*
|
|
|
|
* Most types are unique or shared. Other possible name combinations for these
|
|
|
|
* two: (tree, graph; pruned, pooled; message, local; owned, common) are
|
|
|
|
* plausible but nothing stands out as completely pithy-and-obvious.
|
|
|
|
*
|
2011-09-25 14:42:39 -05:00
|
|
|
* Pinned values arise in 2 contexts: resources and &-closures (blocks). The
|
|
|
|
* latter absolutely must not be moved, since they could escape to the heap;
|
|
|
|
* the former must not be copied, since they'd then be multiply-destructed.
|
|
|
|
* We achieve the no-copy restriction by recycling the no-move restriction
|
|
|
|
* in place on pinned kinds for &-closures; and as a benefit we can guarantee
|
|
|
|
* that a resource passed by reference to C will never move during its life,
|
|
|
|
* occasionally useful for FFI-code.
|
|
|
|
*
|
|
|
|
* Resources cannot be sent because we don't want to oblige the communication
|
|
|
|
* system to run destructors in some weird limbo context of
|
|
|
|
* messages-in-transit. It should always be ok to just free messages it's
|
|
|
|
* dropping. Even if you wanted to send them, you'd need a new sigil for the
|
|
|
|
* NOMOVE + SEND combination, and you couldn't use the move-mode library
|
|
|
|
* interface to chan.send in that case (NOMOVE after all), so the whole thing
|
|
|
|
* wouldn't really work as minimally as the encoding we have here.
|
2011-07-27 19:49:00 -05:00
|
|
|
*
|
|
|
|
* Note that obj~ and fn~ -- those that capture a unique environment -- can be
|
2011-09-25 14:42:39 -05:00
|
|
|
* sent, so satisfy ~T. So can plain obj and fn. They can all also be copied.
|
2011-07-27 19:49:00 -05:00
|
|
|
*
|
|
|
|
* Further notes on copying and moving; sending is accomplished by calling a
|
|
|
|
* move-in operator on something constrained to a unique type ~T.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* COPYING:
|
|
|
|
* --------
|
|
|
|
*
|
|
|
|
* A copy is made any time you pass-by-value or execute the = operator in a
|
2011-09-25 14:42:39 -05:00
|
|
|
* non-init expression. Copying requires discriminating on type constructor.
|
|
|
|
*
|
|
|
|
* @-boxes copy shallow, copying is always legal.
|
|
|
|
*
|
|
|
|
* ~-boxes copy deep, copying is only legal if pointee is unique-kind.
|
2011-07-27 19:49:00 -05:00
|
|
|
*
|
2011-09-25 14:42:39 -05:00
|
|
|
* Pinned-kind values (resources, &-closures) can't be copied. All other
|
|
|
|
* unique-kind (eg. interior) values can be copied, and copy shallow.
|
|
|
|
*
|
|
|
|
* Note: If you have no type constructor -- only an opaque typaram -- then
|
|
|
|
* you can only copy if the typaram is constrained to ~T; this is because @T
|
|
|
|
* might be a "~resource" box, and making a copy would cause a deep
|
|
|
|
* resource-copy.
|
2011-07-27 23:23:40 -05:00
|
|
|
*
|
2011-07-27 19:49:00 -05:00
|
|
|
*
|
|
|
|
* MOVING:
|
|
|
|
* -------
|
|
|
|
*
|
2011-09-25 14:42:39 -05:00
|
|
|
* A move is made any time you pass-by-move (that is, with move mode '-') or
|
|
|
|
* execute the move ('<-') or swap ('<->') operators.
|
2011-07-27 19:49:00 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-11-15 09:45:14 -06:00
|
|
|
import syntax::ast;
|
|
|
|
import ast::{kind, kind_sendable, kind_copyable, kind_noncopyable};
|
2011-07-27 19:49:00 -05:00
|
|
|
|
|
|
|
fn kind_lteq(a: kind, b: kind) -> bool {
|
|
|
|
alt a {
|
2011-11-15 09:45:14 -06:00
|
|
|
kind_noncopyable. { true }
|
|
|
|
kind_copyable. { b != kind_noncopyable }
|
|
|
|
kind_sendable. { b == kind_sendable }
|
2011-07-27 19:49:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_kind(a: kind, b: kind) -> kind {
|
|
|
|
if kind_lteq(a, b) { a } else { b }
|
|
|
|
}
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
fn kind_to_str(k: kind) -> str {
|
2011-07-27 19:49:00 -05:00
|
|
|
alt k {
|
2011-11-15 09:45:14 -06:00
|
|
|
ast::kind_sendable. { "sendable" }
|
|
|
|
ast::kind_copyable. { "copyable" }
|
|
|
|
ast::kind_noncopyable. { "noncopyable" }
|
2011-07-27 19:49:00 -05:00
|
|
|
}
|
|
|
|
}
|
2011-11-15 09:45:14 -06:00
|
|
|
/*
|
2011-09-12 04:27:30 -05:00
|
|
|
fn type_and_kind(tcx: ty::ctxt, e: @ast::expr) ->
|
2011-08-19 17:16:48 -05:00
|
|
|
{ty: ty::t, kind: ast::kind} {
|
2011-07-27 19:49:00 -05:00
|
|
|
let t = ty::expr_ty(tcx, e);
|
|
|
|
let k = ty::type_kind(tcx, t);
|
2011-07-27 23:23:40 -05:00
|
|
|
{ty: t, kind: k}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn need_expr_kind(tcx: ty::ctxt, e: @ast::expr, k_need: ast::kind,
|
|
|
|
descr: str) {
|
2011-07-27 23:23:40 -05:00
|
|
|
let tk = type_and_kind(tcx, e);
|
2011-09-01 20:49:10 -05:00
|
|
|
log #fmt["for %s: want %s type, got %s type %s", descr,
|
2011-08-19 17:16:48 -05:00
|
|
|
kind_to_str(k_need), kind_to_str(tk.kind),
|
2011-08-28 02:24:28 -05:00
|
|
|
util::ppaux::ty_to_str(tcx, tk.ty)];
|
2011-07-27 23:23:40 -05:00
|
|
|
|
2011-09-26 20:43:49 -05:00
|
|
|
demand_kind(tcx, e.span, tk.ty, k_need, descr);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn demand_kind(tcx: ty::ctxt, sp: codemap::span, t: ty::t,
|
|
|
|
k_need: ast::kind, descr: str) {
|
|
|
|
let k = ty::type_kind(tcx, t);
|
|
|
|
if !kind_lteq(k_need, k) {
|
2011-07-27 23:23:40 -05:00
|
|
|
let s =
|
2011-09-01 20:49:10 -05:00
|
|
|
#fmt["mismatched kinds for %s: needed %s type, got %s type %s",
|
2011-09-26 20:43:49 -05:00
|
|
|
descr, kind_to_str(k_need), kind_to_str(k),
|
|
|
|
util::ppaux::ty_to_str(tcx, t)];
|
|
|
|
tcx.sess.span_err(sp, s);
|
2011-07-27 23:23:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn need_shared_lhs_rhs(tcx: ty::ctxt, a: @ast::expr, b: @ast::expr, op: str) {
|
2011-11-15 09:45:14 -06:00
|
|
|
need_expr_kind(tcx, a, ast::kind_copyable, op + " lhs");
|
|
|
|
need_expr_kind(tcx, b, ast::kind_copyable, op + " rhs");
|
2011-07-27 23:23:40 -05:00
|
|
|
}
|
|
|
|
|
2011-09-27 20:45:35 -05:00
|
|
|
/*
|
|
|
|
This ... is a hack (I find myself writing that too often *sadface*).
|
|
|
|
|
|
|
|
We need to be able to put pinned kinds into other types but such operations
|
|
|
|
are conceptually copies, and pinned kinds can't do that, e.g.
|
|
|
|
|
|
|
|
let a = my_resource(x);
|
|
|
|
let b = @a; // no-go
|
|
|
|
|
|
|
|
So this function attempts to make a loophole where resources can be put into
|
|
|
|
other types as long as it's done in a safe way, specifically like
|
|
|
|
|
|
|
|
let b = @my_resource(x);
|
|
|
|
*/
|
|
|
|
fn need_shared_or_pinned_ctor(tcx: ty::ctxt, a: @ast::expr, descr: str) {
|
|
|
|
let tk = type_and_kind(tcx, a);
|
|
|
|
if tk.kind == ast::kind_pinned && !pinned_ctor(a) {
|
|
|
|
let err =
|
|
|
|
#fmt["mismatched kinds for %s: cannot copy pinned type %s",
|
|
|
|
descr, util::ppaux::ty_to_str(tcx, tk.ty)];
|
|
|
|
tcx.sess.span_err(a.span, err);
|
|
|
|
let note =
|
|
|
|
#fmt["try constructing %s directly into %s",
|
|
|
|
util::ppaux::ty_to_str(tcx, tk.ty), descr];
|
|
|
|
tcx.sess.span_note(a.span, note);
|
|
|
|
} else if tk.kind != ast::kind_pinned {
|
|
|
|
need_expr_kind(tcx, a, ast::kind_shared, descr);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pinned_ctor(a: @ast::expr) -> bool {
|
|
|
|
// FIXME: Technically a lambda block is also a pinned ctor
|
|
|
|
alt a.node {
|
2011-10-21 07:11:24 -05:00
|
|
|
ast::expr_call(cexpr, _, _) {
|
2011-09-27 20:45:35 -05:00
|
|
|
// Assuming that if it's a call that it's safe to move in, mostly
|
|
|
|
// because I don't know offhand how to ensure that it's a call
|
|
|
|
// specifically to a resource constructor
|
|
|
|
true
|
|
|
|
}
|
|
|
|
ast::expr_rec(_, _) {
|
|
|
|
true
|
|
|
|
}
|
2011-09-28 00:40:15 -05:00
|
|
|
ast::expr_unary(ast::uniq(_), _) {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
ast::expr_tup(_) {
|
|
|
|
true
|
|
|
|
}
|
2011-09-28 16:37:28 -05:00
|
|
|
ast::expr_vec(exprs, _) {
|
|
|
|
true
|
|
|
|
}
|
2011-09-27 20:45:35 -05:00
|
|
|
_ { false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn check_expr(tcx: ty::ctxt, e: @ast::expr) {
|
2011-07-27 23:23:40 -05:00
|
|
|
alt e.node {
|
2011-09-25 14:42:39 -05:00
|
|
|
|
2011-09-26 20:43:49 -05:00
|
|
|
// FIXME: These rules do not fully implement the copy type-constructor
|
|
|
|
// discrimination described by the block comment at the top of this
|
|
|
|
// file. This code is wrong; it lets you copy anything shared-kind.
|
2011-09-25 14:42:39 -05:00
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
ast::expr_move(a, b) { need_shared_lhs_rhs(tcx, a, b, "<-"); }
|
2011-09-26 20:43:49 -05:00
|
|
|
ast::expr_assign(a, b) {
|
|
|
|
need_shared_lhs_rhs(tcx, a, b, "=");
|
|
|
|
}
|
2011-09-26 21:59:49 -05:00
|
|
|
ast::expr_assign_op(_, a, b) {
|
|
|
|
need_shared_lhs_rhs(tcx, a, b, "op=");
|
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
ast::expr_swap(a, b) { need_shared_lhs_rhs(tcx, a, b, "<->"); }
|
2011-08-23 17:58:53 -05:00
|
|
|
ast::expr_copy(a) {
|
2011-09-02 17:34:58 -05:00
|
|
|
need_expr_kind(tcx, a, ast::kind_shared, "'copy' operand");
|
2011-08-23 17:58:53 -05:00
|
|
|
}
|
|
|
|
ast::expr_ret(option::some(a)) {
|
2011-09-02 17:34:58 -05:00
|
|
|
need_expr_kind(tcx, a, ast::kind_shared, "'ret' operand");
|
2011-08-23 17:58:53 -05:00
|
|
|
}
|
|
|
|
ast::expr_be(a) {
|
2011-09-02 17:34:58 -05:00
|
|
|
need_expr_kind(tcx, a, ast::kind_shared, "'be' operand");
|
2011-08-23 17:58:53 -05:00
|
|
|
}
|
|
|
|
ast::expr_fail(option::some(a)) {
|
2011-09-02 17:34:58 -05:00
|
|
|
need_expr_kind(tcx, a, ast::kind_shared, "'fail' operand");
|
2011-08-23 17:58:53 -05:00
|
|
|
}
|
2011-10-21 07:11:24 -05:00
|
|
|
ast::expr_call(callee, _, _) {
|
2011-07-29 20:38:22 -05:00
|
|
|
let tpt = ty::expr_ty_params_and_ty(tcx, callee);
|
2011-08-19 17:16:48 -05:00
|
|
|
|
2011-07-29 20:38:22 -05:00
|
|
|
// If we have typarams, we're calling an item; we need to check
|
|
|
|
// that all the types we're supplying as typarams conform to the
|
|
|
|
// typaram kind constraints on that item.
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(tpt.params) != 0u {
|
2011-09-02 17:34:58 -05:00
|
|
|
let callee_def =
|
|
|
|
ast_util::def_id_of_def(tcx.def_map.get(callee.id));
|
2011-07-29 20:38:22 -05:00
|
|
|
let item_tk = ty::lookup_item_type(tcx, callee_def);
|
|
|
|
let i = 0;
|
2011-08-19 17:16:48 -05:00
|
|
|
assert (vec::len(item_tk.kinds) == vec::len(tpt.params));
|
2011-07-29 20:38:22 -05:00
|
|
|
for k_need: ast::kind in item_tk.kinds {
|
2011-08-19 17:16:48 -05:00
|
|
|
let t = tpt.params[i];
|
2011-09-26 20:43:49 -05:00
|
|
|
demand_kind(tcx, e.span, t, k_need,
|
|
|
|
#fmt("typaram %d", i));
|
2011-07-29 20:38:22 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
2011-07-29 18:40:23 -05:00
|
|
|
}
|
2011-09-27 20:45:35 -05:00
|
|
|
ast::expr_unary(op, a) {
|
|
|
|
alt op {
|
|
|
|
ast::box(_) {
|
|
|
|
need_shared_or_pinned_ctor(tcx, a, "'@' operand");
|
|
|
|
}
|
|
|
|
ast::uniq(_) {
|
|
|
|
need_shared_or_pinned_ctor(tcx, a, "'~' operand");
|
|
|
|
}
|
|
|
|
_ { /* fall through */ }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::expr_rec(fields, _) {
|
|
|
|
for field in fields {
|
|
|
|
need_shared_or_pinned_ctor(tcx, field.node.expr, "record field");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::expr_tup(exprs) {
|
|
|
|
for expr in exprs {
|
|
|
|
need_shared_or_pinned_ctor(tcx, expr, "tuple parameter");
|
|
|
|
}
|
|
|
|
}
|
2011-09-28 16:37:28 -05:00
|
|
|
ast::expr_vec(exprs, _) {
|
|
|
|
// Putting pinned things into vectors is pretty useless since vector
|
|
|
|
// addition can't work (it's a copy)
|
|
|
|
for expr in exprs {
|
|
|
|
need_expr_kind(tcx, expr, ast::kind_shared, "vector element");
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 23:23:40 -05:00
|
|
|
_ { }
|
|
|
|
}
|
2011-07-27 19:49:00 -05:00
|
|
|
}
|
|
|
|
|
2011-09-26 23:57:42 -05:00
|
|
|
fn check_stmt(tcx: ty::ctxt, stmt: @ast::stmt) {
|
|
|
|
alt stmt.node {
|
|
|
|
ast::stmt_decl(@{node: ast::decl_local(locals), _}, _) {
|
|
|
|
for (let_style, local) in locals {
|
|
|
|
alt local.node.init {
|
|
|
|
option::some({op: ast::init_assign., expr}) {
|
2011-09-28 00:40:15 -05:00
|
|
|
need_shared_or_pinned_ctor(tcx, expr,
|
|
|
|
"local initializer");
|
2011-09-26 23:57:42 -05:00
|
|
|
}
|
2011-09-27 16:50:55 -05:00
|
|
|
option::some({op: ast::init_move., expr}) {
|
2011-09-28 00:40:15 -05:00
|
|
|
need_shared_or_pinned_ctor(tcx, expr,
|
|
|
|
"local initializer");
|
2011-09-27 16:50:55 -05:00
|
|
|
}
|
|
|
|
option::none. { /* fall through */ }
|
2011-09-26 23:57:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { /* fall through */ }
|
|
|
|
}
|
|
|
|
}
|
2011-11-15 09:45:14 -06:00
|
|
|
*/
|
|
|
|
fn check_crate(_tcx: ty::ctxt, _crate: @ast::crate) {
|
|
|
|
// FIXME stubbed out
|
|
|
|
/* let visit =
|
2011-09-26 23:57:42 -05:00
|
|
|
visit::mk_simple_visitor(@{visit_expr: bind check_expr(tcx, _),
|
|
|
|
visit_stmt: bind check_stmt(tcx, _)
|
2011-08-19 17:16:48 -05:00
|
|
|
with *visit::default_simple_visitor()});
|
2011-07-28 00:42:51 -05:00
|
|
|
visit::visit_crate(*crate, (), visit);
|
2011-11-15 09:45:14 -06:00
|
|
|
tcx.sess.abort_if_errors();*/
|
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:
|
|
|
|
//
|