aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-11-04 16:04:31 +0200
committerGitHub <noreply@github.com>2022-11-04 16:04:31 +0200
commit8c4faa5f3f63cfbcfdc12cda7c37be1da642d2f6 (patch)
tree2a90fb00f39cb5eae5f8083e2b8462a2e198f918 /lib/std
parentea54c9a375ef8f419694b39b9f14f181fa0b82ee (diff)
parent577daab08cd2119604b5ee501e4bd8aeb2619668 (diff)
downloadzig-8c4faa5f3f63cfbcfdc12cda7c37be1da642d2f6.tar.gz
zig-8c4faa5f3f63cfbcfdc12cda7c37be1da642d2f6.zip
Merge pull request #13338 from Vexu/stage2-compile-errors
Improve some error messages
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/meta.zig6
-rw-r--r--lib/std/start_windows_tls.zig2
-rw-r--r--lib/std/zig/Ast.zig10
-rw-r--r--lib/std/zig/parse.zig26
-rw-r--r--lib/std/zig/parser_test.zig34
5 files changed, 71 insertions, 7 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index 600a8ab012..e75108e101 100644
--- a/lib/std/meta.zig
+++ b/lib/std/meta.zig
@@ -304,7 +304,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type {
.Array = .{
.len = array_info.len,
.child = array_info.child,
- .sentinel = &sentinel_val,
+ .sentinel = @ptrCast(?*const anyopaque, &sentinel_val),
},
}),
.is_allowzero = info.is_allowzero,
@@ -322,7 +322,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type {
.address_space = info.address_space,
.child = info.child,
.is_allowzero = info.is_allowzero,
- .sentinel = &sentinel_val,
+ .sentinel = @ptrCast(?*const anyopaque, &sentinel_val),
},
}),
else => {},
@@ -340,7 +340,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type {
.address_space = ptr_info.address_space,
.child = ptr_info.child,
.is_allowzero = ptr_info.is_allowzero,
- .sentinel = &sentinel_val,
+ .sentinel = @ptrCast(?*const anyopaque, &sentinel_val),
},
}),
},
diff --git a/lib/std/start_windows_tls.zig b/lib/std/start_windows_tls.zig
index 7c9930fe6b..df25a903a2 100644
--- a/lib/std/start_windows_tls.zig
+++ b/lib/std/start_windows_tls.zig
@@ -42,7 +42,7 @@ export const _tls_used linksection(".rdata$T") = IMAGE_TLS_DIRECTORY{
.StartAddressOfRawData = &_tls_start,
.EndAddressOfRawData = &_tls_end,
.AddressOfIndex = &_tls_index,
- .AddressOfCallBacks = &__xl_a,
+ .AddressOfCallBacks = @ptrCast(*anyopaque, &__xl_a),
.SizeOfZeroFill = 0,
.Characteristics = 0,
};
diff --git a/lib/std/zig/Ast.zig b/lib/std/zig/Ast.zig
index 62a567387f..5dd0cdd5af 100644
--- a/lib/std/zig/Ast.zig
+++ b/lib/std/zig/Ast.zig
@@ -197,7 +197,7 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void {
});
},
.expected_labelable => {
- return stream.print("expected 'while', 'for', 'inline', 'suspend', or '{{', found '{s}'", .{
+ return stream.print("expected 'while', 'for', 'inline', or '{{', found '{s}'", .{
token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)].symbol(),
});
},
@@ -356,6 +356,12 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void {
.next_field => {
return stream.writeAll("field after declarations here");
},
+ .expected_var_const => {
+ return stream.writeAll("expected 'var' or 'const' before variable declaration");
+ },
+ .wrong_equal_var_decl => {
+ return stream.writeAll("variable initialized with '==' instead of '='");
+ },
.expected_token => {
const found_tag = token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)];
@@ -2579,6 +2585,8 @@ pub const Error = struct {
mismatched_binary_op_whitespace,
invalid_ampersand_ampersand,
c_style_container,
+ expected_var_const,
+ wrong_equal_var_decl,
zig_style_container,
previous_field,
diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig
index db56cef21e..1be074fc27 100644
--- a/lib/std/zig/parse.zig
+++ b/lib/std/zig/parse.zig
@@ -812,7 +812,18 @@ const Parser = struct {
const align_node = try p.parseByteAlign();
const addrspace_node = try p.parseAddrSpace();
const section_node = try p.parseLinkSection();
- const init_node: Node.Index = if (p.eatToken(.equal) == null) 0 else try p.expectExpr();
+ const init_node: Node.Index = switch (p.token_tags[p.tok_i]) {
+ .equal_equal => blk: {
+ try p.warn(.wrong_equal_var_decl);
+ p.tok_i += 1;
+ break :blk try p.expectExpr();
+ },
+ .equal => blk: {
+ p.tok_i += 1;
+ break :blk try p.expectExpr();
+ },
+ else => 0,
+ };
if (section_node == 0 and addrspace_node == 0) {
if (align_node == 0) {
return p.addNode(.{
@@ -1118,7 +1129,18 @@ const Parser = struct {
if (loop_stmt != 0) return loop_stmt;
if (label_token != 0) {
- return p.fail(.expected_labelable);
+ const after_colon = p.tok_i;
+ const node = try p.parseTypeExpr();
+ if (node != 0) {
+ const a = try p.parseByteAlign();
+ const b = try p.parseAddrSpace();
+ const c = try p.parseLinkSection();
+ const d = if (p.eatToken(.equal) == null) 0 else try p.expectExpr();
+ if (a != 0 or b != 0 or c != 0 or d != 0) {
+ return p.failMsg(.{ .tag = .expected_var_const, .token = label_token });
+ }
+ }
+ return p.failMsg(.{ .tag = .expected_labelable, .token = after_colon });
}
return null_node;
diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig
index 4e155df6d8..4ecc0d283d 100644
--- a/lib/std/zig/parser_test.zig
+++ b/lib/std/zig/parser_test.zig
@@ -5145,6 +5145,40 @@ test "zig fmt: make single-line if no trailing comma" {
);
}
+test "zig fmt: variable initialized with ==" {
+ try testError(
+ \\comptime {
+ \\ var z: u32 == 12 + 1;
+ \\}
+ , &.{.wrong_equal_var_decl});
+}
+
+test "zig fmt: missing const/var before local variable" {
+ try testError(
+ \\comptime {
+ \\ z: u32;
+ \\}
+ \\comptime {
+ \\ z: u32 align(1);
+ \\}
+ \\comptime {
+ \\ z: u32 addrspace(.generic);
+ \\}
+ \\comptime {
+ \\ z: u32 linksection("foo");
+ \\}
+ \\comptime {
+ \\ z: u32 = 1;
+ \\}
+ , &.{
+ .expected_labelable,
+ .expected_var_const,
+ .expected_var_const,
+ .expected_var_const,
+ .expected_var_const,
+ });
+}
+
test "zig fmt: while continue expr" {
try testCanonical(
\\test {