Skip to content

Commit

Permalink
Ensure that functions that should return a value do; issue 41
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed Jul 16, 2010
1 parent 4d413af commit 1ac01e1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/boot/me/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type ltype =

type fn_ctx = {
fnctx_return_type: Ast.ty;
fnctx_is_iter: bool
fnctx_is_iter: bool;
mutable fnctx_just_saw_ret: bool
}

exception Type_error of string * Ast.ty
Expand Down Expand Up @@ -627,10 +628,19 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
(* Again as above, we explicitly curry [fn_ctx] to avoid threading it
* through these functions. *)
let check_stmt (fn_ctx:fn_ctx) : (Ast.stmt -> unit) =
let check_ret (stmt:Ast.stmt) : unit =
fn_ctx.fnctx_just_saw_ret <-
match stmt.Common.node with
Ast.STMT_ret _ | Ast.STMT_be _ | Ast.STMT_fail
| Ast.STMT_yield _ -> true
| _ -> false
in

let rec check_block (block:Ast.block) : unit =
Array.iter check_stmt block.Common.node

and check_stmt (stmt:Ast.stmt) : unit =
check_ret stmt;
match stmt.Common.node with
Ast.STMT_spawn (dst, _, callee, args) ->
infer_lval Ast.TY_task dst;
Expand Down Expand Up @@ -841,7 +851,11 @@ let process_crate (cx:Semant.ctxt) (crate:Ast.crate) : unit =

let visitor (cx:Semant.ctxt) (inner:Walk.visitor) : Walk.visitor =
let push_fn_ctx (ret_ty:Ast.ty) (is_iter:bool) =
let fn_ctx = { fnctx_return_type = ret_ty; fnctx_is_iter = is_iter } in
let fn_ctx = {
fnctx_return_type = ret_ty;
fnctx_is_iter = is_iter;
fnctx_just_saw_ret = false
} in
Stack.push fn_ctx fn_ctx_stack
in

Expand All @@ -852,10 +866,19 @@ let process_crate (cx:Semant.ctxt) (crate:Ast.crate) : unit =
push_fn_ctx (Common.option_get ret_ty) is_iter
in

let finish_function (item_id:Common.node_id) =
let fn_ctx = Stack.pop fn_ctx_stack in
if not fn_ctx.fnctx_just_saw_ret &&
fn_ctx.fnctx_return_type <> Ast.TY_nil &&
not fn_ctx.fnctx_is_iter then
Common.err (Some item_id) "this function must return a value"
in

let visit_mod_item_pre _ _ item =
let { Common.node = item; Common.id = item_id } = item in
match item.Ast.decl_item with
Ast.MOD_ITEM_fn _ ->
Ast.MOD_ITEM_fn _ when
not (Hashtbl.mem cx.Semant.ctxt_required_items item_id) ->
let fn_ty = Hashtbl.find cx.Semant.ctxt_all_item_types item_id in
begin
match fn_ty with
Expand All @@ -867,9 +890,12 @@ let process_crate (cx:Semant.ctxt) (crate:Ast.crate) : unit =
| _ -> ()
in
let visit_mod_item_post _ _ item =
verify_main item.Common.id;
let item_id = item.Common.id in
verify_main item_id;
match item.Common.node.Ast.decl_item with
Ast.MOD_ITEM_fn _ -> ignore (Stack.pop fn_ctx_stack)
Ast.MOD_ITEM_fn _ when
not (Hashtbl.mem cx.Semant.ctxt_required_items item_id) ->
finish_function item_id
| _ -> ()
in

Expand All @@ -884,7 +910,7 @@ let process_crate (cx:Semant.ctxt) (crate:Ast.crate) : unit =
"Type.visit_obj_fn_pre: item doesn't have an object type (%a)"
Ast.sprintf_ty obj_ty
in
let visit_obj_fn_post _ _ _ = ignore (Stack.pop fn_ctx_stack) in
let visit_obj_fn_post _ _ item = finish_function (item.Common.id) in

let visit_obj_drop_pre _ _ = push_fn_ctx Ast.TY_nil false in
let visit_obj_drop_post _ _ = ignore (Stack.pop fn_ctx_stack) in
Expand Down
1 change: 1 addition & 0 deletions src/lib/_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn new_buf() -> vec[u8] {
}
// FIXME (issue #93): should be:
// ret _vec.alloc[u8](default_bufsz());
ret v;
}

fn new_buf_reader(str s) -> buf_reader {
Expand Down
1 change: 1 addition & 0 deletions src/lib/_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ native "rust" mod rustrt {
}

fn is_utf8(vec[u8] v) -> bool {
fail; // FIXME
}

fn alloc(uint n_bytes) -> str {
Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/impure-pred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ io fn lt(int a, int b) -> bool {
let port[int] p = port();
let chan[int] c = chan(p);
c <| 10;
ret true;
}

fn main() {
Expand Down
9 changes: 9 additions & 0 deletions src/test/compile-fail/missing-return.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// error-pattern: return

fn f() -> int {
}

fn main() {
f();
}

1 change: 1 addition & 0 deletions src/test/run-pass/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn foo(int x) -> int {
let t z;
z = 0x55;
foo(z);
ret 0;
}

fn main() {
Expand Down

0 comments on commit 1ac01e1

Please sign in to comment.