]> git.lizzy.rs Git - rust.git/commitdiff
Support foreign 'static mut' variables as well
authorAlex Crichton <alex@alexcrichton.com>
Sat, 22 Jun 2013 05:46:27 +0000 (22:46 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 24 Jun 2013 01:00:32 +0000 (18:00 -0700)
16 files changed:
src/librustc/metadata/encoder.rs
src/librustc/middle/lint.rs
src/librustc/middle/resolve.rs
src/librustc/middle/trans/base.rs
src/librustc/middle/trans/foreign.rs
src/librustc/middle/typeck/collect.rs
src/librustdoc/extract.rs
src/libsyntax/ast.rs
src/libsyntax/fold.rs
src/libsyntax/parse/parser.rs
src/libsyntax/print/pprust.rs
src/libsyntax/visit.rs
src/rt/rust_builtin.cpp
src/rt/rustrt.def.in
src/test/compile-fail/static-mut-foreign-requires-unsafe.rs [new file with mode: 0644]
src/test/run-pass/static-mut-foreign.rs [new file with mode: 0644]

index 96cf7284169d0be4c89cc73cea919a68a6ad160e..7d18bdb77efef27c3c0942860eca43556216255f 100644 (file)
@@ -1111,9 +1111,13 @@ fn encode_info_for_foreign_item(ecx: &EncodeContext,
         }
         encode_path(ecx, ebml_w, path, ast_map::path_name(nitem.ident));
       }
-      foreign_item_const(*) => {
+      foreign_item_static(_, mutbl) => {
         encode_def_id(ebml_w, local_def(nitem.id));
-        encode_family(ebml_w, 'c');
+        if mutbl {
+            encode_family(ebml_w, 'b');
+        } else {
+            encode_family(ebml_w, 'c');
+        }
         encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, nitem.id));
         encode_symbol(ecx, ebml_w, nitem.id);
         encode_path(ecx, ebml_w, path, ast_map::path_name(nitem.ident));
