Skip to content

Remove pub syntax for container fields #3498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -10086,8 +10086,8 @@ ContainerMembers
<- TestDecl ContainerMembers
/ TopLevelComptime ContainerMembers
/ KEYWORD_pub? TopLevelDecl ContainerMembers
/ KEYWORD_pub? ContainerField COMMA ContainerMembers
/ KEYWORD_pub? ContainerField
/ ContainerField COMMA ContainerMembers
/ ContainerField
/

TestDecl <- KEYWORD_test STRINGLITERAL Block
Expand Down
32 changes: 16 additions & 16 deletions lib/std/child_process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@ const TailQueue = std.TailQueue;
const maxInt = std.math.maxInt;

pub const ChildProcess = struct {
pub pid: if (os.windows.is_the_target) void else i32,
pub handle: if (os.windows.is_the_target) windows.HANDLE else void,
pub thread_handle: if (os.windows.is_the_target) windows.HANDLE else void,
pid: if (os.windows.is_the_target) void else i32,
handle: if (os.windows.is_the_target) windows.HANDLE else void,
thread_handle: if (os.windows.is_the_target) windows.HANDLE else void,

pub allocator: *mem.Allocator,
allocator: *mem.Allocator,

pub stdin: ?File,
pub stdout: ?File,
pub stderr: ?File,
stdin: ?File,
stdout: ?File,
stderr: ?File,

pub term: ?(SpawnError!Term),
term: ?(SpawnError!Term),

pub argv: []const []const u8,
argv: []const []const u8,

/// Leave as null to use the current env map using the supplied allocator.
pub env_map: ?*const BufMap,
env_map: ?*const BufMap,

pub stdin_behavior: StdIo,
pub stdout_behavior: StdIo,
pub stderr_behavior: StdIo,
stdin_behavior: StdIo,
stdout_behavior: StdIo,
stderr_behavior: StdIo,

/// Set to change the user id when spawning the child process.
pub uid: if (os.windows.is_the_target) void else ?u32,
uid: if (os.windows.is_the_target) void else ?u32,

/// Set to change the group id when spawning the child process.
pub gid: if (os.windows.is_the_target) void else ?u32,
gid: if (os.windows.is_the_target) void else ?u32,

/// Set to change the current working directory when spawning the child process.
pub cwd: ?[]const u8,
cwd: ?[]const u8,

err_pipe: if (os.windows.is_the_target) void else [2]os.fd_t,
llnode: if (os.windows.is_the_target) void else TailQueue(*ChildProcess).Node,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/heap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub const HeapAllocator = switch (builtin.os) {
/// This allocator takes an existing allocator, wraps it, and provides an interface
/// where you can allocate without freeing, and then free it all together.
pub const ArenaAllocator = struct {
pub allocator: Allocator,
allocator: Allocator,

child_allocator: *Allocator,
buffer_list: std.SinglyLinkedList([]u8),
Expand Down
6 changes: 3 additions & 3 deletions lib/std/http/headers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ fn never_index_default(name: []const u8) bool {

const HeaderEntry = struct {
allocator: *Allocator,
pub name: []const u8,
pub value: []u8,
pub never_index: bool,
name: []const u8,
value: []u8,
never_index: bool,

const Self = @This();

Expand Down
20 changes: 10 additions & 10 deletions lib/std/io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type)
const Self = @This();
const Stream = InStream(Error);

pub stream: Stream,
stream: Stream,

unbuffered_in_stream: *Stream,

Expand Down Expand Up @@ -273,7 +273,7 @@ pub fn PeekStream(comptime buffer_size: usize, comptime InStreamError: type) typ
pub const Error = InStreamError;
pub const Stream = InStream(Error);

pub stream: Stream,
stream: Stream,
base: *Stream,

// Right now the look-ahead space is statically allocated, but a version with dynamic allocation
Expand Down Expand Up @@ -336,7 +336,7 @@ pub const SliceInStream = struct {
pub const Error = error{};
pub const Stream = InStream(Error);

pub stream: Stream,
stream: Stream,

pos: usize,
slice: []const u8,
Expand Down Expand Up @@ -514,9 +514,9 @@ pub const SliceOutStream = struct {
pub const Error = error{OutOfSpace};
pub const Stream = OutStream(Error);

pub stream: Stream,
stream: Stream,

pub pos: usize,
pos: usize,
slice: []u8,

pub fn init(slice: []u8) SliceOutStream {
Expand Down Expand Up @@ -571,7 +571,7 @@ pub const NullOutStream = struct {
pub const Error = error{};
pub const Stream = OutStream(Error);

pub stream: Stream,
stream: Stream,

pub fn init() NullOutStream {
return NullOutStream{
Expand All @@ -595,8 +595,8 @@ pub fn CountingOutStream(comptime OutStreamError: type) type {
pub const Stream = OutStream(Error);
pub const Error = OutStreamError;

pub stream: Stream,
pub bytes_written: u64,
stream: Stream,
bytes_written: u64,
child_stream: *Stream,

pub fn init(child_stream: *Stream) Self {
Expand Down Expand Up @@ -635,7 +635,7 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize, comptime OutStreamEr
pub const Stream = OutStream(Error);
pub const Error = OutStreamError;

pub stream: Stream,
stream: Stream,

unbuffered_out_stream: *Stream,

Expand Down Expand Up @@ -1084,7 +1084,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
// safety. If it is bad, it will be caught anyway.
const TagInt = @TagType(TagType);
const tag = try self.deserializeInt(TagInt);

inline for (info.fields) |field_info| {
if (field_info.enum_field.?.value == tag) {
const name = field_info.name;
Expand Down
4 changes: 2 additions & 2 deletions lib/std/io/seekable_stream.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub const SliceSeekableInStream = struct {
pub const Stream = InStream(Error);
pub const SeekableInStream = SeekableStream(SeekError, GetSeekPosError);

pub stream: Stream,
pub seekable_stream: SeekableInStream,
stream: Stream,
seekable_stream: SeekableInStream,

pos: usize,
slice: []const u8,
Expand Down
4 changes: 1 addition & 3 deletions lib/std/zig/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub const Error = union(enum) {
pub const ExpectedSuffixOp = SingleTokenError("Expected pointer dereference, optional unwrap, or field access, found '{}'");

pub const ExpectedParamType = SimpleError("Expected parameter type");
pub const ExpectedPubItem = SimpleError("Pub must be followed by fn decl, var decl, or container member");
pub const ExpectedPubItem = SimpleError("Expected function or variable declaration after pub");
pub const UnattachedDocComment = SimpleError("Unattached documentation comment");
pub const ExtraAlignQualifier = SimpleError("Extra align qualifier");
pub const ExtraConstQualifier = SimpleError("Extra const qualifier");
Expand Down Expand Up @@ -757,7 +757,6 @@ pub const Node = struct {
pub const ContainerField = struct {
base: Node,
doc_comments: ?*DocComment,
visib_token: ?TokenIndex,
name_token: TokenIndex,
type_expr: ?*Node,
value_expr: ?*Node,
Expand All @@ -780,7 +779,6 @@ pub const Node = struct {
}

pub fn firstToken(self: *const ContainerField) TokenIndex {
if (self.visib_token) |visib_token| return visib_token;
return self.name_token;
}

Expand Down
16 changes: 7 additions & 9 deletions lib/std/zig/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,15 @@ fn parseContainerMembers(arena: *Allocator, it: *TokenIterator, tree: *Tree) !No
continue;
}

if (visib_token != null) {
try tree.errors.push(AstError{
.ExpectedPubItem = AstError.ExpectedPubItem{ .token = it.index },
});
return error.ParseError;
}

if (try parseContainerField(arena, it, tree)) |node| {
const field = node.cast(Node.ContainerField).?;
field.visib_token = visib_token;
field.doc_comments = doc_comments;
try list.push(node);
const comma = eatToken(it, .Comma) orelse break;
Expand All @@ -149,13 +155,6 @@ fn parseContainerMembers(arena: *Allocator, it: *TokenIterator, tree: *Tree) !No
continue;
}

// Dangling pub
if (visib_token != null) {
try tree.errors.push(AstError{
.ExpectedPubItem = AstError.ExpectedPubItem{ .token = it.index },
});
}

break;
}

Expand Down Expand Up @@ -407,7 +406,6 @@ fn parseContainerField(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No
node.* = Node.ContainerField{
.base = Node{ .id = .ContainerField },
.doc_comments = null,
.visib_token = null,
.name_token = name_token,
.type_expr = type_expr,
.value_expr = value_expr,
Expand Down
6 changes: 3 additions & 3 deletions lib/std/zig/parser_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,7 @@ test "zig fmt: struct declaration" {
\\const S = struct {
\\ const Self = @This();
\\ f1: u8,
\\ pub f3: u8,
\\ f3: u8,
\\
\\ fn method(self: *Self) Self {
\\ return self.*;
Expand All @@ -1777,14 +1777,14 @@ test "zig fmt: struct declaration" {
\\
\\const Ps = packed struct {
\\ a: u8,
\\ pub b: u8,
\\ b: u8,
\\
\\ c: u8,
\\};
\\
\\const Es = extern struct {
\\ a: u8,
\\ pub b: u8,
\\ b: u8,
\\
\\ c: u8,
\\};
Expand Down
8 changes: 2 additions & 6 deletions lib/std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,6 @@ fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, i

try renderDocComments(tree, stream, field, indent, start_col);

if (field.visib_token) |visib_token| {
try renderToken(tree, stream, visib_token, indent, start_col, Space.Space); // pub
}

if (field.type_expr == null and field.value_expr == null) {
return renderToken(tree, stream, field.name_token, indent, start_col, Space.Comma); // name,
} else if (field.type_expr != null and field.value_expr == null) {
Expand Down Expand Up @@ -2206,8 +2202,8 @@ const FindByteOutStream = struct {
pub const Error = error{};
pub const Stream = std.io.OutStream(Error);

pub stream: Stream,
pub byte_found: bool,
stream: Stream,
byte_found: bool,
byte: u8,

pub fn init(byte: u8) Self {
Expand Down
2 changes: 0 additions & 2 deletions src/all_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,6 @@ struct AstNodeStructField {
// populated if the "align(A)" is present
AstNode *align_expr;
Buf doc_comments;

VisibMod visib_mod;
};

struct AstNodeStringLiteral {
Expand Down
16 changes: 7 additions & 9 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ static Token *ast_parse_doc_comments(ParseContext *pc, Buf *buf) {
// <- TestDecl ContainerMembers
// / TopLevelComptime ContainerMembers
// / KEYWORD_pub? TopLevelDecl ContainerMembers
// / KEYWORD_pub? ContainerField COMMA ContainerMembers
// / KEYWORD_pub? ContainerField
// / ContainerField COMMA ContainerMembers
// / ContainerField
// /
static AstNodeContainerDecl ast_parse_container_members(ParseContext *pc) {
AstNodeContainerDecl res = {};
Expand Down Expand Up @@ -548,10 +548,13 @@ static AstNodeContainerDecl ast_parse_container_members(ParseContext *pc) {
continue;
}

if (visib_token != nullptr) {
ast_error(pc, peek_token(pc), "expected function or variable declaration after pub");
}

AstNode *container_field = ast_parse_container_field(pc);
if (container_field != nullptr) {
assert(container_field->type == NodeTypeStructField);
container_field->data.struct_field.visib_mod = visib_mod;
container_field->data.struct_field.doc_comments = doc_comment_buf;
res.fields.append(container_field);
if (eat_token_if(pc, TokenIdComma) != nullptr) {
Expand All @@ -561,12 +564,7 @@ static AstNodeContainerDecl ast_parse_container_members(ParseContext *pc) {
}
}

// We visib_token wasn't eaten, then we haven't consumed the first token in this rule yet.
// It is therefore safe to return and let the caller continue parsing.
if (visib_token == nullptr)
break;

ast_invalid_token_error(pc, peek_token(pc));
break;
}

return res;
Expand Down