Skip to content

Commit a3232c6

Browse files
Vexuandrewrk
authored andcommitted
Merge pull request #13585 from Vexu/stage2-fixes
Stage2 bug fixes
1 parent 10a660e commit a3232c6

20 files changed

+454
-89
lines changed

src/Module.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,8 @@ pub const Struct = struct {
10441044

10451045
.root => return queryFieldSrc(tree.*, query, file, tree.containerDeclRoot()),
10461046

1047-
else => unreachable,
1047+
// This struct was generated using @Type
1048+
else => return s.srcLoc(mod),
10481049
}
10491050
}
10501051

@@ -1270,7 +1271,8 @@ pub const Union = struct {
12701271
.tagged_union_enum_tag,
12711272
.tagged_union_enum_tag_trailing,
12721273
=> return queryFieldSrc(tree.*, query, file, tree.taggedUnionEnumTag(node)),
1273-
else => unreachable,
1274+
// This union was generated using @Type
1275+
else => return u.srcLoc(mod),
12741276
}
12751277
}
12761278

@@ -4631,7 +4633,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
46314633
const address_space_src: LazySrcLoc = .{ .node_offset_var_decl_addrspace = 0 };
46324634
const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = 0 };
46334635
const init_src: LazySrcLoc = .{ .node_offset_var_decl_init = 0 };
4634-
const decl_tv = try sema.resolveInstValue(&block_scope, init_src, result_ref, undefined);
4636+
const decl_tv = try sema.resolveInstValue(&block_scope, init_src, result_ref, "global variable initializer must be comptime-known");
46354637

46364638
// Note this resolves the type of the Decl, not the value; if this Decl
46374639
// is a struct, for example, this resolves `type` (which needs no resolution),

