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...@@ -11395,7 +11395,19 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
11395 }11395 }
11396 break;11396 break;
11397 case VarClassRequiredAny:11397 case VarClassRequiredAny:
11398 // OK11398 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 }
11399 break;11411 break;
11400 }11412 }
11401 }11413 }
std/zig/parser.zig+97-64
...@@ -3614,33 +3614,45 @@ pub const Parser = struct {...@@ -3614,33 +3614,45 @@ pub const Parser = struct {
3614 try stack.append(RenderState { .Expression = suffix_op.lhs });3614 try stack.append(RenderState { .Expression = suffix_op.lhs });
3615 },3615 },
3616 ast.NodeSuffixOp.SuffixOp.StructInitializer => |field_inits| {3616 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 });
3618 var i = field_inits.len;3625 var i = field_inits.len;
3619 while (i != 0) {3626 while (i != 0) {
3620 i -= 1;3627 i -= 1;
3621 const field_init = field_inits.at(i);3628 const field_init = field_inits.at(i);
3629 try stack.append(RenderState { .Text = ",\n" });
3622 try stack.append(RenderState { .FieldInitializer = field_init });3630 try stack.append(RenderState { .FieldInitializer = field_init });
3623 try stack.append(RenderState { .Text = " " });3631 try stack.append(RenderState.PrintIndent);
3624 if (i != 0) {
3625 try stack.append(RenderState { .Text = "," });
3626 }
3627 }3632 }
3628 try stack.append(RenderState { .Text = "{"});3633 try stack.append(RenderState { .Indent = indent + indent_delta });
3634 try stack.append(RenderState { .Text = " {\n"});
3629 try stack.append(RenderState { .Expression = suffix_op.lhs });3635 try stack.append(RenderState { .Expression = suffix_op.lhs });
3630 },3636 },
3631 ast.NodeSuffixOp.SuffixOp.ArrayInitializer => |exprs| {3637 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 });
3633 var i = exprs.len;3646 var i = exprs.len;
3634 while (i != 0) {3647 while (i != 0) {
3635 i -= 1;3648 i -= 1;
3636 const expr = exprs.at(i);3649 const expr = exprs.at(i);
3650 try stack.append(RenderState { .Text = ",\n" });
3637 try stack.append(RenderState { .Expression = expr });3651 try stack.append(RenderState { .Expression = expr });
3638 try stack.append(RenderState { .Text = " " });3652 try stack.append(RenderState.PrintIndent);
3639 if (i != 0) {
3640 try stack.append(RenderState { .Text = "," });
3641 }
3642 }3653 }
3643 try stack.append(RenderState { .Text = "{"});3654 try stack.append(RenderState { .Indent = indent + indent_delta });
3655 try stack.append(RenderState { .Text = " {\n"});
3644 try stack.append(RenderState { .Expression = suffix_op.lhs });3656 try stack.append(RenderState { .Expression = suffix_op.lhs });
3645 },3657 },
3646 }3658 }
...@@ -3784,6 +3796,14 @@ pub const Parser = struct {...@@ -3784,6 +3796,14 @@ pub const Parser = struct {
3784 while (i != 0) {3796 while (i != 0) {
3785 i -= 1;3797 i -= 1;
3786 const node = fields_and_decls[i];3798 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 }
3787 try stack.append(RenderState { .TopLevelDecl = node});3807 try stack.append(RenderState { .TopLevelDecl = node});
3788 try stack.append(RenderState.PrintIndent);3808 try stack.append(RenderState.PrintIndent);
3789 try stack.append(RenderState {3809 try stack.append(RenderState {
...@@ -3798,18 +3818,6 @@ pub const Parser = struct {...@@ -3798,18 +3818,6 @@ pub const Parser = struct {
3798 break :blk "\n";3818 break :blk "\n";
3799 },3819 },
3800 });3820 });
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 }
3813 }3821 }
3814 try stack.append(RenderState { .Indent = indent + indent_delta});3822 try stack.append(RenderState { .Indent = indent + indent_delta});
3815 try stack.append(RenderState { .Text = "{"});3823 try stack.append(RenderState { .Text = "{"});
...@@ -3838,6 +3846,7 @@ pub const Parser = struct {...@@ -3838,6 +3846,7 @@ pub const Parser = struct {
3838 while (i != 0) {3846 while (i != 0) {
3839 i -= 1;3847 i -= 1;
3840 const node = decls[i];3848 const node = decls[i];
3849 try stack.append(RenderState { .Text = "," });
3841 try stack.append(RenderState { .Expression = node });3850 try stack.append(RenderState { .Expression = node });
3842 try stack.append(RenderState.PrintIndent);3851 try stack.append(RenderState.PrintIndent);
3843 try stack.append(RenderState {3852 try stack.append(RenderState {
...@@ -3852,10 +3861,6 @@ pub const Parser = struct {...@@ -3852,10 +3861,6 @@ pub const Parser = struct {
3852 break :blk "\n";3861 break :blk "\n";
3853 },3862 },
3854 });3863 });
3855
3856 if (i != 0) {
3857 try stack.append(RenderState { .Text = "," });
3858 }
3859 }3864 }
3860 try stack.append(RenderState { .Indent = indent + indent_delta});3865 try stack.append(RenderState { .Indent = indent + indent_delta});
3861 try stack.append(RenderState { .Text = "{"});3866 try stack.append(RenderState { .Text = "{"});
...@@ -3968,6 +3973,7 @@ pub const Parser = struct {...@@ -3968,6 +3973,7 @@ pub const Parser = struct {
3968 while (i != 0) {3973 while (i != 0) {
3969 i -= 1;3974 i -= 1;
3970 const node = cases[i];3975 const node = cases[i];
3976 try stack.append(RenderState { .Text = ","});
3971 try stack.append(RenderState { .Expression = &node.base});3977 try stack.append(RenderState { .Expression = &node.base});
3972 try stack.append(RenderState.PrintIndent);3978 try stack.append(RenderState.PrintIndent);
3973 try stack.append(RenderState {3979 try stack.append(RenderState {
...@@ -3982,10 +3988,6 @@ pub const Parser = struct {...@@ -3982,10 +3988,6 @@ pub const Parser = struct {
3982 break :blk "\n";3988 break :blk "\n";
3983 },3989 },
3984 });3990 });
3985
3986 if (i != 0) {
3987 try stack.append(RenderState { .Text = "," });
3988 }
3989 }3991 }
3990 try stack.append(RenderState { .Indent = indent + indent_delta});3992 try stack.append(RenderState { .Indent = indent + indent_delta});
3991 try stack.append(RenderState { .Text = ") {"});3993 try stack.append(RenderState { .Text = ") {"});
...@@ -4008,7 +4010,8 @@ pub const Parser = struct {...@@ -4008,7 +4010,8 @@ pub const Parser = struct {
4008 try stack.append(RenderState { .Expression = items[i] });4010 try stack.append(RenderState { .Expression = items[i] });
40094011
4010 if (i != 0) {4012 if (i != 0) {
4011 try stack.append(RenderState { .Text = ", " });4013 try stack.append(RenderState.PrintIndent);
4014 try stack.append(RenderState { .Text = ",\n" });
4012 }4015 }
4013 }4016 }
4014 },4017 },
...@@ -4600,10 +4603,10 @@ test "zig fmt: precedence" {...@@ -4600,10 +4603,10 @@ test "zig fmt: precedence" {
4600 \\ (a!b)();4603 \\ (a!b)();
4601 \\ !a!b;4604 \\ !a!b;
4602 \\ !(a!b);4605 \\ !(a!b);
4603 \\ !a{ };4606 \\ !a{};
4604 \\ !(a{ });4607 \\ !(a{});
4605 \\ a + b{ };4608 \\ a + b{};
4606 \\ (a + b){ };4609 \\ (a + b){};
4607 \\ a << b + c;4610 \\ a << b + c;
4608 \\ (a << b) + c;4611 \\ (a << b) + c;
4609 \\ a & b << c;4612 \\ a & b << c;
...@@ -4740,21 +4743,21 @@ test "zig fmt: struct declaration" {...@@ -4740,21 +4743,21 @@ test "zig fmt: struct declaration" {
4740 \\ return *self;4743 \\ return *self;
4741 \\ }4744 \\ }
4742 \\4745 \\
4743 \\ f2: u84746 \\ f2: u8,
4744 \\};4747 \\};
4745 \\4748 \\
4746 \\const Ps = packed struct {4749 \\const Ps = packed struct {
4747 \\ a: u8,4750 \\ a: u8,
4748 \\ pub b: u8,4751 \\ pub b: u8,
4749 \\4752 \\
4750 \\ c: u84753 \\ c: u8,
4751 \\};4754 \\};
4752 \\4755 \\
4753 \\const Es = extern struct {4756 \\const Es = extern struct {
4754 \\ a: u8,4757 \\ a: u8,
4755 \\ pub b: u8,4758 \\ pub b: u8,
4756 \\4759 \\
4757 \\ c: u84760 \\ c: u8,
4758 \\};4761 \\};
4759 \\4762 \\
4760 );4763 );
...@@ -4764,25 +4767,25 @@ test "zig fmt: enum declaration" {...@@ -4764,25 +4767,25 @@ test "zig fmt: enum declaration" {
4764 try testCanonical(4767 try testCanonical(
4765 \\const E = enum {4768 \\const E = enum {
4766 \\ Ok,4769 \\ Ok,
4767 \\ SomethingElse = 04770 \\ SomethingElse = 0,
4768 \\};4771 \\};
4769 \\4772 \\
4770 \\const E2 = enum(u8) {4773 \\const E2 = enum(u8) {
4771 \\ Ok,4774 \\ Ok,
4772 \\ SomethingElse = 255,4775 \\ SomethingElse = 255,
4773 \\ SomethingThird4776 \\ SomethingThird,
4774 \\};4777 \\};
4775 \\4778 \\
4776 \\const Ee = extern enum {4779 \\const Ee = extern enum {
4777 \\ Ok,4780 \\ Ok,
4778 \\ SomethingElse,4781 \\ SomethingElse,
4779 \\ SomethingThird4782 \\ SomethingThird,
4780 \\};4783 \\};
4781 \\4784 \\
4782 \\const Ep = packed enum {4785 \\const Ep = packed enum {
4783 \\ Ok,4786 \\ Ok,
4784 \\ SomethingElse,4787 \\ SomethingElse,
4785 \\ SomethingThird4788 \\ SomethingThird,
4786 \\};4789 \\};
4787 \\4790 \\
4788 );4791 );
...@@ -4794,35 +4797,35 @@ test "zig fmt: union declaration" {...@@ -4794,35 +4797,35 @@ test "zig fmt: union declaration" {
4794 \\ Int: u8,4797 \\ Int: u8,
4795 \\ Float: f32,4798 \\ Float: f32,
4796 \\ None,4799 \\ None,
4797 \\ Bool: bool4800 \\ Bool: bool,
4798 \\};4801 \\};
4799 \\4802 \\
4800 \\const Ue = union(enum) {4803 \\const Ue = union(enum) {
4801 \\ Int: u8,4804 \\ Int: u8,
4802 \\ Float: f32,4805 \\ Float: f32,
4803 \\ None,4806 \\ None,
4804 \\ Bool: bool4807 \\ Bool: bool,
4805 \\};4808 \\};
4806 \\4809 \\
4807 \\const E = enum {4810 \\const E = enum {
4808 \\ Int,4811 \\ Int,
4809 \\ Float,4812 \\ Float,
4810 \\ None,4813 \\ None,
4811 \\ Bool4814 \\ Bool,
4812 \\};4815 \\};
4813 \\4816 \\
4814 \\const Ue2 = union(E) {4817 \\const Ue2 = union(E) {
4815 \\ Int: u8,4818 \\ Int: u8,
4816 \\ Float: f32,4819 \\ Float: f32,
4817 \\ None,4820 \\ None,
4818 \\ Bool: bool4821 \\ Bool: bool,
4819 \\};4822 \\};
4820 \\4823 \\
4821 \\const Eu = extern union {4824 \\const Eu = extern union {
4822 \\ Int: u8,4825 \\ Int: u8,
4823 \\ Float: f32,4826 \\ Float: f32,
4824 \\ None,4827 \\ None,
4825 \\ Bool: bool4828 \\ Bool: bool,
4826 \\};4829 \\};
4827 \\4830 \\
4828 );4831 );
...@@ -4834,7 +4837,7 @@ test "zig fmt: error set declaration" {...@@ -4834,7 +4837,7 @@ test "zig fmt: error set declaration" {
4834 \\ A,4837 \\ A,
4835 \\ B,4838 \\ B,
4836 \\4839 \\
4837 \\ C4840 \\ C,
4838 \\};4841 \\};
4839 \\4842 \\
4840 );4843 );
...@@ -4843,9 +4846,15 @@ test "zig fmt: error set declaration" {...@@ -4843,9 +4846,15 @@ test "zig fmt: error set declaration" {
4843test "zig fmt: arrays" {4846test "zig fmt: arrays" {
4844 try testCanonical(4847 try testCanonical(
4845 \\test "test array" {4848 \\test "test array" {
4846 \\ const a: [2]u8 = [2]u8{ 1, 2 };4849 \\ const a: [2]u8 = [2]u8 {
4847 \\ const a: [2]u8 = []u8{ 1, 2 };4850 \\ 1,
4848 \\ const a: [0]u8 = []u8{ };4851 \\ 2,
4852 \\ };
4853 \\ const a: [2]u8 = []u8 {
4854 \\ 1,
4855 \\ 2,
4856 \\ };
4857 \\ const a: [0]u8 = []u8{};
4849 \\}4858 \\}
4850 \\4859 \\
4851 );4860 );
...@@ -4853,10 +4862,18 @@ test "zig fmt: arrays" {...@@ -4853,10 +4862,18 @@ test "zig fmt: arrays" {
48534862
4854test "zig fmt: container initializers" {4863test "zig fmt: container initializers" {
4855 try testCanonical(4864 try testCanonical(
4856 \\const a1 = []u8{ };4865 \\const a1 = []u8{};
4857 \\const a2 = []u8{ 1, 2, 3, 4 };4866 \\const a2 = []u8 {
4858 \\const s1 = S{ };4867 \\ 1,
4859 \\const s2 = S{ .a = 1, .b = 2 };4868 \\ 2,
4869 \\ 3,
4870 \\ 4,
4871 \\};
4872 \\const s1 = S{};
4873 \\const s2 = S {
4874 \\ .a = 1,
4875 \\ .b = 2,
4876 \\};
4860 \\4877 \\
4861 );4878 );
4862}4879}
...@@ -4900,31 +4917,34 @@ test "zig fmt: switch" {...@@ -4900,31 +4917,34 @@ test "zig fmt: switch" {
4900 \\ switch (0) {4917 \\ switch (0) {
4901 \\ 0 => {},4918 \\ 0 => {},
4902 \\ 1 => unreachable,4919 \\ 1 => unreachable,
4903 \\ 2, 3 => {},4920 \\ 2,
4921 \\ 3 => {},
4904 \\ 4 ... 7 => {},4922 \\ 4 ... 7 => {},
4905 \\ 1 + 4 * 3 + 22 => {},4923 \\ 1 + 4 * 3 + 22 => {},
4906 \\ else => {4924 \\ else => {
4907 \\ const a = 1;4925 \\ const a = 1;
4908 \\ const b = a;4926 \\ const b = a;
4909 \\ }4927 \\ },
4910 \\ }4928 \\ }
4911 \\4929 \\
4912 \\ const res = switch (0) {4930 \\ const res = switch (0) {
4913 \\ 0 => 0,4931 \\ 0 => 0,
4914 \\ 1 => 2,4932 \\ 1 => 2,
4915 \\ 1 => a = 4,4933 \\ 1 => a = 4,
4916 \\ else => 44934 \\ else => 4,
4917 \\ };4935 \\ };
4918 \\4936 \\
4919 \\ const Union = union(enum) {4937 \\ const Union = union(enum) {
4920 \\ Int: i64,4938 \\ Int: i64,
4921 \\ Float: f644939 \\ Float: f64,
4922 \\ };4940 \\ };
4923 \\4941 \\
4924 \\ const u = Union{ .Int = 0 };4942 \\ const u = Union {
4943 \\ .Int = 0,
4944 \\ };
4925 \\ switch (u) {4945 \\ switch (u) {
4926 \\ Union.Int => |int| {},4946 \\ Union.Int => |int| {},
4927 \\ Union.Float => |*float| unreachable4947 \\ Union.Float => |*float| unreachable,
4928 \\ }4948 \\ }
4929 \\}4949 \\}
4930 \\4950 \\
...@@ -5000,7 +5020,11 @@ test "zig fmt: while" {...@@ -5000,7 +5020,11 @@ test "zig fmt: while" {
5000test "zig fmt: for" {5020test "zig fmt: for" {
5001 try testCanonical(5021 try testCanonical(
5002 \\test "for" {5022 \\test "for" {
5003 \\ const a = []u8{ 1, 2, 3 };5023 \\ const a = []u8 {
5024 \\ 1,
5025 \\ 2,
5026 \\ 3,
5027 \\ };
5004 \\ for (a) |v| {5028 \\ for (a) |v| {
5005 \\ continue;5029 \\ continue;
5006 \\ }5030 \\ }
...@@ -5230,3 +5254,12 @@ test "zig fmt: error return" {...@@ -5230,3 +5254,12 @@ test "zig fmt: error return" {
5230 \\5254 \\
5231 );5255 );
5232}5256}
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" {...@@ -104,3 +104,10 @@ test "number literal as an argument" {
104fn numberLiteralArg(a: var) void {104fn numberLiteralArg(a: var) void {
105 assert(a == 3);105 assert(a == 3);
106}106}
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 @@...@@ -1,6 +1,15 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: &tests.CompileErrorContext) void {3pub 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
4 cases.add("wrong type passed to @panic",13 cases.add("wrong type passed to @panic",
5 \\export fn entry() void {14 \\export fn entry() void {
6 \\ var e = error.Foo;15 \\ var e = error.Foo;