Skip to content

Commit 133d36f

Browse files
committed
Require body of else-less if expressions to be a value-less block
For consistency with other constructs that could not possibly return a value (say, loops).
1 parent 6615343 commit 133d36f

File tree

8 files changed

+21
-17
lines changed

8 files changed

+21
-17
lines changed

src/comp/middle/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace)
677677
if ns == ns_value {
678678
alt lookup_in_pat(name, a.pats[0]) {
679679
some(did) { ret some(ast::def_binding(did)); }
680-
_ { ret none }
680+
_ { ret none; }
681681
}
682682
}
683683
}

src/comp/middle/trans.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5130,7 +5130,7 @@ fn trans_closure(bcx_maybe: option::t<@block_ctxt>,
51305130
create_real_fn_pair(env.bcx, option::get(llfnty), llfndecl,
51315131
env.ptr);
51325132
if copying {
5133-
add_clean_temp(bcx, closure, node_id_type(cx.ccx, id))
5133+
add_clean_temp(bcx, closure, node_id_type(cx.ccx, id));
51345134
}
51355135
some({fn_pair: closure, bcx: env.bcx})
51365136
}

src/comp/syntax/ext/simplext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ fn transcribe_expr(cx: ext_ctxt, b: bindings, idx_path: @mutable [uint],
359359
expr_path(p) {
360360
// Don't substitute into qualified names.
361361
if vec::len(p.node.types) > 0u || vec::len(p.node.idents) != 1u {
362-
e
362+
e;
363363
}
364364
alt follow_for_trans(cx, b.find(p.node.idents[0]), idx_path) {
365365
some(match_ident(id)) {

src/comp/syntax/parse/lexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn scan_dec_digits_with_prefix(rdr: reader) -> str {
194194
rdr.bump();
195195
}
196196
let digits = scan_dec_digits(rdr);
197-
if negative { str::unshift_char(digits, '-') }
197+
if negative { str::unshift_char(digits, '-'); }
198198
ret digits;
199199
}
200200

src/comp/syntax/parse/parser.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type parser =
2828
fn swap(token::token, uint, uint);
2929
fn look_ahead(uint) -> token::token;
3030
fn fatal(str) -> ! ;
31+
fn span_fatal(span, str) -> ! ;
3132
fn warn(str);
3233
fn restrict(restriction);
3334
fn get_restriction() -> restriction;
@@ -99,7 +100,10 @@ fn new_parser(sess: parse_sess, cfg: ast::crate_cfg, rdr: lexer::reader,
99100
ret buffer[distance - 1u].tok;
100101
}
101102
fn fatal(m: str) -> ! {
102-
codemap::emit_error(some(self.get_span()), m, sess.cm);
103+
self.span_fatal(self.get_span(), m);
104+
}
105+
fn span_fatal(sp: span, m: str) -> ! {
106+
codemap::emit_error(some(sp), m, sess.cm);
103107
fail;
104108
}
105109
fn warn(m: str) {
@@ -1270,6 +1274,9 @@ fn parse_if_expr_1(p: parser) ->
12701274
let elexpr = parse_else_expr(p);
12711275
els = some(elexpr);
12721276
hi = elexpr.span.hi;
1277+
} else if !option::is_none(thn.node.expr) {
1278+
let sp = option::get(thn.node.expr).span;
1279+
p.span_fatal(sp, "if without else can not return a value");
12731280
}
12741281
ret {cond: cond, then: thn, els: els, lo: lo, hi: hi};
12751282
}
@@ -1658,10 +1665,7 @@ fn parse_block_no_value(p: parser) -> ast::blk {
16581665
let blk = parse_block(p);
16591666
if !option::is_none(blk.node.expr) {
16601667
let sp = option::get(blk.node.expr).span;
1661-
codemap::emit_error(some(sp),
1662-
"this block must not return a value",
1663-
p.get_sess().cm);
1664-
fail;
1668+
p.span_fatal(sp, "this block must not return a value");
16651669
}
16661670
ret blk;
16671671
}

src/comp/syntax/print/pprust.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,9 @@ fn print_maybe_parens_discrim(s: ps, e: @ast::expr) {
599599
ast::expr_fail(none.) { true }
600600
_ { false }
601601
};
602-
if disambig { popen(s) }
602+
if disambig { popen(s); }
603603
print_expr(s, e);
604-
if disambig { pclose(s) }
604+
if disambig { pclose(s); }
605605
}
606606

607607
fn print_if(s: ps, test: @ast::expr, blk: ast::blk,
@@ -1504,7 +1504,7 @@ fn print_comment(s: ps, cmnt: lexer::cmnt) {
15041504
pp::STRING(s, _) { s == ";" }
15051505
_ { false }
15061506
};
1507-
if is_semi || is_begin(s) || is_end(s) { hardbreak(s.s) }
1507+
if is_semi || is_begin(s) || is_end(s) { hardbreak(s.s); }
15081508
hardbreak(s.s);
15091509
}
15101510
}

src/test/compiletest/compiletest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ fn is_test(config: config, testfile: str) -> bool {
151151
let valid = false;
152152

153153
for ext in valid_extensions {
154-
if str::ends_with(name, ext) { valid = true }
154+
if str::ends_with(name, ext) { valid = true; }
155155
}
156156

157157
for pre in invalid_prefixes {
158-
if str::starts_with(name, pre) { valid = false }
158+
if str::starts_with(name, pre) { valid = false; }
159159
}
160160

161161
ret valid;

src/test/run-pass/block-expr-precedence.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn main() {
5151

5252
assert if (true) { 12 } else { 12 } - num == 0;
5353
assert 12 - if (true) { 12 } else { 12 } == 0;
54-
if (true) { 12 } {-num};
55-
if (true) { 12 }; {-num};
56-
if (true) { 12 };;; -num;
54+
if (true) { 12; } {-num};
55+
if (true) { 12; }; {-num};
56+
if (true) { 12; };;; -num;
5757
}

0 commit comments

Comments
 (0)