index 821aed731c2298d108cd36308e26d721b90b5713..17ff5930078df40a27580b3556ac90562b3aa5a0 100644 (file)
@@ -709,28 +709,31 @@ fn check_item_default_methods(cx: &Context, item: @ast::item) {
 }
 
 fn check_item_ctypes(cx: &Context, it: @ast::item) {
+    fn check_ty(cx: &Context, ty: @ast::Ty) {
+        match ty.node {
+            ast::ty_path(_, _, id) => {
+                match cx.tcx.def_map.get_copy(&id) {
+                    ast::def_prim_ty(ast::ty_int(ast::ty_i)) => {
+                        cx.span_lint(ctypes, ty.span,
+                                "found rust type `int` in foreign module, while \
+                                libc::c_int or libc::c_long should be used");
+                    }
+                    ast::def_prim_ty(ast::ty_uint(ast::ty_u)) => {
+                        cx.span_lint(ctypes, ty.span,
+                                "found rust type `uint` in foreign module, while \
+                                libc::c_uint or libc::c_ulong should be used");
+                    }
+                    _ => ()
+                }
+            }
+            _ => ()
+        }
+    }
 
     fn check_foreign_fn(cx: &Context, decl: &ast::fn_decl) {
         let tys = vec::map(decl.inputs, |a| a.ty );
         for vec::each(vec::append_one(tys, decl.output)) |ty| {
-            match ty.node {
-              ast::ty_path(_, _, id) => {
-                match cx.tcx.def_map.get_copy(&id) {
-                  ast::def_prim_ty(ast::ty_int(ast::ty_i)) => {
-                    cx.span_lint(ctypes, ty.span,
-                        "found rust type `int` in foreign module, while \
-                         libc::c_int or libc::c_long should be used");
-                  }
-                  ast::def_prim_ty(ast::ty_uint(ast::ty_u)) => {
-                    cx.span_lint(ctypes, ty.span,
-                        "found rust type `uint` in foreign module, while \
-                         libc::c_uint or libc::c_ulong should be used");
-                  }
-                  _ => ()
-                }
-              }
-              _ => ()
-            }
+            check_ty(cx, *ty);
         }
     }
 
@@ -738,11 +741,10 @@ fn check_foreign_fn(cx: &Context, decl: &ast::fn_decl) {
       ast::item_foreign_mod(ref nmod) if !nmod.abis.is_intrinsic() => {
         for nmod.items.iter().advance |ni| {
             match ni.node {
-              ast::foreign_item_fn(ref decl, _, _) => {
-                check_foreign_fn(cx, decl);
-              }
-              // FIXME #4622: Not implemented.
-              ast::foreign_item_const(*) => {}
+                ast::foreign_item_fn(ref decl, _, _) => {
+                    check_foreign_fn(cx, decl);
+                }
+                ast::foreign_item_static(t, _) => { check_ty(cx, t); }
             }
         }
       }
index 7f9086be81d52646a27ec12456dc20c780d2ad8b..ed385ae542154c580111d7cd51497a9a69b0235f 100644 (file)
@@ -1566,8 +1566,8 @@ pub fn build_reduced_graph_for_foreign_item(@mut self,
                     visit_foreign_item(foreign_item, (new_parent, visitor));
                 }
             }
-            foreign_item_const(*) => {
-                let def = def_static(local_def(foreign_item.id), false);
+            foreign_item_static(_, m) => {
+                let def = def_static(local_def(foreign_item.id), m);
                 name_bindings.define_value(Public, def, foreign_item.span);
 
                 visit_foreign_item(foreign_item, (new_parent, visitor));
@@ -3665,7 +3665,7 @@ pub fn resolve_item(@mut self, item: @item, visitor: ResolveVisitor) {
                                     || visit_foreign_item(*foreign_item,
                                                           ((), visitor)));
                             }
-                            foreign_item_const(_) => {
+                            foreign_item_static(*) => {
                                 visit_foreign_item(*foreign_item,
                                                    ((), visitor));
                             }
index 0f6c7dbe75434dec365de1c55d3dd73f1d95919f..0e322c187af2b02c2cd61a5d406b027216090fed 100644 (file)
@@ -2463,7 +2463,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::node_id) -> ValueRef {
                                 ni.id,
                                 ni.attrs)
                 }
-                ast::foreign_item_const(*) => {
+                ast::foreign_item_static(*) => {
                     let typ = ty::node_id_to_type(ccx.tcx, ni.id);
                     let ident = token::ident_to_str(&ni.ident);
                     let g = do str::as_c_str(ident) |buf| {
index 10e63e6af77786cbb4fb1490c7d6e0800caf92b5..54bfc25244f0d3cc1fcf51e42b2b44e8ab867622 100644 (file)
@@ -332,7 +332,7 @@ pub fn trans_foreign_mod(ccx: @mut CrateContext,
                     }
                 }
             }
-            ast::foreign_item_const(*) => {
+            ast::foreign_item_static(*) => {
                 let ident = token::ident_to_str(&foreign_item.ident);
                 ccx.item_symbols.insert(foreign_item.id, /* bad */ident.to_owned());
             }
index aef148830a99389fad86efae5edae71f4975a502..33e483e552a74a451f222a81148c093be6afdb1e 100644 (file)
@@ -1153,7 +1153,7 @@ pub fn ty_of_foreign_item(ccx: &CrateCtxt,
                                   generics,
                                   abis)
         }
-        ast::foreign_item_const(t) => {
+        ast::foreign_item_static(t, _) => {
             ty::ty_param_bounds_and_ty {
                 generics: ty::Generics {
                     type_param_defs: @~[],
index f12f612b036bce0f834fa466bbd5946641ce6de0..b7b2b70769bbdb5c6b37a5cf1ffccef5c68cb57e 100644 (file)
@@ -150,7 +150,7 @@ fn nmoddoc_from_mod(
           ast::foreign_item_fn(*) => {
             fns.push(fndoc_from_fn(ItemDoc));
           }
-          ast::foreign_item_const(*) => {} // XXX: Not implemented.
+          ast::foreign_item_static(*) => {} // XXX: Not implemented.
         }
     }
     doc::NmodDoc {
index 012a1e76228a5fbb3e45e3fac04e18c3698bcaab..c7f3b41475f0723f3ab2d5ad6864cda6ce34f4a6 100644 (file)
@@ -1124,7 +1124,7 @@ pub struct foreign_item {
 #[deriving(Eq, Encodable, Decodable)]
 pub enum foreign_item_ {
     foreign_item_fn(fn_decl, purity, Generics),
-    foreign_item_const(@Ty)
+    foreign_item_static(@Ty, /* is_mutbl */ bool),
 }
 
 // The data we save and restore about an inlined item or method.  This is not
index 8a22dbe9178c42eff026378e05ef26ad96e7ebbb..25839fb46339608117acf12a461ba28df2191385 100644 (file)
@@ -236,8 +236,8 @@ fn noop_fold_foreign_item(ni: @foreign_item, fld: @ast_fold)
                         purity,
                         fold_generics(generics, fld))
                 }
-                foreign_item_const(t) => {
-                    foreign_item_const(fld.fold_ty(t))
+                foreign_item_static(t, m) => {
+                    foreign_item_static(fld.fold_ty(t), m)
                 }
             },
         id: fld.new_id(ni.id),
index a726b3b27d2ed348c083c015f4358f5aa6026c9f..cc4a1f4572267af9bae2e47c1f0c9bd28ecfd202 100644 (file)
@@ -33,7 +33,7 @@
 use ast::{expr_vstore_slice, expr_vstore_box};
 use ast::{expr_vstore_mut_slice, expr_while, extern_fn, field, fn_decl};
 use ast::{expr_vstore_uniq, Onceness, Once, Many};
-use ast::{foreign_item, foreign_item_const, foreign_item_fn, foreign_mod};
+use ast::{foreign_item, foreign_item_static, foreign_item_fn, foreign_mod};
 use ast::{ident, impure_fn, inherited, item, item_, item_static};
 use ast::{item_enum, item_fn, item_foreign_mod, item_impl};
 use ast::{item_mac, item_mod, item_struct, item_trait, item_ty, lit, lit_};
@@ -3684,6 +3684,7 @@ fn parse_item_foreign_const(&self, vis: ast::visibility,
         } else {
             self.expect_keyword(keywords::Static);
         }
+        let mutbl = self.eat_keyword(keywords::Mut);
 
         let ident = self.parse_ident();
         self.expect(&token::COLON);
@@ -3692,7 +3693,7 @@ fn parse_item_foreign_const(&self, vis: ast::visibility,
         self.expect(&token::SEMI);
         @ast::foreign_item { ident: ident,
                              attrs: attrs,
-                             node: foreign_item_const(ty),
+                             node: foreign_item_static(ty, mutbl),
                              id: self.get_id(),
                              span: mk_sp(lo, hi),
                              vis: vis }
index c21c0a0afeedf2e5c291c5ce03cc2e8c6434a555..1a3155337a5e3df6eb2f5f6f6c564bde38a4d67a 100644 (file)
@@ -458,8 +458,11 @@ pub fn print_foreign_item(s: @ps, item: @ast::foreign_item) {
         word(s.s, ";");
         end(s); // end the outer fn box
       }
-      ast::foreign_item_const(t) => {
+      ast::foreign_item_static(t, m) => {
         head(s, "static");
+        if m {
+            word_space(s, "mut");
+        }
         print_ident(s, item.ident);
         word_space(s, ":");
         print_type(s, t);
index fd9350e00051db612e393751b0c902c23933c0b5..d7914832835ac59c685f7b1b3a3053bee3366da8 100644 (file)
@@ -326,7 +326,7 @@ pub fn visit_foreign_item<E: Copy>(ni: @foreign_item, (e, v): (E, vt<E>)) {
             visit_fn_decl(fd, (copy e, v));
             (v.visit_generics)(generics, (e, v));
         }
-        foreign_item_const(t) => {
+        foreign_item_static(t, _) => {
             (v.visit_ty)(t, (e, v));
         }
     }
index e476fa0ad5e07aa9308be4e46c28102edd92df49..6ae5e978106e417abb84dc28cda2c959193c88a6 100644 (file)
@@ -154,6 +154,16 @@ debug_abi_2(floats f) {
     return ff;
 }
 
+extern "C" int
+debug_static_mut;
+
+int debug_static_mut = 3;
+
+extern "C" void
+debug_static_mut_check_four() {
+    assert(debug_static_mut == 4);
+}
+
 /* Debug builtins for std::dbg. */
 
 static void
index ba7ada04a2754fe78fd5f388432fc3b3644dec3b..505de6e20b700f3a65a911c5bfd1331509be6a53 100644 (file)
@@ -7,6 +7,8 @@ debug_tydesc
 debug_get_stk_seg
 debug_abi_1
 debug_abi_2
+debug_static_mut
+debug_static_mut_check_four
 get_task_id
 get_time
 rust_tzset
@@ -239,4 +241,4 @@ rust_valgrind_stack_deregister
 rust_take_env_lock
 rust_drop_env_lock
 rust_update_log_settings
-rust_running_on_valgrind
\ No newline at end of file
+rust_running_on_valgrind
diff --git a/src/test/compile-fail/static-mut-foreign-requires-unsafe.rs b/src/test/compile-fail/static-mut-foreign-requires-unsafe.rs
new file mode 100644 (file)
index 0000000..7b371cf
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::libc;
+
+extern {
+    static mut a: libc::c_int;
+}
+
+fn main() {
+    a += 3;     //~ ERROR: requires unsafe
+    a = 4;      //~ ERROR: requires unsafe
+    let _b = a; //~ ERROR: requires unsafe
+}
diff --git a/src/test/run-pass/static-mut-foreign.rs b/src/test/run-pass/static-mut-foreign.rs
new file mode 100644 (file)
index 0000000..66d34c7
--- /dev/null
@@ -0,0 +1,46 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Constants (static variables) can be used to match in patterns, but mutable
+// statics cannot. This ensures that there's some form of error if this is
+// attempted.
+
+use std::libc;
+
+#[nolink]
+extern {
+    static mut debug_static_mut: libc::c_int;
+    pub fn debug_static_mut_check_four();
+}
+
+unsafe fn static_bound(_: &'static libc::c_int) {}
+
+fn static_bound_set(a: &'static mut libc::c_int) {
+    *a = 3;
+}
+
+unsafe fn run() {
+    assert!(debug_static_mut == 3);
+    debug_static_mut = 4;
+    assert!(debug_static_mut == 4);
+    debug_static_mut_check_four();
+    debug_static_mut += 1;
+    assert!(debug_static_mut == 5);
+    debug_static_mut *= 3;
+    assert!(debug_static_mut == 15);
+    debug_static_mut = -3;
+    assert!(debug_static_mut == -3);
+    static_bound(&debug_static_mut);
+    static_bound_set(&mut debug_static_mut);
+}
+
+fn main() {
+    unsafe { run() }
+}