Skip to content

Commit 2a1b6c4

Browse files
committed
librustc: Implement &static as the replacement for Durable. r=nmatsakis
1 parent 982830c commit 2a1b6c4

21 files changed

+107
-66
lines changed

src/librustc/middle/kind.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn kind_to_str(k: Kind) -> ~str {
7676
if ty::kind_can_be_sent(k) {
7777
kinds.push(~"owned");
7878
} else if ty::kind_is_durable(k) {
79-
kinds.push(~"durable");
79+
kinds.push(~"&static");
8080
}
8181

8282
str::connect(kinds, ~" ")
@@ -571,7 +571,7 @@ fn check_durable(tcx: ty::ctxt, ty: ty::t, sp: span) -> bool {
571571
match ty::get(ty).sty {
572572
ty::ty_param(*) => {
573573
tcx.sess.span_err(sp, ~"value may contain borrowed \
574-
pointers; use `durable` bound");
574+
pointers; use `&static` bound");
575575
}
576576
_ => {
577577
tcx.sess.span_err(sp, ~"value may contain borrowed \

src/librustc/middle/resolve.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use middle::pat_util::{pat_bindings};
2222
use core::cmp;
2323
use core::str;
2424
use core::vec;
25-
use syntax::ast::{_mod, add, arm, binding_mode, bitand, bitor, bitxor, blk};
26-
use syntax::ast::{capture_clause};
25+
use syntax::ast::{RegionTyParamBound, TraitTyParamBound, _mod, add, arm};
26+
use syntax::ast::{binding_mode, bitand, bitor, bitxor, blk, capture_clause};
2727
use syntax::ast::{crate, crate_num, decl_item, def, def_arg, def_binding};
2828
use syntax::ast::{def_const, def_foreign_mod, def_fn, def_id, def_label};
2929
use syntax::ast::{def_local, def_mod, def_prim_ty, def_region, def_self};
@@ -4117,8 +4117,11 @@ impl Resolver {
41174117
fn resolve_type_parameters(type_parameters: ~[ty_param],
41184118
visitor: ResolveVisitor) {
41194119
for type_parameters.each |type_parameter| {
4120-
for type_parameter.bounds.each |bound| {
4121-
self.resolve_type(**bound, visitor);
4120+
for type_parameter.bounds.each |&bound| {
4121+
match bound {
4122+
TraitTyParamBound(ty) => self.resolve_type(ty, visitor),
4123+
RegionTyParamBound => {}
4124+
}
41224125
}
41234126
}
41244127
}

src/librustc/middle/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ fn substs_to_str(cx: ctxt, substs: &substs) -> ~str {
15911591
fn param_bound_to_str(cx: ctxt, pb: &param_bound) -> ~str {
15921592
match *pb {
15931593
bound_copy => ~"copy",
1594-
bound_durable => ~"durable",
1594+
bound_durable => ~"&static",
15951595
bound_owned => ~"owned",
15961596
bound_const => ~"const",
15971597
bound_trait(t) => ::util::ppaux::ty_to_str(cx, t)

src/librustc/middle/typeck/collect.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use util::ppaux::bound_to_str;
5050
use core::dvec;
5151
use core::option;
5252
use core::vec;
53+
use syntax::ast::{RegionTyParamBound, TraitTyParamBound};
5354
use syntax::ast;
5455
use syntax::ast_map;
5556
use syntax::ast_util::{local_def, split_trait_methods};
@@ -908,36 +909,42 @@ fn ty_of_foreign_item(ccx: @crate_ctxt, it: @ast::foreign_item)
908909
}
909910
}
910911

911-
// Translate the AST's notion of ty param bounds (which are just newtyped Tys)
912-
// to ty's notion of ty param bounds, which can either be user-defined traits,
913-
// or one of the four built-in traits (formerly known as kinds): Const, Copy,
914-
// Durable, and Send.
912+
// Translate the AST's notion of ty param bounds (which are an enum consisting
913+
// of a newtyped Ty or a region) to ty's notion of ty param bounds, which can
914+
// either be user-defined traits, or one of the four built-in traits (formerly
915+
// known as kinds): Const, Copy, Durable, and Send.
915916
fn compute_bounds(ccx: @crate_ctxt,
916-
ast_bounds: @~[ast::ty_param_bound]) -> ty::param_bounds {
917+
ast_bounds: @~[ast::ty_param_bound])
918+
-> ty::param_bounds {
917919
@do vec::flat_map(*ast_bounds) |b| {
918-
let li = &ccx.tcx.lang_items;
919-
let ity = ast_ty_to_ty(ccx, empty_rscope, **b);
920-
match ty::get(ity).sty {
921-
ty::ty_trait(did, _, _) => {
922-
if did == li.owned_trait() {
923-
~[ty::bound_owned]
924-
} else if did == li.copy_trait() {
925-
~[ty::bound_copy]
926-
} else if did == li.const_trait() {
927-
~[ty::bound_const]
928-
} else if did == li.durable_trait() {
929-
~[ty::bound_durable]
930-
} else {
931-
// Must be a user-defined trait
932-
~[ty::bound_trait(ity)]
920+
match b {
921+
&TraitTyParamBound(b) => {
922+
let li = &ccx.tcx.lang_items;
923+
let ity = ast_ty_to_ty(ccx, empty_rscope, b);
924+
match ty::get(ity).sty {
925+
ty::ty_trait(did, _, _) => {
926+
if did == li.owned_trait() {
927+
~[ty::bound_owned]
928+
} else if did == li.copy_trait() {
929+
~[ty::bound_copy]
930+
} else if did == li.const_trait() {
931+
~[ty::bound_const]
932+
} else if did == li.durable_trait() {
933+
~[ty::bound_durable]
934+
} else {
935+
// Must be a user-defined trait
936+
~[ty::bound_trait(ity)]
937+
}
938+
}
939+
_ => {
940+
ccx.tcx.sess.span_err(
941+
(*b).span, ~"type parameter bounds must be \
942+
trait types");
943+
~[]
944+
}
933945
}
934946
}
935-
_ => {
936-
ccx.tcx.sess.span_err(
937-
(*b).span, ~"type parameter bounds must be \
938-
trait types");
939-
~[]
940-
}
947+
&RegionTyParamBound => ~[ty::bound_durable]
941948
}
942949
}
943950
}

src/libsyntax/ast.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ const crate_node_id: node_id = 0;
110110
// typeck::collect::compute_bounds matches these against
111111
// the "special" built-in traits (see middle::lang_items) and
112112
// detects Copy, Send, Owned, and Const.
113-
enum ty_param_bound = @Ty;
113+
enum ty_param_bound {
114+
TraitTyParamBound(@Ty),
115+
RegionTyParamBound
116+
}
114117

115118
#[auto_encode]
116119
#[auto_decode]

src/libsyntax/ext/auto_encode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ priv impl ext_ctxt {
245245
path: @ast::path,
246246
bounds: @~[ast::ty_param_bound]
247247
) -> ast::ty_param {
248-
let bound = ast::ty_param_bound(@{
248+
let bound = ast::TraitTyParamBound(@{
249249
id: self.next_id(),
250250
node: ast::ty_path(path, self.next_id()),
251251
span: span,
@@ -397,7 +397,7 @@ fn mk_impl(
397397
let mut trait_tps = vec::append(
398398
~[ty_param],
399399
do tps.map |tp| {
400-
let t_bound = ast::ty_param_bound(@{
400+
let t_bound = ast::TraitTyParamBound(@{
401401
id: cx.next_id(),
402402
node: ast::ty_path(path, cx.next_id()),
403403
span: span,

src/libsyntax/ext/deriving.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
1414
use core::prelude::*;
1515

16-
use ast::{Ty, and, bind_by_ref, binop, deref, enum_def, enum_variant_kind};
17-
use ast::{expr, expr_match, ident, item, item_, item_struct, item_enum};
18-
use ast::{item_impl, m_imm, meta_item, method, named_field, or, pat};
19-
use ast::{pat_ident, pat_wild, public, pure_fn, re_anon, spanned, stmt};
20-
use ast::{struct_def, struct_variant_kind, sty_by_ref, sty_region};
21-
use ast::{tuple_variant_kind, ty_nil, ty_param, ty_param_bound, ty_path};
22-
use ast::{ty_rptr, unnamed_field, variant};
16+
use ast::{TraitTyParamBound, Ty, and, bind_by_ref, binop, deref, enum_def};
17+
use ast::{enum_variant_kind, expr, expr_match, ident, item, item_};
18+
use ast::{item_enum, item_impl, item_struct, m_imm, meta_item, method};
19+
use ast::{named_field, or, pat, pat_ident, pat_wild, public, pure_fn};
20+
use ast::{re_anon, spanned, stmt, struct_def, struct_variant_kind};
21+
use ast::{sty_by_ref, sty_region, tuple_variant_kind, ty_nil, ty_param};
22+
use ast::{ty_param_bound, ty_path, ty_rptr, unnamed_field, variant};
2323
use ext::base::ext_ctxt;
2424
use ext::build;
2525
use codemap::span;
@@ -211,7 +211,7 @@ fn create_derived_impl(cx: ext_ctxt,
211211
let bound = build::mk_ty_path_global(cx,
212212
span,
213213
trait_path.map(|x| *x));
214-
let bounds = @~[ ty_param_bound(bound) ];
214+
let bounds = @~[ TraitTyParamBound(bound) ];
215215
let impl_ty_param = build::mk_ty_param(cx, ty_param.ident, bounds);
216216
impl_ty_params.push(move impl_ty_param);
217217
}

src/libsyntax/fold.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ fn fold_fn_decl(decl: ast::fn_decl, fld: ast_fold) -> ast::fn_decl {
141141
}
142142

143143
fn fold_ty_param_bound(tpb: ty_param_bound, fld: ast_fold) -> ty_param_bound {
144-
ty_param_bound(fld.fold_ty(*tpb))
144+
match tpb {
145+
TraitTyParamBound(ty) => TraitTyParamBound(fld.fold_ty(ty)),
146+
RegionTyParamBound => RegionTyParamBound
147+
}
145148
}
146149

147150
fn fold_ty_param(tp: ty_param, fld: ast_fold) -> ty_param {

src/libsyntax/parse/parser.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
use core::prelude::*;
1212

13-
use ast::{ProtoBox, ProtoUniq, provided, public, pure_fn, purity, re_static};
13+
use ast::{ProtoBox, ProtoUniq, RegionTyParamBound, TraitTyParamBound};
14+
use ast::{provided, public, pure_fn, purity, re_static};
1415
use ast::{_mod, add, arg, arm, attribute, bind_by_ref, bind_infer};
1516
use ast::{bind_by_value, bind_by_move, bitand, bitor, bitxor, blk};
1617
use ast::{blk_check_mode, box, by_copy, by_move, by_ref, by_val};
@@ -2401,8 +2402,16 @@ impl Parser {
24012402
fn parse_optional_ty_param_bounds() -> @~[ty_param_bound] {
24022403
let mut bounds = ~[];
24032404
if self.eat(token::COLON) {
2404-
while is_ident(self.token) {
2405-
if is_ident(self.token) {
2405+
loop {
2406+
if self.eat(token::BINOP(token::AND)) {
2407+
if self.eat_keyword(~"static") {
2408+
bounds.push(RegionTyParamBound);
2409+
} else {
2410+
self.span_err(copy self.span,
2411+
~"`&static` is the only permissible \
2412+
region bound here");
2413+
}
2414+
} else if is_ident(self.token) {
24062415
let maybe_bound = match self.token {
24072416
token::IDENT(copy sid, _) => {
24082417
match *self.id_to_str(sid) {
@@ -2415,7 +2424,7 @@ impl Parser {
24152424
ObsoleteLowerCaseKindBounds);
24162425
// Bogus value, but doesn't matter, since
24172426
// is an error
2418-
Some(ty_param_bound(self.mk_ty_path(sid)))
2427+
Some(TraitTyParamBound(self.mk_ty_path(sid)))
24192428
}
24202429
24212430
_ => None
@@ -2430,11 +2439,12 @@ impl Parser {
24302439
bounds.push(bound);
24312440
}
24322441
None => {
2433-
bounds.push(ty_param_bound(self.parse_ty(false)));
2442+
let ty = self.parse_ty(false);
2443+
bounds.push(TraitTyParamBound(ty));
24342444
}
24352445
}
24362446
} else {
2437-
bounds.push(ty_param_bound(self.parse_ty(false)));
2447+
break;
24382448
}
24392449
}
24402450
}

src/libsyntax/print/pprust.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use core::prelude::*;
1212

13-
use ast::{required, provided};
13+
use ast::{RegionTyParamBound, TraitTyParamBound, required, provided};
1414
use ast;
1515
use ast_util;
1616
use ast_util::{operator_prec};
@@ -1791,9 +1791,12 @@ fn print_arg_mode(s: ps, m: ast::mode) {
17911791
fn print_bounds(s: ps, bounds: @~[ast::ty_param_bound]) {
17921792
if bounds.is_not_empty() {
17931793
word(s.s, ~":");
1794-
for vec::each(*bounds) |bound| {
1794+
for vec::each(*bounds) |&bound| {
17951795
nbsp(s);
1796-
print_type(s, **bound);
1796+
match bound {
1797+
TraitTyParamBound(ty) => print_type(s, ty),
1798+
RegionTyParamBound => word(s.s, ~"&static"),
1799+
}
17971800
}
17981801
}
17991802
}

src/libsyntax/visit.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,11 @@ fn visit_foreign_item<E>(ni: @foreign_item, e: E, v: vt<E>) {
288288
}
289289

290290
fn visit_ty_param_bounds<E>(bounds: @~[ty_param_bound], e: E, v: vt<E>) {
291-
for vec::each(*bounds) |bound| {
292-
(v.visit_ty)(**bound, e, v)
291+
for bounds.each |&bound| {
292+
match bound {
293+
TraitTyParamBound(ty) => (v.visit_ty)(ty, e, v),
294+
RegionTyParamBound => ()
295+
}
293296
}
294297
}
295298

src/test/compile-fail/kindck-owned-trait-scoped.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn to_foo_2<T:Copy>(t: T) -> foo {
3535
{f:t} as foo //~ ERROR value may contain borrowed pointers; use `durable` bound
3636
}
3737

38-
fn to_foo_3<T:Copy Durable>(t: T) -> foo {
38+
fn to_foo_3<T:Copy &static>(t: T) -> foo {
3939
// OK---T may escape as part of the returned foo value, but it is
4040
// owned and hence does not contain borrowed ptrs
4141
{f:t} as foo

src/test/compile-fail/kindck-owned-trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn to_foo<T: Copy foo>(t: T) -> foo {
1414
t as foo //~ ERROR value may contain borrowed pointers; use `durable` bound
1515
}
1616

17-
fn to_foo2<T: Copy foo Durable>(t: T) -> foo {
17+
fn to_foo2<T: Copy foo &static>(t: T) -> foo {
1818
t as foo
1919
}
2020

src/test/compile-fail/kindck-owned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn copy1<T: Copy>(t: T) -> fn@() -> T {
1212
fn@() -> T { t } //~ ERROR value may contain borrowed pointers
1313
}
1414

15-
fn copy2<T: Copy Durable>(t: T) -> fn@() -> T {
15+
fn copy2<T: Copy &static>(t: T) -> fn@() -> T {
1616
fn@() -> T { t }
1717
}
1818

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn f<T:&static>(_: T) {}
2+
3+
fn main() {
4+
let x = @3;
5+
f(x);
6+
let x = &3;
7+
f(x); //~ ERROR instantiating a type parameter with an incompatible type
8+
}
9+

src/test/run-pass/alignment-gep-tup-like-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type pair<A,B> = {
1212
a: A, b: B
1313
};
1414

15-
fn f<A:Copy Durable>(a: A, b: u16) -> fn@() -> (A, u16) {
15+
fn f<A:Copy &static>(a: A, b: u16) -> fn@() -> (A, u16) {
1616
fn@() -> (A, u16) { (a, b) }
1717
}
1818

src/test/run-pass/close-over-big-then-small-data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type pair<A,B> = {
1616
a: A, b: B
1717
};
1818

19-
fn f<A:Copy Durable>(a: A, b: u16) -> fn@() -> (A, u16) {
19+
fn f<A:Copy &static>(a: A, b: u16) -> fn@() -> (A, u16) {
2020
fn@() -> (A, u16) { (a, b) }
2121
}
2222

src/test/run-pass/fixed-point-bind-unique.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
// xfail-fast
1212
#[legacy_modes];
1313

14-
fn fix_help<A: Durable, B: Owned>(f: extern fn(fn@(A) -> B, A) -> B, x: A) -> B {
14+
fn fix_help<A: &static, B: Owned>(f: extern fn(fn@(A) -> B, A) -> B, x: A) -> B {
1515
return f({|a|fix_help(f, a)}, x);
1616
}
1717

18-
fn fix<A: Durable, B: Owned>(f: extern fn(fn@(A) -> B, A) -> B) -> fn@(A) -> B {
18+
fn fix<A: &static, B: Owned>(f: extern fn(fn@(A) -> B, A) -> B) -> fn@(A) -> B {
1919
return {|a|fix_help(f, a)};
2020
}
2121

src/test/run-pass/issue-2734.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
trait hax { }
1212
impl <A> A: hax { }
1313

14-
fn perform_hax<T: Durable>(x: @T) -> hax {
14+
fn perform_hax<T: &static>(x: @T) -> hax {
1515
x as hax
1616
}
1717

src/test/run-pass/issue-2735.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
trait hax { }
1212
impl <A> A: hax { }
1313

14-
fn perform_hax<T: Durable>(x: @T) -> hax {
14+
fn perform_hax<T: &static>(x: @T) -> hax {
1515
x as hax
1616
}
1717

src/test/run-pass/issue-2904.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn square_from_char(c: char) -> square {
5959
}
6060
}
6161

62-
fn read_board_grid<rdr: Durable io::Reader>(+in: rdr) -> ~[~[square]] {
62+
fn read_board_grid<rdr: &static io::Reader>(+in: rdr) -> ~[~[square]] {
6363
let in = (move in) as io::Reader;
6464
let mut grid = ~[];
6565
for in.each_line |line| {

0 commit comments

Comments
 (0)