authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-20 16:08:01-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-21 20:43:41-05:00
logfd6020c4e26c5a1e3652c2f88ab4e668f6fc1dbc
treeb355a679245752973edda218201550def88aa58d
parentcf2fe2536e65e7d0d8892f630282f3d264454cc3
signaturelock-open Commit is signed but in an unrecognized format.

update tests, better error messages, update self-hosted tokenizer


6 files changed, 50 insertions(+), 44 deletions(-)

lib/std/zig/parser_test.zig-6
......@@ -1725,11 +1725,6 @@ test "zig fmt: multiline string" {
17251725 \\ \\two)
17261726 \\ \\three
17271727 \\ ;
1728 \\ const s2 =
1729 \\ c\\one
1730 \\ c\\two)
1731 \\ c\\three
1732 \\ ;
17331728 \\ const s3 = // hi
17341729 \\ \\one
17351730 \\ \\two)
......@@ -1746,7 +1741,6 @@ test "zig fmt: values" {
17461741 \\ 1;
17471742 \\ 1.0;
17481743 \\ "string";
1749 \\ c"cstring";
17501744 \\ 'c';
17511745 \\ true;
17521746 \\ false;
lib/std/zig/tokenizer.zig+1-21
......@@ -351,7 +351,6 @@ pub const Tokenizer = struct {
351351 Start,
352352 Identifier,
353353 Builtin,
354 C,
355354 StringLiteral,
356355 StringLiteralBackslash,
357356 MultilineStringLiteralLine,
......@@ -427,10 +426,6 @@ pub const Tokenizer = struct {
427426 ' ', '\n', '\t', '\r' => {
428427 result.start = self.index + 1;
429428 },
430 'c' => {
431 state = State.C;
432 result.id = Token.Id.Identifier;
433 },
434429 '"' => {
435430 state = State.StringLiteral;
436431 result.id = Token.Id.StringLiteral;
......@@ -438,7 +433,7 @@ pub const Tokenizer = struct {
438433 '\'' => {
439434 state = State.CharLiteral;
440435 },
441 'a'...'b', 'd'...'z', 'A'...'Z', '_' => {
436 'a'...'z', 'A'...'Z', '_' => {
442437 state = State.Identifier;
443438 result.id = Token.Id.Identifier;
444439 },
......@@ -730,20 +725,6 @@ pub const Tokenizer = struct {
730725 },
731726 else => break,
732727 },
733 State.C => switch (c) {
734 '\\' => {
735 state = State.Backslash;
736 result.id = Token.Id.MultilineStringLiteralLine;
737 },
738 '"' => {
739 state = State.StringLiteral;
740 result.id = Token.Id.StringLiteral;
741 },
742 'a'...'z', 'A'...'Z', '_', '0'...'9' => {
743 state = State.Identifier;
744 },
745 else => break,
746 },
747728 State.StringLiteral => switch (c) {
748729 '\\' => {
749730 state = State.StringLiteralBackslash;
......@@ -1204,7 +1185,6 @@ pub const Tokenizer = struct {
12041185 } else if (self.index == self.buffer.len) {
12051186 switch (state) {
12061187 State.Start,
1207 State.C,
12081188 State.IntegerLiteral,
12091189 State.IntegerLiteralWithRadix,
12101190 State.IntegerLiteralWithRadixHex,
src-self-hosted/translate_c.zig+3-4
......@@ -698,10 +698,9 @@ fn transStringLiteral(
698698 len = 0;
699699 for (str) |c| len += escapeChar(c, &char_buf).len;
700700
701 const buf = try rp.c.a().alloc(u8, len + "c\"\"".len);
702 buf[0] = 'c';
703 buf[1] = '"';
704 writeEscapedString(buf[2..], str);
701 const buf = try rp.c.a().alloc(u8, len + "\"\"".len);
702 buf[0] = '"';
703 writeEscapedString(buf[1..], str);
705704 buf[buf.len - 1] = '"';
706705
707706 const token = try appendToken(rp.c, .StringLiteral, buf);
src/ir.cpp+35-3
......@@ -69,6 +69,7 @@ enum ConstCastResultId {
6969 ConstCastResultIdArrayChild,
7070 ConstCastResultIdBadNullTermArrays,
7171 ConstCastResultIdPtrLens,
72 ConstCastResultIdCV,
7273};
7374
7475struct ConstCastOnly;
......@@ -94,6 +95,7 @@ struct ConstCastArrayMismatch;
9495struct ConstCastBadAllowsZero;
9596struct ConstCastBadNullTermArrays;
9697struct ConstCastBadPtrLens;
98struct ConstCastBadCV;
9799
98100struct ConstCastOnly {
99101 ConstCastResultId id;
......@@ -113,6 +115,7 @@ struct ConstCastOnly {
113115 ConstCastBadAllowsZero *bad_allows_zero;
114116 ConstCastBadNullTermArrays *bad_null_term_arrays;
115117 ConstCastBadPtrLens *bad_ptr_lens;
118 ConstCastBadCV *bad_cv;
116119 } data;
117120};
118121
......@@ -177,6 +180,10 @@ struct ConstCastBadPtrLens {
177180 ZigType *actual_type;
178181};
179182
183struct ConstCastBadCV {
184 ZigType *wanted_type;
185 ZigType *actual_type;
186};
180187
181188static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);
182189static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope, LVal lval,
......@@ -9863,6 +9870,17 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
98639870 return result;
98649871 }
98659872
9873 bool ok_cv_qualifiers =
9874 (!actual_ptr_type->data.pointer.is_const || wanted_ptr_type->data.pointer.is_const) &&
9875 (!actual_ptr_type->data.pointer.is_volatile || wanted_ptr_type->data.pointer.is_volatile);
9876 if (!ok_cv_qualifiers) {
9877 result.id = ConstCastResultIdCV;
9878 result.data.bad_cv = allocate_nonzero<ConstCastBadCV>(1);
9879 result.data.bad_cv->wanted_type = wanted_ptr_type;
9880 result.data.bad_cv->actual_type = actual_ptr_type;
9881 return result;
9882 }
9883
98669884 ConstCastOnly child = types_match_const_cast_only(ira, wanted_ptr_type->data.pointer.child_type,
98679885 actual_ptr_type->data.pointer.child_type, source_node, !wanted_ptr_type->data.pointer.is_const);
98689886 if (child.id == ConstCastResultIdInvalid)
......@@ -9902,8 +9920,6 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
99029920 return result;
99039921 }
99049922 if (type_has_bits(wanted_type) == type_has_bits(actual_type) &&
9905 (!actual_ptr_type->data.pointer.is_const || wanted_ptr_type->data.pointer.is_const) &&
9906 (!actual_ptr_type->data.pointer.is_volatile || wanted_ptr_type->data.pointer.is_volatile) &&
99079923 actual_ptr_type->data.pointer.bit_offset_in_host == wanted_ptr_type->data.pointer.bit_offset_in_host &&
99089924 actual_ptr_type->data.pointer.host_int_bytes == wanted_ptr_type->data.pointer.host_int_bytes &&
99099925 get_ptr_align(ira->codegen, actual_ptr_type) >= get_ptr_align(ira->codegen, wanted_ptr_type))
......@@ -12595,6 +12611,20 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
1259512611 }
1259612612 break;
1259712613 }
12614 case ConstCastResultIdCV: {
12615 ZigType *wanted_type = cast_result->data.bad_cv->wanted_type;
12616 ZigType *actual_type = cast_result->data.bad_cv->actual_type;
12617 bool ok_const = !actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const;
12618 bool ok_volatile = !actual_type->data.pointer.is_volatile || wanted_type->data.pointer.is_volatile;
12619 if (!ok_const) {
12620 add_error_note(ira->codegen, parent_msg, source_node, buf_sprintf("cast discards const qualifier"));
12621 } else if (!ok_volatile) {
12622 add_error_note(ira->codegen, parent_msg, source_node, buf_sprintf("cast discards volatile qualifier"));
12623 } else {
12624 zig_unreachable();
12625 }
12626 break;
12627 }
1259812628 case ConstCastResultIdFnIsGeneric:
1259912629 add_error_note(ira->codegen, parent_msg, source_node,
1260012630 buf_sprintf("only one of the functions is generic"));
......@@ -12987,7 +13017,9 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1298713017 wanted_type->data.pointer.ptr_len == PtrLenNull) &&
1298813018 actual_type->id == ZigTypeIdPointer &&
1298913019 actual_type->data.pointer.ptr_len == PtrLenSingle &&
12990 actual_type->data.pointer.child_type->id == ZigTypeIdArray)
13020 actual_type->data.pointer.child_type->id == ZigTypeIdArray &&
13021 (!actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const) &&
13022 (!actual_type->data.pointer.is_volatile || wanted_type->data.pointer.is_volatile))
1299113023 {
1299213024 if (wanted_type->data.pointer.ptr_len != PtrLenNull ||
1299313025 actual_type->data.pointer.child_type->data.array.is_null_terminated)
test/compile_errors.zig+10-9
......@@ -784,7 +784,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
784784 \\ strValue = strValue orelse "";
785785 \\}
786786 ,
787 "tmp.zig:3:32: error: cast discards const qualifier",
787 "tmp.zig:3:32: error: expected type '[*c]u8', found '*const [0]null u8'",
788 "tmp.zig:3:32: note: cast discards const qualifier",
788789 );
789790
790791 cases.add(
......@@ -1323,7 +1324,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
13231324 \\ ptr_opt_many_ptr = c_ptr;
13241325 \\}
13251326 \\export fn entry2() void {
1326 \\ var buf: [4]u8 = "aoeu";
1327 \\ var buf: [4]u8 = "aoeu".*;
13271328 \\ var slice: []u8 = &buf;
13281329 \\ var opt_many_ptr: [*]u8 = slice.ptr;
13291330 \\ var ptr_opt_many_ptr = &opt_many_ptr;
......@@ -1518,7 +1519,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
15181519 cases.add(
15191520 "reading past end of pointer casted array",
15201521 \\comptime {
1521 \\ const array = "aoeu";
1522 \\ const array: [4]u8 = "aoeu".*;
15221523 \\ const slice = array[1..];
15231524 \\ const int_ptr = @ptrCast(*const u24, slice.ptr);
15241525 \\ const deref = int_ptr.*;
......@@ -3356,7 +3357,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
33563357 \\ return a;
33573358 \\}
33583359 ,
3359 "tmp.zig:3:12: error: expected type 'i32', found '[*]const u8'",
3360 "tmp.zig:3:12: error: expected type 'i32', found '*const [1]null u8'",
33603361 );
33613362
33623363 cases.add(
......@@ -3827,12 +3828,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
38273828 cases.add(
38283829 "array concatenation with wrong type",
38293830 \\const src = "aoeu";
3830 \\const derp = @as(usize, 1234);
3831 \\const derp: usize = 1234;
38313832 \\const a = derp ++ "foo";
38323833 \\
38333834 \\export fn entry() usize { return @sizeOf(@typeOf(a)); }
38343835 ,
3835 "tmp.zig:3:11: error: expected array or C string literal, found 'usize'",
3836 "tmp.zig:3:11: error: expected array, found 'usize'",
38363837 );
38373838
38383839 cases.add(
......@@ -6207,11 +6208,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
62076208 cases.add(
62086209 "calling var args extern function, passing array instead of pointer",
62096210 \\export fn entry() void {
6210 \\ foo("hello",);
6211 \\ foo("hello".*,);
62116212 \\}
62126213 \\pub extern fn foo(format: *const u8, ...) void;
62136214 ,
6214 "tmp.zig:2:9: error: expected type '*const u8', found '[5]u8'",
6215 "tmp.zig:2:16: error: expected type '*const u8', found '[5]null u8'",
62156216 );
62166217
62176218 cases.add(
......@@ -6777,7 +6778,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
67776778 \\}
67786779 ,
67796780 "tmp.zig:4:22: error: expected type '*[1]i32', found '*const i32'",
6780 "tmp.zig:4:22: note: pointer type child 'i32' cannot cast into pointer type child '[1]i32'",
6781 "tmp.zig:4:22: note: cast discards const qualifier",
67816782 );
67826783
67836784 cases.add(
test/stage1/c_abi/main.zig+1-1
......@@ -119,7 +119,7 @@ export fn zig_bool(x: bool) void {
119119extern fn c_array([10]u8) void;
120120
121121test "C ABI array" {
122 var array: [10]u8 = "1234567890";
122 var array: [10]u8 = "1234567890".*;
123123 c_array(array);
124124}
125125