Skip to content
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

implement defining C variadic functions #13914

Merged
merged 3 commits into from
Dec 18, 2022
Merged
Changes from 1 commit
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
Next Next commit
Sema: make is_non_{null,err} stricter about types
Closes #13023
Vexu committed Dec 17, 2022
commit 58caed1c71179f48c4e7bffadef0392fa8381e72
4 changes: 2 additions & 2 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
@@ -6071,15 +6071,15 @@ fn whileExpr(
const tag: Zir.Inst.Tag = if (payload_is_ref) .is_non_err_ptr else .is_non_err;
break :c .{
.inst = err_union,
.bool_bit = try cond_scope.addUnNode(tag, err_union, while_full.ast.then_expr),
.bool_bit = try cond_scope.addUnNode(tag, err_union, while_full.ast.cond_expr),
};
} else if (while_full.payload_token) |_| {
const cond_ri: ResultInfo = .{ .rl = if (payload_is_ref) .ref else .none };
const optional = try expr(&cond_scope, &cond_scope.base, cond_ri, while_full.ast.cond_expr);
const tag: Zir.Inst.Tag = if (payload_is_ref) .is_non_null_ptr else .is_non_null;
break :c .{
.inst = optional,
.bool_bit = try cond_scope.addUnNode(tag, optional, while_full.ast.then_expr),
.bool_bit = try cond_scope.addUnNode(tag, optional, while_full.ast.cond_expr),
};
} else {
const cond = try expr(&cond_scope, &cond_scope.base, bool_ri, while_full.ast.cond_expr);
23 changes: 23 additions & 0 deletions src/Sema.zig
Original file line number Diff line number Diff line change
@@ -16356,6 +16356,15 @@ fn finishCondBr(
return Air.indexToRef(block_inst);
}

fn checkNullableType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void {
switch (ty.zigTypeTag()) {
.Optional, .Null, .Undefined => return,
.Pointer => if (ty.isPtrLikeOptional()) return,
else => {},
}
return sema.failWithExpectedOptionalType(block, src, ty);
}

fn zirIsNonNull(
sema: *Sema,
block: *Block,
@@ -16367,6 +16376,7 @@ fn zirIsNonNull(
const inst_data = sema.code.instructions.items(.data)[inst].un_node;
const src = inst_data.src();
const operand = try sema.resolveInst(inst_data.operand);
try sema.checkNullableType(block, src, sema.typeOf(operand));
return sema.analyzeIsNull(block, src, operand, true);
}

@@ -16381,19 +16391,31 @@ fn zirIsNonNullPtr(
const inst_data = sema.code.instructions.items(.data)[inst].un_node;
const src = inst_data.src();
const ptr = try sema.resolveInst(inst_data.operand);
try sema.checkNullableType(block, src, sema.typeOf(ptr).elemType2());
if ((try sema.resolveMaybeUndefVal(ptr)) == null) {
return block.addUnOp(.is_non_null_ptr, ptr);
}
const loaded = try sema.analyzeLoad(block, src, ptr, src);
return sema.analyzeIsNull(block, src, loaded, true);
}

fn checkErrorType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void {
switch (ty.zigTypeTag()) {
.ErrorSet, .ErrorUnion, .Undefined => return,
else => return sema.fail(block, src, "expected error union type, found '{}'", .{
ty.fmt(sema.mod),
}),
}
}

fn zirIsNonErr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
const tracy = trace(@src());
defer tracy.end();

const inst_data = sema.code.instructions.items(.data)[inst].un_node;
const src = inst_data.src();
const operand = try sema.resolveInst(inst_data.operand);
try sema.checkErrorType(block, src, sema.typeOf(operand));
return sema.analyzeIsNonErr(block, inst_data.src(), operand);
}

@@ -16404,6 +16426,7 @@ fn zirIsNonErrPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
const inst_data = sema.code.instructions.items(.data)[inst].un_node;
const src = inst_data.src();
const ptr = try sema.resolveInst(inst_data.operand);
try sema.checkErrorType(block, src, sema.typeOf(ptr).elemType2());
const loaded = try sema.analyzeLoad(block, src, ptr, src);
return sema.analyzeIsNonErr(block, src, loaded);
}
2 changes: 1 addition & 1 deletion src/link/MachO/load_commands.zig
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ fn calcLCsSize(gpa: Allocator, options: *const link.Options, ctx: CalcLCsSizeCtx
// LC_DYLD_INFO_ONLY
sizeofcmds += @sizeOf(macho.dyld_info_command);
// LC_FUNCTION_STARTS
if (has_text_segment and ctx.wants_function_starts) |_| {
if (has_text_segment and ctx.wants_function_starts) {
sizeofcmds += @sizeOf(macho.linkedit_data_command);
}
// LC_DATA_IN_CODE
24 changes: 24 additions & 0 deletions test/cases/compile_errors/invalid_capture_type.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export fn f1() void {
if (true) |x| { _ = x; }
}
export fn f2() void {
if (@as(usize, 5)) |_| {}
}
export fn f3() void {
if (@as(usize, 5)) |_| {} else |_| {}
}
export fn f4() void {
if (null) |_| {}
}
export fn f5() void {
if (error.Foo) |_| {} else |_| {}
}

// error
// backend=stage2
// target=native
//
// :2:9: error: expected optional type, found 'bool'
// :5:9: error: expected optional type, found 'usize'
// :8:9: error: expected error union type, found 'usize'
// :14:9: error: expected error union type, found 'error{Foo}'
9 changes: 0 additions & 9 deletions test/cases/compile_errors/stage1/obj/invalid_maybe_type.zig

This file was deleted.