Skip to content

Commit 87e097a

Browse files
committed
Disallow ret inside of block functions
Also adds proper checking for cont/break being inside a loop. Closes #1854 Issue #1619
1 parent e4c141a commit 87e097a

File tree

16 files changed

+152
-150
lines changed

16 files changed

+152
-150
lines changed

src/libstd/rope.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ Loop through a rope, char by char, until the end.
399399
fn iter_chars(rope: rope, it: fn(char)) {
400400
loop_chars(rope) {|x|
401401
it(x);
402-
ret true
402+
true
403403
};
404404
}
405405

@@ -1043,9 +1043,9 @@ mod node {
10431043

10441044
fn loop_chars(node: @node, it: fn(char) -> bool) -> bool {
10451045
ret loop_leaves(node, {|leaf|
1046-
ret str::all_between(*leaf.content,
1047-
leaf.byte_offset,
1048-
leaf.byte_len, it)
1046+
str::all_between(*leaf.content,
1047+
leaf.byte_offset,
1048+
leaf.byte_len, it)
10491049
})
10501050
}
10511051

src/rustc/driver/driver.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
155155

156156
time(time_passes, "block-use checking",
157157
bind middle::block_use::check_crate(ty_cx, crate));
158+
time(time_passes, "loop checking",
159+
bind middle::check_loop::check_crate(sess, crate));
158160
time(time_passes, "function usage",
159161
bind fn_usage::check_crate_fn_usage(ty_cx, crate));
160162
time(time_passes, "alt checking",

src/rustc/middle/infer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,8 @@ impl unify_methods for infer_ctxt {
654654
self.constrs(a, b)
655655
}
656656
} else {
657-
ret self.uerr(ty::terr_constr_len(a_constrs.len(),
658-
b_constrs.len()));
657+
self.uerr(ty::terr_constr_len(a_constrs.len(),
658+
b_constrs.len()))
659659
}
660660
}
661661
}

