authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-05 16:49:12+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-06 20:09:45+03:00
log446deb31a80a2bd046633fb087d8caee3ed41021
tree800acec67f7a982dddfa52b5f011a3d96554932d
parent775e055b59366f60badfb77a420219691846314b

Sema: validate bitSizeOf operand type

Closes #13080

2 files changed, 36 insertions(+), 0 deletions(-)

src/Sema.zig+32
...@@ -14286,6 +14286,38 @@ fn zirBitSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -14286,6 +14286,38 @@ fn zirBitSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
14286 const src = inst_data.src();14286 const src = inst_data.src();
14287 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };14287 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
14288 const operand_ty = try sema.resolveType(block, operand_src, inst_data.operand);14288 const operand_ty = try sema.resolveType(block, operand_src, inst_data.operand);
14289 switch (operand_ty.zigTypeTag()) {
14290 .Fn,
14291 .NoReturn,
14292 .Undefined,
14293 .Null,
14294 .BoundFn,
14295 .Opaque,
14296 => return sema.fail(block, operand_src, "no size available for type '{}'", .{operand_ty.fmt(sema.mod)}),
14297
14298 .Type,
14299 .EnumLiteral,
14300 .ComptimeFloat,
14301 .ComptimeInt,
14302 .Void,
14303 => return sema.addIntUnsigned(Type.comptime_int, 0),
14304
14305 .Bool,
14306 .Int,
14307 .Float,
14308 .Pointer,
14309 .Array,
14310 .Struct,
14311 .Optional,
14312 .ErrorUnion,
14313 .ErrorSet,
14314 .Enum,
14315 .Union,
14316 .Vector,
14317 .Frame,
14318 .AnyFrame,
14319 => {},
14320 }
14289 const target = sema.mod.getTarget();14321 const target = sema.mod.getTarget();
14290 const bit_size = try operand_ty.bitSizeAdvanced(target, sema.kit(block, src));14322 const bit_size = try operand_ty.bitSizeAdvanced(target, sema.kit(block, src));
14291 return sema.addIntUnsigned(Type.comptime_int, bit_size);14323 return sema.addIntUnsigned(Type.comptime_int, bit_size);
test/behavior/sizeof_and_typeof.zig+4
...@@ -310,3 +310,7 @@ test "lazy size cast to float" {...@@ -310,3 +310,7 @@ test "lazy size cast to float" {
310 try expect(@as(f32, @sizeOf(S)) == 1.0);310 try expect(@as(f32, @sizeOf(S)) == 1.0);
311 }311 }
312}312}
313
314test "bitSizeOf comptime_int" {
315 try expect(@bitSizeOf(comptime_int) == 0);
316}