AstGen: handle rl_ty_inst for mutable variables

This commit is contained in:
Veikka Tuominen
2022-04-15 10:41:35 +03:00
parent 4ef1c1c705
commit 4911d39769
2 changed files with 24 additions and 1 deletions

View File

@@ -2851,6 +2851,9 @@ fn varDecl(
return &sub_scope.base;
},
.keyword_var => {
const old_rl_ty_inst = gz.rl_ty_inst;
defer gz.rl_ty_inst = old_rl_ty_inst;
const is_comptime = var_decl.comptime_token != null or gz.force_comptime;
var resolve_inferred_alloc: Zir.Inst.Ref = .none;
const var_data: struct {
@@ -2875,6 +2878,7 @@ fn varDecl(
});
}
};
gz.rl_ty_inst = type_inst;
break :a .{ .alloc = alloc, .result_loc = .{ .ptr = alloc } };
} else a: {
const alloc = alloc: {
@@ -2894,6 +2898,7 @@ fn varDecl(
});
}
};
gz.rl_ty_inst = .none;
resolve_inferred_alloc = alloc;
break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc } };
};

View File

@@ -859,7 +859,6 @@ test "catch in block has correct result location" {
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
const S = struct {
fn open() error{A}!@This() {
@@ -887,3 +886,22 @@ test "labeled block with runtime branch forwards its result location type to bre
};
try expect(e == .b);
}
test "try in labeled block doesn't cast to wrong type" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
const S = struct {
a: u32,
fn foo() anyerror!u32 {
return 1;
}
};
const s: ?*S = blk: {
var a = try S.foo();
_ = a;
break :blk null;
};
_ = s;
}