diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index e18e5c2c63ea..aed017eeef56 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -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_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier, 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_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier, 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-" + diff --git a/src/test/compile-fail/unsafe-fn-deref-ptr.rs b/src/test/compile-fail/unsafe-fn-deref-ptr.rs index cf5dac3ff89f..238acc8729c2 100644 --- a/src/test/compile-fail/unsafe-fn-deref-ptr.rs +++ b/src/test/compile-fail/unsafe-fn-deref-ptr.rs @@ -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() { diff --git a/src/test/stdtest/str.rs b/src/test/stdtest/str.rs index aa3a61f3dd51..470c8daee5e5 100644 --- a/src/test/stdtest/str.rs +++ b/src/test/stdtest/str.rs @@ -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);