authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-24 22:31:53-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-24 22:31:53-04:00
logfb2acaff067dd6b385de7f2bd2726bdfebbf841f
tree1dce5178adc5066eba68510d36baed06575aad0a
parent733c547a65b005338c89e3b6a6cb6e817bee632b
signaturelock-open Commit is signed but in an unrecognized format.

`@sizeOf` returns 0 for comptime types

This defines `@sizeOf` to be the runtime size of a type, which means that it is zero for types such as comptime_int, type, and (enum literal). See #2209

4 files changed, 25 insertions(+), 5 deletions(-)

doc/langref.html.in+4
...@@ -7275,6 +7275,10 @@ test "@setRuntimeSafety" {...@@ -7275,6 +7275,10 @@ test "@setRuntimeSafety" {
7275 consider whether you want to use {#syntax#}@sizeOf(T){#endsyntax#} or7275 consider whether you want to use {#syntax#}@sizeOf(T){#endsyntax#} or
7276 {#syntax#}@typeInfo(T).Int.bits{#endsyntax#}.7276 {#syntax#}@typeInfo(T).Int.bits{#endsyntax#}.
7277 </p>7277 </p>
7278 <p>
7279 This function measures the size at runtime. For types that are disallowed at runtime, such as
7280 {#syntax#}comptime_int{#endsyntax#} and {#syntax#}type{#endsyntax#}, the result is {#syntax#}0{#endsyntax#}.
7281 </p>
7278 {#see_also|@typeInfo#}7282 {#see_also|@typeInfo#}
7279 {#header_close#}7283 {#header_close#}
72807284
src/ir.cpp+5-5
...@@ -16729,16 +16729,16 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira,...@@ -16729,16 +16729,16 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira,
16729 case ZigTypeIdUnreachable:16729 case ZigTypeIdUnreachable:
16730 case ZigTypeIdUndefined:16730 case ZigTypeIdUndefined:
16731 case ZigTypeIdNull:16731 case ZigTypeIdNull:
16732 case ZigTypeIdComptimeFloat:
16733 case ZigTypeIdComptimeInt:
16734 case ZigTypeIdEnumLiteral:
16735 case ZigTypeIdBoundFn:16732 case ZigTypeIdBoundFn:
16736 case ZigTypeIdMetaType:
16737 case ZigTypeIdArgTuple:16733 case ZigTypeIdArgTuple:
16738 case ZigTypeIdOpaque:16734 case ZigTypeIdOpaque:
16739 ir_add_error_node(ira, size_of_instruction->base.source_node,16735 ir_add_error_node(ira, type_value->source_node,
16740 buf_sprintf("no size available for type '%s'", buf_ptr(&type_entry->name)));16736 buf_sprintf("no size available for type '%s'", buf_ptr(&type_entry->name)));
16741 return ira->codegen->invalid_instruction;16737 return ira->codegen->invalid_instruction;
16738 case ZigTypeIdMetaType:
16739 case ZigTypeIdEnumLiteral:
16740 case ZigTypeIdComptimeFloat:
16741 case ZigTypeIdComptimeInt:
16742 case ZigTypeIdVoid:16742 case ZigTypeIdVoid:
16743 case ZigTypeIdBool:16743 case ZigTypeIdBool:
16744 case ZigTypeIdInt:16744 case ZigTypeIdInt:
test/compile_errors.zig+9
...@@ -2,6 +2,15 @@ const tests = @import("tests.zig");...@@ -2,6 +2,15 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add(
6 "@sizeOf bad type",
7 \\export fn entry() void {
8 \\ _ = @sizeOf(@typeOf(null));
9 \\}
10 ,
11 "tmp.zig:2:17: error: no size available for type '(null)'",
12 );
13
5 cases.add(14 cases.add(
6 "Generic function where return type is self-referenced",15 "Generic function where return type is self-referenced",
7 \\fn Foo(comptime T: type) Foo(T) {16 \\fn Foo(comptime T: type) Foo(T) {
test/stage1/behavior/sizeof_and_typeof.zig+7
...@@ -67,3 +67,10 @@ test "@bitOffsetOf" {...@@ -67,3 +67,10 @@ test "@bitOffsetOf" {
67 expect(@byteOffsetOf(A, "f") * 8 == @bitOffsetOf(A, "f"));67 expect(@byteOffsetOf(A, "f") * 8 == @bitOffsetOf(A, "f"));
68 expect(@byteOffsetOf(A, "g") * 8 == @bitOffsetOf(A, "g"));68 expect(@byteOffsetOf(A, "g") * 8 == @bitOffsetOf(A, "g"));
69}69}
70
71test "@sizeOf on compile-time types" {
72 expect(@sizeOf(comptime_int) == 0);
73 expect(@sizeOf(comptime_float) == 0);
74 expect(@sizeOf(@typeOf(.hi)) == 0);
75 expect(@sizeOf(@typeOf(type)) == 0);
76}