Sema: validate bitSizeOf operand type

Closes #13080
This commit is contained in:
Veikka Tuominen 2022-10-05 16:49:12 +03:00
parent 775e055b59
commit 446deb31a8
2 changed files with 36 additions and 0 deletions

View File

@ -14286,6 +14286,38 @@ fn zirBitSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
const src = inst_data.src();
const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
const operand_ty = try sema.resolveType(block, operand_src, inst_data.operand);
switch (operand_ty.zigTypeTag()) {
.Fn,
.NoReturn,
.Undefined,
.Null,
.BoundFn,
.Opaque,
=> return sema.fail(block, operand_src, "no size available for type '{}'", .{operand_ty.fmt(sema.mod)}),
.Type,
.EnumLiteral,
.ComptimeFloat,
.ComptimeInt,
.Void,
=> return sema.addIntUnsigned(Type.comptime_int, 0),
.Bool,
.Int,
.Float,
.Pointer,
.Array,
.Struct,
.Optional,
.ErrorUnion,
.ErrorSet,
.Enum,
.Union,
.Vector,
.Frame,
.AnyFrame,
=> {},
}
const target = sema.mod.getTarget();
const bit_size = try operand_ty.bitSizeAdvanced(target, sema.kit(block, src));
return sema.addIntUnsigned(Type.comptime_int, bit_size);

View File

@ -310,3 +310,7 @@ test "lazy size cast to float" {
try expect(@as(f32, @sizeOf(S)) == 1.0);
}
}
test "bitSizeOf comptime_int" {
try expect(@bitSizeOf(comptime_int) == 0);
}