Preparation for kind system overhaul

This goes before a snapshot, so that subsequenct patches can make the
transition without breaking the build. Disables kind checking pass, makes
parser accept both new and old-style kind annotation.

Issue #1177
This commit is contained in:
Marijn Haverbeke 2011-11-15 16:45:14 +01:00
parent eff7fae7b9
commit 9cf48d3753
26 changed files with 78 additions and 58 deletions

View File

@ -116,9 +116,9 @@ fn item_ty_param_kinds(item: ebml::doc) -> [ast::kind] {
while i < vi.val {
let k =
alt dat[vi.next + i] as char {
'u' { ast::kind_unique }
's' { ast::kind_shared }
'p' { ast::kind_pinned }
's' { ast::kind_sendable }
'c' { ast::kind_copyable }
'a' { ast::kind_noncopyable }
};
ks += [k];
i += 1u;

View File

@ -170,9 +170,9 @@ fn encode_type_param_kinds(ebml_w: ebml::writer, tps: [ty_param]) {
ebml::write_vint(ebml_w.writer, vec::len::<ty_param>(tps));
for tp: ty_param in tps {
let c = alt ast_util::ty_param_kind(tp) {
kind_unique. { 'u' }
kind_shared. { 's' }
kind_pinned. { 'p' }
kind_sendable. { 's' }
kind_copyable. { 'c' }
kind_noncopyable. { 'a' }
};
ebml_w.writer.write([c as u8]);
}

View File

@ -204,9 +204,9 @@ fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
'p' {
let k =
alt next(st) as char {
'u' { kind_unique }
's' { kind_shared }
'p' { kind_pinned }
's' { kind_sendable }
'c' { kind_copyable }
'a' { kind_noncopyable }
c {
log_err "unexpected char in encoded type param: ";
log_err c;

View File

@ -172,9 +172,9 @@ fn enc_sty(w: io::writer, cx: @ctxt, st: ty::sty) {
}
ty::ty_param(id, k) {
alt k {
kind_unique. { w.write_str("pu"); }
kind_shared. { w.write_str("ps"); }
kind_pinned. { w.write_str("pp"); }
kind_sendable. { w.write_str("ps"); }
kind_copyable. { w.write_str("pc"); }
kind_noncopyable. { w.write_str("pa"); }
}
w.write_str(uint::str(id));
}

View File

@ -84,15 +84,14 @@
*
*/
import syntax::{ast, ast_util, visit, codemap};
import std::{vec, option};
import ast::{kind, kind_unique, kind_shared, kind_pinned};
import syntax::ast;
import ast::{kind, kind_sendable, kind_copyable, kind_noncopyable};
fn kind_lteq(a: kind, b: kind) -> bool {
alt a {
kind_pinned. { true }
kind_shared. { b != kind_pinned }
kind_unique. { b == kind_unique }
kind_noncopyable. { true }
kind_copyable. { b != kind_noncopyable }
kind_sendable. { b == kind_sendable }
}
}
@ -102,12 +101,12 @@ fn lower_kind(a: kind, b: kind) -> kind {
fn kind_to_str(k: kind) -> str {
alt k {
ast::kind_pinned. { "pinned" }
ast::kind_unique. { "unique" }
ast::kind_shared. { "shared" }
ast::kind_sendable. { "sendable" }
ast::kind_copyable. { "copyable" }
ast::kind_noncopyable. { "noncopyable" }
}
}
/*
fn type_and_kind(tcx: ty::ctxt, e: @ast::expr) ->
{ty: ty::t, kind: ast::kind} {
let t = ty::expr_ty(tcx, e);
@ -138,8 +137,8 @@ fn demand_kind(tcx: ty::ctxt, sp: codemap::span, t: ty::t,
}
fn need_shared_lhs_rhs(tcx: ty::ctxt, a: @ast::expr, b: @ast::expr, op: str) {
need_expr_kind(tcx, a, ast::kind_shared, op + " lhs");
need_expr_kind(tcx, b, ast::kind_shared, op + " rhs");
need_expr_kind(tcx, a, ast::kind_copyable, op + " lhs");
need_expr_kind(tcx, b, ast::kind_copyable, op + " rhs");
}
/*
@ -296,14 +295,15 @@ fn check_stmt(tcx: ty::ctxt, stmt: @ast::stmt) {
_ { /* fall through */ }
}
}
fn check_crate(tcx: ty::ctxt, crate: @ast::crate) {
let visit =
*/
fn check_crate(_tcx: ty::ctxt, _crate: @ast::crate) {
// FIXME stubbed out
/* let visit =
visit::mk_simple_visitor(@{visit_expr: bind check_expr(tcx, _),
visit_stmt: bind check_stmt(tcx, _)
with *visit::default_simple_visitor()});
visit::visit_crate(*crate, (), visit);
tcx.sess.abort_if_errors();
tcx.sess.abort_if_errors();*/
}
//

View File

@ -993,7 +993,7 @@ fn type_kind(cx: ctxt, ty: t) -> ast::kind {
none. {/* fall through */ }
}
let result = ast::kind_unique;
let result = ast::kind_noncopyable;
// Insert a default in case we loop back on self recursively.
cx.kind_cache.insert(ty, result);
@ -1011,21 +1011,21 @@ fn type_kind(cx: ctxt, ty: t) -> ast::kind {
// FIXME: obj is broken for now, since we aren't asserting
// anything about its fields.
ty_obj(_) {
result = kind_shared;
result = kind_copyable;
}
// FIXME: the environment capture mode is not fully encoded
// here yet, leading to weirdness around closure.
ty_fn(proto, _, _, _, _) {
result = alt proto {
ast::proto_block. { ast::kind_pinned }
ast::proto_shared(_) { ast::kind_shared }
ast::proto_bare. { ast::kind_unique }
ast::proto_block. { ast::kind_noncopyable }
ast::proto_shared(_) { ast::kind_copyable }
ast::proto_bare. { ast::kind_sendable }
};
}
// Those with refcounts-to-inner raise pinned to shared,
// lower unique to shared. Therefore just set result to shared.
ty_box(mt) {
result = ast::kind_shared;
result = ast::kind_copyable;
}
// Pointers and unique containers raise pinned to shared.
ty_ptr(tm) | ty_vec(tm) | ty_uniq(tm) {
@ -1044,14 +1044,14 @@ fn type_kind(cx: ctxt, ty: t) -> ast::kind {
ty_rec(flds) {
for f: field in flds {
result = kind::lower_kind(result, type_kind(cx, f.mt.ty));
if result == ast::kind_pinned { break; }
if result == ast::kind_noncopyable { break; }
}
}
// Tuples lower to the lowest of their members.
ty_tup(tys) {
for ty: t in tys {
result = kind::lower_kind(result, type_kind(cx, ty));
if result == ast::kind_pinned { break; }
if result == ast::kind_noncopyable { break; }
}
}
// Tags lower to the lowest of their variants.
@ -1062,14 +1062,14 @@ fn type_kind(cx: ctxt, ty: t) -> ast::kind {
// Perform any type parameter substitutions.
let arg_ty = substitute_type_params(cx, tps, aty);
result = kind::lower_kind(result, type_kind(cx, arg_ty));
if result == ast::kind_pinned { break; }
if result == ast::kind_noncopyable { break; }
}
if result == ast::kind_pinned { break; }
if result == ast::kind_noncopyable { break; }
}
}
// Resources are always pinned.
ty_res(did, inner, tps) {
result = ast::kind_pinned;
result = ast::kind_noncopyable;
}
ty_var(_) {
fail;

View File

@ -23,6 +23,8 @@ type def_id = {crate: crate_num, node: node_id};
const local_crate: crate_num = 0;
tag plicit<T> { explicit(T); implicit(T); }
type ty_param = {ident: ident, kind: plicit<kind>};
tag def {
@ -99,8 +101,7 @@ tag pat_ {
tag mutability { mut; imm; maybe_mut; }
tag plicit<T> { explicit(T); implicit(T); }
tag kind { kind_pinned; kind_shared; kind_unique; kind_auto; }
tag kind { kind_sendable; kind_copyable; kind_noncopyable; }
tag _auth { auth_unsafe; }

View File

@ -229,7 +229,7 @@ fn ret_by_ref(style: ret_style) -> bool {
}
fn ty_param_kind(tp: ty_param) -> kind {
alt tp.kind { explicit(x) | implicit(x) { x } }
alt tp.kind { ast::implicit(x) | ast::explicit(x) { x } }
}
// Local Variables:

View File

@ -1738,21 +1738,24 @@ fn parse_block_tail(p: parser, lo: uint, s: ast::blk_check_mode) -> ast::blk {
ret spanned(lo, hi, bloc);
}
fn parse_ty_param(default: ast::kind, p: parser) -> ast::ty_param {
let k = if eat_word(p, "pin") { ast::explicit(ast::kind_pinned) }
else if eat_word(p, "uniq") { ast::explicit(ast::kind_unique) }
else if eat_word(p, "shar") { ast::explicit(ast::kind_shared) }
// FIXME distinguish implied shared from explicit
else { ast::implicit(default) };
fn parse_ty_param(p: parser, def: ast::kind) -> ast::ty_param {
// Accept both old and new kind names for now. FIXME remove this
let k = if eat_word(p, "send") | eat_word(p, "uniq")
{ ast::explicit(ast::kind_sendable) }
else if eat_word(p, "copy") | eat_word(p, "shar")
{ ast::explicit(ast::kind_copyable) }
else if eat_word(p, "nocopy") | eat_word(p, "pin")
{ ast::explicit(ast::kind_noncopyable) }
else { ast::implicit(def) };
ret {ident: parse_ident(p), kind: k};
}
fn parse_ty_params(p: parser, default: ast::kind) -> [ast::ty_param] {
fn parse_ty_params(p: parser, def: ast::kind) -> [ast::ty_param] {
let ty_params: [ast::ty_param] = [];
if p.peek() == token::LT {
p.bump();
ty_params = parse_seq_to_gt(some(token::COMMA),
{|p| parse_ty_param(default, p)}, p);
{|p| parse_ty_param(p, def)}, p);
}
ret ty_params;
}
@ -1805,7 +1808,7 @@ fn parse_fn(p: parser, proto: ast::proto, purity: ast::purity,
fn parse_fn_header(p: parser) -> {ident: ast::ident, tps: [ast::ty_param]} {
let id = parse_value_ident(p);
let ty_params = parse_ty_params(p, ast::kind_shared);
let ty_params = parse_ty_params(p, ast::kind_copyable);
ret {ident: id, tps: ty_params};
}
@ -1858,7 +1861,7 @@ fn parse_method(p: parser) -> @ast::method {
fn parse_item_obj(p: parser, attrs: [ast::attribute]) -> @ast::item {
let lo = p.get_last_lo_pos();
let ident = parse_value_ident(p);
let ty_params = parse_ty_params(p, ast::kind_pinned);
let ty_params = parse_ty_params(p, ast::kind_copyable);
let fields: ast::spanned<[ast::obj_field]> =
parse_seq(token::LPAREN, token::RPAREN, some(token::COMMA),
parse_obj_field, p);
@ -1875,7 +1878,7 @@ fn parse_item_obj(p: parser, attrs: [ast::attribute]) -> @ast::item {
fn parse_item_res(p: parser, attrs: [ast::attribute]) -> @ast::item {
let lo = p.get_last_lo_pos();
let ident = parse_value_ident(p);
let ty_params = parse_ty_params(p, ast::kind_pinned);
let ty_params = parse_ty_params(p, ast::kind_noncopyable);
expect(p, token::LPAREN);
let arg_ident = parse_value_ident(p);
expect(p, token::COLON);
@ -2039,7 +2042,7 @@ fn parse_type_decl(p: parser) -> {lo: uint, ident: ast::ident} {
fn parse_item_type(p: parser, attrs: [ast::attribute]) -> @ast::item {
let t = parse_type_decl(p);
let tps = parse_ty_params(p, ast::kind_pinned);
let tps = parse_ty_params(p, ast::kind_noncopyable);
expect(p, token::EQ);
let ty = parse_ty(p, false);
let hi = p.get_hi_pos();
@ -2050,7 +2053,7 @@ fn parse_item_type(p: parser, attrs: [ast::attribute]) -> @ast::item {
fn parse_item_tag(p: parser, attrs: [ast::attribute]) -> @ast::item {
let lo = p.get_last_lo_pos();
let id = parse_ident(p);
let ty_params = parse_ty_params(p, ast::kind_pinned);
let ty_params = parse_ty_params(p, ast::kind_noncopyable);
let variants: [ast::variant] = [];
// Newtype syntax
if p.peek() == token::EQ {

View File

@ -1168,10 +1168,10 @@ fn print_arg_mode(s: ps, m: ast::mode) {
fn print_kind(s: ps, kind: ast::plicit<ast::kind>) {
alt kind {
ast::explicit(ast::kind_sendable.) { word_nbsp(s, "send"); }
ast::explicit(ast::kind_copyable.) { word_nbsp(s, "copy"); }
ast::explicit(ast::kind_noncopyable.) { word_nbsp(s, "nocopy"); }
ast::implicit(_) {}
ast::explicit(ast::kind_unique.) { word_nbsp(s, "uniq"); }
ast::explicit(ast::kind_pinned.) { word_nbsp(s, "pin"); }
ast::explicit(ast::kind_shared.) { word_nbsp(s, "shar"); }
}
}

View File

@ -1,4 +1,5 @@
// error-pattern: needed shared type, got pinned type block
// xfail-test
fn lol(f: block()) -> block() { ret f; }
fn main() { let i = 8; let f = lol(block () { log_err i; }); f(); }

View File

@ -1,4 +1,5 @@
// error-pattern:cannot copy pinned type foo
// xfail-test
resource foo(i: int) { }

View File

@ -1,4 +1,5 @@
// error-pattern:mismatched kinds for '@' operand
// xfail-test
resource r(i: @mutable int) {
*i = *i + 1;
}

View File

@ -1,4 +1,5 @@
// error-pattern:mismatched kinds for record field
// xfail-test
resource r(i: @mutable int) {
*i = *i + 1;
}

View File

@ -1,4 +1,5 @@
// error-pattern:mismatched kinds for tuple parameter
// xfail-test
resource r(i: @mutable int) {
*i = *i + 1;
}

View File

@ -1,4 +1,5 @@
// error-pattern:mismatched kinds for '~' operand
// xfail-test
resource r(i: @mutable int) {
*i = *i + 1;
}

View File

@ -1,4 +1,5 @@
// error-pattern: cannot copy pinned type ~~~{y: r}
// xfail-test
resource r(i: @mutable int) {
*i = *i + 1;

View File

@ -1,4 +1,5 @@
// error-pattern: mismatched kind
// xfail-test
resource r(b: bool) {
}

View File

@ -1,4 +1,5 @@
// error-pattern: mismatched kind
// xfail-test
resource r(b: bool) {
}

View File

@ -1,4 +1,5 @@
// error-pattern:needed shared type, got pinned type ~r
// xfail-test
resource r(i: @mutable int) {
*i += 1;

View File

@ -1,4 +1,5 @@
// error-pattern: needed unique type
// xfail-test
fn f<uniq T>(i: T) {
}

View File

@ -1,4 +1,5 @@
// error-pattern: needed shared type, got pinned type ~r
// xfail-test
resource r(i: @mutable int) {
*i = *i + 1;

View File

@ -1,4 +1,5 @@
// error-pattern: mismatched kind
// xfail-test
resource r(b: bool) {
}

View File

@ -1,4 +1,5 @@
// error-pattern: mismatched kind
// xfail-test
resource r(b: bool) {
}

View File

@ -1,4 +1,5 @@
// error-pattern: mismatched kind
// xfail-test
resource r(b: bool) {
}

View File

@ -1,4 +1,5 @@
// error-pattern:mismatched kinds
// xfail-test
resource r(i: int) {
}