Skip to content

Commit f3c68e7

Browse files
nikomatsakisbrson
authored andcommitted
enable unsafe checking but only with a flag --check-unsafe
1 parent cbe8da0 commit f3c68e7

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/comp/driver/rustc.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ options:
264264
--test build test harness
265265
--gc garbage collect shared data (experimental/temporary)
266266
--stack-growth perform stack checks (experimental)
267+
--check-unsafe disallow unsafe actions in non-unsafe functions (temporary option)
267268
268269
");
269270
}
@@ -322,6 +323,7 @@ fn build_session_options(match: getopts::match)
322323

323324
let parse_only = opt_present(match, "parse-only");
324325
let no_trans = opt_present(match, "no-trans");
326+
let check_unsafe = opt_present(match, "check-unsafe");
325327

326328
let output_type =
327329
if parse_only || no_trans {
@@ -393,7 +395,8 @@ fn build_session_options(match: getopts::match)
393395
parse_only: parse_only,
394396
no_trans: no_trans,
395397
do_gc: do_gc,
396-
stack_growth: stack_growth};
398+
stack_growth: stack_growth,
399+
check_unsafe: check_unsafe};
397400
ret sopts;
398401
}
399402

@@ -432,7 +435,7 @@ fn opts() -> [getopts::opt] {
432435
optflag("no-typestate"), optflag("noverify"),
433436
optmulti("cfg"), optflag("test"),
434437
optflag("lib"), optflag("static"), optflag("gc"),
435-
optflag("stack-growth")];
438+
optflag("stack-growth"), optflag("check-unsafe")];
436439
}
437440

438441
fn main(args: [str]) {

src/comp/driver/session.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ type options =
4141
parse_only: bool,
4242
no_trans: bool,
4343
do_gc: bool,
44-
stack_growth: bool};
44+
stack_growth: bool,
45+
check_unsafe: bool};
4546

4647
type crate_metadata = {name: str, data: [u8]};
4748

src/comp/middle/typeck.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,11 +1524,13 @@ fn check_pat(fcx: @fn_ctxt, map: ast_util::pat_id_map, pat: @ast::pat,
15241524
}
15251525

15261526
fn require_unsafe(sess: session::session, f_purity: ast::purity, sp: span) {
1527-
alt f_purity {
1528-
ast::unsafe_fn. { ret; }
1529-
_ {
1530-
sess.span_fatal(sp, "Found unsafe expression in safe function decl");
1531-
}
1527+
if sess.get_opts().check_unsafe {
1528+
alt f_purity {
1529+
ast::unsafe_fn. { ret; }
1530+
_ {
1531+
sess.span_fatal(sp, "Found unsafe expression in safe function decl");
1532+
}
1533+
}
15321534
}
15331535
}
15341536

@@ -1547,17 +1549,22 @@ fn require_pure_call(ccx: @crate_ctxt, caller_purity: ast::purity,
15471549
alt caller_purity {
15481550
ast::unsafe_fn. { ret; }
15491551
ast::impure_fn. {
1552+
let sess = ccx.tcx.sess;
15501553
alt ccx.tcx.def_map.find(callee.id) {
15511554
some(ast::def_fn(_, ast::unsafe_fn.)) {
1552-
ccx.tcx.sess.span_fatal
1553-
(sp, "safe function calls function marked unsafe");
1555+
if sess.get_opts().check_unsafe {
1556+
ccx.tcx.sess.span_fatal(
1557+
sp,
1558+
"safe function calls function marked unsafe");
1559+
}
15541560
}
1555-
/* Temporarily disable until unsafe blocks parse!
15561561
some(ast::def_native_fn(_)) {
1557-
ccx.tcx.sess.span_fatal
1558-
(sp, "native functions can only be invoked from unsafe code");
1562+
if sess.get_opts().check_unsafe {
1563+
ccx.tcx.sess.span_fatal(
1564+
sp,
1565+
"native functions can only be invoked from unsafe code");
1566+
}
15591567
}
1560-
*/
15611568
_ {
15621569
}
15631570
}

0 commit comments

Comments
 (0)