Skip to content

Commit f74a480

Browse files
committed
librustc: Implement &static as the replacement for Durable
1 parent 4b51892 commit f74a480

File tree

12 files changed

+97
-56
lines changed

12 files changed

+97
-56
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
@@ -1592,7 +1592,7 @@ fn substs_to_str(cx: ctxt, substs: &substs) -> ~str {
15921592
fn param_bound_to_str(cx: ctxt, pb: &param_bound) -> ~str {
15931593
match *pb {
15941594
bound_copy => ~"copy",
1595-
bound_durable => ~"durable",
1595+
bound_durable => ~"&static",
15961596
bound_owned => ~"owned",
15971597
bound_const => ~"const",
15981598
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};
@@ -906,36 +907,42 @@ fn ty_of_foreign_item(ccx: @crate_ctxt, it: @ast::foreign_item)
906907
}
907908
}
908909

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

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, stmt, struct_def};
20-
use ast::{struct_variant_kind, sty_by_ref, sty_region, tuple_variant_kind};
21-
use ast::{ty_nil, ty_param, ty_param_bound, ty_path, ty_rptr, unnamed_field};
22-
use ast::{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_, item_enum};
18+
use ast::{item_impl, item_struct, m_imm, meta_item, method, named_field, or};
19+
use ast::{pat, pat_ident, pat_wild, public, pure_fn, re_anon, 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_path, ty_rptr};
22+
use ast::{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
@@ -138,7 +138,10 @@ fn fold_fn_decl(decl: ast::fn_decl, fld: ast_fold) -> ast::fn_decl {
138138
}
139139

140140
fn fold_ty_param_bound(tpb: ty_param_bound, fld: ast_fold) -> ty_param_bound {
141-
ty_param_bound(fld.fold_ty(*tpb))
141+
match tpb {
142+
TraitTyParamBound(ty) => TraitTyParamBound(fld.fold_ty(ty)),
143+
RegionTyParamBound => RegionTyParamBound
144+
}
142145
}
143146

144147
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};
@@ -2399,8 +2400,16 @@ impl Parser {
23992400
fn parse_optional_ty_param_bounds() -> @~[ty_param_bound] {
24002401
let mut bounds = ~[];
24012402
if self.eat(token::COLON) {
2402-
while is_ident(self.token) {
2403-
if is_ident(self.token) {
2403+
loop {
2404+
if self.eat(token::BINOP(token::AND)) {
2405+
if self.eat_keyword(~"static") {
2406+
bounds.push(RegionTyParamBound);
2407+
} else {
2408+
self.span_err(copy self.span,
2409+
~"`&static` is the only permissible \
2410+
region bound here");
2411+
}
2412+
} else if is_ident(self.token) {
24042413
let maybe_bound = match self.token {
24052414
token::IDENT(copy sid, _) => {
24062415
match *self.id_to_str(sid) {
@@ -2413,7 +2422,7 @@ impl Parser {
24132422
ObsoleteLowerCaseKindBounds);
24142423
// Bogus value, but doesn't matter, since
24152424
// is an error
2416-
Some(ty_param_bound(self.mk_ty_path(sid)))
2425+
Some(TraitTyParamBound(self.mk_ty_path(sid)))
24172426
}
24182427
24192428
_ => None
@@ -2428,11 +2437,12 @@ impl Parser {
24282437
bounds.push(bound);
24292438
}
24302439
None => {
2431-
bounds.push(ty_param_bound(self.parse_ty(false)));
2440+
let ty = self.parse_ty(false);
2441+
bounds.push(TraitTyParamBound(ty));
24322442
}
24332443
}
24342444
} else {
2435-
bounds.push(ty_param_bound(self.parse_ty(false)));
2445+
break;
24362446
}
24372447
}
24382448
}

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};
@@ -1787,9 +1787,12 @@ fn print_arg_mode(s: ps, m: ast::mode) {
17871787
fn print_bounds(s: ps, bounds: @~[ast::ty_param_bound]) {
17881788
if bounds.is_not_empty() {
17891789
word(s.s, ~":");
1790-
for vec::each(*bounds) |bound| {
1790+
for vec::each(*bounds) |&bound| {
17911791
nbsp(s);
1792-
print_type(s, **bound);
1792+
match bound {
1793+
TraitTyParamBound(ty) => print_type(s, ty),
1794+
RegionTyParamBound => word(s.s, ~"&static"),
1795+
}
17931796
}
17941797
}
17951798
}

src/libsyntax/visit.rs

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

285285
fn visit_ty_param_bounds<E>(bounds: @~[ty_param_bound], e: E, v: vt<E>) {
286-
for vec::each(*bounds) |bound| {
287-
(v.visit_ty)(**bound, e, v)
286+
for bounds.each |&bound| {
287+
match bound {
288+
TraitTyParamBound(ty) => (v.visit_ty)(ty, e, v),
289+
RegionTyParamBound => ()
290+
}
288291
}
289292
}
290293

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+

0 commit comments

Comments
 (0)