authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-13 10:40:37+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-13 10:40:37+02:00
loga498993fd102c649c3d24f73f064c5bbafb9b3ec
tree9793afe4632a0be0b6868dc5b4f7173e0152ba0f
parent44c53c9979e30653a109b07ee90ba57e3bc8a7df
parent0f652b4d80a57f5b5a1054d06cd5767ce52402a1

Merged with master


4 files changed, 126 insertions(+), 65 deletions(-)

src/ir.cpp+13-1
......@@ -11395,7 +11395,19 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
1139511395 }
1139611396 break;
1139711397 case VarClassRequiredAny:
11398 // OK
11398 if (casted_init_value->value.special == ConstValSpecialStatic &&
11399 casted_init_value->value.type->id == TypeTableEntryIdFn &&
11400 casted_init_value->value.data.x_ptr.data.fn.fn_entry->fn_inline == FnInlineAlways)
11401 {
11402 var_class_requires_const = true;
11403 if (!var->src_is_const && !is_comptime_var) {
11404 ErrorMsg *msg = ir_add_error_node(ira, source_node,
11405 buf_sprintf("functions marked inline must be stored in const or comptime var"));
11406 AstNode *proto_node = casted_init_value->value.data.x_ptr.data.fn.fn_entry->proto_node;
11407 add_error_note(ira->codegen, msg, proto_node, buf_sprintf("declared here"));
11408 result_type = ira->codegen->builtin_types.entry_invalid;
11409 }
11410 }
1139911411 break;
1140011412 }
1140111413 }
std/zig/parser.zig+97-64
......@@ -3614,33 +3614,45 @@ pub const Parser = struct {
36143614 try stack.append(RenderState { .Expression = suffix_op.lhs });
36153615 },
36163616 ast.NodeSuffixOp.SuffixOp.StructInitializer => |field_inits| {
3617 try stack.append(RenderState { .Text = " }"});
3617 if (field_inits.len == 0) {
3618 try stack.append(RenderState { .Text = "{}" });
3619 try stack.append(RenderState { .Expression = suffix_op.lhs });
3620 continue;
3621 }
3622 try stack.append(RenderState { .Text = "}"});
3623 try stack.append(RenderState.PrintIndent);
3624 try stack.append(RenderState { .Indent = indent });
36183625 var i = field_inits.len;
36193626 while (i != 0) {
36203627 i -= 1;
36213628 const field_init = field_inits.at(i);
3629 try stack.append(RenderState { .Text = ",\n" });
36223630 try stack.append(RenderState { .FieldInitializer = field_init });
3623 try stack.append(RenderState { .Text = " " });
3624 if (i != 0) {
3625 try stack.append(RenderState { .Text = "," });
3626 }
3631 try stack.append(RenderState.PrintIndent);
36273632 }
3628 try stack.append(RenderState { .Text = "{"});
3633 try stack.append(RenderState { .Indent = indent + indent_delta });
3634 try stack.append(RenderState { .Text = " {\n"});
36293635 try stack.append(RenderState { .Expression = suffix_op.lhs });
36303636 },
36313637 ast.NodeSuffixOp.SuffixOp.ArrayInitializer => |exprs| {
3632 try stack.append(RenderState { .Text = " }"});
3638 if (exprs.len == 0) {
3639 try stack.append(RenderState { .Text = "{}" });
3640 try stack.append(RenderState { .Expression = suffix_op.lhs });
3641 continue;
3642 }
3643 try stack.append(RenderState { .Text = "}"});
3644 try stack.append(RenderState.PrintIndent);
3645 try stack.append(RenderState { .Indent = indent });
36333646 var i = exprs.len;
36343647 while (i != 0) {
36353648 i -= 1;
36363649 const expr = exprs.at(i);
3650 try stack.append(RenderState { .Text = ",\n" });
36373651 try stack.append(RenderState { .Expression = expr });
3638 try stack.append(RenderState { .Text = " " });
3639 if (i != 0) {
3640 try stack.append(RenderState { .Text = "," });
3641 }
3652 try stack.append(RenderState.PrintIndent);
36423653 }
3643 try stack.append(RenderState { .Text = "{"});
3654 try stack.append(RenderState { .Indent = indent + indent_delta });
3655 try stack.append(RenderState { .Text = " {\n"});
36443656 try stack.append(RenderState { .Expression = suffix_op.lhs });
36453657 },
36463658 }
......@@ -3784,6 +3796,14 @@ pub const Parser = struct {
37843796 while (i != 0) {
37853797 i -= 1;
37863798 const node = fields_and_decls[i];
3799 switch (node.id) {
3800 ast.Node.Id.StructField,
3801 ast.Node.Id.UnionTag,
3802 ast.Node.Id.EnumTag => {
3803 try stack.append(RenderState { .Text = "," });
3804 },
3805 else => { }
3806 }
37873807 try stack.append(RenderState { .TopLevelDecl = node});
37883808 try stack.append(RenderState.PrintIndent);
37893809 try stack.append(RenderState {
......@@ -3798,18 +3818,6 @@ pub const Parser = struct {
37983818 break :blk "\n";
37993819 },
38003820 });
3801
3802 if (i != 0) {
3803 const prev_node = fields_and_decls[i - 1];
3804 switch (prev_node.id) {
3805 ast.Node.Id.StructField,
3806 ast.Node.Id.UnionTag,
3807 ast.Node.Id.EnumTag => {
3808 try stack.append(RenderState { .Text = "," });
3809 },
3810 else => { }
3811 }
3812 }
38133821 }
38143822 try stack.append(RenderState { .Indent = indent + indent_delta});
38153823 try stack.append(RenderState { .Text = "{"});
......@@ -3838,6 +3846,7 @@ pub const Parser = struct {
38383846 while (i != 0) {
38393847 i -= 1;
38403848 const node = decls[i];
3849 try stack.append(RenderState { .Text = "," });
38413850 try stack.append(RenderState { .Expression = node });
38423851 try stack.append(RenderState.PrintIndent);
38433852 try stack.append(RenderState {
......@@ -3852,10 +3861,6 @@ pub const Parser = struct {
38523861 break :blk "\n";
38533862 },
38543863 });
3855
3856 if (i != 0) {
3857 try stack.append(RenderState { .Text = "," });
3858 }
38593864 }
38603865 try stack.append(RenderState { .Indent = indent + indent_delta});
38613866 try stack.append(RenderState { .Text = "{"});
......@@ -3968,6 +3973,7 @@ pub const Parser = struct {
39683973 while (i != 0) {
39693974 i -= 1;
39703975 const node = cases[i];
3976 try stack.append(RenderState { .Text = ","});
39713977 try stack.append(RenderState { .Expression = &node.base});
39723978 try stack.append(RenderState.PrintIndent);
39733979 try stack.append(RenderState {
......@@ -3982,10 +3988,6 @@ pub const Parser = struct {
39823988 break :blk "\n";
39833989 },
39843990 });
3985
3986 if (i != 0) {
3987 try stack.append(RenderState { .Text = "," });
3988 }
39893991 }
39903992 try stack.append(RenderState { .Indent = indent + indent_delta});
39913993 try stack.append(RenderState { .Text = ") {"});
......@@ -4008,7 +4010,8 @@ pub const Parser = struct {
40084010 try stack.append(RenderState { .Expression = items[i] });
40094011
40104012 if (i != 0) {
4011 try stack.append(RenderState { .Text = ", " });
4013 try stack.append(RenderState.PrintIndent);
4014 try stack.append(RenderState { .Text = ",\n" });
40124015 }
40134016 }
40144017 },
......@@ -4600,10 +4603,10 @@ test "zig fmt: precedence" {
46004603 \\ (a!b)();
46014604 \\ !a!b;
46024605 \\ !(a!b);
4603 \\ !a{ };
4604 \\ !(a{ });
4605 \\ a + b{ };
4606 \\ (a + b){ };
4606 \\ !a{};
4607 \\ !(a{});
4608 \\ a + b{};
4609 \\ (a + b){};
46074610 \\ a << b + c;
46084611 \\ (a << b) + c;
46094612 \\ a & b << c;
......@@ -4740,21 +4743,21 @@ test "zig fmt: struct declaration" {
47404743 \\ return *self;
47414744 \\ }
47424745 \\
4743 \\ f2: u8
4746 \\ f2: u8,
47444747 \\};
47454748 \\
47464749 \\const Ps = packed struct {
47474750 \\ a: u8,
47484751 \\ pub b: u8,
47494752 \\
4750 \\ c: u8
4753 \\ c: u8,
47514754 \\};
47524755 \\
47534756 \\const Es = extern struct {
47544757 \\ a: u8,
47554758 \\ pub b: u8,
47564759 \\
4757 \\ c: u8
4760 \\ c: u8,
47584761 \\};
47594762 \\
47604763 );
......@@ -4764,25 +4767,25 @@ test "zig fmt: enum declaration" {
47644767 try testCanonical(
47654768 \\const E = enum {
47664769 \\ Ok,
4767 \\ SomethingElse = 0
4770 \\ SomethingElse = 0,
47684771 \\};
47694772 \\
47704773 \\const E2 = enum(u8) {
47714774 \\ Ok,
47724775 \\ SomethingElse = 255,
4773 \\ SomethingThird
4776 \\ SomethingThird,
47744777 \\};
47754778 \\
47764779 \\const Ee = extern enum {
47774780 \\ Ok,
47784781 \\ SomethingElse,
4779 \\ SomethingThird
4782 \\ SomethingThird,
47804783 \\};
47814784 \\
47824785 \\const Ep = packed enum {
47834786 \\ Ok,
47844787 \\ SomethingElse,
4785 \\ SomethingThird
4788 \\ SomethingThird,
47864789 \\};
47874790 \\
47884791 );
......@@ -4794,35 +4797,35 @@ test "zig fmt: union declaration" {
47944797 \\ Int: u8,
47954798 \\ Float: f32,
47964799 \\ None,
4797 \\ Bool: bool
4800 \\ Bool: bool,
47984801 \\};
47994802 \\
48004803 \\const Ue = union(enum) {
48014804 \\ Int: u8,
48024805 \\ Float: f32,
48034806 \\ None,
4804 \\ Bool: bool
4807 \\ Bool: bool,
48054808 \\};
48064809 \\
48074810 \\const E = enum {
48084811 \\ Int,
48094812 \\ Float,
48104813 \\ None,
4811 \\ Bool
4814 \\ Bool,
48124815 \\};
48134816 \\
48144817 \\const Ue2 = union(E) {
48154818 \\ Int: u8,
48164819 \\ Float: f32,
48174820 \\ None,
4818 \\ Bool: bool
4821 \\ Bool: bool,
48194822 \\};
48204823 \\
48214824 \\const Eu = extern union {
48224825 \\ Int: u8,
48234826 \\ Float: f32,
48244827 \\ None,
4825 \\ Bool: bool
4828 \\ Bool: bool,
48264829 \\};
48274830 \\
48284831 );
......@@ -4834,7 +4837,7 @@ test "zig fmt: error set declaration" {
48344837 \\ A,
48354838 \\ B,
48364839 \\
4837 \\ C
4840 \\ C,
48384841 \\};
48394842 \\
48404843 );
......@@ -4843,9 +4846,15 @@ test "zig fmt: error set declaration" {
48434846test "zig fmt: arrays" {
48444847 try testCanonical(
48454848 \\test "test array" {
4846 \\ const a: [2]u8 = [2]u8{ 1, 2 };
4847 \\ const a: [2]u8 = []u8{ 1, 2 };
4848 \\ const a: [0]u8 = []u8{ };
4849 \\ const a: [2]u8 = [2]u8 {
4850 \\ 1,
4851 \\ 2,
4852 \\ };
4853 \\ const a: [2]u8 = []u8 {
4854 \\ 1,
4855 \\ 2,
4856 \\ };
4857 \\ const a: [0]u8 = []u8{};
48494858 \\}
48504859 \\
48514860 );
......@@ -4853,10 +4862,18 @@ test "zig fmt: arrays" {
48534862
48544863test "zig fmt: container initializers" {
48554864 try testCanonical(
4856 \\const a1 = []u8{ };
4857 \\const a2 = []u8{ 1, 2, 3, 4 };
4858 \\const s1 = S{ };
4859 \\const s2 = S{ .a = 1, .b = 2 };
4865 \\const a1 = []u8{};
4866 \\const a2 = []u8 {
4867 \\ 1,
4868 \\ 2,
4869 \\ 3,
4870 \\ 4,
4871 \\};
4872 \\const s1 = S{};
4873 \\const s2 = S {
4874 \\ .a = 1,
4875 \\ .b = 2,
4876 \\};
48604877 \\
48614878 );
48624879}
......@@ -4900,31 +4917,34 @@ test "zig fmt: switch" {
49004917 \\ switch (0) {
49014918 \\ 0 => {},
49024919 \\ 1 => unreachable,
4903 \\ 2, 3 => {},
4920 \\ 2,
4921 \\ 3 => {},
49044922 \\ 4 ... 7 => {},
49054923 \\ 1 + 4 * 3 + 22 => {},
49064924 \\ else => {
49074925 \\ const a = 1;
49084926 \\ const b = a;
4909 \\ }
4927 \\ },
49104928 \\ }
49114929 \\
49124930 \\ const res = switch (0) {
49134931 \\ 0 => 0,
49144932 \\ 1 => 2,
49154933 \\ 1 => a = 4,
4916 \\ else => 4
4934 \\ else => 4,
49174935 \\ };
49184936 \\
49194937 \\ const Union = union(enum) {
49204938 \\ Int: i64,
4921 \\ Float: f64
4939 \\ Float: f64,
49224940 \\ };
49234941 \\
4924 \\ const u = Union{ .Int = 0 };
4942 \\ const u = Union {
4943 \\ .Int = 0,
4944 \\ };
49254945 \\ switch (u) {
49264946 \\ Union.Int => |int| {},
4927 \\ Union.Float => |*float| unreachable
4947 \\ Union.Float => |*float| unreachable,
49284948 \\ }
49294949 \\}
49304950 \\
......@@ -5000,7 +5020,11 @@ test "zig fmt: while" {
50005020test "zig fmt: for" {
50015021 try testCanonical(
50025022 \\test "for" {
5003 \\ const a = []u8{ 1, 2, 3 };
5023 \\ const a = []u8 {
5024 \\ 1,
5025 \\ 2,
5026 \\ 3,
5027 \\ };
50045028 \\ for (a) |v| {
50055029 \\ continue;
50065030 \\ }
......@@ -5230,3 +5254,12 @@ test "zig fmt: error return" {
52305254 \\
52315255 );
52325256}
5257
5258test "zig fmt: struct literals with fields on each line" {
5259 try testCanonical(
5260 \\var self = BufSet {
5261 \\ .hash_map = BufSetHashMap.init(a),
5262 \\};
5263 \\
5264 );
5265}
test/cases/fn.zig+7
......@@ -104,3 +104,10 @@ test "number literal as an argument" {
104104fn numberLiteralArg(a: var) void {
105105 assert(a == 3);
106106}
107
108test "assign inline fn to const variable" {
109 const a = inlineFn;
110 a();
111}
112
113inline fn inlineFn() void { }
test/compile_errors.zig+9
......@@ -1,6 +1,15 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: &tests.CompileErrorContext) void {
4 cases.add("assign inline fn to non-comptime var",
5 \\export fn entry() void {
6 \\ var a = b;
7 \\}
8 \\inline fn b() void { }
9 ,
10 ".tmp_source.zig:2:5: error: functions marked inline must be stored in const or comptime var",
11 ".tmp_source.zig:4:8: note: declared here");
12
413 cases.add("wrong type passed to @panic",
514 \\export fn entry() void {
615 \\ var e = error.Foo;