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

Add missing cast to return type #3718

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion lib/std/child_process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub const ChildProcess = struct {

/// Windows-only. `cwd` was provided, but the path did not exist when spawning the child process.
CurrentWorkingDirectoryUnlinked,
} || os.ExecveError || os.SetIdError || os.ChangeCurDirError || windows.CreateProcessError;
} || os.ExecveError || os.SetIdError || os.ChangeCurDirError || windows.CreateProcessError || windows.WaitForSingleObjectError;

pub const Term = union(enum) {
Exited: u32,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/coff.zig
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub const Coff = struct {
if (byte != 0 and i == buffer.len)
return error.NameTooLong;

return i;
return @as(usize, i);
}

pub fn loadSections(self: *Coff) !void {
Expand Down
2 changes: 1 addition & 1 deletion src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7725,7 +7725,7 @@ static void do_code_gen(CodeGen *g) {

char *error = nullptr;
if (LLVMVerifyModule(g->module, LLVMReturnStatusAction, &error)) {
zig_panic("broken LLVM module found: %s", error);
zig_panic("broken LLVM module found: %s\nThis is a bug in the Zig compiler.", error);
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13535,16 +13535,6 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio
if (type_is_invalid(operand->value.type))
return ir_unreach_error(ira);

if (!instr_is_comptime(operand) && ira->explicit_return_type != nullptr &&
handle_is_ptr(ira->explicit_return_type))
{
// result location mechanism took care of it.
IrInstruction *result = ir_build_return(&ira->new_irb, instruction->base.scope,
instruction->base.source_node, nullptr);
result->value.type = ira->codegen->builtin_types.entry_unreachable;
return ir_finish_anal(ira, result);
}

IrInstruction *casted_operand = ir_implicit_cast(ira, operand, ira->explicit_return_type);
if (type_is_invalid(casted_operand->value.type)) {
AstNode *source_node = ira->explicit_return_type_source_node;
Expand All @@ -13556,6 +13546,16 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio
return ir_unreach_error(ira);
}

if (!instr_is_comptime(operand) && ira->explicit_return_type != nullptr &&
handle_is_ptr(ira->explicit_return_type))
{
// result location mechanism took care of it.
IrInstruction *result = ir_build_return(&ira->new_irb, instruction->base.scope,
instruction->base.source_node, nullptr);
result->value.type = ira->codegen->builtin_types.entry_unreachable;
return ir_finish_anal(ira, result);
}

if (casted_operand->value.special == ConstValSpecialRuntime &&
casted_operand->value.type->id == ZigTypeIdPointer &&
casted_operand->value.data.rh_ptr == RuntimeHintPtrStack)
Expand Down
21 changes: 21 additions & 0 deletions test/compile_errors.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ const tests = @import("tests.zig");
const builtin = @import("builtin");

pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add(
"incorrect return type",
\\ pub export fn entry() void{
\\ _ = foo();
\\ }
\\ const A = struct {
\\ a: u32,
\\ };
\\ fn foo() A {
\\ return bar();
\\ }
\\ const B = struct {
\\ a: u32,
\\ };
\\ fn bar() B {
\\ unreachable;
\\ }
,
"tmp.zig:8:16: error: expected type 'A', found 'B'",
);

cases.add(
"regression test #2980: base type u32 is not type checked properly when assigning a value within a struct",
\\const Foo = struct {
Expand Down