Compare commits

...

9 Commits

Author SHA1 Message Date
Luna 5f7b97e84c add AddressFamilyNotSupported to SendError 2020-12-07 16:40:03 -05:00
antlilja 5c60558796 Utilize std.fmt.parseUnsigned in main.zig.
* Remove function parseAnyBaseInt.
* Replace calls to parseAnyBaseInt to calls to std.fmt.parseUnsigned
with radix 0.
* Replace calls to parseInt where the type is unsigned with calls to
parseUnsigned. Note that the functionality of these arguments haven't
changed, they still retain the original radix specified.
2020-12-07 16:38:52 -05:00
xackus 85dc039632 std.c: freeing null is a no-op 2020-12-07 16:33:27 -05:00
g-w1 6294c1136c
stage2: variable shadowing detection (#6969) 2020-12-06 19:36:49 +02:00
Veikka Tuominen 0268f54fcf
Merge pull request #7313 from LemonBoy/booo
Fix a few unsound optimizations on single-element union/enum
2020-12-06 19:31:07 +02:00
LemonBoy b45d2968e5 Add some test cases for the previous commits 2020-12-05 20:14:04 +01:00
LemonBoy 97f36d93f4 stage1: Don't fold single-element enums too aggressively
When the tag type is not a zero-sized type (eg. `enum(i32)`) we
absolutely need to avoid constant-folding this values. Doing so masked
any invalid input and, since the folding was not even applied
consistently, introduced some hard to catch errors.
2020-12-05 20:14:04 +01:00
LemonBoy 0f7954831a stage1: Initialize single-element union tag correctly
Use the field value instead of defaulting to zero.
2020-12-05 18:58:54 +01:00
LemonBoy 865d6df03e stage1: Fix constant folding of single-element enums
Fill in the correct value instead of leaving everything uninitialized.
This problem can be noticed in behavior/union.zig but it's masked by
some other "optimization" kicking in at the wrong time, the following
commits will address that.
2020-12-05 18:58:37 +01:00
9 changed files with 181 additions and 47 deletions

View File

@ -248,7 +248,7 @@ pub extern "c" fn setresgid(rgid: gid_t, egid: gid_t, sgid: gid_t) c_int;
pub extern "c" fn malloc(usize) ?*c_void;
pub extern "c" fn realloc(?*c_void, usize) ?*c_void;
pub extern "c" fn free(*c_void) void;
pub extern "c" fn free(?*c_void) void;
pub extern "c" fn futimes(fd: fd_t, times: *[2]timeval) c_int;
pub extern "c" fn utimes(path: [*:0]const u8, times: *[2]timeval) c_int;

View File

@ -698,6 +698,7 @@ pub const File = struct {
error.FastOpenAlreadyInProgress,
error.MessageTooBig,
error.FileDescriptorNotASocket,
error.AddressFamilyNotSupported,
=> return self.writeFileAllUnseekable(in_file, args),
else => |e| return e,

View File

@ -2711,7 +2711,6 @@ pub const ShutdownError = error{
/// Connection was reset by peer, application should close socket as it is no longer usable.
ConnectionResetByPeer,
BlockingOperationInProgress,
/// The network subsystem has failed.
@ -2719,8 +2718,7 @@ pub const ShutdownError = error{
/// The socket is not connected (connection-oriented sockets only).
SocketNotConnected,
SystemResources
SystemResources,
} || UnexpectedError;
pub const ShutdownHow = enum { recv, send, both };
@ -4776,6 +4774,7 @@ pub const SendError = error{
BrokenPipe,
FileDescriptorNotASocket,
AddressFamilyNotSupported,
} || UnexpectedError;
/// Transmit a message to another socket.
@ -4822,6 +4821,7 @@ pub fn sendto(
.WSAEMSGSIZE => return error.MessageTooBig,
.WSAENOBUFS => return error.SystemResources,
.WSAENOTSOCK => return error.FileDescriptorNotASocket,
.WSAEAFNOSUPPORT => return error.AddressFamilyNotSupported,
// TODO: handle more errors
else => |err| return windows.unexpectedWSAError(err),
}
@ -4849,6 +4849,7 @@ pub fn sendto(
ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket.
EOPNOTSUPP => unreachable, // Some bit in the flags argument is inappropriate for the socket type.
EPIPE => return error.BrokenPipe,
EAFNOSUPPORT => return error.AddressFamilyNotSupported,
else => |err| return unexpectedErrno(err),
}
}

View File

@ -489,7 +489,6 @@ fn varDecl(
node: *ast.Node.VarDecl,
block_arena: *Allocator,
) InnerError!*Scope {
// TODO implement detection of shadowing
if (node.getComptimeToken()) |comptime_token| {
return mod.failTok(scope, comptime_token, "TODO implement comptime locals", .{});
}
@ -499,6 +498,34 @@ fn varDecl(
const tree = scope.tree();
const name_src = tree.token_locs[node.name_token].start;
const ident_name = try identifierTokenString(mod, scope, node.name_token);
// Local variables shadowing detection, including function parameters.
{
var s = scope;
while (true) switch (s.tag) {
.local_val => {
const local_val = s.cast(Scope.LocalVal).?;
if (mem.eql(u8, local_val.name, ident_name)) {
return mod.fail(scope, name_src, "redefinition of '{}'", .{ident_name});
}
s = local_val.parent;
},
.local_ptr => {
const local_ptr = s.cast(Scope.LocalPtr).?;
if (mem.eql(u8, local_ptr.name, ident_name)) {
return mod.fail(scope, name_src, "redefinition of '{}'", .{ident_name});
}
s = local_ptr.parent;
},
.gen_zir => s = s.cast(Scope.GenZIR).?.parent,
else => break,
};
}
// Namespace vars shadowing detection
if (mod.lookupDeclName(scope, ident_name)) |_| {
return mod.fail(scope, name_src, "redefinition of '{}'", .{ident_name});
}
const init_node = node.getInitNode() orelse
return mod.fail(scope, name_src, "variables must be initialized", .{});

View File

@ -688,11 +688,15 @@ fn buildOutputType(
} else if (mem.eql(u8, arg, "--stack")) {
if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
i += 1;
stack_size_override = parseAnyBaseInt(args[i]);
stack_size_override = std.fmt.parseUnsigned(u64, args[i], 0) catch |err| {
fatal("unable to parse '{}': {}", .{ arg, @errorName(err) });
};
} else if (mem.eql(u8, arg, "--image-base")) {
if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
i += 1;
image_base_override = parseAnyBaseInt(args[i]);
image_base_override = std.fmt.parseUnsigned(u64, args[i], 0) catch |err| {
fatal("unable to parse '{}': {}", .{ arg, @errorName(err) });
};
} else if (mem.eql(u8, arg, "--name")) {
if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
i += 1;
@ -1225,7 +1229,7 @@ fn buildOutputType(
if (i >= linker_args.items.len) {
fatal("expected linker arg after '{}'", .{arg});
}
version.major = std.fmt.parseInt(u32, linker_args.items[i], 10) catch |err| {
version.major = std.fmt.parseUnsigned(u32, linker_args.items[i], 10) catch |err| {
fatal("unable to parse '{}': {}", .{ arg, @errorName(err) });
};
have_version = true;
@ -1234,7 +1238,7 @@ fn buildOutputType(
if (i >= linker_args.items.len) {
fatal("expected linker arg after '{}'", .{arg});
}
version.minor = std.fmt.parseInt(u32, linker_args.items[i], 10) catch |err| {
version.minor = std.fmt.parseUnsigned(u32, linker_args.items[i], 10) catch |err| {
fatal("unable to parse '{}': {}", .{ arg, @errorName(err) });
};
have_version = true;
@ -1243,13 +1247,17 @@ fn buildOutputType(
if (i >= linker_args.items.len) {
fatal("expected linker arg after '{}'", .{arg});
}
stack_size_override = parseAnyBaseInt(linker_args.items[i]);
stack_size_override = std.fmt.parseUnsigned(u64, linker_args.items[i], 0) catch |err| {
fatal("unable to parse '{}': {}", .{ arg, @errorName(err) });
};
} else if (mem.eql(u8, arg, "--image-base")) {
i += 1;
if (i >= linker_args.items.len) {
fatal("expected linker arg after '{}'", .{arg});
}
image_base_override = parseAnyBaseInt(linker_args.items[i]);
image_base_override = std.fmt.parseUnsigned(u64, linker_args.items[i], 0) catch |err| {
fatal("unable to parse '{}': {}", .{ arg, @errorName(err) });
};
} else {
warn("unsupported linker arg: {}", .{arg});
}
@ -3188,18 +3196,3 @@ pub fn cleanExit() void {
process.exit(0);
}
}
fn parseAnyBaseInt(prefixed_bytes: []const u8) u64 {
const base: u8 = if (mem.startsWith(u8, prefixed_bytes, "0x"))
16
else if (mem.startsWith(u8, prefixed_bytes, "0o"))
8
else if (mem.startsWith(u8, prefixed_bytes, "0b"))
2
else
@as(u8, 10);
const bytes = if (base == 10) prefixed_bytes else prefixed_bytes[2..];
return std.fmt.parseInt(u64, bytes, base) catch |err| {
fatal("unable to parse '{}': {}", .{ prefixed_bytes, @errorName(err) });
};
}

View File

@ -6113,7 +6113,7 @@ ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry) {
TypeUnionField *only_field = &union_type->data.unionation.fields[0];
ZigType *field_type = resolve_union_field_type(g, only_field);
assert(field_type);
bigint_init_unsigned(&result->data.x_union.tag, 0);
bigint_init_bigint(&result->data.x_union.tag, &only_field->enum_field->value);
result->data.x_union.payload = g->pass1_arena->create<ZigValue>();
copy_const_val(g, result->data.x_union.payload,
get_the_one_possible_value(g, field_type));
@ -6122,6 +6122,11 @@ ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry) {
result->data.x_ptr.mut = ConstPtrMutComptimeConst;
result->data.x_ptr.special = ConstPtrSpecialRef;
result->data.x_ptr.data.ref.pointee = get_the_one_possible_value(g, result->type->data.pointer.child_type);
} else if (result->type->id == ZigTypeIdEnum) {
ZigType *enum_type = result->type;
assert(enum_type->data.enumeration.src_field_count == 1);
TypeEnumField *only_field = &result->type->data.enumeration.fields[0];
bigint_init_bigint(&result->data.x_enum_tag, &only_field->value);
}
g->one_possible_values.put(type_entry, result);
return result;

View File

@ -14186,6 +14186,18 @@ static ZigType *ir_resolve_union_tag_type(IrAnalyze *ira, AstNode *source_node,
}
}
static bool can_fold_enum_type(ZigType *ty) {
assert(ty->id == ZigTypeIdEnum);
// We can fold the enum type (and avoid any check, be it at runtime or at
// compile time) iff it has only a single element and its tag type is
// zero-sized.
ZigType *tag_int_type = ty->data.enumeration.tag_int_type;
return ty->data.enumeration.layout == ContainerLayoutAuto &&
ty->data.enumeration.src_field_count == 1 &&
!ty->data.enumeration.non_exhaustive &&
(tag_int_type->id == ZigTypeIdInt && tag_int_type->data.integral.bit_count == 0);
}
static IrInstGen *ir_analyze_enum_to_int(IrAnalyze *ira, IrInst *source_instr, IrInstGen *target) {
Error err;
@ -14214,10 +14226,7 @@ static IrInstGen *ir_analyze_enum_to_int(IrAnalyze *ira, IrInst *source_instr, I
assert(tag_type->id == ZigTypeIdInt || tag_type->id == ZigTypeIdComptimeInt);
// If there is only one possible tag, then we know at comptime what it is.
if (enum_type->data.enumeration.layout == ContainerLayoutAuto &&
enum_type->data.enumeration.src_field_count == 1 &&
!enum_type->data.enumeration.non_exhaustive)
{
if (can_fold_enum_type(enum_type)) {
IrInstGen *result = ir_const(ira, source_instr, tag_type);
init_const_bigint(result->value, tag_type,
&enum_type->data.enumeration.fields[0].value);
@ -14255,10 +14264,7 @@ static IrInstGen *ir_analyze_union_to_tag(IrAnalyze *ira, IrInst* source_instr,
}
// If there is only 1 possible tag, then we know at comptime what it is.
if (wanted_type->data.enumeration.layout == ContainerLayoutAuto &&
wanted_type->data.enumeration.src_field_count == 1 &&
!wanted_type->data.enumeration.non_exhaustive)
{
if (can_fold_enum_type(wanted_type)) {
IrInstGen *result = ir_const(ira, source_instr, wanted_type);
result->value->special = ConstValSpecialStatic;
result->value->type = wanted_type;
@ -24039,7 +24045,8 @@ static IrInstGen *ir_analyze_instruction_switch_target(IrAnalyze *ira,
bigint_init_bigint(&result->value->data.x_enum_tag, &pointee_val->data.x_union.tag);
return result;
}
if (tag_type->data.enumeration.src_field_count == 1 && !tag_type->data.enumeration.non_exhaustive) {
if (can_fold_enum_type(tag_type)) {
IrInstGen *result = ir_const(ira, &switch_target_instruction->base.base, tag_type);
TypeEnumField *only_field = &tag_type->data.enumeration.fields[0];
bigint_init_bigint(&result->value->data.x_enum_tag, &only_field->value);
@ -24054,7 +24061,8 @@ static IrInstGen *ir_analyze_instruction_switch_target(IrAnalyze *ira,
case ZigTypeIdEnum: {
if ((err = type_resolve(ira->codegen, target_type, ResolveStatusZeroBitsKnown)))
return ira->codegen->invalid_inst_gen;
if (target_type->data.enumeration.src_field_count == 1 && !target_type->data.enumeration.non_exhaustive) {
if (can_fold_enum_type(target_type)) {
TypeEnumField *only_field = &target_type->data.enumeration.fields[0];
IrInstGen *result = ir_const(ira, &switch_target_instruction->base.base, target_type);
bigint_init_bigint(&result->value->data.x_enum_tag, &only_field->value);
@ -24789,7 +24797,9 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc
if (type_is_invalid(target->value->type))
return ira->codegen->invalid_inst_gen;
if (target->value->type->id == ZigTypeIdEnumLiteral) {
ZigType *target_type = target->value->type;
if (target_type->id == ZigTypeIdEnumLiteral) {
IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr);
Buf *field_name = target->value->data.x_enum_literal;
ZigValue *array_val = create_const_str_lit(ira->codegen, field_name)->data.x_ptr.data.ref.pointee;
@ -24797,21 +24807,21 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc
return result;
}
if (target->value->type->id == ZigTypeIdUnion) {
if (target_type->id == ZigTypeIdUnion) {
target = ir_analyze_union_tag(ira, &instruction->base.base, target, instruction->base.is_gen);
if (type_is_invalid(target->value->type))
return ira->codegen->invalid_inst_gen;
target_type = target->value->type;
}
if (target->value->type->id != ZigTypeIdEnum) {
if (target_type->id != ZigTypeIdEnum) {
ir_add_error(ira, &target->base,
buf_sprintf("expected enum tag, found '%s'", buf_ptr(&target->value->type->name)));
buf_sprintf("expected enum tag, found '%s'", buf_ptr(&target_type->name)));
return ira->codegen->invalid_inst_gen;
}
if (target->value->type->data.enumeration.src_field_count == 1 &&
!target->value->type->data.enumeration.non_exhaustive) {
TypeEnumField *only_field = &target->value->type->data.enumeration.fields[0];
if (can_fold_enum_type(target_type)) {
TypeEnumField *only_field = &target_type->data.enumeration.fields[0];
ZigValue *array_val = create_const_str_lit(ira->codegen, only_field->name)->data.x_ptr.data.ref.pointee;
IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr);
init_const_slice(ira->codegen, result->value, array_val, 0, buf_len(only_field->name), true);
@ -24819,9 +24829,9 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc
}
if (instr_is_comptime(target)) {
if ((err = type_resolve(ira->codegen, target->value->type, ResolveStatusZeroBitsKnown)))
if ((err = type_resolve(ira->codegen, target_type, ResolveStatusZeroBitsKnown)))
return ira->codegen->invalid_inst_gen;
TypeEnumField *field = find_enum_field_by_tag(target->value->type, &target->value->data.x_bigint);
TypeEnumField *field = find_enum_field_by_tag(target_type, &target->value->data.x_bigint);
if (field == nullptr) {
Buf *int_buf = buf_alloc();
bigint_append_buf(int_buf, &target->value->data.x_bigint, 10);

View File

@ -1,6 +1,85 @@
const tests = @import("tests.zig");
pub fn addCases(cases: *tests.CompareOutputContext) void {
{
const check_panic_msg =
\\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
\\ if (std.mem.eql(u8, message, "reached unreachable code")) {
\\ std.process.exit(126); // good
\\ }
\\ std.process.exit(0); // test failed
\\}
;
cases.addRuntimeSafety("switch on corrupted enum value",
\\const std = @import("std");
++ check_panic_msg ++
\\const E = enum(u32) {
\\ X = 1,
\\};
\\pub fn main() void {
\\ var e: E = undefined;
\\ @memset(@ptrCast([*]u8, &e), 0x55, @sizeOf(E));
\\ switch (e) {
\\ .X => @breakpoint(),
\\ }
\\}
);
cases.addRuntimeSafety("switch on corrupted union value",
\\const std = @import("std");
++ check_panic_msg ++
\\const U = union(enum(u32)) {
\\ X: u8,
\\};
\\pub fn main() void {
\\ var u: U = undefined;
\\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
\\ switch (u) {
\\ .X => @breakpoint(),
\\ }
\\}
);
}
{
const check_panic_msg =
\\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
\\ if (std.mem.eql(u8, message, "invalid enum value")) {
\\ std.process.exit(126); // good
\\ }
\\ std.process.exit(0); // test failed
\\}
;
cases.addRuntimeSafety("@tagName on corrupted enum value",
\\const std = @import("std");
++ check_panic_msg ++
\\const E = enum(u32) {
\\ X = 1,
\\};
\\pub fn main() void {
\\ var e: E = undefined;
\\ @memset(@ptrCast([*]u8, &e), 0x55, @sizeOf(E));
\\ var n = @tagName(e);
\\}
);
cases.addRuntimeSafety("@tagName on corrupted union value",
\\const std = @import("std");
++ check_panic_msg ++
\\const U = union(enum(u32)) {
\\ X: u8,
\\};
\\pub fn main() void {
\\ var u: U = undefined;
\\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
\\ var t: @TagType(U) = u;
\\ var n = @tagName(t);
\\}
);
}
{
const check_panic_msg =
\\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {

View File

@ -830,7 +830,7 @@ pub fn addCases(ctx: *TestContext) !void {
// Character literals and multiline strings.
case.addCompareOutput(
\\export fn _start() noreturn {
\\ const ignore =
\\ const ignore =
\\ \\ cool thx
\\ \\
\\ ;
@ -1113,6 +1113,24 @@ pub fn addCases(ctx: *TestContext) !void {
\\fn entry() void {}
, &[_][]const u8{":2:4: error: redefinition of 'entry'"});
{
var case = ctx.obj("variable shadowing", linux_x64);
case.addError(
\\export fn _start() noreturn {
\\ var i: u32 = 10;
\\ var i: u32 = 10;
\\ unreachable;
\\}
, &[_][]const u8{":3:9: error: redefinition of 'i'"});
case.addError(
\\var testing: i64 = 10;
\\export fn _start() noreturn {
\\ var testing: i64 = 20;
\\ unreachable;
\\}
, &[_][]const u8{":3:9: error: redefinition of 'testing'"});
}
{
var case = ctx.obj("extern variable has no type", linux_x64);
case.addError(