Skip to content

Commit d917815

Browse files
committedDec 22, 2017
explicitly return from blocks
instead of last statement being expression value closes #629
·
0.15.20.2.0
1 parent 8bc5232 commit d917815

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+1202
-1230
lines changed
 

‎doc/docgen.zig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn gen(in: &io.InStream, out: &io.OutStream) {
4949
if (err == error.EndOfStream) {
5050
return;
5151
}
52-
std.debug.panic("{}", err)
52+
std.debug.panic("{}", err);
5353
};
5454
switch (state) {
5555
State.Start => switch (byte) {

‎doc/langref.html.in‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,14 +3021,13 @@ const assert = @import("std").debug.assert;</code></pre>
30213021
<pre><code class="zig">const assert = @import("std").debug.assert;
30223022

30233023
// Functions are declared like this
3024-
// The last expression in the function can be used as the return value.
30253024
fn add(a: i8, b: i8) -&gt; i8 {
30263025
if (a == 0) {
30273026
// You can still return manually if needed.
30283027
return b;
30293028
}
30303029

3031-
a + b
3030+
return a + b;
30323031
}
30333032

30343033
// The export specifier makes a function externally visible in the generated
@@ -5847,7 +5846,7 @@ ParamDeclList = "(" list(ParamDecl, ",") ")"
58475846

58485847
ParamDecl = option("noalias" | "comptime") option(Symbol ":") (TypeExpr | "...")
58495848

5850-
Block = option(Symbol ":") "{" many(Statement) option(Expression) "}"
5849+
Block = option(Symbol ":") "{" many(Statement) "}"
58515850

58525851
Statement = LocalVarDecl ";" | Defer(Block) | Defer(Expression) ";" | BlockExpression(Block) | Expression ";" | ";"
58535852

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export fn add(a: i32, b: i32) -> i32 {
2-
a + b
2+
return a + b;
33
}

‎src-self-hosted/parser.zig‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ pub const Parser = struct {
111111
}
112112

113113
pub fn parse(self: &Parser) -> %&ast.NodeRoot {
114-
const result = self.parseInner() %% |err| {
114+
const result = self.parseInner() %% |err| x: {
115115
if (self.cleanup_root_node) |root_node| {
116116
self.freeAst(root_node);
117117
}
118-
err
118+
break :x err;
119119
};
120120
self.cleanup_root_node = null;
121121
return result;
@@ -125,12 +125,12 @@ pub const Parser = struct {
125125
var stack = self.initUtilityArrayList(State);
126126
defer self.deinitUtilityArrayList(stack);
127127

128-
const root_node = {
128+
const root_node = x: {
129129
const root_node = %return self.createRoot();
130130
%defer self.allocator.destroy(root_node);
131131
// This stack append has to succeed for freeAst to work
132132
%return stack.append(State.TopLevel);
133-
root_node
133+
break :x root_node;
134134
};
135135
assert(self.cleanup_root_node == null);
136136
self.cleanup_root_node = root_node;
@@ -462,7 +462,7 @@ pub const Parser = struct {
462462
} else if (token.id == Token.Id.Keyword_noalias) {
463463
param_decl.noalias_token = token;
464464
token = self.getNextToken();
465-
};
465+
}
466466
if (token.id == Token.Id.Identifier) {
467467
const next_token = self.getNextToken();
468468
if (next_token.id == Token.Id.Colon) {
@@ -793,14 +793,14 @@ pub const Parser = struct {
793793
}
794794

795795
fn getNextToken(self: &Parser) -> Token {
796-
return if (self.put_back_count != 0) {
796+
if (self.put_back_count != 0) {
797797
const put_back_index = self.put_back_count - 1;
798798
const put_back_token = self.put_back_tokens[put_back_index];
799799
self.put_back_count = put_back_index;
800-
put_back_token
800+
return put_back_token;
801801
} else {
802-
self.tokenizer.next()
803-
};
802+
return self.tokenizer.next();
803+
}
804804
}
805805

806806
const RenderAstFrame = struct {
@@ -873,7 +873,7 @@ pub const Parser = struct {
873873
Token.Id.Keyword_pub => %return stream.print("pub "),
874874
Token.Id.Keyword_export => %return stream.print("export "),
875875
else => unreachable,
876-
};
876+
}
877877
}
878878
if (fn_proto.extern_token) |extern_token| {
879879
%return stream.print("{} ", self.tokenizer.getTokenSlice(extern_token));
@@ -1102,7 +1102,7 @@ fn testParse(source: []const u8, allocator: &mem.Allocator) -> %[]u8 {
11021102
// TODO test for memory leaks
11031103
// TODO test for valid frees
11041104
fn testCanonical(source: []const u8) {
1105-
const needed_alloc_count = {
1105+
const needed_alloc_count = x: {
11061106
// Try it once with unlimited memory, make sure it works
11071107
var fixed_allocator = mem.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
11081108
var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, @maxValue(usize));
@@ -1116,7 +1116,7 @@ fn testCanonical(source: []const u8) {
11161116
@panic("test failed");
11171117
}
11181118
failing_allocator.allocator.free(result_source);
1119-
failing_allocator.index
1119+
break :x failing_allocator.index;
11201120
};
11211121

11221122
// TODO make this pass

0 commit comments

Comments
 (0)
Please sign in to comment.