Skip to content

Commit 7dacccd

Browse files
committed
Make shared kind the default only for generic functions
You almost never want a function with pinned type params. For types, objects, resources, and tags, pinned types are actually often more sane. For most of these, shared rarely makes sense. Only tricky case is objs -- you'll have to think about the kinds you want there. Issue #1076
1 parent 6fe7aa4 commit 7dacccd

File tree

16 files changed

+63
-71
lines changed

16 files changed

+63
-71
lines changed

src/comp/metadata/encoder.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,11 @@ fn encode_type_param_kinds(ebml_w: ebml::writer, tps: [ty_param]) {
170170
ebml::start_tag(ebml_w, tag_items_data_item_ty_param_kinds);
171171
ebml::write_vint(ebml_w.writer, vec::len::<ty_param>(tps));
172172
for tp: ty_param in tps {
173-
let c =
174-
alt tp.kind {
175-
kind_unique. { 'u' }
176-
kind_shared. { 's' }
177-
kind_pinned. { 'p' }
178-
};
173+
let c = alt ast_util::ty_param_kind(tp) {
174+
kind_unique. { 'u' }
175+
kind_shared. { 's' }
176+
kind_pinned. { 'p' }
177+
};
179178
ebml_w.writer.write([c as u8]);
180179
}
181180
ebml::end_tag(ebml_w);

src/comp/middle/ast_map.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ fn new_smallintmap_adapter<K, V>(key_idx: fn(K) -> uint,
9090
idx_key: fn(uint) -> K)
9191
-> std::map::hashmap<K, V> {
9292

93-
obj adapter<K, V>(map: smallintmap::smallintmap<V>,
94-
key_idx: fn(K) -> uint,
95-
idx_key: fn(uint) -> K) {
93+
obj adapter<shar K, shar V>(map: smallintmap::smallintmap<V>,
94+
key_idx: fn(K) -> uint,
95+
idx_key: fn(uint) -> K) {
9696

9797
fn size() -> uint { fail }
9898

src/comp/middle/resolve.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ fn def_is_obj_field(d: def) -> bool {
619619
}
620620

621621
fn def_is_ty_arg(d: def) -> bool {
622-
ret alt d { ast::def_ty_arg(_, _) { true } _ { false } };
622+
ret alt d { ast::def_ty_param(_, _) { true } _ { false } };
623623
}
624624

625625
fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace)
@@ -734,7 +734,9 @@ fn lookup_in_ty_params(name: ident, ty_params: [ast::ty_param]) ->
734734
option::t<def> {
735735
let i = 0u;
736736
for tp: ast::ty_param in ty_params {
737-
if str::eq(tp.ident, name) { ret some(ast::def_ty_arg(i, tp.kind)); }
737+
if str::eq(tp.ident, name) {
738+
ret some(ast::def_ty_param(i, ast_util::ty_param_kind(tp)));
739+
}
738740
i += 1u;
739741
}
740742
ret none::<def>;

src/comp/middle/trans.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5227,7 +5227,8 @@ fn trans_tag_variant(cx: @local_ctxt, tag_id: ast::node_id,
52275227
let ty_param_substs: [ty::t] = [];
52285228
i = 0u;
52295229
for tp: ast::ty_param in ty_params {
5230-
ty_param_substs += [ty::mk_param(cx.ccx.tcx, i, tp.kind)];
5230+
ty_param_substs += [ty::mk_param(cx.ccx.tcx, i,
5231+
ast_util::ty_param_kind(tp))];
52315232
i += 1u;
52325233
}
52335234
let arg_tys = arg_tys_of_fn(cx.ccx, variant.node.id);

src/comp/middle/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2752,7 +2752,7 @@ fn def_has_ty_params(def: ast::def) -> bool {
27522752
ast::def_upvar(_, _, _) { ret false; }
27532753
ast::def_variant(_, _) { ret true; }
27542754
ast::def_ty(_) { ret false; }
2755-
ast::def_ty_arg(_, _) { ret false; }
2755+
ast::def_ty_param(_, _) { ret false; }
27562756
ast::def_binding(_) { ret false; }
27572757
ast::def_use(_) { ret false; }
27582758
ast::def_native_ty(_) { ret false; }

src/comp/middle/typeck.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import syntax::{ast, ast_util};
22
import ast::{mutability, spanned};
3-
import syntax::ast_util::{local_def, respan};
3+
import syntax::ast_util::{local_def, respan, ty_param_kind};
44
import syntax::visit;
55
import metadata::csearch;
66
import driver::session;
@@ -342,7 +342,7 @@ fn ast_ty_to_ty(tcx: ty::ctxt, getter: ty_getter, &&ast_ty: @ast::ty)
342342
typ = instantiate(tcx, ast_ty.span, getter, id, path.node.types);
343343
}
344344
some(ast::def_native_ty(id)) { typ = getter(id).ty; }
345-
some(ast::def_ty_arg(id, k)) { typ = ty::mk_param(tcx, id, k); }
345+
some(ast::def_ty_param(id, k)) { typ = ty::mk_param(tcx, id, k); }
346346
some(_) {
347347
tcx.sess.span_fatal(ast_ty.span,
348348
"found type name used as a variable");
@@ -495,15 +495,15 @@ mod collect {
495495
let tps = [];
496496
let i = 0u;
497497
for atp: ast::ty_param in atps {
498-
tps += [ty::mk_param(cx.tcx, i, atp.kind)];
498+
tps += [ty::mk_param(cx.tcx, i, ty_param_kind(atp))];
499499
i += 1u;
500500
}
501501
ret tps;
502502
}
503503

504504
fn ty_param_kinds(tps: [ast::ty_param]) -> [ast::kind] {
505505
let k: [ast::kind] = [];
506-
for p: ast::ty_param in tps { k += [p.kind]; }
506+
for p: ast::ty_param in tps { k += [ty_param_kind(p)]; }
507507
ret k;
508508
}
509509

src/comp/syntax/ast.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type def_id = {crate: crate_num, node: node_id};
2323

2424
const local_crate: crate_num = 0;
2525

26-
type ty_param = {ident: ident, kind: kind};
26+
type ty_param = {ident: ident, kind: plicit<kind>};
2727

2828
tag def {
2929
def_fn(def_id, purity);
@@ -37,7 +37,7 @@ tag def {
3737

3838
/* variant */
3939
def_ty(def_id);
40-
def_ty_arg(uint, kind);
40+
def_ty_param(uint, kind);
4141
def_binding(def_id);
4242
def_use(def_id);
4343
def_native_ty(def_id);
@@ -99,7 +99,8 @@ tag pat_ {
9999

100100
tag mutability { mut; imm; maybe_mut; }
101101

102-
tag kind { kind_pinned; kind_shared; kind_unique; }
102+
tag plicit<T> { explicit(T); implicit(T); }
103+
tag kind { kind_pinned; kind_shared; kind_unique; kind_auto; }
103104

104105
tag _auth { auth_unsafe; }
105106

@@ -489,16 +490,10 @@ tag item_ {
489490
item_ty(@ty, [ty_param]);
490491
item_tag([variant], [ty_param]);
491492
item_obj(_obj, [ty_param], /* constructor id */node_id);
492-
item_res(_fn,
493-
494-
/* dtor */
495-
node_id,
496-
497-
/* dtor id */
493+
item_res(_fn /* dtor */,
494+
node_id /* dtor id */,
498495
[ty_param],
499-
500-
/* ctor id */
501-
node_id);
496+
node_id /* ctor id */);
502497
}
503498

504499
type native_item =

src/comp/syntax/ast_util.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn def_id_of_def(d: def) -> def_id {
3333
def_local(id, _) { ret id; }
3434
def_variant(_, id) { ret id; }
3535
def_ty(id) { ret id; }
36-
def_ty_arg(_, _) { fail; }
36+
def_ty_param(_, _) { fail; }
3737
def_binding(id) { ret id; }
3838
def_use(id) { ret id; }
3939
def_native_ty(id) { ret id; }
@@ -228,6 +228,10 @@ fn ret_by_ref(style: ret_style) -> bool {
228228
}
229229
}
230230

231+
fn ty_param_kind(tp: ty_param) -> kind {
232+
alt tp.kind { explicit(x) | implicit(x) { x } }
233+
}
234+
231235
// Local Variables:
232236
// mode: rust
233237
// fill-column: 78;

src/comp/syntax/parse/parser.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,25 +1735,21 @@ fn parse_block_tail(p: parser, lo: uint, s: ast::blk_check_mode) -> ast::blk {
17351735
ret spanned(lo, hi, bloc);
17361736
}
17371737

1738-
fn parse_ty_param(p: parser) -> ast::ty_param {
1739-
let k = if eat_word(p, "pin") { ast::kind_pinned }
1740-
else if eat_word(p, "uniq") { ast::kind_unique }
1741-
else if eat_word(p, "shar") { ast::kind_shared }
1738+
fn parse_ty_param(default: ast::kind, p: parser) -> ast::ty_param {
1739+
let k = if eat_word(p, "pin") { ast::explicit(ast::kind_pinned) }
1740+
else if eat_word(p, "uniq") { ast::explicit(ast::kind_unique) }
1741+
else if eat_word(p, "shar") { ast::explicit(ast::kind_shared) }
17421742
// FIXME distinguish implied shared from explicit
1743-
else { ast::kind_shared };
1743+
else { ast::implicit(default) };
17441744
ret {ident: parse_ident(p), kind: k};
17451745
}
17461746

1747-
fn parse_ty_params(p: parser) -> [ast::ty_param] {
1747+
fn parse_ty_params(p: parser, default: ast::kind) -> [ast::ty_param] {
17481748
let ty_params: [ast::ty_param] = [];
17491749
if p.peek() == token::LT {
17501750
p.bump();
1751-
ty_params = parse_seq_to_gt(some(token::COMMA), parse_ty_param, p);
1752-
}
1753-
if p.peek() == token::LT {
1754-
ty_params =
1755-
parse_seq(token::LT, token::GT, some(token::COMMA),
1756-
parse_ty_param, p).node;
1751+
ty_params = parse_seq_to_gt(some(token::COMMA),
1752+
{|p| parse_ty_param(default, p)}, p);
17571753
}
17581754
ret ty_params;
17591755
}
@@ -1806,7 +1802,7 @@ fn parse_fn(p: parser, proto: ast::proto, purity: ast::purity,
18061802

18071803
fn parse_fn_header(p: parser) -> {ident: ast::ident, tps: [ast::ty_param]} {
18081804
let id = parse_value_ident(p);
1809-
let ty_params = parse_ty_params(p);
1805+
let ty_params = parse_ty_params(p, ast::kind_shared);
18101806
ret {ident: id, tps: ty_params};
18111807
}
18121808

@@ -1859,7 +1855,7 @@ fn parse_method(p: parser) -> @ast::method {
18591855
fn parse_item_obj(p: parser, attrs: [ast::attribute]) -> @ast::item {
18601856
let lo = p.get_last_lo_pos();
18611857
let ident = parse_value_ident(p);
1862-
let ty_params = parse_ty_params(p);
1858+
let ty_params = parse_ty_params(p, ast::kind_pinned);
18631859
let fields: ast::spanned<[ast::obj_field]> =
18641860
parse_seq(token::LPAREN, token::RPAREN, some(token::COMMA),
18651861
parse_obj_field, p);
@@ -1876,7 +1872,7 @@ fn parse_item_obj(p: parser, attrs: [ast::attribute]) -> @ast::item {
18761872
fn parse_item_res(p: parser, attrs: [ast::attribute]) -> @ast::item {
18771873
let lo = p.get_last_lo_pos();
18781874
let ident = parse_value_ident(p);
1879-
let ty_params = parse_ty_params(p);
1875+
let ty_params = parse_ty_params(p, ast::kind_pinned);
18801876
expect(p, token::LPAREN);
18811877
let arg_ident = parse_value_ident(p);
18821878
expect(p, token::COLON);
@@ -2052,7 +2048,7 @@ fn parse_type_decl(p: parser) -> {lo: uint, ident: ast::ident} {
20522048

20532049
fn parse_item_type(p: parser, attrs: [ast::attribute]) -> @ast::item {
20542050
let t = parse_type_decl(p);
2055-
let tps = parse_ty_params(p);
2051+
let tps = parse_ty_params(p, ast::kind_pinned);
20562052
expect(p, token::EQ);
20572053
let ty = parse_ty(p, false);
20582054
let hi = p.get_hi_pos();
@@ -2063,7 +2059,7 @@ fn parse_item_type(p: parser, attrs: [ast::attribute]) -> @ast::item {
20632059
fn parse_item_tag(p: parser, attrs: [ast::attribute]) -> @ast::item {
20642060
let lo = p.get_last_lo_pos();
20652061
let id = parse_ident(p);
2066-
let ty_params = parse_ty_params(p);
2062+
let ty_params = parse_ty_params(p, ast::kind_pinned);
20672063
let variants: [ast::variant] = [];
20682064
// Newtype syntax
20692065
if p.peek() == token::EQ {

src/comp/syntax/print/pprust.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,11 +1191,12 @@ fn print_arg_mode(s: ps, m: ast::mode) {
11911191
}
11921192
}
11931193
1194-
fn print_kind(s: ps, kind: ast::kind) {
1194+
fn print_kind(s: ps, kind: ast::plicit<ast::kind>) {
11951195
alt kind {
1196-
ast::kind_unique. { word_nbsp(s, "uniq"); }
1197-
ast::kind_pinned. { word_nbsp(s, "pin"); }
1198-
_ {/* fallthrough */ }
1196+
ast::implicit(_) {}
1197+
ast::explicit(ast::kind_unique.) { word_nbsp(s, "uniq"); }
1198+
ast::explicit(ast::kind_pinned.) { word_nbsp(s, "pin"); }
1199+
ast::explicit(ast::kind_shared.) { word_nbsp(s, "shar"); }
11991200
}
12001201
}
12011202

src/lib/deque.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ fn create<T>() -> t<T> {
4141
* Grow is only called on full elts, so nelts is also len(elts), unlike
4242
* elsewhere.
4343
*/
44-
45-
46-
47-
48-
49-
5044
fn grow<T>(nelts: uint, lo: uint, elts: [mutable cell<T>]) ->
5145
[mutable cell<T>] {
5246
assert (nelts == vec::len(elts));
@@ -66,10 +60,10 @@ fn create<T>() -> t<T> {
6660
fn get<T>(elts: [mutable cell<T>], i: uint) -> T {
6761
ret alt elts[i] { option::some(t) { t } _ { fail } };
6862
}
69-
obj deque<T>(mutable nelts: uint,
70-
mutable lo: uint,
71-
mutable hi: uint,
72-
mutable elts: [mutable cell<T>]) {
63+
obj deque<shar T>(mutable nelts: uint,
64+
mutable lo: uint,
65+
mutable hi: uint,
66+
mutable elts: [mutable cell<T>]) {
7367
fn size() -> uint { ret nelts; }
7468
fn add_front(t: T) {
7569
let oldlo: uint = lo;

src/lib/map.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,12 @@ fn mk_hashmap<K, V>(hasher: hashfn<K>, eqer: eqfn<K>) -> hashmap<K, V> {
201201
}
202202
}
203203
}
204-
obj hashmap<K, V>(hasher: hashfn<K>,
205-
eqer: eqfn<K>,
206-
mutable bkts: [mutable bucket<K, V>],
207-
mutable nbkts: uint,
208-
mutable nelts: uint,
209-
lf: util::rational) {
204+
obj hashmap<shar K, shar V>(hasher: hashfn<K>,
205+
eqer: eqfn<K>,
206+
mutable bkts: [mutable bucket<K, V>],
207+
mutable nbkts: uint,
208+
mutable nelts: uint,
209+
lf: util::rational) {
210210
fn size() -> uint { ret nelts; }
211211
fn insert(key: K, val: V) -> bool {
212212
let load: util::rational =

src/test/run-pass/foreach-unique-drop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
obj ob<K>(k: K) {
2+
obj ob<shar K>(k: K) {
33
fn foo(it: block(~{a: K})) { it(~{a: k}); }
44
}
55

src/test/run-pass/generic-obj-with-derived-type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
obj handle<T>(data: T) {
3+
obj handle<shar T>(data: T) {
44
fn get() -> T { ret data; }
55
}
66

src/test/run-pass/generic-obj.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
obj buf<T>(data: {_0: T, _1: T, _2: T}) {
3+
obj buf<shar T>(data: {_0: T, _1: T, _2: T}) {
44
fn get(i: int) -> T {
55
if i == 0 {
66
ret data._0;

src/test/run-pass/obj-return-polytypes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ tag clam<T> { signed(int); unsigned(uint); }
66

77
fn getclam<T>() -> clam<T> { ret signed::<T>(42); }
88

9-
obj impatience<T>() {
9+
obj impatience<shar T>() {
1010
fn moreclam() -> clam<T> { be getclam::<T>(); }
1111
}
1212

0 commit comments

Comments
 (0)