src/Sema.zig

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub const Block = struct {
128128
/// Shared among all child blocks.
129129
sema: *Sema,
130130
/// The namespace to use for lookups from this source block
131-
/// When analyzing fields, this is different from src_decl.src_namepsace.
131+
/// When analyzing fields, this is different from src_decl.src_namespace.
132132
namespace: *Namespace,
133133
/// The AIR instructions generated for this block.
134134
instructions: std.ArrayListUnmanaged(Air.Inst.Index),
@@ -1897,10 +1897,15 @@ fn resolveMaybeUndefValAllowVariablesMaybeRuntime(
18971897
}
18981898
i -= Air.Inst.Ref.typed_value_map.len;
18991899

1900+
const air_tags = sema.air_instructions.items(.tag);
19001901
if (try sema.typeHasOnePossibleValue(sema.typeOf(inst))) |opv| {
1902+
if (air_tags[i] == .constant) {
1903+
const ty_pl = sema.air_instructions.items(.data)[i].ty_pl;
1904+
const val = sema.air_values.items[ty_pl.payload];
1905+
if (val.tag() == .variable) return val;
1906+
}
19011907
return opv;
19021908
}
1903-
const air_tags = sema.air_instructions.items(.tag);
19041909
switch (air_tags[i]) {
19051910
.constant => {
19061911
const ty_pl = sema.air_instructions.items(.data)[i].ty_pl;
@@ -4106,6 +4111,7 @@ fn validateStructInit(
41064111
.{fqn},
41074112
);
41084113
}
4114+
root_msg = null;
41094115
return sema.failWithOwnedErrorMsg(msg);
41104116
}
41114117

@@ -4225,7 +4231,6 @@ fn validateStructInit(
42254231
}
42264232

42274233
if (root_msg) |msg| {
4228-
root_msg = null;
42294234
if (struct_ty.castTag(.@"struct")) |struct_obj| {
42304235
const fqn = try struct_obj.data.getFullyQualifiedName(sema.mod);
42314236
defer gpa.free(fqn);
@@ -4236,6 +4241,7 @@ fn validateStructInit(
42364241
.{fqn},
42374242
);
42384243
}
4244+
root_msg = null;
42394245
return sema.failWithOwnedErrorMsg(msg);
42404246
}
42414247

@@ -4283,29 +4289,42 @@ fn zirValidateArrayInit(
42834289
const array_ty = sema.typeOf(array_ptr).childType();
42844290
const array_len = array_ty.arrayLen();
42854291

4286-
if (instrs.len != array_len and array_ty.isTuple()) {
4287-
const struct_obj = array_ty.castTag(.tuple).?.data;
4288-
var root_msg: ?*Module.ErrorMsg = null;
4289-
errdefer if (root_msg) |msg| msg.destroy(sema.gpa);
4292+
if (instrs.len != array_len) switch (array_ty.zigTypeTag()) {
4293+
.Struct => {
4294+
const struct_obj = array_ty.castTag(.tuple).?.data;
4295+
var root_msg: ?*Module.ErrorMsg = null;
4296+
errdefer if (root_msg) |msg| msg.destroy(sema.gpa);
42904297

4291-
for (struct_obj.values) |default_val, i| {
4292-
if (i < instrs.len) continue;
4298+
for (struct_obj.values) |default_val, i| {
4299+
if (i < instrs.len) continue;
42934300

4294-
if (default_val.tag() == .unreachable_value) {
4295-
const template = "missing tuple field with index {d}";
4296-
if (root_msg) |msg| {
4297-
try sema.errNote(block, init_src, msg, template, .{i});
4298-
} else {
4299-
root_msg = try sema.errMsg(block, init_src, template, .{i});
4301+
if (default_val.tag() == .unreachable_value) {
4302+
const template = "missing tuple field with index {d}";
4303+
if (root_msg) |msg| {
4304+
try sema.errNote(block, init_src, msg, template, .{i});
4305+
} else {
4306+
root_msg = try sema.errMsg(block, init_src, template, .{i});
4307+
}
43004308
}
43014309
}
4302-
}
43034310

4304-
if (root_msg) |msg| {
4305-
root_msg = null;
4306-
return sema.failWithOwnedErrorMsg(msg);
4307-
}
4308-
}
4311+
if (root_msg) |msg| {
4312+
root_msg = null;
4313+
return sema.failWithOwnedErrorMsg(msg);
4314+
}
4315+
},
4316+
.Array => {
4317+
return sema.fail(block, init_src, "expected {d} array elements; found {d}", .{
4318+
array_len, instrs.len,
4319+
});
4320+
},
4321+
.Vector => {
4322+
return sema.fail(block, init_src, "expected {d} vector elements; found {d}", .{
4323+
array_len, instrs.len,
4324+
});
4325+
},
4326+
else => unreachable,
4327+
};
43094328

43104329
if ((is_comptime or block.is_comptime) and
43114330
(try sema.resolveDefinedValue(block, init_src, array_ptr)) != null)
@@ -17054,7 +17073,6 @@ fn finishStructInit(
1705417073
}
1705517074

1705617075
if (root_msg) |msg| {
17057-
root_msg = null;
1705817076
if (struct_ty.castTag(.@"struct")) |struct_obj| {
1705917077
const fqn = try struct_obj.data.getFullyQualifiedName(sema.mod);
1706017078
defer gpa.free(fqn);
@@ -17065,6 +17083,7 @@ fn finishStructInit(
1706517083
.{fqn},
1706617084
);
1706717085
}
17086+
root_msg = null;
1706817087
return sema.failWithOwnedErrorMsg(msg);
1706917088
}
1707017089

@@ -18752,8 +18771,8 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1875218771

1875318772
const type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
1875418773
const ptr_ty = try sema.resolveType(block, src, extra.lhs);
18755-
const elem_ty = ptr_ty.elemType2();
1875618774
try sema.checkPtrType(block, type_src, ptr_ty);
18775+
const elem_ty = ptr_ty.elemType2();
1875718776
const target = sema.mod.getTarget();
1875818777
const ptr_align = try ptr_ty.ptrAlignmentAdvanced(target, sema);
1875918778

@@ -24281,7 +24300,10 @@ fn coerceExtra(
2428124300
},
2428224301
.Int, .ComptimeInt => switch (inst_ty.zigTypeTag()) {
2428324302
.Float, .ComptimeFloat => float: {
24284-
const val = (try sema.resolveDefinedValue(block, inst_src, inst)) orelse {
24303+
if (is_undef) {
24304+
return sema.addConstUndef(dest_ty);
24305+
}
24306+
const val = (try sema.resolveMaybeUndefVal(inst)) orelse {
2428524307
if (dest_ty.zigTypeTag() == .ComptimeInt) {
2428624308
if (!opts.report_err) return error.NotCoercible;
2428724309
return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_int' must be comptime-known");
@@ -24301,7 +24323,10 @@ fn coerceExtra(
2430124323
return try sema.addConstant(dest_ty, result_val);
2430224324
},
2430324325
.Int, .ComptimeInt => {
24304-
if (try sema.resolveDefinedValue(block, inst_src, inst)) |val| {
24326+
if (is_undef) {
24327+
return sema.addConstUndef(dest_ty);
24328+
}
24329+
if (try sema.resolveMaybeUndefVal(inst)) |val| {
2430524330
// comptime-known integer to other number
2430624331
if (!(try sema.intFitsInType(val, dest_ty, null))) {
2430724332
if (!opts.report_err) return error.NotCoercible;
@@ -24338,7 +24363,10 @@ fn coerceExtra(
2433824363
return try sema.addConstant(dest_ty, result_val);
2433924364
},
2434024365
.Float => {
24341-
if (try sema.resolveDefinedValue(block, inst_src, inst)) |val| {
24366+
if (is_undef) {
24367+
return sema.addConstUndef(dest_ty);
24368+
}
24369+
if (try sema.resolveMaybeUndefVal(inst)) |val| {
2434224370
const result_val = try val.floatCast(sema.arena, dest_ty, target);
2434324371
if (!val.eql(result_val, dest_ty, sema.mod)) {
2434424372
return sema.fail(
@@ -24363,7 +24391,10 @@ fn coerceExtra(
2436324391
}
2436424392
},
2436524393
.Int, .ComptimeInt => int: {
24366-
const val = (try sema.resolveDefinedValue(block, inst_src, inst)) orelse {
24394+
if (is_undef) {
24395+
return sema.addConstUndef(dest_ty);
24396+
}
24397+
const val = (try sema.resolveMaybeUndefVal(inst)) orelse {
2436724398
if (dest_ty.zigTypeTag() == .ComptimeFloat) {
2436824399
if (!opts.report_err) return error.NotCoercible;
2436924400
return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_float' must be comptime-known");
@@ -26517,6 +26548,10 @@ fn beginComptimePtrLoad(
2651726548
.null_value => {
2651826549
return sema.fail(block, src, "attempt to use null value", .{});
2651926550
},
26551+
.opt_payload => blk: {
26552+
const opt_payload = ptr_val.castTag(.opt_payload).?.data;
26553+
break :blk try sema.beginComptimePtrLoad(block, src, opt_payload, null);
26554+
},
2652026555

2652126556
.zero,
2652226557
.one,
@@ -27165,8 +27200,8 @@ fn coerceTupleToStruct(
2716527200
}
2716627201

2716727202
if (root_msg) |msg| {
27168-
root_msg = null;
2716927203
try sema.addDeclaredHereNote(msg, struct_ty);
27204+
root_msg = null;
2717027205
return sema.failWithOwnedErrorMsg(msg);
2717127206
}
2717227207

@@ -27271,8 +27306,8 @@ fn coerceTupleToTuple(
2727127306
}
2727227307

2727327308
if (root_msg) |msg| {
27274-
root_msg = null;
2727527309
try sema.addDeclaredHereNote(msg, tuple_ty);
27310+
root_msg = null;
2727627311
return sema.failWithOwnedErrorMsg(msg);
2727727312
}
2727827313

@@ -31262,7 +31297,10 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3126231297
}
3126331298

3126431299
pub fn typeHasRuntimeBits(sema: *Sema, ty: Type) CompileError!bool {
31265-
return ty.hasRuntimeBitsAdvanced(false, sema);
31300+
return ty.hasRuntimeBitsAdvanced(false, .{ .sema = sema }) catch |err| switch (err) {
31301+
error.NeedLazy => unreachable,
31302+
else => |e| return e,
31303+
};
3126631304
}
3126731305

3126831306
fn typeAbiSize(sema: *Sema, ty: Type) !u64 {

0 commit comments

Comments
 (0)