src/rustc/middle/last_use.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,22 @@ fn find_last_uses(c: @crate, def_map: resolve::def_map,
7171
visit::visit_crate(*c, cx, v);
7272
let mini_table = std::map::int_hash();
7373
cx.last_uses.items {|key, val|
74-
if !val { ret; }
75-
alt key {
76-
path(id) {
77-
mini_table.insert(id, is_last_use);
78-
let def_node = ast_util::def_id_of_def(def_map.get(id)).node;
79-
cx.spill_map.insert(def_node, ());
80-
}
81-
close(fn_id, local_id) {
82-
cx.spill_map.insert(local_id, ());
83-
let known = alt check mini_table.find(fn_id) {
84-
some(closes_over(ids)) { ids }
85-
none { [] }
86-
};
87-
mini_table.insert(fn_id, closes_over(known + [local_id]));
88-
}
74+
if val {
75+
alt key {
76+
path(id) {
77+
mini_table.insert(id, is_last_use);
78+
let def_node = ast_util::def_id_of_def(def_map.get(id)).node;
79+
cx.spill_map.insert(def_node, ());
80+
}
81+
close(fn_id, local_id) {
82+
cx.spill_map.insert(local_id, ());
83+
let known = alt check mini_table.find(fn_id) {
84+
some(closes_over(ids)) { ids }
85+
none { [] }
86+
};
87+
mini_table.insert(fn_id, closes_over(known + [local_id]));
88+
}
89+
}
8990
}
9091
}
9192
ret (mini_table, cx.spill_map);
@@ -211,15 +212,16 @@ fn visit_stmt(s: @stmt, cx: ctx, v: visit::vt<ctx>) {
211212
alt s.node {
212213
stmt_decl(@{node: decl_local(ls), _}, _) {
213214
shadow_in_current(cx, {|id|
215+
let mut rslt = false;
214216
for local in ls {
215217
let mut found = false;
216218
pat_util::pat_bindings(cx.tcx.def_map, local.node.pat,
217219
{|pid, _a, _b|
218220
if pid == id { found = true; }
219221
});
220-
if found { ret true; }
222+
if found { rslt = true; break; }
221223
}
222-
false
224+
rslt
223225
});
224226
}
225227
_ {}
@@ -236,8 +238,7 @@ fn visit_fn(fk: visit::fn_kind, decl: fn_decl, body: blk,
236238
proto_any | proto_block {
237239
visit_block(func, cx, {||
238240
shadow_in_current(cx, {|id|
239-
for arg in decl.inputs { if arg.id == id { ret true; } }
240-
false
241+
vec::any(decl.inputs, {|arg| arg.id == id})
241242
});
242243
visit::visit_fn(fk, decl, body, sp, id, cx, v);
243244
});

src/rustc/middle/resolve.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,13 +2162,14 @@ fn find_impls_in_view_item(e: env, vi: @ast::view_item,
21622162
if vec::len(*pt) == 1u {
21632163
option::may(sc) {|sc|
21642164
list::iter(sc) {|level|
2165-
if vec::len(found) > 0u { ret; }
2166-
for imp in *level {
2167-
if imp.ident == pt[0] {
2168-
found += [@{ident: name with *imp}];
2165+
if vec::len(found) == 0u {
2166+
for imp in *level {
2167+
if imp.ident == pt[0] {
2168+
found += [@{ident: name with *imp}];
2169+
}
21692170
}
2171+
if vec::len(found) > 0u { impls += found; }
21702172
}
2171-
if vec::len(found) > 0u { impls += found; }
21722173
}
21732174
}
21742175
} else {

src/rustc/middle/trans/base.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,11 +2772,12 @@ fn get_landing_pad(bcx: block) -> BasicBlockRef {
27722772
in_lpad_scope_cx(bcx) {|info|
27732773
// If there is a valid landing pad still around, use it
27742774
alt info.landing_pad {
2775-
some(target) { cached = some(target); ret; }
2776-
none {}
2775+
some(target) { cached = some(target); }
2776+
none {
2777+
pad_bcx = sub_block(bcx, "unwind");
2778+
info.landing_pad = some(pad_bcx.llbb);
2779+
}
27772780
}
2778-
pad_bcx = sub_block(bcx, "unwind");
2779-
info.landing_pad = some(pad_bcx.llbb);
27802781
}
27812782
alt cached { some(b) { ret b; } none {} } // Can't return from block above
27822783
// The landing pad return type (the type being propagated). Not sure what
@@ -3374,13 +3375,7 @@ fn trans_break_cont(bcx: block, to_end: bool)
33743375
}
33753376
_ {}
33763377
}
3377-
unwind = alt check unwind.parent {
3378-
parent_some(cx) { cx }
3379-
parent_none {
3380-
bcx.sess().bug
3381-
(if to_end { "break" } else { "cont" } + " outside a loop");
3382-
}
3383-
};
3378+
unwind = alt check unwind.parent { parent_some(cx) { cx } };
33843379
}
33853380
cleanup_and_Br(bcx, unwind, target.llbb);
33863381
Unreachable(bcx);

src/rustc/middle/trans/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ fn revoke_clean(cx: block, val: ValueRef) {
265265
vec::slice(info.cleanups, 0u, i) +
266266
vec::slice(info.cleanups, i + 1u, info.cleanups.len());
267267
scope_clean_changed(info);
268-
ret;
268+
break;
269269
}
270270
_ {}
271271
}

src/rustc/middle/trans/tvec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ fn trans_append(bcx: block, vec_ty: ty::t, lhsptr: ValueRef,
180180
load_if_immediate(bcx, addr, unit_ty), unit_ty);
181181
Store(bcx, InBoundsGEP(bcx, write_ptr, [C_int(ccx, 1)]),
182182
write_ptr_ptr);
183-
ret bcx;
183+
bcx
184184
})
185185
}
186186

src/rustc/middle/trans/type_use.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,27 @@ fn type_needs(cx: ctx, use: uint, ty: ty::t) {
9696

9797
fn type_needs_inner(cx: ctx, use: uint, ty: ty::t) {
9898
ty::maybe_walk_ty(ty) {|ty|
99-
if !ty::type_has_params(ty) { ret false; }
100-
alt ty::get(ty).struct {
101-
ty::ty_fn(_) | ty::ty_ptr(_) | ty::ty_rptr(_, _) |
102-
ty::ty_box(_) | ty::ty_iface(_, _) { ret false; }
103-
ty::ty_enum(did, tps) {
104-
for v in *ty::enum_variants(cx.ccx.tcx, did) {
105-
for aty in v.args {
106-
let t = ty::substitute_type_params(cx.ccx.tcx, tps, aty);
107-
type_needs_inner(cx, use, t);
99+
if ty::type_has_params(ty) {
100+
alt ty::get(ty).struct {
101+
ty::ty_fn(_) | ty::ty_ptr(_) | ty::ty_rptr(_, _) |
102+
ty::ty_box(_) | ty::ty_iface(_, _) { false }
103+
ty::ty_enum(did, tps) {
104+
for v in *ty::enum_variants(cx.ccx.tcx, did) {
105+
for aty in v.args {
106+
let t = ty::substitute_type_params(cx.ccx.tcx, tps,
107+
aty);
108+
type_needs_inner(cx, use, t);
109+
}
108110
}
111+
false
112+
}
113+
ty::ty_param(n, _) {
114+
cx.uses[n] |= use;
115+
false
116+
}
117+
_ { true }
109118
}
110-
ret false;
111-
}
112-
ty::ty_param(n, _) {
113-
cx.uses[n] |= use;
114-
}
115-
_ {}
116-
}
117-
ret true;
119+
} else { false }
118120
}
119121
}
120122

src/rustc/middle/ty.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,12 +1036,7 @@ fn type_allows_implicit_copy(cx: ctxt, ty: t) -> bool {
10361036
mt.mutbl != ast::m_imm
10371037
}
10381038
ty_rec(fields) {
1039-
for field in fields {
1040-
if field.mt.mutbl != ast::m_imm {
1041-
ret true;
1042-
}
1043-
}
1044-
false
1039+
vec::any(fields, {|f| f.mt.mutbl != ast::m_imm})
10451040
}
10461041
_ { false }
10471042
}
@@ -1050,12 +1045,12 @@ fn type_allows_implicit_copy(cx: ctxt, ty: t) -> bool {
10501045

10511046
fn type_structurally_contains_uniques(cx: ctxt, ty: t) -> bool {
10521047
ret type_structurally_contains(cx, ty, {|sty|
1053-
ret alt sty {
1054-
ty_uniq(_) { ret true; }
1048+
alt sty {
1049+
ty_uniq(_) { true }
10551050
ty_vec(_) { true }
10561051
ty_str { true }
1057-
_ { ret false; }
1058-
};
1052+
_ { false }
1053+
}
10591054
});
10601055
}
10611056

0 commit comments

Comments
 (0)