]> git.lizzy.rs Git - rust.git/commitdiff
make compiler emit more than 1 error in the case of unsafe
authorNiko Matsakis <niko@alum.mit.edu>
Wed, 12 Oct 2011 04:21:52 +0000 (21:21 -0700)
committerBrian Anderson <banderson@mozilla.com>
Wed, 12 Oct 2011 23:33:07 +0000 (16:33 -0700)
src/comp/middle/typeck.rs
src/test/compile-fail/unsafe-fn-deref-ptr.rs
src/test/stdtest/str.rs

index e18e5c2c63ea9dfd197a3a4d96f298550f4451f3..aed017eeef56e10d9c6ecd386be2c093dae7aa7d 100644 (file)
@@ -1528,7 +1528,7 @@ fn require_unsafe(sess: session::session, f_purity: ast::purity, sp: span) {
         alt f_purity {
           ast::unsafe_fn. { ret; }
           _ {
-            sess.span_fatal(
+            sess.span_err(
                 sp,
                 "unsafe operation requires unsafe function or block");
           }
@@ -1541,7 +1541,7 @@ fn require_impure(sess: session::session, f_purity: ast::purity, sp: span) {
       ast::unsafe_fn. { ret; }
       ast::impure_fn. { ret; }
       ast::pure_fn. {
-        sess.span_fatal(sp, "Found impure expression in pure function decl");
+        sess.span_err(sp, "Found impure expression in pure function decl");
       }
     }
 }
@@ -1556,7 +1556,7 @@ fn require_pure_call(ccx: @crate_ctxt, caller_purity: ast::purity,
           some(ast::def_fn(_, ast::unsafe_fn.)) |
           some(ast::def_native_fn(_, ast::unsafe_fn.)) {
             if sess.get_opts().check_unsafe {
-                ccx.tcx.sess.span_fatal(
+                ccx.tcx.sess.span_err(
                     sp,
                     "safe function calls function marked unsafe");
             }
@@ -1570,7 +1570,7 @@ fn require_pure_call(ccx: @crate_ctxt, caller_purity: ast::purity,
         alt ccx.tcx.def_map.find(callee.id) {
           some(ast::def_fn(_, ast::pure_fn.)) { ret; }
           _ {
-            ccx.tcx.sess.span_fatal
+            ccx.tcx.sess.span_err
                 (sp, "pure function calls function not known to be pure");
           }
         }
@@ -1591,23 +1591,6 @@ fn check_expr_with(fcx: @fn_ctxt, expr: @ast::expr, expected: ty::t) -> bool {
     ret check_expr_with_unifier(fcx, expr, demand::simple, expected);
 }
 
-fn check_for_unsafe_assignments(fcx: @fn_ctxt, lhs: @ast::expr) {
-    alt lhs.node {
-      ast::expr_unary(ast::deref., ptr) {
-        let ty = expr_ty(fcx.ccx.tcx, ptr);
-        let sty = structure_of(fcx, ptr.span, ty);
-        alt sty {
-          ty::ty_ptr(_) {
-            require_unsafe(fcx.ccx.tcx.sess, fcx.purity, lhs.span);
-          }
-          _ {}
-        }
-      }
-      _ {
-      }
-    }
-}
-
 fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
                            expected: ty::t) -> bool {
     //log_err "typechecking expr " + syntax::print::pprust::expr_to_str(expr);
@@ -1719,7 +1702,6 @@ fn check_assignment(fcx: @fn_ctxt, _sp: span, lhs: @ast::expr,
                         rhs: @ast::expr, id: ast::node_id) -> bool {
         let t = next_ty_var(fcx);
         let bot = check_expr_with(fcx, lhs, t) | check_expr_with(fcx, rhs, t);
-        check_for_unsafe_assignments(fcx, lhs);
         write::ty_only_fixup(fcx, id, ty::mk_nil(fcx.ccx.tcx));
         ret bot;
     }
@@ -1872,7 +1854,10 @@ fn check_binop_type_compat(fcx: @fn_ctxt, span: span, ty: ty::t,
                 oper_t =
                     ty::substitute_type_params(tcx, tps, variants[0].args[0]);
               }
-              ty::ty_ptr(inner) { oper_t = inner.ty; }
+              ty::ty_ptr(inner) {
+                oper_t = inner.ty;
+                require_unsafe(fcx.ccx.tcx.sess, fcx.purity, expr.span);
+              }
               _ {
                 tcx.sess.span_fatal(expr.span,
                                     "dereferencing non-" +
index cf5dac3ff89f40aa64130229b5fea14296f01690..238acc8729c2b8103ddcd3e416da23437d1bff46 100644 (file)
@@ -1,9 +1,8 @@
 // -*- rust -*-
 // error-pattern: unsafe operation requires unsafe function or block
 
-fn f(p: *u8) {
-    *p = 0u8;
-    ret;
+fn f(p: *u8) -> u8 {
+    ret *p;
 }
 
 fn main() {
index aa3a61f3dd51553c1468ea8338c9946d2aa66ad5..470c8daee5e57350b17112c02ff1978db5308007 100644 (file)
@@ -264,14 +264,14 @@ fn str_from_cstr() unsafe {
 }
 
 #[test]
-fn as_buf() {
+fn as_buf() unsafe {
     let a = "Abcdefg";
     let b = str::as_buf(a, {|buf| assert (*buf == 65u8); 100 });
     assert (b == 100);
 }
 
 #[test]
-fn as_buf_small() {
+fn as_buf_small() unsafe {
     let a = "A";
     let b = str::as_buf(a, {|buf| assert (*buf == 65u8); 100 });
     assert (b == 100);