From 67bcc70754ffa27b37c10646daef43d83dd9a1b4 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 28 Oct 2010 15:02:00 -0700 Subject: [PATCH] rustboot: Emit an error instead of asserting in trans when a T is passed by value --- src/boot/me/type.ml | 21 ++++++++++++++++++- src/lib/list.rs | 2 +- .../drop-parametric-closure-with-bound-box.rs | 4 ++-- src/test/run-pass/generic-bind.rs | 4 ++-- src/test/run-pass/generic-derived-type.rs | 4 ++-- src/test/run-pass/generic-drop-glue.rs | 4 ++-- src/test/run-pass/generic-exterior-box.rs | 4 ++-- src/test/run-pass/generic-fn.rs | 2 +- src/test/run-pass/generic-iter-frame.rs | 2 +- src/test/run-pass/leak-box-as-tydesc.rs | 4 ++-- src/test/run-pass/lib-deque.rs | 2 +- 11 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/boot/me/type.ml b/src/boot/me/type.ml index 2ff5e1286d7..21aecaf1455 100644 --- a/src/boot/me/type.ml +++ b/src/boot/me/type.ml @@ -1308,6 +1308,23 @@ let process_crate (cx:Semant.ctxt) (crate:Ast.crate) : unit = Common.err (Some item_id) "this function must return a value" in + let check_fn_ty_validity item_id (ty_sig, _) = + let check_input_slot i slot = + match slot with + { + Ast.slot_ty = Some (Ast.TY_param _); + Ast.slot_mode = Ast.MODE_local + } -> + Common.err + (Some item_id) + "parameter %d of this type-parametric function must be \ + passed by reference, not by value" + (i + 1) + | _ -> () + in + Array.iteri check_input_slot ty_sig.Ast.sig_input_slots + in + let visit_mod_item_pre _ _ item = let { Common.node = item; Common.id = item_id } = item in match item.Ast.decl_item with @@ -1316,7 +1333,9 @@ let process_crate (cx:Semant.ctxt) (crate:Ast.crate) : unit = let fn_ty = Hashtbl.find cx.Semant.ctxt_all_item_types item_id in begin match fn_ty with - Ast.TY_fn ty_fn -> push_fn_ctx_of_ty_fn ty_fn + Ast.TY_fn ty_fn -> + check_fn_ty_validity item_id ty_fn; + push_fn_ctx_of_ty_fn ty_fn | _ -> Common.bug () "Type.visit_mod_item_pre: fn item didn't have a fn type" diff --git a/src/lib/list.rs b/src/lib/list.rs index 7b8613151e7..da8d3c0aff7 100644 --- a/src/lib/list.rs +++ b/src/lib/list.rs @@ -12,7 +12,7 @@ tag list[T] { nil; } -fn foldl[T,U](&list[T] ls, U u, fn(&T t, U u) -> U f) -> U { +fn foldl[T,U](&list[T] ls, &U u, fn(&T t, U u) -> U f) -> U { alt(ls) { case (cons[T](?hd, ?tl)) { auto u_ = f(hd, u); diff --git a/src/test/run-pass/drop-parametric-closure-with-bound-box.rs b/src/test/run-pass/drop-parametric-closure-with-bound-box.rs index cf1ff7502b3..dcdc4b035ab 100644 --- a/src/test/run-pass/drop-parametric-closure-with-bound-box.rs +++ b/src/test/run-pass/drop-parametric-closure-with-bound-box.rs @@ -1,5 +1,5 @@ -fn f[T](@int i, T t) {} +fn f[T](@int i, &T t) {} fn main() { auto x = bind f[char](@0xdeafbeef, _); -} \ No newline at end of file +} diff --git a/src/test/run-pass/generic-bind.rs b/src/test/run-pass/generic-bind.rs index 851f2831719..61085eb5485 100644 --- a/src/test/run-pass/generic-bind.rs +++ b/src/test/run-pass/generic-bind.rs @@ -1,8 +1,8 @@ -fn id[T](T t) -> T { +fn id[T](&T t) -> T { ret t; } fn main() { auto f = bind id[int](_); check (f(10) == 10); -} \ No newline at end of file +} diff --git a/src/test/run-pass/generic-derived-type.rs b/src/test/run-pass/generic-derived-type.rs index 9ed493a2322..54c3f9564ec 100644 --- a/src/test/run-pass/generic-derived-type.rs +++ b/src/test/run-pass/generic-derived-type.rs @@ -1,8 +1,8 @@ -fn g[X](X x) -> X { +fn g[X](&X x) -> X { ret x; } -fn f[T](T t) -> tup(T,T) { +fn f[T](&T t) -> tup(T,T) { type pair = tup(T,T); let pair x = tup(t,t); ret g[pair](x); diff --git a/src/test/run-pass/generic-drop-glue.rs b/src/test/run-pass/generic-drop-glue.rs index 3b439b817ba..6cf16802f5a 100644 --- a/src/test/run-pass/generic-drop-glue.rs +++ b/src/test/run-pass/generic-drop-glue.rs @@ -1,4 +1,4 @@ -fn f[T](T t) { +fn f[T](&T t) { log "dropping"; } @@ -6,4 +6,4 @@ fn main() { type r = rec(@int x, @int y); auto x = rec(x=@10, y=@12); f[r](x); -} \ No newline at end of file +} diff --git a/src/test/run-pass/generic-exterior-box.rs b/src/test/run-pass/generic-exterior-box.rs index 797b0f6f28c..0ec7020cdad 100644 --- a/src/test/run-pass/generic-exterior-box.rs +++ b/src/test/run-pass/generic-exterior-box.rs @@ -1,8 +1,8 @@ type tupbox[T] = tup(@T); type recbox[T] = rec(@T x); -fn tuplift[T](T t) -> tupbox[T] { ret tup(@t); } -fn reclift[T](T t) -> recbox[T] { ret rec(x=@t); } +fn tuplift[T](&T t) -> tupbox[T] { ret tup(@t); } +fn reclift[T](&T t) -> recbox[T] { ret rec(x=@t); } fn main() { let int foo = 17; diff --git a/src/test/run-pass/generic-fn.rs b/src/test/run-pass/generic-fn.rs index 68e5fa5b379..238ef2d1aea 100644 --- a/src/test/run-pass/generic-fn.rs +++ b/src/test/run-pass/generic-fn.rs @@ -1,6 +1,6 @@ // -*- rust -*- -fn id[T](T x) -> T { +fn id[T](&T x) -> T { ret x; } diff --git a/src/test/run-pass/generic-iter-frame.rs b/src/test/run-pass/generic-iter-frame.rs index a3c48d78455..d22886b4a6e 100644 --- a/src/test/run-pass/generic-iter-frame.rs +++ b/src/test/run-pass/generic-iter-frame.rs @@ -4,7 +4,7 @@ iter i() -> () { put (); } -fn foo[T](T t) { +fn foo[T](&T t) { let int x = 10; for each (() j in i()) { log x; diff --git a/src/test/run-pass/leak-box-as-tydesc.rs b/src/test/run-pass/leak-box-as-tydesc.rs index 7da40cfe9fa..efd55d79b73 100644 --- a/src/test/run-pass/leak-box-as-tydesc.rs +++ b/src/test/run-pass/leak-box-as-tydesc.rs @@ -1,7 +1,7 @@ -fn leaky[T](T t) { +fn leaky[T](&T t) { } fn main() { auto x = @10; leaky[@int](x); -} \ No newline at end of file +} diff --git a/src/test/run-pass/lib-deque.rs b/src/test/run-pass/lib-deque.rs index 4ba772f0159..ab45b397559 100644 --- a/src/test/run-pass/lib-deque.rs +++ b/src/test/run-pass/lib-deque.rs @@ -92,7 +92,7 @@ fn test_boxes(@int a, @int b, @int c, @int d) { type eqfn[T] = fn(T a, T b) -> bool; -fn test_parameterized[T](eqfn[T] e, T a, T b, T c, T d) { +fn test_parameterized[T](eqfn[T] e, &T a, &T b, &T c, &T d) { let deque.t[T] deq = deque.create[T](); check (deq.size() == 0u); deq.add_front(a);