From 9cf48d3753e48ec37116d00cd3d4be12b5f71b5a Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Tue, 15 Nov 2011 16:45:14 +0100 Subject: [PATCH] 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 --- src/comp/metadata/decoder.rs | 6 ++-- src/comp/metadata/encoder.rs | 6 ++-- src/comp/metadata/tydecode.rs | 6 ++-- src/comp/metadata/tyencode.rs | 6 ++-- src/comp/middle/kind.rs | 32 +++++++++---------- src/comp/middle/ty.rs | 22 ++++++------- src/comp/syntax/ast.rs | 5 +-- src/comp/syntax/ast_util.rs | 2 +- src/comp/syntax/parse/parser.rs | 29 +++++++++-------- src/comp/syntax/print/pprust.rs | 6 ++-- src/test/compile-fail/block-copy.rs | 1 + src/test/compile-fail/copy-a-resource.rs | 1 + src/test/compile-fail/copy-res-into-box.rs | 1 + src/test/compile-fail/copy-res-into-rec.rs | 1 + src/test/compile-fail/copy-res-into-tup.rs | 1 + src/test/compile-fail/copy-res-into-unique.rs | 1 + src/test/compile-fail/pinned-deep-copy.rs | 1 + src/test/compile-fail/resource-let2.rs | 1 + src/test/compile-fail/unique-pinned-nocopy.rs | 1 + src/test/compile-fail/unique-swap2.rs | 1 + src/test/compile-fail/unique-unique-kind.rs | 1 + src/test/compile-fail/unique-vec-res.rs | 1 + src/test/compile-fail/vec-pinned-nocopy-2.rs | 1 + src/test/compile-fail/vec-pinned-nocopy-3.rs | 1 + src/test/compile-fail/vec-pinned-nocopy.rs | 1 + src/test/compile-fail/vec-res-add.rs | 1 + 26 files changed, 78 insertions(+), 58 deletions(-) diff --git a/src/comp/metadata/decoder.rs b/src/comp/metadata/decoder.rs index 2bba05197f6..b5c32efe66f 100644 --- a/src/comp/metadata/decoder.rs +++ b/src/comp/metadata/decoder.rs @@ -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; diff --git a/src/comp/metadata/encoder.rs b/src/comp/metadata/encoder.rs index 8227fca0a9f..bee96f53f0d 100644 --- a/src/comp/metadata/encoder.rs +++ b/src/comp/metadata/encoder.rs @@ -170,9 +170,9 @@ fn encode_type_param_kinds(ebml_w: ebml::writer, tps: [ty_param]) { ebml::write_vint(ebml_w.writer, vec::len::(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]); } diff --git a/src/comp/metadata/tydecode.rs b/src/comp/metadata/tydecode.rs index c8039556170..5239e70a5a8 100644 --- a/src/comp/metadata/tydecode.rs +++ b/src/comp/metadata/tydecode.rs @@ -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; diff --git a/src/comp/metadata/tyencode.rs b/src/comp/metadata/tyencode.rs index e010fe9c565..89a1f3e05ff 100644 --- a/src/comp/metadata/tyencode.rs +++ b/src/comp/metadata/tyencode.rs @@ -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)); } diff --git a/src/comp/middle/kind.rs b/src/comp/middle/kind.rs index cffd3fcf7cd..c82792fddf0 100644 --- a/src/comp/middle/kind.rs +++ b/src/comp/middle/kind.rs @@ -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();*/ } // diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs index d4aecfbba8a..a039337f5cb 100644 --- a/src/comp/middle/ty.rs +++ b/src/comp/middle/ty.rs @@ -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; diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs index 86074181b88..b77c58709ae 100644 --- a/src/comp/syntax/ast.rs +++ b/src/comp/syntax/ast.rs @@ -23,6 +23,8 @@ type def_id = {crate: crate_num, node: node_id}; const local_crate: crate_num = 0; +tag plicit { explicit(T); implicit(T); } + type ty_param = {ident: ident, kind: plicit}; tag def { @@ -99,8 +101,7 @@ tag pat_ { tag mutability { mut; imm; maybe_mut; } -tag plicit { 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; } diff --git a/src/comp/syntax/ast_util.rs b/src/comp/syntax/ast_util.rs index b31f928cd6c..6091b24ac1b 100644 --- a/src/comp/syntax/ast_util.rs +++ b/src/comp/syntax/ast_util.rs @@ -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: diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 794750ca61b..3731a6af7d0 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -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 { diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index 97c90d2c9a8..b5c466bd308 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -1168,10 +1168,10 @@ fn print_arg_mode(s: ps, m: ast::mode) { fn print_kind(s: ps, kind: ast::plicit) { 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"); } } } diff --git a/src/test/compile-fail/block-copy.rs b/src/test/compile-fail/block-copy.rs index c1a1971ee52..12bc0a0b2b9 100644 --- a/src/test/compile-fail/block-copy.rs +++ b/src/test/compile-fail/block-copy.rs @@ -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(); } diff --git a/src/test/compile-fail/copy-a-resource.rs b/src/test/compile-fail/copy-a-resource.rs index 621f7b99d90..f9e38941aa6 100644 --- a/src/test/compile-fail/copy-a-resource.rs +++ b/src/test/compile-fail/copy-a-resource.rs @@ -1,4 +1,5 @@ // error-pattern:cannot copy pinned type foo +// xfail-test resource foo(i: int) { } diff --git a/src/test/compile-fail/copy-res-into-box.rs b/src/test/compile-fail/copy-res-into-box.rs index 15f22b5eb6e..b4038b61d3e 100644 --- a/src/test/compile-fail/copy-res-into-box.rs +++ b/src/test/compile-fail/copy-res-into-box.rs @@ -1,4 +1,5 @@ // error-pattern:mismatched kinds for '@' operand +// xfail-test resource r(i: @mutable int) { *i = *i + 1; } diff --git a/src/test/compile-fail/copy-res-into-rec.rs b/src/test/compile-fail/copy-res-into-rec.rs index f22c4fd25ab..0fec1ba8a04 100644 --- a/src/test/compile-fail/copy-res-into-rec.rs +++ b/src/test/compile-fail/copy-res-into-rec.rs @@ -1,4 +1,5 @@ // error-pattern:mismatched kinds for record field +// xfail-test resource r(i: @mutable int) { *i = *i + 1; } diff --git a/src/test/compile-fail/copy-res-into-tup.rs b/src/test/compile-fail/copy-res-into-tup.rs index 4808b947045..e9318ae4280 100644 --- a/src/test/compile-fail/copy-res-into-tup.rs +++ b/src/test/compile-fail/copy-res-into-tup.rs @@ -1,4 +1,5 @@ // error-pattern:mismatched kinds for tuple parameter +// xfail-test resource r(i: @mutable int) { *i = *i + 1; } diff --git a/src/test/compile-fail/copy-res-into-unique.rs b/src/test/compile-fail/copy-res-into-unique.rs index 380f1e44a00..6fa1e852b4a 100644 --- a/src/test/compile-fail/copy-res-into-unique.rs +++ b/src/test/compile-fail/copy-res-into-unique.rs @@ -1,4 +1,5 @@ // error-pattern:mismatched kinds for '~' operand +// xfail-test resource r(i: @mutable int) { *i = *i + 1; } diff --git a/src/test/compile-fail/pinned-deep-copy.rs b/src/test/compile-fail/pinned-deep-copy.rs index e5652637c6b..9e8081ca6ce 100644 --- a/src/test/compile-fail/pinned-deep-copy.rs +++ b/src/test/compile-fail/pinned-deep-copy.rs @@ -1,4 +1,5 @@ // error-pattern: cannot copy pinned type ~~~{y: r} +// xfail-test resource r(i: @mutable int) { *i = *i + 1; diff --git a/src/test/compile-fail/resource-let2.rs b/src/test/compile-fail/resource-let2.rs index 695d50182b5..17855a30bd0 100644 --- a/src/test/compile-fail/resource-let2.rs +++ b/src/test/compile-fail/resource-let2.rs @@ -1,4 +1,5 @@ // error-pattern: mismatched kind +// xfail-test resource r(b: bool) { } diff --git a/src/test/compile-fail/unique-pinned-nocopy.rs b/src/test/compile-fail/unique-pinned-nocopy.rs index 544b8c3ed8f..612dd0dda03 100644 --- a/src/test/compile-fail/unique-pinned-nocopy.rs +++ b/src/test/compile-fail/unique-pinned-nocopy.rs @@ -1,4 +1,5 @@ // error-pattern: mismatched kind +// xfail-test resource r(b: bool) { } diff --git a/src/test/compile-fail/unique-swap2.rs b/src/test/compile-fail/unique-swap2.rs index 5a33fb3a503..2a653010701 100644 --- a/src/test/compile-fail/unique-swap2.rs +++ b/src/test/compile-fail/unique-swap2.rs @@ -1,4 +1,5 @@ // error-pattern:needed shared type, got pinned type ~r +// xfail-test resource r(i: @mutable int) { *i += 1; diff --git a/src/test/compile-fail/unique-unique-kind.rs b/src/test/compile-fail/unique-unique-kind.rs index 2504a4d6890..b20c552a011 100644 --- a/src/test/compile-fail/unique-unique-kind.rs +++ b/src/test/compile-fail/unique-unique-kind.rs @@ -1,4 +1,5 @@ // error-pattern: needed unique type +// xfail-test fn f(i: T) { } diff --git a/src/test/compile-fail/unique-vec-res.rs b/src/test/compile-fail/unique-vec-res.rs index dd077da4813..434b18bbb06 100644 --- a/src/test/compile-fail/unique-vec-res.rs +++ b/src/test/compile-fail/unique-vec-res.rs @@ -1,4 +1,5 @@ // error-pattern: needed shared type, got pinned type ~r +// xfail-test resource r(i: @mutable int) { *i = *i + 1; diff --git a/src/test/compile-fail/vec-pinned-nocopy-2.rs b/src/test/compile-fail/vec-pinned-nocopy-2.rs index 1c578f03983..168429b6c8f 100644 --- a/src/test/compile-fail/vec-pinned-nocopy-2.rs +++ b/src/test/compile-fail/vec-pinned-nocopy-2.rs @@ -1,4 +1,5 @@ // error-pattern: mismatched kind +// xfail-test resource r(b: bool) { } diff --git a/src/test/compile-fail/vec-pinned-nocopy-3.rs b/src/test/compile-fail/vec-pinned-nocopy-3.rs index 950c19a8388..54493ea4536 100644 --- a/src/test/compile-fail/vec-pinned-nocopy-3.rs +++ b/src/test/compile-fail/vec-pinned-nocopy-3.rs @@ -1,4 +1,5 @@ // error-pattern: mismatched kind +// xfail-test resource r(b: bool) { } diff --git a/src/test/compile-fail/vec-pinned-nocopy.rs b/src/test/compile-fail/vec-pinned-nocopy.rs index e3f83bf1923..91dfb4fd6f0 100644 --- a/src/test/compile-fail/vec-pinned-nocopy.rs +++ b/src/test/compile-fail/vec-pinned-nocopy.rs @@ -1,4 +1,5 @@ // error-pattern: mismatched kind +// xfail-test resource r(b: bool) { } diff --git a/src/test/compile-fail/vec-res-add.rs b/src/test/compile-fail/vec-res-add.rs index 9389d793f71..3d240a76480 100644 --- a/src/test/compile-fail/vec-res-add.rs +++ b/src/test/compile-fail/vec-res-add.rs @@ -1,4 +1,5 @@ // error-pattern:mismatched kinds +// xfail-test resource r(i: int) { }