authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-10-03 22:46:22-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-10-03 22:46:22-04:00
log54eb0f2daa2cf5237da05e3cfad5800b9ccf424e
treedd1eb2e1a9a936a6d48bacff436a4ded191ffb34
parent8eb96c32e38d687c2b2f1488105538dda8dd5b0b
parent9cad44770a15d189cddb749bc1b2fca9e24b858f
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #13032 from jacobly0/br-on-undef-val

stage2: fix branches on undefined values

6 files changed, 45 insertions(+), 6 deletions(-)

src/Module.zig+2
......@@ -4430,6 +4430,8 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void {
44304430 new_decl.has_linksection_or_addrspace = false;
44314431 new_decl.ty = ty_ty;
44324432 new_decl.val = struct_val;
4433 new_decl.@"align" = 0;
4434 new_decl.@"linksection" = null;
44334435 new_decl.has_tv = true;
44344436 new_decl.owns_tv = true;
44354437 new_decl.alive = true; // This Decl corresponds to a File and is therefore always alive.
src/Sema.zig+11-4
......@@ -8107,6 +8107,13 @@ fn funcCommon(
81078107 for (comptime_params) |ct| is_generic = is_generic or ct;
81088108 is_generic = is_generic or ret_ty_requires_comptime;
81098109
8110 if (!is_generic and sema.wantErrorReturnTracing(return_type)) {
8111 // Make sure that StackTrace's fields are resolved so that the backend can
8112 // lower this fn type.
8113 const unresolved_stack_trace_ty = try sema.getBuiltinType(block, ret_ty_src, "StackTrace");
8114 _ = try sema.resolveTypeFields(block, ret_ty_src, unresolved_stack_trace_ty);
8115 }
8116
81108117 break :fn_ty try Type.Tag.function.create(sema.arena, .{
81118118 .param_types = param_types,
81128119 .comptime_params = comptime_params.ptr,
......@@ -15631,7 +15638,7 @@ fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir
1563115638 return sema.analyzeRet(block, operand, src);
1563215639 }
1563315640
15634 if (sema.wantErrorReturnTracing()) {
15641 if (sema.wantErrorReturnTracing(sema.fn_ret_ty)) {
1563515642 const is_non_err = try sema.analyzePtrIsNonErr(block, src, ret_ptr);
1563615643 return retWithErrTracing(sema, block, src, is_non_err, .ret_load, ret_ptr);
1563715644 }
......@@ -15698,11 +15705,11 @@ fn retWithErrTracing(
1569815705 return always_noreturn;
1569915706}
1570015707
15701fn wantErrorReturnTracing(sema: *Sema) bool {
15708fn wantErrorReturnTracing(sema: *Sema, fn_ret_ty: Type) bool {
1570215709 // TODO implement this feature in all the backends and then delete this check.
1570315710 const backend_supports_error_return_tracing = sema.mod.comp.bin_file.options.use_llvm;
1570415711
15705 return sema.fn_ret_ty.isError() and
15712 return fn_ret_ty.isError() and
1570615713 sema.mod.comp.bin_file.options.error_return_tracing and
1570715714 backend_supports_error_return_tracing;
1570815715}
......@@ -15754,7 +15761,7 @@ fn analyzeRet(
1575415761
1575515762 try sema.resolveTypeLayout(block, src, sema.fn_ret_ty);
1575615763
15757 if (sema.wantErrorReturnTracing()) {
15764 if (sema.wantErrorReturnTracing(sema.fn_ret_ty)) {
1575815765 // Avoid adding a frame to the error return trace in case the value is comptime-known
1575915766 // to be not an error.
1576015767 const is_non_err = try sema.analyzeIsNonErr(block, src, operand);
src/codegen/llvm.zig+5-2
......@@ -2332,10 +2332,13 @@ pub const Object = struct {
23322332 // buffer is only used for int_type, `builtin` is a struct.
23332333 const builtin_ty = mod.declPtr(builtin_decl).val.toType(undefined);
23342334 const builtin_namespace = builtin_ty.getNamespace().?;
2335 const stack_trace_decl = builtin_namespace.decls
2335 const stack_trace_decl_index = builtin_namespace.decls
23362336 .getKeyAdapted(stack_trace_str, Module.DeclAdapter{ .mod = mod }).?;
2337 const stack_trace_decl = mod.declPtr(stack_trace_decl_index);
23372338
2338 return mod.declPtr(stack_trace_decl).val.toType(undefined);
2339 // Sema should have ensured that StackTrace was analyzed.
2340 assert(stack_trace_decl.has_tv);
2341 return stack_trace_decl.val.toType(undefined);
23392342 }
23402343};
23412344
test/standalone.zig+2
......@@ -97,4 +97,6 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
9797 // Disabled due to tripping LLVM 13 assertion:
9898 // https://github.com/ziglang/zig/issues/12015
9999 //cases.add("tools/update_spirv_features.zig");
100
101 cases.addBuildFile("test/standalone/issue_13030/build.zig", .{ .build_modes = true });
100102}
test/standalone/issue_13030/build.zig created+18
......@@ -0,0 +1,18 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const Builder = std.build.Builder;
4const CrossTarget = std.zig.CrossTarget;
5
6pub fn build(b: *Builder) void {
7 const mode = b.standardReleaseOptions();
8 const target = b.standardTargetOptions(.{});
9
10 const obj = b.addObject("main", "main.zig");
11 obj.setBuildMode(mode);
12
13 obj.setTarget(target);
14 b.default_step.dependOn(&obj.step);
15
16 const test_step = b.step("test", "Test the program");
17 test_step.dependOn(&obj.step);
18}
test/standalone/issue_13030/main.zig created+7
......@@ -0,0 +1,7 @@
1fn b(comptime T: type) ?*const fn () error{}!T {
2 return null;
3}
4
5export fn entry() void {
6 _ = b(void);
7}