Skip to content

Commit 1841b31

Browse files
committed
Add 'static mut' items to the language
1 parent f827561 commit 1841b31

34 files changed

+230
-70
lines changed

src/librustc/metadata/decoder.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ fn lookup_item(item_id: int, data: @~[u8]) -> ebml::Doc {
9797

9898
#[deriving(Eq)]
9999
enum Family {
100-
Const, // c
100+
ImmStatic, // c
101+
MutStatic, // b
101102
Fn, // f
102103
UnsafeFn, // u
103104
PureFn, // p
@@ -122,7 +123,8 @@ enum Family {
122123
fn item_family(item: ebml::Doc) -> Family {
123124
let fam = reader::get_doc(item, tag_items_data_item_family);
124125
match reader::doc_as_u8(fam) as char {
125-
'c' => Const,
126+
'c' => ImmStatic,
127+
'b' => MutStatic,
126128
'f' => Fn,
127129
'u' => UnsafeFn,
128130
'p' => PureFn,
@@ -321,7 +323,8 @@ fn item_to_def_like(item: ebml::Doc, did: ast::def_id, cnum: ast::crate_num)
321323
-> def_like {
322324
let fam = item_family(item);
323325
match fam {
324-
Const => dl_def(ast::def_const(did)),
326+
ImmStatic => dl_def(ast::def_static(did, false)),
327+
MutStatic => dl_def(ast::def_static(did, true)),
325328
Struct => dl_def(ast::def_struct(did)),
326329
UnsafeFn => dl_def(ast::def_fn(did, ast::unsafe_fn)),
327330
Fn => dl_def(ast::def_fn(did, ast::impure_fn)),
@@ -900,8 +903,8 @@ pub fn get_item_visibility(cdata: cmd, id: ast::node_id)
900903

901904
fn family_has_type_params(fam: Family) -> bool {
902905
match fam {
903-
Const | ForeignType | Mod | ForeignMod | PublicField | PrivateField
904-
| ForeignFn => false,
906+
ImmStatic | ForeignType | Mod | ForeignMod | PublicField | PrivateField
907+
| ForeignFn | MutStatic => false,
905908
_ => true
906909
}
907910
}
@@ -931,7 +934,8 @@ fn describe_def(items: ebml::Doc, id: ast::def_id) -> ~str {
931934

932935
fn item_family_to_str(fam: Family) -> ~str {
933936
match fam {
934-
Const => ~"const",
937+
ImmStatic => ~"static",
938+
MutStatic => ~"static mut",
935939
Fn => ~"fn",
936940
UnsafeFn => ~"unsafe fn",
937941
PureFn => ~"pure fn",

src/librustc/metadata/encoder.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
785785
let must_write =
786786
match item.node {
787787
item_enum(_, _) | item_impl(*) | item_trait(*) | item_struct(*) |
788-
item_mod(*) | item_foreign_mod(*) | item_const(*) => true,
788+
item_mod(*) | item_foreign_mod(*) | item_static(*) => true,
789789
_ => false
790790
};
791791
if !must_write && !reachable(ecx, item.id) { return; }
@@ -800,11 +800,15 @@ fn encode_info_for_item(ecx: &EncodeContext,
800800
ecx.tcx.sess.codemap.span_to_str(item.span));
801801

802802
match item.node {
803-
item_const(_, _) => {
803+
item_static(_, m, _) => {
804804
add_to_index();
805805
ebml_w.start_tag(tag_items_data_item);
806806
encode_def_id(ebml_w, local_def(item.id));
807-
encode_family(ebml_w, 'c');
807+
if m == ast::m_mutbl {
808+
encode_family(ebml_w, 'b');
809+
} else {
810+
encode_family(ebml_w, 'c');
811+
}
808812
encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id));
809813
encode_symbol(ecx, ebml_w, item.id);
810814
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));

src/librustc/middle/astencode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl tr for ast::def {
384384
ast::def_self(nid, i) => { ast::def_self(xcx.tr_id(nid), i) }
385385
ast::def_mod(did) => { ast::def_mod(did.tr(xcx)) }
386386
ast::def_foreign_mod(did) => { ast::def_foreign_mod(did.tr(xcx)) }
387-
ast::def_const(did) => { ast::def_const(did.tr(xcx)) }
387+
ast::def_static(did, m) => { ast::def_static(did.tr(xcx), m) }
388388
ast::def_arg(nid, b) => { ast::def_arg(xcx.tr_id(nid), b) }
389389
ast::def_local(nid, b) => { ast::def_local(xcx.tr_id(nid), b) }
390390
ast::def_variant(e_did, v_did) => {

src/librustc/middle/check_const.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn check_item(sess: Session,
4343
(_is_const, v): (bool,
4444
visit::vt<bool>)) {
4545
match it.node {
46-
item_const(_, ex) => {
46+
item_static(_, _, ex) => {
4747
(v.visit_expr)(ex, (true, v));
4848
check_item_recursion(sess, ast_map, def_map, it);
4949
}
@@ -124,7 +124,7 @@ pub fn check_expr(sess: Session,
124124
items without type parameters");
125125
}
126126
match def_map.find(&e.id) {
127-
Some(&def_const(_)) |
127+
Some(&def_static(*)) |
128128
Some(&def_fn(_, _)) |
129129
Some(&def_variant(_, _)) |
130130
Some(&def_struct(_)) => { }
@@ -237,7 +237,7 @@ pub fn check_item_recursion(sess: Session,
237237
match e.node {
238238
expr_path(*) => {
239239
match env.def_map.find(&e.id) {
240-
Some(&def_const(def_id)) => {
240+
Some(&def_static(def_id, _)) => {
241241
if ast_util::is_local(def_id) {
242242
match env.ast_map.get_copy(&def_id.node) {
243243
ast_map::node_item(it, _) => {

src/librustc/middle/check_match.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ pub fn pat_ctor_id(cx: @MatchCheckCtxt, p: @pat) -> Option<ctor> {
304304
pat_ident(_, _, _) | pat_enum(_, _) => {
305305
match cx.tcx.def_map.find(&pat.id) {
306306
Some(&def_variant(_, id)) => Some(variant(id)),
307-
Some(&def_const(did)) => {
307+
Some(&def_static(did, false)) => {
308308
let const_expr = lookup_const_by_id(cx.tcx, did).get();
309309
Some(val(eval_const_expr(cx.tcx, const_expr)))
310310
}
@@ -339,7 +339,7 @@ pub fn is_wild(cx: @MatchCheckCtxt, p: @pat) -> bool {
339339
pat_wild => { true }
340340
pat_ident(_, _, _) => {
341341
match cx.tcx.def_map.find(&pat.id) {
342-
Some(&def_variant(_, _)) | Some(&def_const(*)) => { false }
342+
Some(&def_variant(_, _)) | Some(&def_static(*)) => { false }
343343
_ => { true }
344344
}
345345
}
@@ -499,7 +499,7 @@ pub fn specialize(cx: @MatchCheckCtxt,
499499
None
500500
}
501501
}
502-
Some(&def_const(did)) => {
502+
Some(&def_static(did, _)) => {
503503
let const_expr =
504504
lookup_const_by_id(cx.tcx, did).get();
505505
let e_v = eval_const_expr(cx.tcx, const_expr);
@@ -549,7 +549,7 @@ pub fn specialize(cx: @MatchCheckCtxt,
549549
}
550550
pat_enum(_, args) => {
551551
match cx.tcx.def_map.get_copy(&pat_id) {
552-
def_const(did) => {
552+
def_static(did, _) => {
553553
let const_expr =
554554
lookup_const_by_id(cx.tcx, did).get();
555555
let e_v = eval_const_expr(cx.tcx, const_expr);
@@ -790,7 +790,7 @@ pub fn is_refutable(cx: @MatchCheckCtxt, pat: &pat) -> bool {
790790
return true;
791791
}
792792
}
793-
Some(&def_const(*)) => return true,
793+
Some(&def_static(*)) => return true,
794794
_ => ()
795795
}
796796

src/librustc/middle/const_eval.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ pub fn classify(e: @expr,
166166

167167
pub fn lookup_const(tcx: ty::ctxt, e: @expr) -> Option<@expr> {
168168
match tcx.def_map.find(&e.id) {
169-
Some(&ast::def_const(def_id)) => lookup_const_by_id(tcx, def_id),
169+
Some(&ast::def_static(def_id, false)) => lookup_const_by_id(tcx, def_id),
170170
_ => None
171171
}
172172
}
@@ -178,7 +178,7 @@ pub fn lookup_const_by_id(tcx: ty::ctxt,
178178
match tcx.items.find(&def_id.node) {
179179
None => None,
180180
Some(&ast_map::node_item(it, _)) => match it.node {
181-
item_const(_, const_expr) => Some(const_expr),
181+
item_static(_, ast::m_imm, const_expr) => Some(const_expr),
182182
_ => None
183183
},
184184
Some(_) => None
@@ -195,7 +195,7 @@ pub fn lookup_const_by_id(tcx: ty::ctxt,
195195
match csearch::maybe_get_item_ast(tcx, def_id,
196196
|a, b, c, d| astencode::decode_inlined_item(a, b, maps, /*bar*/ copy c, d)) {
197197
csearch::found(ast::ii_item(item)) => match item.node {
198-
item_const(_, const_expr) => Some(const_expr),
198+
item_static(_, ast::m_imm, const_expr) => Some(const_expr),
199199
_ => None
200200
},
201201
_ => None

src/librustc/middle/effect.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use middle::typeck::method_map;
1717
use util::ppaux;
1818

1919
use syntax::ast::{deref, expr_call, expr_inline_asm, expr_method_call};
20-
use syntax::ast::{expr_unary, node_id, unsafe_blk, unsafe_fn};
20+
use syntax::ast::{expr_unary, node_id, unsafe_blk, unsafe_fn, expr_path};
2121
use syntax::ast;
2222
use syntax::codemap::span;
2323
use syntax::visit::{fk_item_fn, fk_method};
@@ -143,6 +143,14 @@ pub fn check_crate(tcx: ty::ctxt,
143143
expr_inline_asm(*) => {
144144
require_unsafe(expr.span, "use of inline assembly")
145145
}
146+
expr_path(*) => {
147+
match ty::resolve_expr(tcx, expr) {
148+
ast::def_static(_, true) => {
149+
require_unsafe(expr.span, "use of mutable static")
150+
}
151+
_ => {}
152+
}
153+
}
146154
_ => {}
147155
}
148156

src/librustc/middle/mem_categorization.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -447,19 +447,29 @@ impl mem_categorization_ctxt {
447447
-> cmt {
448448
match def {
449449
ast::def_fn(*) | ast::def_static_method(*) | ast::def_mod(_) |
450-
ast::def_foreign_mod(_) | ast::def_const(_) |
450+
ast::def_foreign_mod(_) | ast::def_static(_, false) |
451451
ast::def_use(_) | ast::def_variant(*) |
452452
ast::def_trait(_) | ast::def_ty(_) | ast::def_prim_ty(_) |
453453
ast::def_ty_param(*) | ast::def_struct(*) |
454454
ast::def_typaram_binder(*) | ast::def_region(_) |
455455
ast::def_label(_) | ast::def_self_ty(*) => {
456-
@cmt_ {
457-
id:id,
458-
span:span,
459-
cat:cat_static_item,
460-
mutbl: McImmutable,
461-
ty:expr_ty
462-
}
456+
@cmt_ {
457+
id:id,
458+
span:span,
459+
cat:cat_static_item,
460+
mutbl: McImmutable,
461+
ty:expr_ty
462+
}
463+
}
464+
465+
ast::def_static(_, true) => {
466+
@cmt_ {
467+
id:id,
468+
span:span,
469+
cat:cat_static_item,
470+
mutbl: McDeclared,
471+
ty:expr_ty
472+
}
463473
}
464474

465475
ast::def_arg(vid, mutbl) => {
@@ -894,7 +904,7 @@ impl mem_categorization_ctxt {
894904
self.cat_pattern(cmt_field, subpat, op);
895905
}
896906
}
897-
Some(&ast::def_const(*)) => {
907+
Some(&ast::def_static(*)) => {
898908
for subpats.iter().advance |&subpat| {
899909
self.cat_pattern(cmt, subpat, op);
900910
}

src/librustc/middle/pat_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn pat_is_const(dm: resolve::DefMap, pat: &pat) -> bool {
4545
match pat.node {
4646
pat_ident(_, _, None) | pat_enum(*) => {
4747
match dm.find(&pat.id) {
48-
Some(&def_const(*)) => true,
48+
Some(&def_static(_, false)) => true,
4949
_ => false
5050
}
5151
}

src/librustc/middle/resolve.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,12 +1146,13 @@ impl Resolver {
11461146
}
11471147

11481148
// These items live in the value namespace.
1149-
item_const(*) => {
1149+
item_static(_, m, _) => {
11501150
let (name_bindings, _) =
11511151
self.add_child(ident, parent, ForbidDuplicateValues, sp);
1152+
let mutbl = m == ast::m_mutbl;
11521153

11531154
name_bindings.define_value
1154-
(privacy, def_const(local_def(item.id)), sp);
1155+
(privacy, def_static(local_def(item.id), mutbl), sp);
11551156
}
11561157
item_fn(_, purity, _, _, _) => {
11571158
let (name_bindings, new_parent) =
@@ -1566,7 +1567,7 @@ impl Resolver {
15661567
}
15671568
}
15681569
foreign_item_const(*) => {
1569-
let def = def_const(local_def(foreign_item.id));
1570+
let def = def_static(local_def(foreign_item.id), false);
15701571
name_bindings.define_value(Public, def, foreign_item.span);
15711572

15721573
visit_foreign_item(foreign_item, (new_parent, visitor));
@@ -1673,7 +1674,7 @@ impl Resolver {
16731674
let privacy = variant_visibility_to_privacy(visibility, true);
16741675
child_name_bindings.define_value(privacy, def, dummy_sp());
16751676
}
1676-
def_fn(*) | def_static_method(*) | def_const(*) => {
1677+
def_fn(*) | def_static_method(*) | def_static(*) => {
16771678
debug!("(building reduced graph for external \
16781679
crate) building value %s", final_ident);
16791680
child_name_bindings.define_value(privacy, def, dummy_sp());
@@ -3686,7 +3687,7 @@ impl Resolver {
36863687
visitor);
36873688
}
36883689

3689-
item_const(*) => {
3690+
item_static(*) => {
36903691
self.with_constant_rib(|| {
36913692
visit_item(item, ((), visitor));
36923693
});
@@ -4344,7 +4345,7 @@ impl Resolver {
43444345
Some(def @ def_struct(*)) => {
43454346
self.record_def(pattern.id, def);
43464347
}
4347-
Some(def @ def_const(*)) => {
4348+
Some(def @ def_static(*)) => {
43484349
self.enforce_default_binding_mode(
43494350
pattern,
43504351
binding_mode,
@@ -4376,7 +4377,7 @@ impl Resolver {
43764377
Some(def @ def_fn(*)) |
43774378
Some(def @ def_variant(*)) |
43784379
Some(def @ def_struct(*)) |
4379-
Some(def @ def_const(*)) => {
4380+
Some(def @ def_static(*)) => {
43804381
self.record_def(pattern.id, def);
43814382
}
43824383
Some(_) => {
@@ -4459,7 +4460,7 @@ impl Resolver {
44594460
def @ def_variant(*) | def @ def_struct(*) => {
44604461
return FoundStructOrEnumVariant(def);
44614462
}
4462-
def @ def_const(*) => {
4463+
def @ def_static(_, false) => {
44634464
return FoundConst(def);
44644465
}
44654466
_ => {

src/librustc/middle/trans/_match.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ pub fn get_options(bcx: block, m: &[@Match], col: uint) -> ~[Opt] {
820820
add_to_set(ccx.tcx, &mut found,
821821
lit(UnitLikeStructLit(cur.id)));
822822
}
823-
Some(&ast::def_const(const_did)) => {
823+
Some(&ast::def_static(const_did, false)) => {
824824
add_to_set(ccx.tcx, &mut found,
825825
lit(ConstLit(const_did)));
826826
}
@@ -836,7 +836,7 @@ pub fn get_options(bcx: block, m: &[@Match], col: uint) -> ~[Opt] {
836836
add_to_set(ccx.tcx, &mut found,
837837
variant_opt(bcx, cur.id));
838838
}
839-
Some(&ast::def_const(const_did)) => {
839+
Some(&ast::def_static(const_did, false)) => {
840840
add_to_set(ccx.tcx, &mut found,
841841
lit(ConstLit(const_did)));
842842
}
@@ -1831,8 +1831,9 @@ pub fn bind_irrefutable_pat(bcx: block,
18311831
}
18321832
}
18331833
}
1834-
Some(&ast::def_const(*)) => {
1835-
bcx = bind_irrefutable_pat(bcx, pat, val, make_copy, binding_mode);
1834+
Some(&ast::def_static(_, false)) => {
1835+
bcx = bind_irrefutable_pat(bcx, pat, val, make_copy,
1836+
binding_mode);
18361837
}
18371838
_ => {
18381839
// Nothing to do here.

0 commit comments

Comments
 (0)