authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-05 10:29:54-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-05 10:29:54-04:00
log7dd18294b7988062461430d431eaad2ad7b19daa
tree8ce835394267efdf369948f0438de35a29db3b9f
parent58ce79f9352a6139c873df6d99d1531101350e9f
parent677eaf29b10df9b1dc461b37e7be78734b11ef19

Merge remote-tracking branch 'origin/master' into llvm7


34 files changed, 954 insertions(+), 636 deletions(-)

doc/langref.html.in+24-21
......@@ -1565,7 +1565,7 @@ var foo: u8 align(4) = 100;
15651565test "global variable alignment" {
15661566 assert(@typeOf(&foo).alignment == 4);
15671567 assert(@typeOf(&foo) == *align(4) u8);
1568 const slice = (&foo)[0..1];
1568 const slice = (*[1]u8)(&foo)[0..];
15691569 assert(@typeOf(slice) == []align(4) u8);
15701570}
15711571
......@@ -1671,7 +1671,7 @@ test "using slices for strings" {
16711671
16721672test "slice pointer" {
16731673 var array: [10]u8 = undefined;
1674 const ptr = &array[0];
1674 const ptr = &array;
16751675
16761676 // You can use slicing syntax to convert a pointer into a slice:
16771677 const slice = ptr[0..5];
......@@ -4893,10 +4893,10 @@ pub const TypeId = enum {
48934893 Pointer,
48944894 Array,
48954895 Struct,
4896 FloatLiteral,
4897 IntLiteral,
4898 UndefinedLiteral,
4899 NullLiteral,
4896 ComptimeFloat,
4897 ComptimeInt,
4898 Undefined,
4899 Null,
49004900 Nullable,
49014901 ErrorUnion,
49024902 Error,
......@@ -4927,10 +4927,10 @@ pub const TypeInfo = union(TypeId) {
49274927 Pointer: Pointer,
49284928 Array: Array,
49294929 Struct: Struct,
4930 FloatLiteral: void,
4931 IntLiteral: void,
4932 UndefinedLiteral: void,
4933 NullLiteral: void,
4930 ComptimeFloat: void,
4931 ComptimeInt: void,
4932 Undefined: void,
4933 Null: void,
49344934 Nullable: Nullable,
49354935 ErrorUnion: ErrorUnion,
49364936 ErrorSet: ErrorSet,
......@@ -5685,10 +5685,10 @@ pub const TypeId = enum {
56855685 Pointer,
56865686 Array,
56875687 Struct,
5688 FloatLiteral,
5689 IntLiteral,
5690 UndefinedLiteral,
5691 NullLiteral,
5688 ComptimeFloat,
5689 ComptimeInt,
5690 Undefined,
5691 Null,
56925692 Nullable,
56935693 ErrorUnion,
56945694 ErrorSet,
......@@ -5713,10 +5713,10 @@ pub const TypeInfo = union(TypeId) {
57135713 Pointer: Pointer,
57145714 Array: Array,
57155715 Struct: Struct,
5716 FloatLiteral: void,
5717 IntLiteral: void,
5718 UndefinedLiteral: void,
5719 NullLiteral: void,
5716 ComptimeFloat: void,
5717 ComptimeInt: void,
5718 Undefined: void,
5719 Null: void,
57205720 Nullable: Nullable,
57215721 ErrorUnion: ErrorUnion,
57225722 ErrorSet: ErrorSet,
......@@ -6004,9 +6004,12 @@ const c = @cImport({
60046004 {#code_begin|syntax#}
60056005const base64 = @import("std").base64;
60066006
6007export fn decode_base_64(dest_ptr: *u8, dest_len: usize,
6008 source_ptr: *const u8, source_len: usize) usize
6009{
6007export fn decode_base_64(
6008 dest_ptr: [*]u8,
6009 dest_len: usize,
6010 source_ptr: [*]const u8,
6011 source_len: usize,
6012) usize {
60106013 const src = source_ptr[0..source_len];
60116014 const dest = dest_ptr[0..dest_len];
60126015 const base64_decoder = base64.standard_decoder_unsafe;
example/mix_o_files/base64.zig+1-1
......@@ -1,6 +1,6 @@
11const base64 = @import("std").base64;
22
3export fn decode_base_64(dest_ptr: *u8, dest_len: usize, source_ptr: *const u8, source_len: usize) usize {
3export fn decode_base_64(dest_ptr: [*]u8, dest_len: usize, source_ptr: [*]const u8, source_len: usize) usize {
44 const src = source_ptr[0..source_len];
55 const dest = dest_ptr[0..dest_len];
66 const base64_decoder = base64.standard_decoder_unsafe;
src/all_types.hpp+9-4
......@@ -83,6 +83,7 @@ enum ConstParentId {
8383 ConstParentIdStruct,
8484 ConstParentIdArray,
8585 ConstParentIdUnion,
86 ConstParentIdScalar,
8687};
8788
8889struct ConstParent {
......@@ -100,6 +101,9 @@ struct ConstParent {
100101 struct {
101102 ConstExprValue *union_val;
102103 } p_union;
104 struct {
105 ConstExprValue *scalar_val;
106 } p_scalar;
103107 } data;
104108};
105109
......@@ -578,6 +582,7 @@ enum CastOp {
578582 CastOpBytesToSlice,
579583 CastOpNumLitToConcrete,
580584 CastOpErrSet,
585 CastOpBitCast,
581586};
582587
583588struct AstNodeFnCallExpr {
......@@ -1154,10 +1159,10 @@ enum TypeTableEntryId {
11541159 TypeTableEntryIdPointer,
11551160 TypeTableEntryIdArray,
11561161 TypeTableEntryIdStruct,
1157 TypeTableEntryIdNumLitFloat,
1158 TypeTableEntryIdNumLitInt,
1159 TypeTableEntryIdUndefLit,
1160 TypeTableEntryIdNullLit,
1162 TypeTableEntryIdComptimeFloat,
1163 TypeTableEntryIdComptimeInt,
1164 TypeTableEntryIdUndefined,
1165 TypeTableEntryIdNull,
11611166 TypeTableEntryIdMaybe,
11621167 TypeTableEntryIdErrorUnion,
11631168 TypeTableEntryIdErrorSet,
src/analyze.cpp+103-99
......@@ -232,10 +232,10 @@ bool type_is_complete(TypeTableEntry *type_entry) {
232232 case TypeTableEntryIdFloat:
233233 case TypeTableEntryIdPointer:
234234 case TypeTableEntryIdArray:
235 case TypeTableEntryIdNumLitFloat:
236 case TypeTableEntryIdNumLitInt:
237 case TypeTableEntryIdUndefLit:
238 case TypeTableEntryIdNullLit:
235 case TypeTableEntryIdComptimeFloat:
236 case TypeTableEntryIdComptimeInt:
237 case TypeTableEntryIdUndefined:
238 case TypeTableEntryIdNull:
239239 case TypeTableEntryIdMaybe:
240240 case TypeTableEntryIdErrorUnion:
241241 case TypeTableEntryIdErrorSet:
......@@ -268,10 +268,10 @@ bool type_has_zero_bits_known(TypeTableEntry *type_entry) {
268268 case TypeTableEntryIdFloat:
269269 case TypeTableEntryIdPointer:
270270 case TypeTableEntryIdArray:
271 case TypeTableEntryIdNumLitFloat:
272 case TypeTableEntryIdNumLitInt:
273 case TypeTableEntryIdUndefLit:
274 case TypeTableEntryIdNullLit:
271 case TypeTableEntryIdComptimeFloat:
272 case TypeTableEntryIdComptimeInt:
273 case TypeTableEntryIdUndefined:
274 case TypeTableEntryIdNull:
275275 case TypeTableEntryIdMaybe:
276276 case TypeTableEntryIdErrorUnion:
277277 case TypeTableEntryIdErrorSet:
......@@ -1333,10 +1333,10 @@ static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {
13331333 zig_unreachable();
13341334 case TypeTableEntryIdMetaType:
13351335 case TypeTableEntryIdUnreachable:
1336 case TypeTableEntryIdNumLitFloat:
1337 case TypeTableEntryIdNumLitInt:
1338 case TypeTableEntryIdUndefLit:
1339 case TypeTableEntryIdNullLit:
1336 case TypeTableEntryIdComptimeFloat:
1337 case TypeTableEntryIdComptimeInt:
1338 case TypeTableEntryIdUndefined:
1339 case TypeTableEntryIdNull:
13401340 case TypeTableEntryIdErrorUnion:
13411341 case TypeTableEntryIdErrorSet:
13421342 case TypeTableEntryIdNamespace:
......@@ -1374,10 +1374,10 @@ static bool type_allowed_in_extern(CodeGen *g, TypeTableEntry *type_entry) {
13741374 case TypeTableEntryIdInvalid:
13751375 zig_unreachable();
13761376 case TypeTableEntryIdMetaType:
1377 case TypeTableEntryIdNumLitFloat:
1378 case TypeTableEntryIdNumLitInt:
1379 case TypeTableEntryIdUndefLit:
1380 case TypeTableEntryIdNullLit:
1377 case TypeTableEntryIdComptimeFloat:
1378 case TypeTableEntryIdComptimeInt:
1379 case TypeTableEntryIdUndefined:
1380 case TypeTableEntryIdNull:
13811381 case TypeTableEntryIdErrorUnion:
13821382 case TypeTableEntryIdErrorSet:
13831383 case TypeTableEntryIdNamespace:
......@@ -1511,15 +1511,15 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
15111511 case TypeTableEntryIdInvalid:
15121512 return g->builtin_types.entry_invalid;
15131513 case TypeTableEntryIdUnreachable:
1514 case TypeTableEntryIdUndefLit:
1515 case TypeTableEntryIdNullLit:
1514 case TypeTableEntryIdUndefined:
1515 case TypeTableEntryIdNull:
15161516 case TypeTableEntryIdArgTuple:
15171517 case TypeTableEntryIdOpaque:
15181518 add_node_error(g, param_node->data.param_decl.type,
15191519 buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&type_entry->name)));
15201520 return g->builtin_types.entry_invalid;
1521 case TypeTableEntryIdNumLitFloat:
1522 case TypeTableEntryIdNumLitInt:
1521 case TypeTableEntryIdComptimeFloat:
1522 case TypeTableEntryIdComptimeInt:
15231523 case TypeTableEntryIdNamespace:
15241524 case TypeTableEntryIdBlock:
15251525 case TypeTableEntryIdBoundFn:
......@@ -1599,16 +1599,16 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
15991599 case TypeTableEntryIdInvalid:
16001600 zig_unreachable();
16011601
1602 case TypeTableEntryIdUndefLit:
1603 case TypeTableEntryIdNullLit:
1602 case TypeTableEntryIdUndefined:
1603 case TypeTableEntryIdNull:
16041604 case TypeTableEntryIdArgTuple:
16051605 case TypeTableEntryIdOpaque:
16061606 add_node_error(g, fn_proto->return_type,
16071607 buf_sprintf("return type '%s' not allowed", buf_ptr(&fn_type_id.return_type->name)));
16081608 return g->builtin_types.entry_invalid;
16091609
1610 case TypeTableEntryIdNumLitFloat:
1611 case TypeTableEntryIdNumLitInt:
1610 case TypeTableEntryIdComptimeFloat:
1611 case TypeTableEntryIdComptimeInt:
16121612 case TypeTableEntryIdNamespace:
16131613 case TypeTableEntryIdBlock:
16141614 case TypeTableEntryIdBoundFn:
......@@ -1860,7 +1860,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
18601860 }
18611861
18621862 assert(!struct_type->data.structure.zero_bits_loop_flag);
1863 assert(struct_type->data.structure.fields);
1863 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);
18641864 assert(decl_node->type == NodeTypeContainerDecl);
18651865
18661866 size_t field_count = struct_type->data.structure.src_field_count;
......@@ -2677,8 +2677,8 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
26772677 return;
26782678 }
26792679 tag_type = enum_type;
2680 abi_alignment_so_far = get_abi_alignment(g, enum_type); // this populates src_field_count
26802681 covered_enum_fields = allocate<bool>(enum_type->data.enumeration.src_field_count);
2681 abi_alignment_so_far = get_abi_alignment(g, enum_type);
26822682 } else {
26832683 tag_type = nullptr;
26842684 abi_alignment_so_far = 0;
......@@ -3337,16 +3337,16 @@ TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEnt
33373337 case TypeTableEntryIdInvalid:
33383338 return g->builtin_types.entry_invalid;
33393339 case TypeTableEntryIdUnreachable:
3340 case TypeTableEntryIdNumLitFloat:
3341 case TypeTableEntryIdNumLitInt:
3342 case TypeTableEntryIdUndefLit:
3343 case TypeTableEntryIdNullLit:
3340 case TypeTableEntryIdUndefined:
3341 case TypeTableEntryIdNull:
33443342 case TypeTableEntryIdBlock:
33453343 case TypeTableEntryIdArgTuple:
33463344 case TypeTableEntryIdOpaque:
33473345 add_node_error(g, source_node, buf_sprintf("variable of type '%s' not allowed",
33483346 buf_ptr(&type_entry->name)));
33493347 return g->builtin_types.entry_invalid;
3348 case TypeTableEntryIdComptimeFloat:
3349 case TypeTableEntryIdComptimeInt:
33503350 case TypeTableEntryIdNamespace:
33513351 case TypeTableEntryIdMetaType:
33523352 case TypeTableEntryIdVoid:
......@@ -3480,12 +3480,12 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
34803480 add_node_error(g, source_node, buf_sprintf("variable initialization is unreachable"));
34813481 implicit_type = g->builtin_types.entry_invalid;
34823482 } else if ((!is_const || linkage == VarLinkageExternal) &&
3483 (implicit_type->id == TypeTableEntryIdNumLitFloat ||
3484 implicit_type->id == TypeTableEntryIdNumLitInt))
3483 (implicit_type->id == TypeTableEntryIdComptimeFloat ||
3484 implicit_type->id == TypeTableEntryIdComptimeInt))
34853485 {
34863486 add_node_error(g, source_node, buf_sprintf("unable to infer variable type"));
34873487 implicit_type = g->builtin_types.entry_invalid;
3488 } else if (implicit_type->id == TypeTableEntryIdNullLit) {
3488 } else if (implicit_type->id == TypeTableEntryIdNull) {
34893489 add_node_error(g, source_node, buf_sprintf("unable to infer variable type"));
34903490 implicit_type = g->builtin_types.entry_invalid;
34913491 } else if (implicit_type->id == TypeTableEntryIdMetaType && !is_const) {
......@@ -3730,10 +3730,10 @@ static bool is_container(TypeTableEntry *type_entry) {
37303730 case TypeTableEntryIdInt:
37313731 case TypeTableEntryIdFloat:
37323732 case TypeTableEntryIdArray:
3733 case TypeTableEntryIdNumLitFloat:
3734 case TypeTableEntryIdNumLitInt:
3735 case TypeTableEntryIdUndefLit:
3736 case TypeTableEntryIdNullLit:
3733 case TypeTableEntryIdComptimeFloat:
3734 case TypeTableEntryIdComptimeInt:
3735 case TypeTableEntryIdUndefined:
3736 case TypeTableEntryIdNull:
37373737 case TypeTableEntryIdMaybe:
37383738 case TypeTableEntryIdErrorUnion:
37393739 case TypeTableEntryIdErrorSet:
......@@ -3779,10 +3779,10 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) {
37793779 case TypeTableEntryIdInt:
37803780 case TypeTableEntryIdFloat:
37813781 case TypeTableEntryIdArray:
3782 case TypeTableEntryIdNumLitFloat:
3783 case TypeTableEntryIdNumLitInt:
3784 case TypeTableEntryIdUndefLit:
3785 case TypeTableEntryIdNullLit:
3782 case TypeTableEntryIdComptimeFloat:
3783 case TypeTableEntryIdComptimeInt:
3784 case TypeTableEntryIdUndefined:
3785 case TypeTableEntryIdNull:
37863786 case TypeTableEntryIdMaybe:
37873787 case TypeTableEntryIdErrorUnion:
37883788 case TypeTableEntryIdErrorSet:
......@@ -4283,10 +4283,10 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
42834283 switch (type_entry->id) {
42844284 case TypeTableEntryIdInvalid:
42854285 case TypeTableEntryIdMetaType:
4286 case TypeTableEntryIdNumLitFloat:
4287 case TypeTableEntryIdNumLitInt:
4288 case TypeTableEntryIdUndefLit:
4289 case TypeTableEntryIdNullLit:
4286 case TypeTableEntryIdComptimeFloat:
4287 case TypeTableEntryIdComptimeInt:
4288 case TypeTableEntryIdUndefined:
4289 case TypeTableEntryIdNull:
42904290 case TypeTableEntryIdNamespace:
42914291 case TypeTableEntryIdBlock:
42924292 case TypeTableEntryIdBoundFn:
......@@ -4568,7 +4568,7 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
45684568 case TypeTableEntryIdVoid:
45694569 return (uint32_t)4149439618;
45704570 case TypeTableEntryIdInt:
4571 case TypeTableEntryIdNumLitInt:
4571 case TypeTableEntryIdComptimeInt:
45724572 {
45734573 uint32_t result = 1331471175;
45744574 for (size_t i = 0; i < const_val->data.x_bigint.digit_count; i += 1) {
......@@ -4609,7 +4609,7 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
46094609 default:
46104610 zig_unreachable();
46114611 }
4612 case TypeTableEntryIdNumLitFloat:
4612 case TypeTableEntryIdComptimeFloat:
46134613 {
46144614 float128_t f128 = bigfloat_to_f128(&const_val->data.x_bigfloat);
46154615 uint32_t ints[4];
......@@ -4672,9 +4672,9 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
46724672 case TypeTableEntryIdPromise:
46734673 // TODO better hashing algorithm
46744674 return 223048345;
4675 case TypeTableEntryIdUndefLit:
4675 case TypeTableEntryIdUndefined:
46764676 return 162837799;
4677 case TypeTableEntryIdNullLit:
4677 case TypeTableEntryIdNull:
46784678 return 844854567;
46794679 case TypeTableEntryIdArray:
46804680 // TODO better hashing algorithm
......@@ -4754,10 +4754,10 @@ static bool can_mutate_comptime_var_state(ConstExprValue *value) {
47544754 case TypeTableEntryIdUnreachable:
47554755 case TypeTableEntryIdInt:
47564756 case TypeTableEntryIdFloat:
4757 case TypeTableEntryIdNumLitFloat:
4758 case TypeTableEntryIdNumLitInt:
4759 case TypeTableEntryIdUndefLit:
4760 case TypeTableEntryIdNullLit:
4757 case TypeTableEntryIdComptimeFloat:
4758 case TypeTableEntryIdComptimeInt:
4759 case TypeTableEntryIdUndefined:
4760 case TypeTableEntryIdNull:
47614761 case TypeTableEntryIdNamespace:
47624762 case TypeTableEntryIdBoundFn:
47634763 case TypeTableEntryIdFn:
......@@ -4819,10 +4819,10 @@ static bool return_type_is_cacheable(TypeTableEntry *return_type) {
48194819 case TypeTableEntryIdUnreachable:
48204820 case TypeTableEntryIdInt:
48214821 case TypeTableEntryIdFloat:
4822 case TypeTableEntryIdNumLitFloat:
4823 case TypeTableEntryIdNumLitInt:
4824 case TypeTableEntryIdUndefLit:
4825 case TypeTableEntryIdNullLit:
4822 case TypeTableEntryIdComptimeFloat:
4823 case TypeTableEntryIdComptimeInt:
4824 case TypeTableEntryIdUndefined:
4825 case TypeTableEntryIdNull:
48264826 case TypeTableEntryIdNamespace:
48274827 case TypeTableEntryIdBoundFn:
48284828 case TypeTableEntryIdFn:
......@@ -4930,10 +4930,10 @@ bool type_requires_comptime(TypeTableEntry *type_entry) {
49304930 case TypeTableEntryIdInvalid:
49314931 case TypeTableEntryIdOpaque:
49324932 zig_unreachable();
4933 case TypeTableEntryIdNumLitFloat:
4934 case TypeTableEntryIdNumLitInt:
4935 case TypeTableEntryIdUndefLit:
4936 case TypeTableEntryIdNullLit:
4933 case TypeTableEntryIdComptimeFloat:
4934 case TypeTableEntryIdComptimeInt:
4935 case TypeTableEntryIdUndefined:
4936 case TypeTableEntryIdNull:
49374937 case TypeTableEntryIdMetaType:
49384938 case TypeTableEntryIdNamespace:
49394939 case TypeTableEntryIdBlock:
......@@ -5070,7 +5070,7 @@ ConstExprValue *create_const_signed(TypeTableEntry *type, int64_t x) {
50705070void init_const_float(ConstExprValue *const_val, TypeTableEntry *type, double value) {
50715071 const_val->special = ConstValSpecialStatic;
50725072 const_val->type = type;
5073 if (type->id == TypeTableEntryIdNumLitFloat) {
5073 if (type->id == TypeTableEntryIdComptimeFloat) {
50745074 bigfloat_init_64(&const_val->data.x_bigfloat, value);
50755075 } else if (type->id == TypeTableEntryIdFloat) {
50765076 switch (type->data.floating.bit_count) {
......@@ -5158,7 +5158,8 @@ void init_const_slice(CodeGen *g, ConstExprValue *const_val, ConstExprValue *arr
51585158 const_val->type = get_slice_type(g, ptr_type);
51595159 const_val->data.x_struct.fields = create_const_vals(2);
51605160
5161 init_const_ptr_array(g, &const_val->data.x_struct.fields[slice_ptr_index], array_val, start, is_const);
5161 init_const_ptr_array(g, &const_val->data.x_struct.fields[slice_ptr_index], array_val, start, is_const,
5162 PtrLenUnknown);
51625163 init_const_usize(g, &const_val->data.x_struct.fields[slice_len_index], len);
51635164}
51645165
......@@ -5169,21 +5170,24 @@ ConstExprValue *create_const_slice(CodeGen *g, ConstExprValue *array_val, size_t
51695170}
51705171
51715172void init_const_ptr_array(CodeGen *g, ConstExprValue *const_val, ConstExprValue *array_val,
5172 size_t elem_index, bool is_const)
5173 size_t elem_index, bool is_const, PtrLen ptr_len)
51735174{
51745175 assert(array_val->type->id == TypeTableEntryIdArray);
51755176 TypeTableEntry *child_type = array_val->type->data.array.child_type;
51765177
51775178 const_val->special = ConstValSpecialStatic;
5178 const_val->type = get_pointer_to_type(g, child_type, is_const);
5179 const_val->type = get_pointer_to_type_extra(g, child_type, is_const, false,
5180 ptr_len, get_abi_alignment(g, child_type), 0, 0);
51795181 const_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
51805182 const_val->data.x_ptr.data.base_array.array_val = array_val;
51815183 const_val->data.x_ptr.data.base_array.elem_index = elem_index;
51825184}
51835185
5184ConstExprValue *create_const_ptr_array(CodeGen *g, ConstExprValue *array_val, size_t elem_index, bool is_const) {
5186ConstExprValue *create_const_ptr_array(CodeGen *g, ConstExprValue *array_val, size_t elem_index, bool is_const,
5187 PtrLen ptr_len)
5188{
51855189 ConstExprValue *const_val = create_const_vals(1);
5186 init_const_ptr_array(g, const_val, array_val, elem_index, is_const);
5190 init_const_ptr_array(g, const_val, array_val, elem_index, is_const, ptr_len);
51875191 return const_val;
51885192}
51895193
......@@ -5346,10 +5350,10 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
53465350 default:
53475351 zig_unreachable();
53485352 }
5349 case TypeTableEntryIdNumLitFloat:
5353 case TypeTableEntryIdComptimeFloat:
53505354 return bigfloat_cmp(&a->data.x_bigfloat, &b->data.x_bigfloat) == CmpEQ;
53515355 case TypeTableEntryIdInt:
5352 case TypeTableEntryIdNumLitInt:
5356 case TypeTableEntryIdComptimeInt:
53535357 return bigint_cmp(&a->data.x_bigint, &b->data.x_bigint) == CmpEQ;
53545358 case TypeTableEntryIdPointer:
53555359 case TypeTableEntryIdFn:
......@@ -5406,9 +5410,9 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
54065410 return false;
54075411 }
54085412 return true;
5409 case TypeTableEntryIdUndefLit:
5413 case TypeTableEntryIdUndefined:
54105414 zig_panic("TODO");
5411 case TypeTableEntryIdNullLit:
5415 case TypeTableEntryIdNull:
54125416 zig_panic("TODO");
54135417 case TypeTableEntryIdMaybe:
54145418 if (a->data.x_maybe == nullptr || b->data.x_maybe == nullptr) {
......@@ -5510,7 +5514,7 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
55105514 case TypeTableEntryIdVoid:
55115515 buf_appendf(buf, "{}");
55125516 return;
5513 case TypeTableEntryIdNumLitFloat:
5517 case TypeTableEntryIdComptimeFloat:
55145518 bigfloat_append_buf(buf, &const_val->data.x_bigfloat);
55155519 return;
55165520 case TypeTableEntryIdFloat:
......@@ -5538,7 +5542,7 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
55385542 default:
55395543 zig_unreachable();
55405544 }
5541 case TypeTableEntryIdNumLitInt:
5545 case TypeTableEntryIdComptimeInt:
55425546 case TypeTableEntryIdInt:
55435547 bigint_append_buf(buf, &const_val->data.x_bigint, 10);
55445548 return;
......@@ -5642,12 +5646,12 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
56425646 buf_appendf(buf, "}");
56435647 return;
56445648 }
5645 case TypeTableEntryIdNullLit:
5649 case TypeTableEntryIdNull:
56465650 {
56475651 buf_appendf(buf, "null");
56485652 return;
56495653 }
5650 case TypeTableEntryIdUndefLit:
5654 case TypeTableEntryIdUndefined:
56515655 {
56525656 buf_appendf(buf, "undefined");
56535657 return;
......@@ -5757,10 +5761,10 @@ uint32_t type_id_hash(TypeId x) {
57575761 case TypeTableEntryIdUnreachable:
57585762 case TypeTableEntryIdFloat:
57595763 case TypeTableEntryIdStruct:
5760 case TypeTableEntryIdNumLitFloat:
5761 case TypeTableEntryIdNumLitInt:
5762 case TypeTableEntryIdUndefLit:
5763 case TypeTableEntryIdNullLit:
5764 case TypeTableEntryIdComptimeFloat:
5765 case TypeTableEntryIdComptimeInt:
5766 case TypeTableEntryIdUndefined:
5767 case TypeTableEntryIdNull:
57645768 case TypeTableEntryIdMaybe:
57655769 case TypeTableEntryIdErrorSet:
57665770 case TypeTableEntryIdEnum:
......@@ -5803,10 +5807,10 @@ bool type_id_eql(TypeId a, TypeId b) {
58035807 case TypeTableEntryIdUnreachable:
58045808 case TypeTableEntryIdFloat:
58055809 case TypeTableEntryIdStruct:
5806 case TypeTableEntryIdNumLitFloat:
5807 case TypeTableEntryIdNumLitInt:
5808 case TypeTableEntryIdUndefLit:
5809 case TypeTableEntryIdNullLit:
5810 case TypeTableEntryIdComptimeFloat:
5811 case TypeTableEntryIdComptimeInt:
5812 case TypeTableEntryIdUndefined:
5813 case TypeTableEntryIdNull:
58105814 case TypeTableEntryIdMaybe:
58115815 case TypeTableEntryIdPromise:
58125816 case TypeTableEntryIdErrorSet:
......@@ -5925,10 +5929,10 @@ static const TypeTableEntryId all_type_ids[] = {
59255929 TypeTableEntryIdPointer,
59265930 TypeTableEntryIdArray,
59275931 TypeTableEntryIdStruct,
5928 TypeTableEntryIdNumLitFloat,
5929 TypeTableEntryIdNumLitInt,
5930 TypeTableEntryIdUndefLit,
5931 TypeTableEntryIdNullLit,
5932 TypeTableEntryIdComptimeFloat,
5933 TypeTableEntryIdComptimeInt,
5934 TypeTableEntryIdUndefined,
5935 TypeTableEntryIdNull,
59325936 TypeTableEntryIdMaybe,
59335937 TypeTableEntryIdErrorUnion,
59345938 TypeTableEntryIdErrorSet,
......@@ -5976,13 +5980,13 @@ size_t type_id_index(TypeTableEntry *entry) {
59765980 if (entry->data.structure.is_slice)
59775981 return 25;
59785982 return 8;
5979 case TypeTableEntryIdNumLitFloat:
5983 case TypeTableEntryIdComptimeFloat:
59805984 return 9;
5981 case TypeTableEntryIdNumLitInt:
5985 case TypeTableEntryIdComptimeInt:
59825986 return 10;
5983 case TypeTableEntryIdUndefLit:
5987 case TypeTableEntryIdUndefined:
59845988 return 11;
5985 case TypeTableEntryIdNullLit:
5989 case TypeTableEntryIdNull:
59865990 return 12;
59875991 case TypeTableEntryIdMaybe:
59885992 return 13;
......@@ -6034,14 +6038,14 @@ const char *type_id_name(TypeTableEntryId id) {
60346038 return "Array";
60356039 case TypeTableEntryIdStruct:
60366040 return "Struct";
6037 case TypeTableEntryIdNumLitFloat:
6038 return "FloatLiteral";
6039 case TypeTableEntryIdNumLitInt:
6040 return "IntLiteral";
6041 case TypeTableEntryIdUndefLit:
6042 return "UndefinedLiteral";
6043 case TypeTableEntryIdNullLit:
6044 return "NullLiteral";
6041 case TypeTableEntryIdComptimeFloat:
6042 return "ComptimeFloat";
6043 case TypeTableEntryIdComptimeInt:
6044 return "ComptimeInt";
6045 case TypeTableEntryIdUndefined:
6046 return "Undefined";
6047 case TypeTableEntryIdNull:
6048 return "Null";
60456049 case TypeTableEntryIdMaybe:
60466050 return "Nullable";
60476051 case TypeTableEntryIdErrorUnion:
src/analyze.hpp+3-2
......@@ -152,8 +152,9 @@ ConstExprValue *create_const_ptr_hard_coded_addr(CodeGen *g, TypeTableEntry *poi
152152 size_t addr, bool is_const);
153153
154154void init_const_ptr_array(CodeGen *g, ConstExprValue *const_val, ConstExprValue *array_val,
155 size_t elem_index, bool is_const);
156ConstExprValue *create_const_ptr_array(CodeGen *g, ConstExprValue *array_val, size_t elem_index, bool is_const);
155 size_t elem_index, bool is_const, PtrLen ptr_len);
156ConstExprValue *create_const_ptr_array(CodeGen *g, ConstExprValue *array_val, size_t elem_index,
157 bool is_const, PtrLen ptr_len);
157158
158159void init_const_slice(CodeGen *g, ConstExprValue *const_val, ConstExprValue *array_val,
159160 size_t start, size_t len, bool is_const);
src/codegen.cpp+57-36
......@@ -2541,6 +2541,8 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
25412541 add_error_range_check(g, wanted_type, g->err_tag_type, expr_val);
25422542 }
25432543 return expr_val;
2544 case CastOpBitCast:
2545 return LLVMBuildBitCast(g->builder, expr_val, wanted_type->type_ref, "");
25442546 }
25452547 zig_unreachable();
25462548}
......@@ -2823,7 +2825,13 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
28232825
28242826 bool safety_check_on = ir_want_runtime_safety(g, &instruction->base) && instruction->safety_check_on;
28252827
2826 if (array_type->id == TypeTableEntryIdArray) {
2828 if (array_type->id == TypeTableEntryIdArray ||
2829 (array_type->id == TypeTableEntryIdPointer && array_type->data.pointer.ptr_len == PtrLenSingle))
2830 {
2831 if (array_type->id == TypeTableEntryIdPointer) {
2832 assert(array_type->data.pointer.child_type->id == TypeTableEntryIdArray);
2833 array_type = array_type->data.pointer.child_type;
2834 }
28272835 if (safety_check_on) {
28282836 LLVMValueRef end = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
28292837 array_type->data.array.len, false);
......@@ -3709,7 +3717,12 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
37093717
37103718 bool want_runtime_safety = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base);
37113719
3712 if (array_type->id == TypeTableEntryIdArray) {
3720 if (array_type->id == TypeTableEntryIdArray ||
3721 (array_type->id == TypeTableEntryIdPointer && array_type->data.pointer.ptr_len == PtrLenSingle))
3722 {
3723 if (array_type->id == TypeTableEntryIdPointer) {
3724 array_type = array_type->data.pointer.child_type;
3725 }
37133726 LLVMValueRef start_val = ir_llvm_value(g, instruction->start);
37143727 LLVMValueRef end_val;
37153728 if (instruction->end) {
......@@ -3750,6 +3763,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
37503763
37513764 return tmp_struct_ptr;
37523765 } else if (array_type->id == TypeTableEntryIdPointer) {
3766 assert(array_type->data.pointer.ptr_len == PtrLenUnknown);
37533767 LLVMValueRef start_val = ir_llvm_value(g, instruction->start);
37543768 LLVMValueRef end_val = ir_llvm_value(g, instruction->end);
37553769
......@@ -4727,7 +4741,7 @@ static void ir_render(CodeGen *g, FnTableEntry *fn_entry) {
47274741
47284742static LLVMValueRef gen_const_ptr_struct_recursive(CodeGen *g, ConstExprValue *struct_const_val, size_t field_index);
47294743static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *array_const_val, size_t index);
4730static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *array_const_val);
4744static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *union_const_val);
47314745
47324746static LLVMValueRef gen_parent_ptr(CodeGen *g, ConstExprValue *val, ConstParent *parent) {
47334747 switch (parent->id) {
......@@ -4743,6 +4757,10 @@ static LLVMValueRef gen_parent_ptr(CodeGen *g, ConstExprValue *val, ConstParent
47434757 parent->data.p_array.elem_index);
47444758 case ConstParentIdUnion:
47454759 return gen_const_ptr_union_recursive(g, parent->data.p_union.union_val);
4760 case ConstParentIdScalar:
4761 render_const_val(g, parent->data.p_scalar.scalar_val, "");
4762 render_const_val_global(g, parent->data.p_scalar.scalar_val, "");
4763 return parent->data.p_scalar.scalar_val->global_refs->llvm_global;
47464764 }
47474765 zig_unreachable();
47484766}
......@@ -4768,7 +4786,8 @@ static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *ar
47684786 };
47694787 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
47704788 } else {
4771 zig_unreachable();
4789 assert(parent->id == ConstParentIdScalar);
4790 return base_ptr;
47724791 }
47734792}
47744793
......@@ -4812,10 +4831,10 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
48124831 case TypeTableEntryIdInvalid:
48134832 case TypeTableEntryIdMetaType:
48144833 case TypeTableEntryIdUnreachable:
4815 case TypeTableEntryIdNumLitFloat:
4816 case TypeTableEntryIdNumLitInt:
4817 case TypeTableEntryIdUndefLit:
4818 case TypeTableEntryIdNullLit:
4834 case TypeTableEntryIdComptimeFloat:
4835 case TypeTableEntryIdComptimeInt:
4836 case TypeTableEntryIdUndefined:
4837 case TypeTableEntryIdNull:
48194838 case TypeTableEntryIdErrorUnion:
48204839 case TypeTableEntryIdErrorSet:
48214840 case TypeTableEntryIdNamespace:
......@@ -5258,10 +5277,10 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
52585277 case TypeTableEntryIdInvalid:
52595278 case TypeTableEntryIdMetaType:
52605279 case TypeTableEntryIdUnreachable:
5261 case TypeTableEntryIdNumLitFloat:
5262 case TypeTableEntryIdNumLitInt:
5263 case TypeTableEntryIdUndefLit:
5264 case TypeTableEntryIdNullLit:
5280 case TypeTableEntryIdComptimeFloat:
5281 case TypeTableEntryIdComptimeInt:
5282 case TypeTableEntryIdUndefined:
5283 case TypeTableEntryIdNull:
52655284 case TypeTableEntryIdNamespace:
52665285 case TypeTableEntryIdBlock:
52675286 case TypeTableEntryIdBoundFn:
......@@ -5500,7 +5519,7 @@ static void do_code_gen(CodeGen *g) {
55005519 TldVar *tld_var = g->global_vars.at(i);
55015520 VariableTableEntry *var = tld_var->var;
55025521
5503 if (var->value->type->id == TypeTableEntryIdNumLitFloat) {
5522 if (var->value->type->id == TypeTableEntryIdComptimeFloat) {
55045523 // Generate debug info for it but that's it.
55055524 ConstExprValue *const_val = var->value;
55065525 assert(const_val->special != ConstValSpecialRuntime);
......@@ -5514,7 +5533,7 @@ static void do_code_gen(CodeGen *g) {
55145533 continue;
55155534 }
55165535
5517 if (var->value->type->id == TypeTableEntryIdNumLitInt) {
5536 if (var->value->type->id == TypeTableEntryIdComptimeInt) {
55185537 // Generate debug info for it but that's it.
55195538 ConstExprValue *const_val = var->value;
55205539 assert(const_val->special != ConstValSpecialRuntime);
......@@ -5908,25 +5927,27 @@ static void define_builtin_types(CodeGen *g) {
59085927 g->builtin_types.entry_block = entry;
59095928 }
59105929 {
5911 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdNumLitFloat);
5912 buf_init_from_str(&entry->name, "(float literal)");
5930 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdComptimeFloat);
5931 buf_init_from_str(&entry->name, "comptime_float");
59135932 entry->zero_bits = true;
59145933 g->builtin_types.entry_num_lit_float = entry;
5934 g->primitive_type_table.put(&entry->name, entry);
59155935 }
59165936 {
5917 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdNumLitInt);
5918 buf_init_from_str(&entry->name, "(integer literal)");
5937 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdComptimeInt);
5938 buf_init_from_str(&entry->name, "comptime_int");
59195939 entry->zero_bits = true;
59205940 g->builtin_types.entry_num_lit_int = entry;
5941 g->primitive_type_table.put(&entry->name, entry);
59215942 }
59225943 {
5923 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdUndefLit);
5944 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdUndefined);
59245945 buf_init_from_str(&entry->name, "(undefined)");
59255946 entry->zero_bits = true;
59265947 g->builtin_types.entry_undef = entry;
59275948 }
59285949 {
5929 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdNullLit);
5950 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdNull);
59305951 buf_init_from_str(&entry->name, "(null)");
59315952 entry->zero_bits = true;
59325953 g->builtin_types.entry_null = entry;
......@@ -6391,10 +6412,10 @@ static void define_builtin_compile_vars(CodeGen *g) {
63916412 " Slice: Slice,\n"
63926413 " Array: Array,\n"
63936414 " Struct: Struct,\n"
6394 " FloatLiteral: void,\n"
6395 " IntLiteral: void,\n"
6396 " UndefinedLiteral: void,\n"
6397 " NullLiteral: void,\n"
6415 " ComptimeFloat: void,\n"
6416 " ComptimeInt: void,\n"
6417 " Undefined: void,\n"
6418 " Null: void,\n"
63986419 " Nullable: Nullable,\n"
63996420 " ErrorUnion: ErrorUnion,\n"
64006421 " ErrorSet: ErrorSet,\n"
......@@ -6966,10 +6987,10 @@ static void prepend_c_type_to_decl_list(CodeGen *g, GenH *gen_h, TypeTableEntry
69666987 switch (type_entry->id) {
69676988 case TypeTableEntryIdInvalid:
69686989 case TypeTableEntryIdMetaType:
6969 case TypeTableEntryIdNumLitFloat:
6970 case TypeTableEntryIdNumLitInt:
6971 case TypeTableEntryIdUndefLit:
6972 case TypeTableEntryIdNullLit:
6990 case TypeTableEntryIdComptimeFloat:
6991 case TypeTableEntryIdComptimeInt:
6992 case TypeTableEntryIdUndefined:
6993 case TypeTableEntryIdNull:
69736994 case TypeTableEntryIdNamespace:
69746995 case TypeTableEntryIdBlock:
69756996 case TypeTableEntryIdBoundFn:
......@@ -7151,10 +7172,10 @@ static void get_c_type(CodeGen *g, GenH *gen_h, TypeTableEntry *type_entry, Buf
71517172 case TypeTableEntryIdBoundFn:
71527173 case TypeTableEntryIdNamespace:
71537174 case TypeTableEntryIdBlock:
7154 case TypeTableEntryIdNumLitFloat:
7155 case TypeTableEntryIdNumLitInt:
7156 case TypeTableEntryIdUndefLit:
7157 case TypeTableEntryIdNullLit:
7175 case TypeTableEntryIdComptimeFloat:
7176 case TypeTableEntryIdComptimeInt:
7177 case TypeTableEntryIdUndefined:
7178 case TypeTableEntryIdNull:
71587179 case TypeTableEntryIdArgTuple:
71597180 case TypeTableEntryIdPromise:
71607181 zig_unreachable();
......@@ -7303,11 +7324,11 @@ static void gen_h_file(CodeGen *g) {
73037324 case TypeTableEntryIdInt:
73047325 case TypeTableEntryIdFloat:
73057326 case TypeTableEntryIdPointer:
7306 case TypeTableEntryIdNumLitFloat:
7307 case TypeTableEntryIdNumLitInt:
7327 case TypeTableEntryIdComptimeFloat:
7328 case TypeTableEntryIdComptimeInt:
73087329 case TypeTableEntryIdArray:
7309 case TypeTableEntryIdUndefLit:
7310 case TypeTableEntryIdNullLit:
7330 case TypeTableEntryIdUndefined:
7331 case TypeTableEntryIdNull:
73117332 case TypeTableEntryIdErrorUnion:
73127333 case TypeTableEntryIdErrorSet:
73137334 case TypeTableEntryIdNamespace:
src/ir.cpp+305-192
......@@ -107,6 +107,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
107107static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, VariableTableEntry *var);
108108static TypeTableEntry *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstruction *op);
109109static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval);
110static TypeTableEntry *adjust_ptr_align(CodeGen *g, TypeTableEntry *ptr_type, uint32_t new_align);
110111
111112ConstExprValue *const_ptr_pointee(CodeGen *g, ConstExprValue *const_val) {
112113 assert(const_val->type->id == TypeTableEntryIdPointer);
......@@ -6849,7 +6850,11 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
68496850 IrInstruction *free_fn = ir_build_load_ptr(irb, scope, node, free_fn_ptr);
68506851 IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0);
68516852 IrInstruction *coro_mem_ptr_maybe = ir_build_coro_free(irb, scope, node, coro_id, irb->exec->coro_handle);
6852 IrInstruction *coro_mem_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, coro_mem_ptr_maybe);
6853 IrInstruction *u8_ptr_type_unknown_len = ir_build_const_type(irb, scope, node,
6854 get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8,
6855 false, false, PtrLenUnknown, get_abi_alignment(irb->codegen, irb->codegen->builtin_types.entry_u8),
6856 0, 0));
6857 IrInstruction *coro_mem_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type_unknown_len, coro_mem_ptr_maybe);
68536858 IrInstruction *coro_mem_ptr_ref = ir_build_ref(irb, scope, node, coro_mem_ptr, true, false);
68546859 IrInstruction *coro_size_ptr = ir_build_var_ptr(irb, scope, node, coro_size_var);
68556860 IrInstruction *coro_size = ir_build_load_ptr(irb, scope, node, coro_size_ptr);
......@@ -6940,14 +6945,14 @@ static bool ir_emit_global_runtime_side_effect(IrAnalyze *ira, IrInstruction *so
69406945}
69416946
69426947static bool const_val_fits_in_num_lit(ConstExprValue *const_val, TypeTableEntry *num_lit_type) {
6943 return ((num_lit_type->id == TypeTableEntryIdNumLitFloat &&
6944 (const_val->type->id == TypeTableEntryIdFloat || const_val->type->id == TypeTableEntryIdNumLitFloat)) ||
6945 (num_lit_type->id == TypeTableEntryIdNumLitInt &&
6946 (const_val->type->id == TypeTableEntryIdInt || const_val->type->id == TypeTableEntryIdNumLitInt)));
6948 return ((num_lit_type->id == TypeTableEntryIdComptimeFloat &&
6949 (const_val->type->id == TypeTableEntryIdFloat || const_val->type->id == TypeTableEntryIdComptimeFloat)) ||
6950 (num_lit_type->id == TypeTableEntryIdComptimeInt &&
6951 (const_val->type->id == TypeTableEntryIdInt || const_val->type->id == TypeTableEntryIdComptimeInt)));
69476952}
69486953
69496954static bool float_has_fraction(ConstExprValue *const_val) {
6950 if (const_val->type->id == TypeTableEntryIdNumLitFloat) {
6955 if (const_val->type->id == TypeTableEntryIdComptimeFloat) {
69516956 return bigfloat_has_fraction(&const_val->data.x_bigfloat);
69526957 } else if (const_val->type->id == TypeTableEntryIdFloat) {
69536958 switch (const_val->type->data.floating.bit_count) {
......@@ -6970,7 +6975,7 @@ static bool float_has_fraction(ConstExprValue *const_val) {
69706975}
69716976
69726977static void float_append_buf(Buf *buf, ConstExprValue *const_val) {
6973 if (const_val->type->id == TypeTableEntryIdNumLitFloat) {
6978 if (const_val->type->id == TypeTableEntryIdComptimeFloat) {
69746979 bigfloat_append_buf(buf, &const_val->data.x_bigfloat);
69756980 } else if (const_val->type->id == TypeTableEntryIdFloat) {
69766981 switch (const_val->type->data.floating.bit_count) {
......@@ -7005,7 +7010,7 @@ static void float_append_buf(Buf *buf, ConstExprValue *const_val) {
70057010}
70067011
70077012static void float_init_bigint(BigInt *bigint, ConstExprValue *const_val) {
7008 if (const_val->type->id == TypeTableEntryIdNumLitFloat) {
7013 if (const_val->type->id == TypeTableEntryIdComptimeFloat) {
70097014 bigint_init_bigfloat(bigint, &const_val->data.x_bigfloat);
70107015 } else if (const_val->type->id == TypeTableEntryIdFloat) {
70117016 switch (const_val->type->data.floating.bit_count) {
......@@ -7041,7 +7046,7 @@ static void float_init_bigint(BigInt *bigint, ConstExprValue *const_val) {
70417046}
70427047
70437048static void float_init_bigfloat(ConstExprValue *dest_val, BigFloat *bigfloat) {
7044 if (dest_val->type->id == TypeTableEntryIdNumLitFloat) {
7049 if (dest_val->type->id == TypeTableEntryIdComptimeFloat) {
70457050 bigfloat_init_bigfloat(&dest_val->data.x_bigfloat, bigfloat);
70467051 } else if (dest_val->type->id == TypeTableEntryIdFloat) {
70477052 switch (dest_val->type->data.floating.bit_count) {
......@@ -7063,7 +7068,7 @@ static void float_init_bigfloat(ConstExprValue *dest_val, BigFloat *bigfloat) {
70637068}
70647069
70657070static void float_init_f32(ConstExprValue *dest_val, float x) {
7066 if (dest_val->type->id == TypeTableEntryIdNumLitFloat) {
7071 if (dest_val->type->id == TypeTableEntryIdComptimeFloat) {
70677072 bigfloat_init_32(&dest_val->data.x_bigfloat, x);
70687073 } else if (dest_val->type->id == TypeTableEntryIdFloat) {
70697074 switch (dest_val->type->data.floating.bit_count) {
......@@ -7089,7 +7094,7 @@ static void float_init_f32(ConstExprValue *dest_val, float x) {
70897094}
70907095
70917096static void float_init_f64(ConstExprValue *dest_val, double x) {
7092 if (dest_val->type->id == TypeTableEntryIdNumLitFloat) {
7097 if (dest_val->type->id == TypeTableEntryIdComptimeFloat) {
70937098 bigfloat_init_64(&dest_val->data.x_bigfloat, x);
70947099 } else if (dest_val->type->id == TypeTableEntryIdFloat) {
70957100 switch (dest_val->type->data.floating.bit_count) {
......@@ -7115,7 +7120,7 @@ static void float_init_f64(ConstExprValue *dest_val, double x) {
71157120}
71167121
71177122static void float_init_f128(ConstExprValue *dest_val, float128_t x) {
7118 if (dest_val->type->id == TypeTableEntryIdNumLitFloat) {
7123 if (dest_val->type->id == TypeTableEntryIdComptimeFloat) {
71197124 bigfloat_init_128(&dest_val->data.x_bigfloat, x);
71207125 } else if (dest_val->type->id == TypeTableEntryIdFloat) {
71217126 switch (dest_val->type->data.floating.bit_count) {
......@@ -7145,7 +7150,7 @@ static void float_init_f128(ConstExprValue *dest_val, float128_t x) {
71457150}
71467151
71477152static void float_init_float(ConstExprValue *dest_val, ConstExprValue *src_val) {
7148 if (src_val->type->id == TypeTableEntryIdNumLitFloat) {
7153 if (src_val->type->id == TypeTableEntryIdComptimeFloat) {
71497154 float_init_bigfloat(dest_val, &src_val->data.x_bigfloat);
71507155 } else if (src_val->type->id == TypeTableEntryIdFloat) {
71517156 switch (src_val->type->data.floating.bit_count) {
......@@ -7168,7 +7173,7 @@ static void float_init_float(ConstExprValue *dest_val, ConstExprValue *src_val)
71687173
71697174static Cmp float_cmp(ConstExprValue *op1, ConstExprValue *op2) {
71707175 assert(op1->type == op2->type);
7171 if (op1->type->id == TypeTableEntryIdNumLitFloat) {
7176 if (op1->type->id == TypeTableEntryIdComptimeFloat) {
71727177 return bigfloat_cmp(&op1->data.x_bigfloat, &op2->data.x_bigfloat);
71737178 } else if (op1->type->id == TypeTableEntryIdFloat) {
71747179 switch (op1->type->data.floating.bit_count) {
......@@ -7205,7 +7210,7 @@ static Cmp float_cmp(ConstExprValue *op1, ConstExprValue *op2) {
72057210}
72067211
72077212static Cmp float_cmp_zero(ConstExprValue *op) {
7208 if (op->type->id == TypeTableEntryIdNumLitFloat) {
7213 if (op->type->id == TypeTableEntryIdComptimeFloat) {
72097214 return bigfloat_cmp_zero(&op->data.x_bigfloat);
72107215 } else if (op->type->id == TypeTableEntryIdFloat) {
72117216 switch (op->type->data.floating.bit_count) {
......@@ -7246,7 +7251,7 @@ static Cmp float_cmp_zero(ConstExprValue *op) {
72467251static void float_add(ConstExprValue *out_val, ConstExprValue *op1, ConstExprValue *op2) {
72477252 assert(op1->type == op2->type);
72487253 out_val->type = op1->type;
7249 if (op1->type->id == TypeTableEntryIdNumLitFloat) {
7254 if (op1->type->id == TypeTableEntryIdComptimeFloat) {
72507255 bigfloat_add(&out_val->data.x_bigfloat, &op1->data.x_bigfloat, &op2->data.x_bigfloat);
72517256 } else if (op1->type->id == TypeTableEntryIdFloat) {
72527257 switch (op1->type->data.floating.bit_count) {
......@@ -7270,7 +7275,7 @@ static void float_add(ConstExprValue *out_val, ConstExprValue *op1, ConstExprVal
72707275static void float_sub(ConstExprValue *out_val, ConstExprValue *op1, ConstExprValue *op2) {
72717276 assert(op1->type == op2->type);
72727277 out_val->type = op1->type;
7273 if (op1->type->id == TypeTableEntryIdNumLitFloat) {
7278 if (op1->type->id == TypeTableEntryIdComptimeFloat) {
72747279 bigfloat_sub(&out_val->data.x_bigfloat, &op1->data.x_bigfloat, &op2->data.x_bigfloat);
72757280 } else if (op1->type->id == TypeTableEntryIdFloat) {
72767281 switch (op1->type->data.floating.bit_count) {
......@@ -7294,7 +7299,7 @@ static void float_sub(ConstExprValue *out_val, ConstExprValue *op1, ConstExprVal
72947299static void float_mul(ConstExprValue *out_val, ConstExprValue *op1, ConstExprValue *op2) {
72957300 assert(op1->type == op2->type);
72967301 out_val->type = op1->type;
7297 if (op1->type->id == TypeTableEntryIdNumLitFloat) {
7302 if (op1->type->id == TypeTableEntryIdComptimeFloat) {
72987303 bigfloat_mul(&out_val->data.x_bigfloat, &op1->data.x_bigfloat, &op2->data.x_bigfloat);
72997304 } else if (op1->type->id == TypeTableEntryIdFloat) {
73007305 switch (op1->type->data.floating.bit_count) {
......@@ -7318,7 +7323,7 @@ static void float_mul(ConstExprValue *out_val, ConstExprValue *op1, ConstExprVal
73187323static void float_div(ConstExprValue *out_val, ConstExprValue *op1, ConstExprValue *op2) {
73197324 assert(op1->type == op2->type);
73207325 out_val->type = op1->type;
7321 if (op1->type->id == TypeTableEntryIdNumLitFloat) {
7326 if (op1->type->id == TypeTableEntryIdComptimeFloat) {
73227327 bigfloat_div(&out_val->data.x_bigfloat, &op1->data.x_bigfloat, &op2->data.x_bigfloat);
73237328 } else if (op1->type->id == TypeTableEntryIdFloat) {
73247329 switch (op1->type->data.floating.bit_count) {
......@@ -7342,7 +7347,7 @@ static void float_div(ConstExprValue *out_val, ConstExprValue *op1, ConstExprVal
73427347static void float_div_trunc(ConstExprValue *out_val, ConstExprValue *op1, ConstExprValue *op2) {
73437348 assert(op1->type == op2->type);
73447349 out_val->type = op1->type;
7345 if (op1->type->id == TypeTableEntryIdNumLitFloat) {
7350 if (op1->type->id == TypeTableEntryIdComptimeFloat) {
73467351 bigfloat_div_trunc(&out_val->data.x_bigfloat, &op1->data.x_bigfloat, &op2->data.x_bigfloat);
73477352 } else if (op1->type->id == TypeTableEntryIdFloat) {
73487353 switch (op1->type->data.floating.bit_count) {
......@@ -7377,7 +7382,7 @@ static void float_div_trunc(ConstExprValue *out_val, ConstExprValue *op1, ConstE
73777382static void float_div_floor(ConstExprValue *out_val, ConstExprValue *op1, ConstExprValue *op2) {
73787383 assert(op1->type == op2->type);
73797384 out_val->type = op1->type;
7380 if (op1->type->id == TypeTableEntryIdNumLitFloat) {
7385 if (op1->type->id == TypeTableEntryIdComptimeFloat) {
73817386 bigfloat_div_floor(&out_val->data.x_bigfloat, &op1->data.x_bigfloat, &op2->data.x_bigfloat);
73827387 } else if (op1->type->id == TypeTableEntryIdFloat) {
73837388 switch (op1->type->data.floating.bit_count) {
......@@ -7402,7 +7407,7 @@ static void float_div_floor(ConstExprValue *out_val, ConstExprValue *op1, ConstE
74027407static void float_rem(ConstExprValue *out_val, ConstExprValue *op1, ConstExprValue *op2) {
74037408 assert(op1->type == op2->type);
74047409 out_val->type = op1->type;
7405 if (op1->type->id == TypeTableEntryIdNumLitFloat) {
7410 if (op1->type->id == TypeTableEntryIdComptimeFloat) {
74067411 bigfloat_rem(&out_val->data.x_bigfloat, &op1->data.x_bigfloat, &op2->data.x_bigfloat);
74077412 } else if (op1->type->id == TypeTableEntryIdFloat) {
74087413 switch (op1->type->data.floating.bit_count) {
......@@ -7426,7 +7431,7 @@ static void float_rem(ConstExprValue *out_val, ConstExprValue *op1, ConstExprVal
74267431static void float_mod(ConstExprValue *out_val, ConstExprValue *op1, ConstExprValue *op2) {
74277432 assert(op1->type == op2->type);
74287433 out_val->type = op1->type;
7429 if (op1->type->id == TypeTableEntryIdNumLitFloat) {
7434 if (op1->type->id == TypeTableEntryIdComptimeFloat) {
74307435 bigfloat_mod(&out_val->data.x_bigfloat, &op1->data.x_bigfloat, &op2->data.x_bigfloat);
74317436 } else if (op1->type->id == TypeTableEntryIdFloat) {
74327437 switch (op1->type->data.floating.bit_count) {
......@@ -7451,7 +7456,7 @@ static void float_mod(ConstExprValue *out_val, ConstExprValue *op1, ConstExprVal
74517456
74527457static void float_negate(ConstExprValue *out_val, ConstExprValue *op) {
74537458 out_val->type = op->type;
7454 if (op->type->id == TypeTableEntryIdNumLitFloat) {
7459 if (op->type->id == TypeTableEntryIdComptimeFloat) {
74557460 bigfloat_negate(&out_val->data.x_bigfloat, &op->data.x_bigfloat);
74567461 } else if (op->type->id == TypeTableEntryIdFloat) {
74577462 switch (op->type->data.floating.bit_count) {
......@@ -7525,9 +7530,9 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
75257530 assert(const_val->special != ConstValSpecialRuntime);
75267531
75277532 bool const_val_is_int = (const_val->type->id == TypeTableEntryIdInt ||
7528 const_val->type->id == TypeTableEntryIdNumLitInt);
7533 const_val->type->id == TypeTableEntryIdComptimeInt);
75297534 bool const_val_is_float = (const_val->type->id == TypeTableEntryIdFloat ||
7530 const_val->type->id == TypeTableEntryIdNumLitFloat);
7535 const_val->type->id == TypeTableEntryIdComptimeFloat);
75317536 if (other_type->id == TypeTableEntryIdFloat) {
75327537 return true;
75337538 } else if (other_type->id == TypeTableEntryIdInt && const_val_is_int) {
......@@ -7571,7 +7576,7 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
75717576 return true;
75727577 }
75737578 }
7574 if (explicit_cast && (other_type->id == TypeTableEntryIdInt || other_type->id == TypeTableEntryIdNumLitInt) &&
7579 if (explicit_cast && (other_type->id == TypeTableEntryIdInt || other_type->id == TypeTableEntryIdComptimeInt) &&
75757580 const_val_is_float)
75767581 {
75777582 if (float_has_fraction(const_val)) {
......@@ -7584,7 +7589,7 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
75847589 buf_ptr(&other_type->name)));
75857590 return false;
75867591 } else {
7587 if (other_type->id == TypeTableEntryIdNumLitInt) {
7592 if (other_type->id == TypeTableEntryIdComptimeInt) {
75887593 return true;
75897594 } else {
75907595 BigInt bigint;
......@@ -7955,7 +7960,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
79557960
79567961 // implicit conversion from null literal to maybe type
79577962 if (expected_type->id == TypeTableEntryIdMaybe &&
7958 actual_type->id == TypeTableEntryIdNullLit)
7963 actual_type->id == TypeTableEntryIdNull)
79597964 {
79607965 return ImplicitCastMatchResultYes;
79617966 }
......@@ -8073,8 +8078,8 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
80738078
80748079 // implicit number literal to typed number
80758080 // implicit number literal to &const integer
8076 if (actual_type->id == TypeTableEntryIdNumLitFloat ||
8077 actual_type->id == TypeTableEntryIdNumLitInt)
8081 if (actual_type->id == TypeTableEntryIdComptimeFloat ||
8082 actual_type->id == TypeTableEntryIdComptimeInt)
80788083 {
80798084 if (expected_type->id == TypeTableEntryIdPointer &&
80808085 expected_type->data.pointer.is_const)
......@@ -8094,9 +8099,9 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
80948099 // implicit typed number to integer or float literal.
80958100 // works when the number is known
80968101 if (value->value.special == ConstValSpecialStatic) {
8097 if (actual_type->id == TypeTableEntryIdInt && expected_type->id == TypeTableEntryIdNumLitInt) {
8102 if (actual_type->id == TypeTableEntryIdInt && expected_type->id == TypeTableEntryIdComptimeInt) {
80988103 return ImplicitCastMatchResultYes;
8099 } else if (actual_type->id == TypeTableEntryIdFloat && expected_type->id == TypeTableEntryIdNumLitFloat) {
8104 } else if (actual_type->id == TypeTableEntryIdFloat && expected_type->id == TypeTableEntryIdComptimeFloat) {
81008105 return ImplicitCastMatchResultYes;
81018106 }
81028107 }
......@@ -8137,7 +8142,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
81378142 }
81388143
81398144 // implicit undefined literal to anything
8140 if (actual_type->id == TypeTableEntryIdUndefLit) {
8145 if (actual_type->id == TypeTableEntryIdUndefined) {
81418146 return ImplicitCastMatchResultYes;
81428147 }
81438148
......@@ -8185,7 +8190,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
81858190 }
81868191 }
81878192
8188 bool any_are_null = (prev_inst->value.type->id == TypeTableEntryIdNullLit);
8193 bool any_are_null = (prev_inst->value.type->id == TypeTableEntryIdNull);
81898194 bool convert_to_const_slice = false;
81908195 for (size_t i = 1; i < instruction_count; i += 1) {
81918196 IrInstruction *cur_inst = instructions[i];
......@@ -8464,12 +8469,12 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
84648469 }
84658470 }
84668471
8467 if (prev_type->id == TypeTableEntryIdNullLit) {
8472 if (prev_type->id == TypeTableEntryIdNull) {
84688473 prev_inst = cur_inst;
84698474 continue;
84708475 }
84718476
8472 if (cur_type->id == TypeTableEntryIdNullLit) {
8477 if (cur_type->id == TypeTableEntryIdNull) {
84738478 any_are_null = true;
84748479 continue;
84758480 }
......@@ -8541,17 +8546,17 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
85418546 continue;
85428547 }
85438548
8544 if (cur_type->id == TypeTableEntryIdUndefLit) {
8549 if (cur_type->id == TypeTableEntryIdUndefined) {
85458550 continue;
85468551 }
85478552
8548 if (prev_type->id == TypeTableEntryIdUndefLit) {
8553 if (prev_type->id == TypeTableEntryIdUndefined) {
85498554 prev_inst = cur_inst;
85508555 continue;
85518556 }
85528557
8553 if (prev_type->id == TypeTableEntryIdNumLitInt ||
8554 prev_type->id == TypeTableEntryIdNumLitFloat)
8558 if (prev_type->id == TypeTableEntryIdComptimeInt ||
8559 prev_type->id == TypeTableEntryIdComptimeFloat)
85558560 {
85568561 if (ir_num_lit_fits_in_other_type(ira, prev_inst, cur_type, false)) {
85578562 prev_inst = cur_inst;
......@@ -8561,8 +8566,8 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
85618566 }
85628567 }
85638568
8564 if (cur_type->id == TypeTableEntryIdNumLitInt ||
8565 cur_type->id == TypeTableEntryIdNumLitFloat)
8569 if (cur_type->id == TypeTableEntryIdComptimeInt ||
8570 cur_type->id == TypeTableEntryIdComptimeFloat)
85668571 {
85678572 if (ir_num_lit_fits_in_other_type(ira, cur_inst, prev_type, false)) {
85688573 continue;
......@@ -8666,13 +8671,13 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
86668671 } else if (expected_type != nullptr && expected_type->id == TypeTableEntryIdErrorUnion) {
86678672 return get_error_union_type(ira->codegen, err_set_type, expected_type->data.error_union.payload_type);
86688673 } else {
8669 if (prev_inst->value.type->id == TypeTableEntryIdNumLitInt ||
8670 prev_inst->value.type->id == TypeTableEntryIdNumLitFloat)
8674 if (prev_inst->value.type->id == TypeTableEntryIdComptimeInt ||
8675 prev_inst->value.type->id == TypeTableEntryIdComptimeFloat)
86718676 {
86728677 ir_add_error_node(ira, source_node,
86738678 buf_sprintf("unable to make error union out of number literal"));
86748679 return ira->codegen->builtin_types.entry_invalid;
8675 } else if (prev_inst->value.type->id == TypeTableEntryIdNullLit) {
8680 } else if (prev_inst->value.type->id == TypeTableEntryIdNull) {
86768681 ir_add_error_node(ira, source_node,
86778682 buf_sprintf("unable to make error union out of null literal"));
86788683 return ira->codegen->builtin_types.entry_invalid;
......@@ -8680,9 +8685,9 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
86808685 return get_error_union_type(ira->codegen, err_set_type, prev_inst->value.type);
86818686 }
86828687 }
8683 } else if (any_are_null && prev_inst->value.type->id != TypeTableEntryIdNullLit) {
8684 if (prev_inst->value.type->id == TypeTableEntryIdNumLitInt ||
8685 prev_inst->value.type->id == TypeTableEntryIdNumLitFloat)
8688 } else if (any_are_null && prev_inst->value.type->id != TypeTableEntryIdNull) {
8689 if (prev_inst->value.type->id == TypeTableEntryIdComptimeInt ||
8690 prev_inst->value.type->id == TypeTableEntryIdComptimeFloat)
86868691 {
86878692 ir_add_error_node(ira, source_node,
86888693 buf_sprintf("unable to make maybe out of number literal"));
......@@ -8729,6 +8734,7 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,
87298734 case CastOpNoCast:
87308735 zig_unreachable();
87318736 case CastOpErrSet:
8737 case CastOpBitCast:
87328738 zig_panic("TODO");
87338739 case CastOpNoop:
87348740 {
......@@ -8737,7 +8743,7 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,
87378743 break;
87388744 }
87398745 case CastOpNumLitToConcrete:
8740 if (other_val->type->id == TypeTableEntryIdNumLitFloat) {
8746 if (other_val->type->id == TypeTableEntryIdComptimeFloat) {
87418747 assert(new_type->id == TypeTableEntryIdFloat);
87428748 switch (new_type->data.floating.bit_count) {
87438749 case 32:
......@@ -8752,7 +8758,7 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,
87528758 default:
87538759 zig_unreachable();
87548760 }
8755 } else if (other_val->type->id == TypeTableEntryIdNumLitInt) {
8761 } else if (other_val->type->id == TypeTableEntryIdComptimeInt) {
87568762 bigint_init_bigint(&const_val->data.x_bigint, &other_val->data.x_bigint);
87578763 } else {
87588764 zig_unreachable();
......@@ -9595,9 +9601,9 @@ static IrInstruction *ir_analyze_number_to_literal(IrAnalyze *ira, IrInstruction
95959601
95969602 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
95979603 source_instr->source_node, wanted_type);
9598 if (wanted_type->id == TypeTableEntryIdNumLitFloat) {
9604 if (wanted_type->id == TypeTableEntryIdComptimeFloat) {
95999605 float_init_float(&result->value, val);
9600 } else if (wanted_type->id == TypeTableEntryIdNumLitInt) {
9606 } else if (wanted_type->id == TypeTableEntryIdComptimeInt) {
96019607 bigint_init_bigint(&result->value.data.x_bigint, &val->data.x_bigint);
96029608 } else {
96039609 zig_unreachable();
......@@ -9750,6 +9756,49 @@ static IrInstruction *ir_analyze_err_to_int(IrAnalyze *ira, IrInstruction *sourc
97509756 return result;
97519757}
97529758
9759static IrInstruction *ir_analyze_ptr_to_array(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *target,
9760 TypeTableEntry *wanted_type)
9761{
9762 assert(wanted_type->id == TypeTableEntryIdPointer);
9763 wanted_type = adjust_ptr_align(ira->codegen, wanted_type, target->value.type->data.pointer.alignment);
9764 TypeTableEntry *array_type = wanted_type->data.pointer.child_type;
9765 assert(array_type->id == TypeTableEntryIdArray);
9766 assert(array_type->data.array.len == 1);
9767
9768 if (instr_is_comptime(target)) {
9769 ConstExprValue *val = ir_resolve_const(ira, target, UndefBad);
9770 if (!val)
9771 return ira->codegen->invalid_instruction;
9772
9773 assert(val->type->id == TypeTableEntryIdPointer);
9774 ConstExprValue *pointee = const_ptr_pointee(ira->codegen, val);
9775 if (pointee->special != ConstValSpecialRuntime) {
9776 ConstExprValue *array_val = create_const_vals(1);
9777 array_val->special = ConstValSpecialStatic;
9778 array_val->type = array_type;
9779 array_val->data.x_array.special = ConstArraySpecialNone;
9780 array_val->data.x_array.s_none.elements = pointee;
9781 array_val->data.x_array.s_none.parent.id = ConstParentIdScalar;
9782 array_val->data.x_array.s_none.parent.data.p_scalar.scalar_val = pointee;
9783
9784 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
9785 source_instr->scope, source_instr->source_node);
9786 const_instruction->base.value.type = wanted_type;
9787 const_instruction->base.value.special = ConstValSpecialStatic;
9788 const_instruction->base.value.data.x_ptr.special = ConstPtrSpecialRef;
9789 const_instruction->base.value.data.x_ptr.data.ref.pointee = array_val;
9790 const_instruction->base.value.data.x_ptr.mut = val->data.x_ptr.mut;
9791 return &const_instruction->base;
9792 }
9793 }
9794
9795 // pointer to array and pointer to single item are represented the same way at runtime
9796 IrInstruction *result = ir_build_cast(&ira->new_irb, target->scope, target->source_node,
9797 wanted_type, target, CastOpBitCast);
9798 result->value.type = wanted_type;
9799 return result;
9800}
9801
97539802static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_instr,
97549803 TypeTableEntry *wanted_type, IrInstruction *value)
97559804{
......@@ -9929,8 +9978,8 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
99299978 TypeTableEntry *wanted_child_type = wanted_type->data.maybe.child_type;
99309979 if (types_match_const_cast_only(ira, wanted_child_type, actual_type, source_node).id == ConstCastResultIdOk) {
99319980 return ir_analyze_maybe_wrap(ira, source_instr, value, wanted_type);
9932 } else if (actual_type->id == TypeTableEntryIdNumLitInt ||
9933 actual_type->id == TypeTableEntryIdNumLitFloat)
9981 } else if (actual_type->id == TypeTableEntryIdComptimeInt ||
9982 actual_type->id == TypeTableEntryIdComptimeFloat)
99349983 {
99359984 if (ir_num_lit_fits_in_other_type(ira, value, wanted_child_type, true)) {
99369985 return ir_analyze_maybe_wrap(ira, source_instr, value, wanted_type);
......@@ -9955,7 +10004,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
995510004
995610005 // explicit cast from null literal to maybe type
995710006 if (wanted_type->id == TypeTableEntryIdMaybe &&
9958 actual_type->id == TypeTableEntryIdNullLit)
10007 actual_type->id == TypeTableEntryIdNull)
995910008 {
996010009 return ir_analyze_null_to_maybe(ira, source_instr, value, wanted_type);
996110010 }
......@@ -9964,8 +10013,8 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
996410013 if (wanted_type->id == TypeTableEntryIdErrorUnion) {
996510014 if (types_match_const_cast_only(ira, wanted_type->data.error_union.payload_type, actual_type, source_node).id == ConstCastResultIdOk) {
996610015 return ir_analyze_err_wrap_payload(ira, source_instr, value, wanted_type);
9967 } else if (actual_type->id == TypeTableEntryIdNumLitInt ||
9968 actual_type->id == TypeTableEntryIdNumLitFloat)
10016 } else if (actual_type->id == TypeTableEntryIdComptimeInt ||
10017 actual_type->id == TypeTableEntryIdComptimeFloat)
996910018 {
997010019 if (ir_num_lit_fits_in_other_type(ira, value, wanted_type->data.error_union.payload_type, true)) {
997110020 return ir_analyze_err_wrap_payload(ira, source_instr, value, wanted_type);
......@@ -10012,9 +10061,9 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1001210061 {
1001310062 TypeTableEntry *wanted_child_type = wanted_type->data.error_union.payload_type->data.maybe.child_type;
1001410063 if (types_match_const_cast_only(ira, wanted_child_type, actual_type, source_node).id == ConstCastResultIdOk ||
10015 actual_type->id == TypeTableEntryIdNullLit ||
10016 actual_type->id == TypeTableEntryIdNumLitInt ||
10017 actual_type->id == TypeTableEntryIdNumLitFloat)
10064 actual_type->id == TypeTableEntryIdNull ||
10065 actual_type->id == TypeTableEntryIdComptimeInt ||
10066 actual_type->id == TypeTableEntryIdComptimeFloat)
1001810067 {
1001910068 IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, wanted_type->data.error_union.payload_type, value);
1002010069 if (type_is_invalid(cast1->value.type))
......@@ -10030,8 +10079,8 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1003010079
1003110080 // explicit cast from number literal to another type
1003210081 // explicit cast from number literal to &const integer
10033 if (actual_type->id == TypeTableEntryIdNumLitFloat ||
10034 actual_type->id == TypeTableEntryIdNumLitInt)
10082 if (actual_type->id == TypeTableEntryIdComptimeFloat ||
10083 actual_type->id == TypeTableEntryIdComptimeInt)
1003510084 {
1003610085 ensure_complete_type(ira->codegen, wanted_type);
1003710086 if (type_is_invalid(wanted_type))
......@@ -10060,9 +10109,9 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1006010109 return cast2;
1006110110 } else if (ir_num_lit_fits_in_other_type(ira, value, wanted_type, true)) {
1006210111 CastOp op;
10063 if ((actual_type->id == TypeTableEntryIdNumLitFloat &&
10112 if ((actual_type->id == TypeTableEntryIdComptimeFloat &&
1006410113 wanted_type->id == TypeTableEntryIdFloat) ||
10065 (actual_type->id == TypeTableEntryIdNumLitInt &&
10114 (actual_type->id == TypeTableEntryIdComptimeInt &&
1006610115 wanted_type->id == TypeTableEntryIdInt))
1006710116 {
1006810117 op = CastOpNumLitToConcrete;
......@@ -10082,8 +10131,8 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1008210131 // explicit cast from typed number to integer or float literal.
1008310132 // works when the number is known at compile time
1008410133 if (instr_is_comptime(value) &&
10085 ((actual_type->id == TypeTableEntryIdInt && wanted_type->id == TypeTableEntryIdNumLitInt) ||
10086 (actual_type->id == TypeTableEntryIdFloat && wanted_type->id == TypeTableEntryIdNumLitFloat)))
10134 ((actual_type->id == TypeTableEntryIdInt && wanted_type->id == TypeTableEntryIdComptimeInt) ||
10135 (actual_type->id == TypeTableEntryIdFloat && wanted_type->id == TypeTableEntryIdComptimeFloat)))
1008710136 {
1008810137 return ir_analyze_number_to_literal(ira, source_instr, value, wanted_type);
1008910138 }
......@@ -10156,8 +10205,32 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1015610205 }
1015710206 }
1015810207
10208 // explicit cast from *T to *[1]T
10209 if (wanted_type->id == TypeTableEntryIdPointer && wanted_type->data.pointer.ptr_len == PtrLenSingle &&
10210 actual_type->id == TypeTableEntryIdPointer && actual_type->data.pointer.ptr_len == PtrLenSingle)
10211 {
10212 TypeTableEntry *array_type = wanted_type->data.pointer.child_type;
10213 if (array_type->id == TypeTableEntryIdArray && array_type->data.array.len == 1 &&
10214 types_match_const_cast_only(ira, array_type->data.array.child_type,
10215 actual_type->data.pointer.child_type, source_node).id == ConstCastResultIdOk)
10216 {
10217 if (wanted_type->data.pointer.alignment > actual_type->data.pointer.alignment) {
10218 ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("cast increases pointer alignment"));
10219 add_error_note(ira->codegen, msg, value->source_node,
10220 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&actual_type->name),
10221 actual_type->data.pointer.alignment));
10222 add_error_note(ira->codegen, msg, source_instr->source_node,
10223 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&wanted_type->name),
10224 wanted_type->data.pointer.alignment));
10225 return ira->codegen->invalid_instruction;
10226 }
10227 return ir_analyze_ptr_to_array(ira, source_instr, value, wanted_type);
10228 }
10229 }
10230
10231
1015910232 // explicit cast from undefined to anything
10160 if (actual_type->id == TypeTableEntryIdUndefLit) {
10233 if (actual_type->id == TypeTableEntryIdUndefined) {
1016110234 return ir_analyze_undefined_to_anything(ira, source_instr, value, wanted_type);
1016210235 }
1016310236
......@@ -10554,19 +10627,19 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
1055410627 IrBinOp op_id = bin_op_instruction->op_id;
1055510628 bool is_equality_cmp = (op_id == IrBinOpCmpEq || op_id == IrBinOpCmpNotEq);
1055610629 if (is_equality_cmp &&
10557 ((op1->value.type->id == TypeTableEntryIdNullLit && op2->value.type->id == TypeTableEntryIdMaybe) ||
10558 (op2->value.type->id == TypeTableEntryIdNullLit && op1->value.type->id == TypeTableEntryIdMaybe) ||
10559 (op1->value.type->id == TypeTableEntryIdNullLit && op2->value.type->id == TypeTableEntryIdNullLit)))
10630 ((op1->value.type->id == TypeTableEntryIdNull && op2->value.type->id == TypeTableEntryIdMaybe) ||
10631 (op2->value.type->id == TypeTableEntryIdNull && op1->value.type->id == TypeTableEntryIdMaybe) ||
10632 (op1->value.type->id == TypeTableEntryIdNull && op2->value.type->id == TypeTableEntryIdNull)))
1056010633 {
10561 if (op1->value.type->id == TypeTableEntryIdNullLit && op2->value.type->id == TypeTableEntryIdNullLit) {
10634 if (op1->value.type->id == TypeTableEntryIdNull && op2->value.type->id == TypeTableEntryIdNull) {
1056210635 ConstExprValue *out_val = ir_build_const_from(ira, &bin_op_instruction->base);
1056310636 out_val->data.x_bool = (op_id == IrBinOpCmpEq);
1056410637 return ira->codegen->builtin_types.entry_bool;
1056510638 }
1056610639 IrInstruction *maybe_op;
10567 if (op1->value.type->id == TypeTableEntryIdNullLit) {
10640 if (op1->value.type->id == TypeTableEntryIdNull) {
1056810641 maybe_op = op2;
10569 } else if (op2->value.type->id == TypeTableEntryIdNullLit) {
10642 } else if (op2->value.type->id == TypeTableEntryIdNull) {
1057010643 maybe_op = op1;
1057110644 } else {
1057210645 zig_unreachable();
......@@ -10686,8 +10759,8 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
1068610759 case TypeTableEntryIdInvalid:
1068710760 zig_unreachable(); // handled above
1068810761
10689 case TypeTableEntryIdNumLitFloat:
10690 case TypeTableEntryIdNumLitInt:
10762 case TypeTableEntryIdComptimeFloat:
10763 case TypeTableEntryIdComptimeInt:
1069110764 case TypeTableEntryIdInt:
1069210765 case TypeTableEntryIdFloat:
1069310766 break;
......@@ -10722,8 +10795,8 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
1072210795 case TypeTableEntryIdUnreachable:
1072310796 case TypeTableEntryIdArray:
1072410797 case TypeTableEntryIdStruct:
10725 case TypeTableEntryIdUndefLit:
10726 case TypeTableEntryIdNullLit:
10798 case TypeTableEntryIdUndefined:
10799 case TypeTableEntryIdNull:
1072710800 case TypeTableEntryIdMaybe:
1072810801 case TypeTableEntryIdErrorUnion:
1072910802 case TypeTableEntryIdUnion:
......@@ -10745,10 +10818,10 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
1074510818 bool one_possible_value = !type_requires_comptime(resolved_type) && !type_has_bits(resolved_type);
1074610819 if (one_possible_value || (value_is_comptime(op1_val) && value_is_comptime(op2_val))) {
1074710820 bool answer;
10748 if (resolved_type->id == TypeTableEntryIdNumLitFloat || resolved_type->id == TypeTableEntryIdFloat) {
10821 if (resolved_type->id == TypeTableEntryIdComptimeFloat || resolved_type->id == TypeTableEntryIdFloat) {
1074910822 Cmp cmp_result = float_cmp(op1_val, op2_val);
1075010823 answer = resolve_cmp_op_id(op_id, cmp_result);
10751 } else if (resolved_type->id == TypeTableEntryIdNumLitInt || resolved_type->id == TypeTableEntryIdInt) {
10824 } else if (resolved_type->id == TypeTableEntryIdComptimeInt || resolved_type->id == TypeTableEntryIdInt) {
1075210825 Cmp cmp_result = bigint_cmp(&op1_val->data.x_bigint, &op2_val->data.x_bigint);
1075310826 answer = resolve_cmp_op_id(op_id, cmp_result);
1075410827 } else {
......@@ -10812,12 +10885,12 @@ static int ir_eval_math_op(TypeTableEntry *type_entry, ConstExprValue *op1_val,
1081210885 bool is_int;
1081310886 bool is_float;
1081410887 Cmp op2_zcmp;
10815 if (type_entry->id == TypeTableEntryIdInt || type_entry->id == TypeTableEntryIdNumLitInt) {
10888 if (type_entry->id == TypeTableEntryIdInt || type_entry->id == TypeTableEntryIdComptimeInt) {
1081610889 is_int = true;
1081710890 is_float = false;
1081810891 op2_zcmp = bigint_cmp_zero(&op2_val->data.x_bigint);
1081910892 } else if (type_entry->id == TypeTableEntryIdFloat ||
10820 type_entry->id == TypeTableEntryIdNumLitFloat)
10893 type_entry->id == TypeTableEntryIdComptimeFloat)
1082110894 {
1082210895 is_int = false;
1082310896 is_float = true;
......@@ -10991,7 +11064,7 @@ static TypeTableEntry *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *
1099111064 if (type_is_invalid(op1->value.type))
1099211065 return ira->codegen->builtin_types.entry_invalid;
1099311066
10994 if (op1->value.type->id != TypeTableEntryIdInt && op1->value.type->id != TypeTableEntryIdNumLitInt) {
11067 if (op1->value.type->id != TypeTableEntryIdInt && op1->value.type->id != TypeTableEntryIdComptimeInt) {
1099511068 ir_add_error(ira, &bin_op_instruction->base,
1099611069 buf_sprintf("bit shifting operation expected integer type, found '%s'",
1099711070 buf_ptr(&op1->value.type->name)));
......@@ -11004,7 +11077,7 @@ static TypeTableEntry *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *
1100411077
1100511078 IrInstruction *casted_op2;
1100611079 IrBinOp op_id = bin_op_instruction->op_id;
11007 if (op1->value.type->id == TypeTableEntryIdNumLitInt) {
11080 if (op1->value.type->id == TypeTableEntryIdComptimeInt) {
1100811081 casted_op2 = op2;
1100911082
1101011083 if (op_id == IrBinOpBitShiftLeftLossy) {
......@@ -11049,7 +11122,7 @@ static TypeTableEntry *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *
1104911122
1105011123 ir_num_lit_fits_in_other_type(ira, result_instruction, op1->value.type, false);
1105111124 return op1->value.type;
11052 } else if (op1->value.type->id == TypeTableEntryIdNumLitInt) {
11125 } else if (op1->value.type->id == TypeTableEntryIdComptimeInt) {
1105311126 ir_add_error(ira, &bin_op_instruction->base,
1105411127 buf_sprintf("LHS of shift must be an integer type, or RHS must be compile-time known"));
1105511128 return ira->codegen->builtin_types.entry_invalid;
......@@ -11085,15 +11158,15 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
1108511158 if (type_is_invalid(resolved_type))
1108611159 return resolved_type;
1108711160
11088 bool is_int = resolved_type->id == TypeTableEntryIdInt || resolved_type->id == TypeTableEntryIdNumLitInt;
11089 bool is_float = resolved_type->id == TypeTableEntryIdFloat || resolved_type->id == TypeTableEntryIdNumLitFloat;
11161 bool is_int = resolved_type->id == TypeTableEntryIdInt || resolved_type->id == TypeTableEntryIdComptimeInt;
11162 bool is_float = resolved_type->id == TypeTableEntryIdFloat || resolved_type->id == TypeTableEntryIdComptimeFloat;
1109011163 bool is_signed_div = (
1109111164 (resolved_type->id == TypeTableEntryIdInt && resolved_type->data.integral.is_signed) ||
1109211165 resolved_type->id == TypeTableEntryIdFloat ||
11093 (resolved_type->id == TypeTableEntryIdNumLitFloat &&
11166 (resolved_type->id == TypeTableEntryIdComptimeFloat &&
1109411167 ((bigfloat_cmp_zero(&op1->value.data.x_bigfloat) != CmpGT) !=
1109511168 (bigfloat_cmp_zero(&op2->value.data.x_bigfloat) != CmpGT))) ||
11096 (resolved_type->id == TypeTableEntryIdNumLitInt &&
11169 (resolved_type->id == TypeTableEntryIdComptimeInt &&
1109711170 ((bigint_cmp_zero(&op1->value.data.x_bigint) != CmpGT) !=
1109811171 (bigint_cmp_zero(&op2->value.data.x_bigint) != CmpGT)))
1109911172 );
......@@ -11194,7 +11267,7 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
1119411267 return ira->codegen->builtin_types.entry_invalid;
1119511268 }
1119611269
11197 if (resolved_type->id == TypeTableEntryIdNumLitInt) {
11270 if (resolved_type->id == TypeTableEntryIdComptimeInt) {
1119811271 if (op_id == IrBinOpAddWrap) {
1119911272 op_id = IrBinOpAdd;
1120011273 } else if (op_id == IrBinOpSubWrap) {
......@@ -11568,11 +11641,11 @@ static VarClassRequired get_var_class_required(TypeTableEntry *type_entry) {
1156811641 case TypeTableEntryIdFn:
1156911642 case TypeTableEntryIdPromise:
1157011643 return VarClassRequiredAny;
11571 case TypeTableEntryIdNumLitFloat:
11572 case TypeTableEntryIdNumLitInt:
11573 case TypeTableEntryIdUndefLit:
11644 case TypeTableEntryIdComptimeFloat:
11645 case TypeTableEntryIdComptimeInt:
11646 case TypeTableEntryIdUndefined:
1157411647 case TypeTableEntryIdBlock:
11575 case TypeTableEntryIdNullLit:
11648 case TypeTableEntryIdNull:
1157611649 case TypeTableEntryIdOpaque:
1157711650 case TypeTableEntryIdMetaType:
1157811651 case TypeTableEntryIdNamespace:
......@@ -11837,10 +11910,10 @@ static TypeTableEntry *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructi
1183711910 case TypeTableEntryIdMetaType:
1183811911 case TypeTableEntryIdVoid:
1183911912 case TypeTableEntryIdUnreachable:
11840 case TypeTableEntryIdNumLitFloat:
11841 case TypeTableEntryIdNumLitInt:
11842 case TypeTableEntryIdUndefLit:
11843 case TypeTableEntryIdNullLit:
11913 case TypeTableEntryIdComptimeFloat:
11914 case TypeTableEntryIdComptimeInt:
11915 case TypeTableEntryIdUndefined:
11916 case TypeTableEntryIdNull:
1184411917 case TypeTableEntryIdMaybe:
1184511918 case TypeTableEntryIdErrorUnion:
1184611919 case TypeTableEntryIdErrorSet:
......@@ -11861,10 +11934,10 @@ static TypeTableEntry *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructi
1186111934 case TypeTableEntryIdFloat:
1186211935 case TypeTableEntryIdPointer:
1186311936 case TypeTableEntryIdArray:
11864 case TypeTableEntryIdNumLitFloat:
11865 case TypeTableEntryIdNumLitInt:
11866 case TypeTableEntryIdUndefLit:
11867 case TypeTableEntryIdNullLit:
11937 case TypeTableEntryIdComptimeFloat:
11938 case TypeTableEntryIdComptimeInt:
11939 case TypeTableEntryIdUndefined:
11940 case TypeTableEntryIdNull:
1186811941 case TypeTableEntryIdMaybe:
1186911942 case TypeTableEntryIdErrorUnion:
1187011943 case TypeTableEntryIdErrorSet:
......@@ -12076,7 +12149,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1207612149 }
1207712150
1207812151 bool comptime_arg = param_decl_node->data.param_decl.is_inline ||
12079 casted_arg->value.type->id == TypeTableEntryIdNumLitInt || casted_arg->value.type->id == TypeTableEntryIdNumLitFloat;
12152 casted_arg->value.type->id == TypeTableEntryIdComptimeInt || casted_arg->value.type->id == TypeTableEntryIdComptimeFloat;
1208012153
1208112154 ConstExprValue *arg_val;
1208212155
......@@ -12101,8 +12174,8 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1210112174 var->shadowable = !comptime_arg;
1210212175
1210312176 *next_proto_i += 1;
12104 } else if (casted_arg->value.type->id == TypeTableEntryIdNumLitInt ||
12105 casted_arg->value.type->id == TypeTableEntryIdNumLitFloat)
12177 } else if (casted_arg->value.type->id == TypeTableEntryIdComptimeInt ||
12178 casted_arg->value.type->id == TypeTableEntryIdComptimeFloat)
1210612179 {
1210712180 ir_add_error(ira, casted_arg,
1210812181 buf_sprintf("compiler bug: integer and float literals in var args function must be casted. https://github.com/ziglang/zig/issues/557"));
......@@ -12825,10 +12898,10 @@ static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op
1282512898 case TypeTableEntryIdPointer:
1282612899 case TypeTableEntryIdArray:
1282712900 case TypeTableEntryIdStruct:
12828 case TypeTableEntryIdNumLitFloat:
12829 case TypeTableEntryIdNumLitInt:
12830 case TypeTableEntryIdUndefLit:
12831 case TypeTableEntryIdNullLit:
12901 case TypeTableEntryIdComptimeFloat:
12902 case TypeTableEntryIdComptimeInt:
12903 case TypeTableEntryIdUndefined:
12904 case TypeTableEntryIdNull:
1283212905 case TypeTableEntryIdMaybe:
1283312906 case TypeTableEntryIdErrorUnion:
1283412907 case TypeTableEntryIdErrorSet:
......@@ -12862,10 +12935,10 @@ static TypeTableEntry *ir_analyze_negation(IrAnalyze *ira, IrInstructionUnOp *un
1286212935
1286312936 bool is_wrap_op = (un_op_instruction->op_id == IrUnOpNegationWrap);
1286412937
12865 bool is_float = (expr_type->id == TypeTableEntryIdFloat || expr_type->id == TypeTableEntryIdNumLitFloat);
12938 bool is_float = (expr_type->id == TypeTableEntryIdFloat || expr_type->id == TypeTableEntryIdComptimeFloat);
1286612939
1286712940 if ((expr_type->id == TypeTableEntryIdInt && expr_type->data.integral.is_signed) ||
12868 expr_type->id == TypeTableEntryIdNumLitInt || (is_float && !is_wrap_op))
12941 expr_type->id == TypeTableEntryIdComptimeInt || (is_float && !is_wrap_op))
1286912942 {
1287012943 if (instr_is_comptime(value)) {
1287112944 ConstExprValue *target_const_val = ir_resolve_const(ira, value, UndefBad);
......@@ -12881,7 +12954,7 @@ static TypeTableEntry *ir_analyze_negation(IrAnalyze *ira, IrInstructionUnOp *un
1288112954 } else {
1288212955 bigint_negate(&out_val->data.x_bigint, &target_const_val->data.x_bigint);
1288312956 }
12884 if (is_wrap_op || is_float || expr_type->id == TypeTableEntryIdNumLitInt) {
12957 if (is_wrap_op || is_float || expr_type->id == TypeTableEntryIdComptimeInt) {
1288512958 return expr_type;
1288612959 }
1288712960
......@@ -13077,10 +13150,10 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
1307713150 if (type_is_invalid(resolved_type))
1307813151 return resolved_type;
1307913152
13080 if (resolved_type->id == TypeTableEntryIdNumLitFloat ||
13081 resolved_type->id == TypeTableEntryIdNumLitInt ||
13082 resolved_type->id == TypeTableEntryIdNullLit ||
13083 resolved_type->id == TypeTableEntryIdUndefLit)
13153 if (resolved_type->id == TypeTableEntryIdComptimeFloat ||
13154 resolved_type->id == TypeTableEntryIdComptimeInt ||
13155 resolved_type->id == TypeTableEntryIdNull ||
13156 resolved_type->id == TypeTableEntryIdUndefined)
1308413157 {
1308513158 ir_add_error_node(ira, phi_instruction->base.source_node,
1308613159 buf_sprintf("unable to infer expression type"));
......@@ -13162,11 +13235,13 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
1316213235 if (type_is_invalid(array_ptr->value.type))
1316313236 return ira->codegen->builtin_types.entry_invalid;
1316413237
13238 ConstExprValue *orig_array_ptr_val = &array_ptr->value;
13239
1316513240 IrInstruction *elem_index = elem_ptr_instruction->elem_index->other;
1316613241 if (type_is_invalid(elem_index->value.type))
1316713242 return ira->codegen->builtin_types.entry_invalid;
1316813243
13169 TypeTableEntry *ptr_type = array_ptr->value.type;
13244 TypeTableEntry *ptr_type = orig_array_ptr_val->type;
1317013245 assert(ptr_type->id == TypeTableEntryIdPointer);
1317113246
1317213247 TypeTableEntry *array_type = ptr_type->data.pointer.child_type;
......@@ -13177,7 +13252,18 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
1317713252
1317813253 if (type_is_invalid(array_type)) {
1317913254 return array_type;
13180 } else if (array_type->id == TypeTableEntryIdArray) {
13255 } else if (array_type->id == TypeTableEntryIdArray ||
13256 (array_type->id == TypeTableEntryIdPointer &&
13257 array_type->data.pointer.ptr_len == PtrLenSingle &&
13258 array_type->data.pointer.child_type->id == TypeTableEntryIdArray))
13259 {
13260 if (array_type->id == TypeTableEntryIdPointer) {
13261 array_type = array_type->data.pointer.child_type;
13262 ptr_type = ptr_type->data.pointer.child_type;
13263 if (orig_array_ptr_val->special != ConstValSpecialRuntime) {
13264 orig_array_ptr_val = const_ptr_pointee(ira->codegen, orig_array_ptr_val);
13265 }
13266 }
1318113267 if (array_type->data.array.len == 0) {
1318213268 ir_add_error_node(ira, elem_ptr_instruction->base.source_node,
1318313269 buf_sprintf("index 0 outside array of size 0"));
......@@ -13205,7 +13291,7 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
1320513291 } else if (array_type->id == TypeTableEntryIdPointer) {
1320613292 if (array_type->data.pointer.ptr_len == PtrLenSingle) {
1320713293 ir_add_error_node(ira, elem_ptr_instruction->base.source_node,
13208 buf_sprintf("indexing not allowed on pointer to single item"));
13294 buf_sprintf("index of single-item pointer"));
1320913295 return ira->codegen->builtin_types.entry_invalid;
1321013296 }
1321113297 return_type = adjust_ptr_len(ira->codegen, array_type, elem_ptr_instruction->ptr_len);
......@@ -13294,9 +13380,9 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
1329413380 }
1329513381
1329613382 ConstExprValue *array_ptr_val;
13297 if (array_ptr->value.special != ConstValSpecialRuntime &&
13298 (array_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar || array_type->id == TypeTableEntryIdArray) &&
13299 (array_ptr_val = const_ptr_pointee(ira->codegen, &array_ptr->value)) &&
13383 if (orig_array_ptr_val->special != ConstValSpecialRuntime &&
13384 (orig_array_ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar || array_type->id == TypeTableEntryIdArray) &&
13385 (array_ptr_val = const_ptr_pointee(ira->codegen, orig_array_ptr_val)) &&
1330013386 array_ptr_val->special != ConstValSpecialRuntime &&
1330113387 (array_type->id != TypeTableEntryIdPointer ||
1330213388 array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr))
......@@ -13401,7 +13487,7 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
1340113487 } else if (array_type->id == TypeTableEntryIdArray) {
1340213488 ConstExprValue *out_val = ir_build_const_from(ira, &elem_ptr_instruction->base);
1340313489 out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
13404 out_val->data.x_ptr.mut = array_ptr->value.data.x_ptr.mut;
13490 out_val->data.x_ptr.mut = orig_array_ptr_val->data.x_ptr.mut;
1340513491 out_val->data.x_ptr.data.base_array.array_val = array_ptr_val;
1340613492 out_val->data.x_ptr.data.base_array.elem_index = index;
1340713493 return return_type;
......@@ -14127,10 +14213,10 @@ static TypeTableEntry *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructi
1412714213 switch (type_entry->id) {
1412814214 case TypeTableEntryIdInvalid:
1412914215 zig_unreachable(); // handled above
14130 case TypeTableEntryIdNumLitFloat:
14131 case TypeTableEntryIdNumLitInt:
14132 case TypeTableEntryIdUndefLit:
14133 case TypeTableEntryIdNullLit:
14216 case TypeTableEntryIdComptimeFloat:
14217 case TypeTableEntryIdComptimeInt:
14218 case TypeTableEntryIdUndefined:
14219 case TypeTableEntryIdNull:
1413414220 case TypeTableEntryIdNamespace:
1413514221 case TypeTableEntryIdBlock:
1413614222 case TypeTableEntryIdBoundFn:
......@@ -14393,8 +14479,8 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1439314479 case TypeTableEntryIdInvalid: // handled above
1439414480 zig_unreachable();
1439514481 case TypeTableEntryIdUnreachable:
14396 case TypeTableEntryIdUndefLit:
14397 case TypeTableEntryIdNullLit:
14482 case TypeTableEntryIdUndefined:
14483 case TypeTableEntryIdNull:
1439814484 case TypeTableEntryIdBlock:
1439914485 case TypeTableEntryIdArgTuple:
1440014486 case TypeTableEntryIdOpaque:
......@@ -14409,8 +14495,8 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1440914495 case TypeTableEntryIdPointer:
1441014496 case TypeTableEntryIdArray:
1441114497 case TypeTableEntryIdStruct:
14412 case TypeTableEntryIdNumLitFloat:
14413 case TypeTableEntryIdNumLitInt:
14498 case TypeTableEntryIdComptimeFloat:
14499 case TypeTableEntryIdComptimeInt:
1441414500 case TypeTableEntryIdMaybe:
1441514501 case TypeTableEntryIdErrorUnion:
1441614502 case TypeTableEntryIdErrorSet:
......@@ -14501,8 +14587,8 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,
1450114587 case TypeTableEntryIdInvalid: // handled above
1450214588 zig_unreachable();
1450314589 case TypeTableEntryIdUnreachable:
14504 case TypeTableEntryIdUndefLit:
14505 case TypeTableEntryIdNullLit:
14590 case TypeTableEntryIdUndefined:
14591 case TypeTableEntryIdNull:
1450614592 case TypeTableEntryIdBlock:
1450714593 case TypeTableEntryIdArgTuple:
1450814594 case TypeTableEntryIdOpaque:
......@@ -14517,8 +14603,8 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,
1451714603 case TypeTableEntryIdPointer:
1451814604 case TypeTableEntryIdArray:
1451914605 case TypeTableEntryIdStruct:
14520 case TypeTableEntryIdNumLitFloat:
14521 case TypeTableEntryIdNumLitInt:
14606 case TypeTableEntryIdComptimeFloat:
14607 case TypeTableEntryIdComptimeInt:
1452214608 case TypeTableEntryIdMaybe:
1452314609 case TypeTableEntryIdErrorUnion:
1452414610 case TypeTableEntryIdErrorSet:
......@@ -14570,11 +14656,11 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
1457014656 case TypeTableEntryIdInvalid: // handled above
1457114657 zig_unreachable();
1457214658 case TypeTableEntryIdUnreachable:
14573 case TypeTableEntryIdUndefLit:
14574 case TypeTableEntryIdNullLit:
14659 case TypeTableEntryIdUndefined:
14660 case TypeTableEntryIdNull:
1457514661 case TypeTableEntryIdBlock:
14576 case TypeTableEntryIdNumLitFloat:
14577 case TypeTableEntryIdNumLitInt:
14662 case TypeTableEntryIdComptimeFloat:
14663 case TypeTableEntryIdComptimeInt:
1457814664 case TypeTableEntryIdBoundFn:
1457914665 case TypeTableEntryIdMetaType:
1458014666 case TypeTableEntryIdNamespace:
......@@ -14627,7 +14713,7 @@ static TypeTableEntry *ir_analyze_instruction_test_non_null(IrAnalyze *ira, IrIn
1462714713
1462814714 ir_build_test_nonnull_from(&ira->new_irb, &instruction->base, value);
1462914715 return ira->codegen->builtin_types.entry_bool;
14630 } else if (type_entry->id == TypeTableEntryIdNullLit) {
14716 } else if (type_entry->id == TypeTableEntryIdNull) {
1463114717 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1463214718 out_val->data.x_bool = false;
1463314719 return ira->codegen->builtin_types.entry_bool;
......@@ -14934,8 +15020,8 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1493415020 case TypeTableEntryIdBool:
1493515021 case TypeTableEntryIdInt:
1493615022 case TypeTableEntryIdFloat:
14937 case TypeTableEntryIdNumLitFloat:
14938 case TypeTableEntryIdNumLitInt:
15023 case TypeTableEntryIdComptimeFloat:
15024 case TypeTableEntryIdComptimeInt:
1493915025 case TypeTableEntryIdPointer:
1494015026 case TypeTableEntryIdPromise:
1494115027 case TypeTableEntryIdFn:
......@@ -15013,8 +15099,8 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1501315099 case TypeTableEntryIdUnreachable:
1501415100 case TypeTableEntryIdArray:
1501515101 case TypeTableEntryIdStruct:
15016 case TypeTableEntryIdUndefLit:
15017 case TypeTableEntryIdNullLit:
15102 case TypeTableEntryIdUndefined:
15103 case TypeTableEntryIdNull:
1501815104 case TypeTableEntryIdMaybe:
1501915105 case TypeTableEntryIdBlock:
1502015106 case TypeTableEntryIdBoundFn:
......@@ -15532,10 +15618,10 @@ static TypeTableEntry *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_
1553215618 case TypeTableEntryIdPromise:
1553315619 case TypeTableEntryIdArray:
1553415620 case TypeTableEntryIdStruct:
15535 case TypeTableEntryIdNumLitFloat:
15536 case TypeTableEntryIdNumLitInt:
15537 case TypeTableEntryIdUndefLit:
15538 case TypeTableEntryIdNullLit:
15621 case TypeTableEntryIdComptimeFloat:
15622 case TypeTableEntryIdComptimeInt:
15623 case TypeTableEntryIdUndefined:
15624 case TypeTableEntryIdNull:
1553915625 case TypeTableEntryIdMaybe:
1554015626 case TypeTableEntryIdErrorUnion:
1554115627 case TypeTableEntryIdErrorSet:
......@@ -16194,10 +16280,10 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1619416280 case TypeTableEntryIdVoid:
1619516281 case TypeTableEntryIdBool:
1619616282 case TypeTableEntryIdUnreachable:
16197 case TypeTableEntryIdNumLitFloat:
16198 case TypeTableEntryIdNumLitInt:
16199 case TypeTableEntryIdUndefLit:
16200 case TypeTableEntryIdNullLit:
16283 case TypeTableEntryIdComptimeFloat:
16284 case TypeTableEntryIdComptimeInt:
16285 case TypeTableEntryIdUndefined:
16286 case TypeTableEntryIdNull:
1620116287 case TypeTableEntryIdNamespace:
1620216288 case TypeTableEntryIdBlock:
1620316289 case TypeTableEntryIdArgTuple:
......@@ -17057,7 +17143,7 @@ static TypeTableEntry *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstruc
1705717143 return ira->codegen->builtin_types.entry_invalid;
1705817144
1705917145 if (dest_type->id != TypeTableEntryIdInt &&
17060 dest_type->id != TypeTableEntryIdNumLitInt)
17146 dest_type->id != TypeTableEntryIdComptimeInt)
1706117147 {
1706217148 ir_add_error(ira, dest_type_value, buf_sprintf("expected integer type, found '%s'", buf_ptr(&dest_type->name)));
1706317149 return ira->codegen->builtin_types.entry_invalid;
......@@ -17069,7 +17155,7 @@ static TypeTableEntry *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstruc
1706917155 return ira->codegen->builtin_types.entry_invalid;
1707017156
1707117157 if (src_type->id != TypeTableEntryIdInt &&
17072 src_type->id != TypeTableEntryIdNumLitInt)
17158 src_type->id != TypeTableEntryIdComptimeInt)
1707317159 {
1707417160 ir_add_error(ira, target, buf_sprintf("expected integer type, found '%s'", buf_ptr(&src_type->name)));
1707517161 return ira->codegen->builtin_types.entry_invalid;
......@@ -17406,14 +17492,29 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
1740617492 byte_alignment, 0, 0);
1740717493 return_type = get_slice_type(ira->codegen, slice_ptr_type);
1740817494 } else if (array_type->id == TypeTableEntryIdPointer) {
17409 TypeTableEntry *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, array_type->data.pointer.child_type,
17410 array_type->data.pointer.is_const, array_type->data.pointer.is_volatile,
17411 PtrLenUnknown,
17412 array_type->data.pointer.alignment, 0, 0);
17413 return_type = get_slice_type(ira->codegen, slice_ptr_type);
17414 if (!end) {
17415 ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value"));
17416 return ira->codegen->builtin_types.entry_invalid;
17495 if (array_type->data.pointer.ptr_len == PtrLenSingle) {
17496 TypeTableEntry *main_type = array_type->data.pointer.child_type;
17497 if (main_type->id == TypeTableEntryIdArray) {
17498 TypeTableEntry *slice_ptr_type = get_pointer_to_type_extra(ira->codegen,
17499 main_type->data.pointer.child_type,
17500 array_type->data.pointer.is_const, array_type->data.pointer.is_volatile,
17501 PtrLenUnknown,
17502 array_type->data.pointer.alignment, 0, 0);
17503 return_type = get_slice_type(ira->codegen, slice_ptr_type);
17504 } else {
17505 ir_add_error(ira, &instruction->base, buf_sprintf("slice of single-item pointer"));
17506 return ira->codegen->builtin_types.entry_invalid;
17507 }
17508 } else {
17509 TypeTableEntry *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, array_type->data.pointer.child_type,
17510 array_type->data.pointer.is_const, array_type->data.pointer.is_volatile,
17511 PtrLenUnknown,
17512 array_type->data.pointer.alignment, 0, 0);
17513 return_type = get_slice_type(ira->codegen, slice_ptr_type);
17514 if (!end) {
17515 ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value"));
17516 return ira->codegen->builtin_types.entry_invalid;
17517 }
1741717518 }
1741817519 } else if (is_slice(array_type)) {
1741917520 TypeTableEntry *ptr_type = array_type->data.structure.fields[slice_ptr_index].type_entry;
......@@ -17433,12 +17534,24 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
1743317534 size_t abs_offset;
1743417535 size_t rel_end;
1743517536 bool ptr_is_undef = false;
17436 if (array_type->id == TypeTableEntryIdArray) {
17437 array_val = const_ptr_pointee(ira->codegen, &ptr_ptr->value);
17438 abs_offset = 0;
17439 rel_end = array_type->data.array.len;
17440 parent_ptr = nullptr;
17537 if (array_type->id == TypeTableEntryIdArray ||
17538 (array_type->id == TypeTableEntryIdPointer && array_type->data.pointer.ptr_len == PtrLenSingle))
17539 {
17540 if (array_type->id == TypeTableEntryIdPointer) {
17541 TypeTableEntry *child_array_type = array_type->data.pointer.child_type;
17542 assert(child_array_type->id == TypeTableEntryIdArray);
17543 parent_ptr = const_ptr_pointee(ira->codegen, &ptr_ptr->value);
17544 array_val = const_ptr_pointee(ira->codegen, parent_ptr);
17545 rel_end = child_array_type->data.array.len;
17546 abs_offset = 0;
17547 } else {
17548 array_val = const_ptr_pointee(ira->codegen, &ptr_ptr->value);
17549 rel_end = array_type->data.array.len;
17550 parent_ptr = nullptr;
17551 abs_offset = 0;
17552 }
1744117553 } else if (array_type->id == TypeTableEntryIdPointer) {
17554 assert(array_type->data.pointer.ptr_len == PtrLenUnknown);
1744217555 parent_ptr = const_ptr_pointee(ira->codegen, &ptr_ptr->value);
1744317556 if (parent_ptr->special == ConstValSpecialUndef) {
1744417557 array_val = nullptr;
......@@ -17537,7 +17650,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
1753717650 if (array_val) {
1753817651 size_t index = abs_offset + start_scalar;
1753917652 bool is_const = slice_is_const(return_type);
17540 init_const_ptr_array(ira->codegen, ptr_val, array_val, index, is_const);
17653 init_const_ptr_array(ira->codegen, ptr_val, array_val, index, is_const, PtrLenUnknown);
1754117654 if (array_type->id == TypeTableEntryIdArray) {
1754217655 ptr_val->data.x_ptr.mut = ptr_ptr->value.data.x_ptr.mut;
1754317656 } else if (is_slice(array_type)) {
......@@ -17763,10 +17876,10 @@ static TypeTableEntry *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruc
1776317876 zig_unreachable();
1776417877 case TypeTableEntryIdMetaType:
1776517878 case TypeTableEntryIdUnreachable:
17766 case TypeTableEntryIdNumLitFloat:
17767 case TypeTableEntryIdNumLitInt:
17768 case TypeTableEntryIdUndefLit:
17769 case TypeTableEntryIdNullLit:
17879 case TypeTableEntryIdComptimeFloat:
17880 case TypeTableEntryIdComptimeInt:
17881 case TypeTableEntryIdUndefined:
17882 case TypeTableEntryIdNull:
1777017883 case TypeTableEntryIdNamespace:
1777117884 case TypeTableEntryIdBlock:
1777217885 case TypeTableEntryIdBoundFn:
......@@ -18264,8 +18377,8 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira
1826418377 if (!end_val)
1826518378 return ira->codegen->builtin_types.entry_invalid;
1826618379
18267 assert(start_val->type->id == TypeTableEntryIdInt || start_val->type->id == TypeTableEntryIdNumLitInt);
18268 assert(end_val->type->id == TypeTableEntryIdInt || end_val->type->id == TypeTableEntryIdNumLitInt);
18380 assert(start_val->type->id == TypeTableEntryIdInt || start_val->type->id == TypeTableEntryIdComptimeInt);
18381 assert(end_val->type->id == TypeTableEntryIdInt || end_val->type->id == TypeTableEntryIdComptimeInt);
1826918382 AstNode *prev_node = rangeset_add_range(&rs, &start_val->data.x_bigint, &end_val->data.x_bigint,
1827018383 start_value->source_node);
1827118384 if (prev_node != nullptr) {
......@@ -18497,10 +18610,10 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
1849718610 case TypeTableEntryIdNamespace:
1849818611 case TypeTableEntryIdBlock:
1849918612 case TypeTableEntryIdUnreachable:
18500 case TypeTableEntryIdNumLitFloat:
18501 case TypeTableEntryIdNumLitInt:
18502 case TypeTableEntryIdUndefLit:
18503 case TypeTableEntryIdNullLit:
18613 case TypeTableEntryIdComptimeFloat:
18614 case TypeTableEntryIdComptimeInt:
18615 case TypeTableEntryIdUndefined:
18616 case TypeTableEntryIdNull:
1850418617 case TypeTableEntryIdPromise:
1850518618 zig_unreachable();
1850618619 case TypeTableEntryIdVoid:
......@@ -18564,10 +18677,10 @@ static void buf_read_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
1856418677 case TypeTableEntryIdNamespace:
1856518678 case TypeTableEntryIdBlock:
1856618679 case TypeTableEntryIdUnreachable:
18567 case TypeTableEntryIdNumLitFloat:
18568 case TypeTableEntryIdNumLitInt:
18569 case TypeTableEntryIdUndefLit:
18570 case TypeTableEntryIdNullLit:
18680 case TypeTableEntryIdComptimeFloat:
18681 case TypeTableEntryIdComptimeInt:
18682 case TypeTableEntryIdUndefined:
18683 case TypeTableEntryIdNull:
1857118684 case TypeTableEntryIdPromise:
1857218685 zig_unreachable();
1857318686 case TypeTableEntryIdVoid:
......@@ -18645,10 +18758,10 @@ static TypeTableEntry *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstruc
1864518758 case TypeTableEntryIdNamespace:
1864618759 case TypeTableEntryIdBlock:
1864718760 case TypeTableEntryIdUnreachable:
18648 case TypeTableEntryIdNumLitFloat:
18649 case TypeTableEntryIdNumLitInt:
18650 case TypeTableEntryIdUndefLit:
18651 case TypeTableEntryIdNullLit:
18761 case TypeTableEntryIdComptimeFloat:
18762 case TypeTableEntryIdComptimeInt:
18763 case TypeTableEntryIdUndefined:
18764 case TypeTableEntryIdNull:
1865218765 ir_add_error(ira, dest_type_value,
1865318766 buf_sprintf("unable to @bitCast from type '%s'", buf_ptr(&src_type->name)));
1865418767 return ira->codegen->builtin_types.entry_invalid;
......@@ -18671,10 +18784,10 @@ static TypeTableEntry *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstruc
1867118784 case TypeTableEntryIdNamespace:
1867218785 case TypeTableEntryIdBlock:
1867318786 case TypeTableEntryIdUnreachable:
18674 case TypeTableEntryIdNumLitFloat:
18675 case TypeTableEntryIdNumLitInt:
18676 case TypeTableEntryIdUndefLit:
18677 case TypeTableEntryIdNullLit:
18787 case TypeTableEntryIdComptimeFloat:
18788 case TypeTableEntryIdComptimeInt:
18789 case TypeTableEntryIdUndefined:
18790 case TypeTableEntryIdNull:
1867818791 ir_add_error(ira, dest_type_value,
1867918792 buf_sprintf("unable to @bitCast to type '%s'", buf_ptr(&dest_type->name)));
1868018793 return ira->codegen->builtin_types.entry_invalid;
......@@ -19447,7 +19560,7 @@ static TypeTableEntry *ir_analyze_instruction_sqrt(IrAnalyze *ira, IrInstruction
1944719560 if (type_is_invalid(op->value.type))
1944819561 return ira->codegen->builtin_types.entry_invalid;
1944919562
19450 bool ok_type = float_type->id == TypeTableEntryIdNumLitFloat || float_type->id == TypeTableEntryIdFloat;
19563 bool ok_type = float_type->id == TypeTableEntryIdComptimeFloat || float_type->id == TypeTableEntryIdFloat;
1945119564 if (!ok_type) {
1945219565 ir_add_error(ira, instruction->type, buf_sprintf("@sqrt does not support type '%s'", buf_ptr(&float_type->name)));
1945319566 return ira->codegen->builtin_types.entry_invalid;
......@@ -19464,7 +19577,7 @@ static TypeTableEntry *ir_analyze_instruction_sqrt(IrAnalyze *ira, IrInstruction
1946419577
1946519578 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1946619579
19467 if (float_type->id == TypeTableEntryIdNumLitFloat) {
19580 if (float_type->id == TypeTableEntryIdComptimeFloat) {
1946819581 bigfloat_sqrt(&out_val->data.x_bigfloat, &val->data.x_bigfloat);
1946919582 } else if (float_type->id == TypeTableEntryIdFloat) {
1947019583 switch (float_type->data.floating.bit_count) {
src/util.hpp+16-3
......@@ -65,6 +65,11 @@ static inline int clzll(unsigned long long mask) {
6565
6666template<typename T>
6767ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate_nonzero(size_t count) {
68#ifndef NDEBUG
69 // make behavior when size == 0 portable
70 if (count == 0)
71 return nullptr;
72#endif
6873 T *ptr = reinterpret_cast<T*>(malloc(count * sizeof(T)));
6974 if (!ptr)
7075 zig_panic("allocation failed");
......@@ -73,6 +78,11 @@ ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate_nonzero(size_t count) {
7378
7479template<typename T>
7580ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate(size_t count) {
81#ifndef NDEBUG
82 // make behavior when size == 0 portable
83 if (count == 0)
84 return nullptr;
85#endif
7686 T *ptr = reinterpret_cast<T*>(calloc(count, sizeof(T)));
7787 if (!ptr)
7888 zig_panic("allocation failed");
......@@ -93,9 +103,7 @@ static inline void safe_memcpy(T *dest, const T *src, size_t count) {
93103
94104template<typename T>
95105static inline T *reallocate(T *old, size_t old_count, size_t new_count) {
96 T *ptr = reinterpret_cast<T*>(realloc(old, new_count * sizeof(T)));
97 if (!ptr)
98 zig_panic("allocation failed");
106 T *ptr = reallocate_nonzero(old, old_count, new_count);
99107 if (new_count > old_count) {
100108 memset(&ptr[old_count], 0, (new_count - old_count) * sizeof(T));
101109 }
......@@ -104,6 +112,11 @@ static inline T *reallocate(T *old, size_t old_count, size_t new_count) {
104112
105113template<typename T>
106114static inline T *reallocate_nonzero(T *old, size_t old_count, size_t new_count) {
115#ifndef NDEBUG
116 // make behavior when size == 0 portable
117 if (new_count == 0 && old == nullptr)
118 return nullptr;
119#endif
107120 T *ptr = reinterpret_cast<T*>(realloc(old, new_count * sizeof(T)));
108121 if (!ptr)
109122 zig_panic("allocation failed");
std/fmt/errol/index.zig+1-1
......@@ -59,7 +59,7 @@ pub fn roundToPrecision(float_decimal: *FloatDecimal, precision: usize, mode: Ro
5959 float_decimal.exp += 1;
6060
6161 // Re-size the buffer to use the reserved leading byte.
62 const one_before = @intToPtr(*u8, @ptrToInt(&float_decimal.digits[0]) - 1);
62 const one_before = @intToPtr([*]u8, @ptrToInt(&float_decimal.digits[0]) - 1);
6363 float_decimal.digits = one_before[0 .. float_decimal.digits.len + 1];
6464 float_decimal.digits[0] = '1';
6565 return;
std/fmt/index.zig+246-212
......@@ -16,27 +16,12 @@ pub fn format(context: var, comptime Errors: type, output: fn (@typeOf(context),
1616 Start,
1717 OpenBrace,
1818 CloseBrace,
19 Integer,
20 IntegerWidth,
21 Float,
22 FloatWidth,
23 FloatScientific,
24 FloatScientificWidth,
25 Character,
26 Buf,
27 BufWidth,
28 Bytes,
29 BytesBase,
30 BytesWidth,
19 FormatString,
3120 };
3221
3322 comptime var start_index = 0;
3423 comptime var state = State.Start;
3524 comptime var next_arg = 0;
36 comptime var radix = 0;
37 comptime var uppercase = false;
38 comptime var width = 0;
39 comptime var width_start = 0;
4025
4126 inline for (fmt) |c, i| {
4227 switch (state) {
......@@ -45,8 +30,10 @@ pub fn format(context: var, comptime Errors: type, output: fn (@typeOf(context),
4530 if (start_index < i) {
4631 try output(context, fmt[start_index..i]);
4732 }
33 start_index = i;
4834 state = State.OpenBrace;
4935 },
36
5037 '}' => {
5138 if (start_index < i) {
5239 try output(context, fmt[start_index..i]);
......@@ -61,57 +48,14 @@ pub fn format(context: var, comptime Errors: type, output: fn (@typeOf(context),
6148 start_index = i;
6249 },
6350 '}' => {
64 try formatValue(args[next_arg], context, Errors, output);
51 try formatType(args[next_arg], fmt[0..0], context, Errors, output);
6552 next_arg += 1;
6653 state = State.Start;
6754 start_index = i + 1;
6855 },
69 'd' => {
70 radix = 10;
71 uppercase = false;
72 width = 0;
73 state = State.Integer;
74 },
75 'x' => {
76 radix = 16;
77 uppercase = false;
78 width = 0;
79 state = State.Integer;
80 },
81 'X' => {
82 radix = 16;
83 uppercase = true;
84 width = 0;
85 state = State.Integer;
86 },
87 'c' => {
88 state = State.Character;
89 },
90 's' => {
91 state = State.Buf;
92 },
93 'e' => {
94 state = State.FloatScientific;
95 },
96 '.' => {
97 state = State.Float;
98 },
99 'B' => {
100 width = 0;
101 radix = 1000;
102 state = State.Bytes;
103 },
104 else => @compileError("Unknown format character: " ++ []u8{c}),
105 },
106 State.Buf => switch (c) {
107 '}' => {
108 return output(context, args[next_arg]);
109 },
110 '0'...'9' => {
111 width_start = i;
112 state = State.BufWidth;
56 else => {
57 state = State.FormatString;
11358 },
114 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
11559 },
11660 State.CloseBrace => switch (c) {
11761 '}' => {
......@@ -120,138 +64,15 @@ pub fn format(context: var, comptime Errors: type, output: fn (@typeOf(context),
12064 },
12165 else => @compileError("Single '}' encountered in format string"),
12266 },
123 State.Integer => switch (c) {
124 '}' => {
125 try formatInt(args[next_arg], radix, uppercase, width, context, Errors, output);
126 next_arg += 1;
127 state = State.Start;
128 start_index = i + 1;
129 },
130 '0'...'9' => {
131 width_start = i;
132 state = State.IntegerWidth;
133 },
134 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
135 },
136 State.IntegerWidth => switch (c) {
67 State.FormatString => switch (c) {
13768 '}' => {
138 width = comptime (parseUnsigned(usize, fmt[width_start..i], 10) catch unreachable);
139 try formatInt(args[next_arg], radix, uppercase, width, context, Errors, output);
69 const s = start_index + 1;
70 try formatType(args[next_arg], fmt[s..i], context, Errors, output);
14071 next_arg += 1;
14172 state = State.Start;
14273 start_index = i + 1;
14374 },
144 '0'...'9' => {},
145 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
146 },
147 State.FloatScientific => switch (c) {
148 '}' => {
149 try formatFloatScientific(args[next_arg], null, context, Errors, output);
150 next_arg += 1;
151 state = State.Start;
152 start_index = i + 1;
153 },
154 '0'...'9' => {
155 width_start = i;
156 state = State.FloatScientificWidth;
157 },
158 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
159 },
160 State.FloatScientificWidth => switch (c) {
161 '}' => {
162 width = comptime (parseUnsigned(usize, fmt[width_start..i], 10) catch unreachable);
163 try formatFloatScientific(args[next_arg], width, context, Errors, output);
164 next_arg += 1;
165 state = State.Start;
166 start_index = i + 1;
167 },
168 '0'...'9' => {},
169 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
170 },
171 State.Float => switch (c) {
172 '}' => {
173 try formatFloatDecimal(args[next_arg], null, context, Errors, output);
174 next_arg += 1;
175 state = State.Start;
176 start_index = i + 1;
177 },
178 '0'...'9' => {
179 width_start = i;
180 state = State.FloatWidth;
181 },
182 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
183 },
184 State.FloatWidth => switch (c) {
185 '}' => {
186 width = comptime (parseUnsigned(usize, fmt[width_start..i], 10) catch unreachable);
187 try formatFloatDecimal(args[next_arg], width, context, Errors, output);
188 next_arg += 1;
189 state = State.Start;
190 start_index = i + 1;
191 },
192 '0'...'9' => {},
193 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
194 },
195 State.BufWidth => switch (c) {
196 '}' => {
197 width = comptime (parseUnsigned(usize, fmt[width_start..i], 10) catch unreachable);
198 try formatBuf(args[next_arg], width, context, Errors, output);
199 next_arg += 1;
200 state = State.Start;
201 start_index = i + 1;
202 },
203 '0'...'9' => {},
204 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
205 },
206 State.Character => switch (c) {
207 '}' => {
208 try formatAsciiChar(args[next_arg], context, Errors, output);
209 next_arg += 1;
210 state = State.Start;
211 start_index = i + 1;
212 },
213 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
214 },
215 State.Bytes => switch (c) {
216 '}' => {
217 try formatBytes(args[next_arg], 0, radix, context, Errors, output);
218 next_arg += 1;
219 state = State.Start;
220 start_index = i + 1;
221 },
222 'i' => {
223 radix = 1024;
224 state = State.BytesBase;
225 },
226 '0'...'9' => {
227 width_start = i;
228 state = State.BytesWidth;
229 },
230 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
231 },
232 State.BytesBase => switch (c) {
233 '}' => {
234 try formatBytes(args[next_arg], 0, radix, context, Errors, output);
235 next_arg += 1;
236 state = State.Start;
237 start_index = i + 1;
238 },
239 '0'...'9' => {
240 width_start = i;
241 state = State.BytesWidth;
242 },
243 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
244 },
245 State.BytesWidth => switch (c) {
246 '}' => {
247 width = comptime (parseUnsigned(usize, fmt[width_start..i], 10) catch unreachable);
248 try formatBytes(args[next_arg], width, radix, context, Errors, output);
249 next_arg += 1;
250 state = State.Start;
251 start_index = i + 1;
252 },
253 '0'...'9' => {},
254 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
75 else => {},
25576 },
25677 }
25778 }
......@@ -268,14 +89,17 @@ pub fn format(context: var, comptime Errors: type, output: fn (@typeOf(context),
26889 }
26990}
27091
271pub fn formatValue(value: var, context: var, comptime Errors: type, output: fn (@typeOf(context), []const u8) Errors!void) Errors!void {
92pub fn formatType(
93 value: var,
94 comptime fmt: []const u8,
95 context: var,
96 comptime Errors: type,
97 output: fn (@typeOf(context), []const u8) Errors!void,
98) Errors!void {
27299 const T = @typeOf(value);
273100 switch (@typeId(T)) {
274 builtin.TypeId.Int => {
275 return formatInt(value, 10, false, 0, context, Errors, output);
276 },
277 builtin.TypeId.Float => {
278 return formatFloatScientific(value, null, context, Errors, output);
101 builtin.TypeId.Int, builtin.TypeId.Float => {
102 return formatValue(value, fmt, context, Errors, output);
279103 },
280104 builtin.TypeId.Void => {
281105 return output(context, "void");
......@@ -285,16 +109,16 @@ pub fn formatValue(value: var, context: var, comptime Errors: type, output: fn (
285109 },
286110 builtin.TypeId.Nullable => {
287111 if (value) |payload| {
288 return formatValue(payload, context, Errors, output);
112 return formatType(payload, fmt, context, Errors, output);
289113 } else {
290114 return output(context, "null");
291115 }
292116 },
293117 builtin.TypeId.ErrorUnion => {
294118 if (value) |payload| {
295 return formatValue(payload, context, Errors, output);
119 return formatType(payload, fmt, context, Errors, output);
296120 } else |err| {
297 return formatValue(err, context, Errors, output);
121 return formatType(err, fmt, context, Errors, output);
298122 }
299123 },
300124 builtin.TypeId.ErrorSet => {
......@@ -302,10 +126,34 @@ pub fn formatValue(value: var, context: var, comptime Errors: type, output: fn (
302126 return output(context, @errorName(value));
303127 },
304128 builtin.TypeId.Pointer => {
305 if (@typeId(T.Child) == builtin.TypeId.Array and T.Child.Child == u8) {
306 return output(context, (value.*)[0..]);
307 } else {
308 return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value));
129 switch (@typeId(T.Child)) {
130 builtin.TypeId.Array => {
131 if (T.Child.Child == u8) {
132 return formatText(value, fmt, context, Errors, output);
133 }
134 },
135 builtin.TypeId.Enum, builtin.TypeId.Union, builtin.TypeId.Struct => {
136 const has_cust_fmt = comptime cf: {
137 const info = @typeInfo(T.Child);
138 const defs = switch (info) {
139 builtin.TypeId.Struct => |s| s.defs,
140 builtin.TypeId.Union => |u| u.defs,
141 builtin.TypeId.Enum => |e| e.defs,
142 else => unreachable,
143 };
144
145 for (defs) |def| {
146 if (mem.eql(u8, def.name, "format")) {
147 break :cf true;
148 }
149 }
150 break :cf false;
151 };
152
153 if (has_cust_fmt) return value.format(fmt, context, Errors, output);
154 return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value));
155 },
156 else => return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value)),
309157 }
310158 },
311159 else => if (@canImplicitCast([]const u8, value)) {
......@@ -317,11 +165,129 @@ pub fn formatValue(value: var, context: var, comptime Errors: type, output: fn (
317165 }
318166}
319167
320pub fn formatAsciiChar(c: u8, context: var, comptime Errors: type, output: fn (@typeOf(context), []const u8) Errors!void) Errors!void {
321 return output(context, (&c)[0..1]);
168fn formatValue(
169 value: var,
170 comptime fmt: []const u8,
171 context: var,
172 comptime Errors: type,
173 output: fn (@typeOf(context), []const u8) Errors!void,
174) Errors!void {
175 if (fmt.len > 0) {
176 if (fmt[0] == 'B') {
177 comptime var width: ?usize = null;
178 if (fmt.len > 1) {
179 if (fmt[1] == 'i') {
180 if (fmt.len > 2) width = comptime (parseUnsigned(usize, fmt[2..], 10) catch unreachable);
181 return formatBytes(value, width, 1024, context, Errors, output);
182 }
183 width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
184 }
185 return formatBytes(value, width, 1000, context, Errors, output);
186 }
187 }
188
189 comptime var T = @typeOf(value);
190 switch (@typeId(T)) {
191 builtin.TypeId.Float => return formatFloatValue(value, fmt, context, Errors, output),
192 builtin.TypeId.Int => return formatIntValue(value, fmt, context, Errors, output),
193 else => unreachable,
194 }
195}
196
197pub fn formatIntValue(
198 value: var,
199 comptime fmt: []const u8,
200 context: var,
201 comptime Errors: type,
202 output: fn (@typeOf(context), []const u8) Errors!void,
203) Errors!void {
204 comptime var radix = 10;
205 comptime var uppercase = false;
206 comptime var width = 0;
207 if (fmt.len > 0) {
208 switch (fmt[0]) {
209 'c' => {
210 if (@typeOf(value) == u8) {
211 if (fmt.len > 1) @compileError("Unknown format character: " ++ []u8{fmt[1]});
212 return formatAsciiChar(value, context, Errors, output);
213 }
214 },
215 'd' => {
216 radix = 10;
217 uppercase = false;
218 width = 0;
219 },
220 'x' => {
221 radix = 16;
222 uppercase = false;
223 width = 0;
224 },
225 'X' => {
226 radix = 16;
227 uppercase = true;
228 width = 0;
229 },
230 else => @compileError("Unknown format character: " ++ []u8{fmt[0]}),
231 }
232 if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
233 }
234 return formatInt(value, radix, uppercase, width, context, Errors, output);
235}
236
237fn formatFloatValue(
238 value: var,
239 comptime fmt: []const u8,
240 context: var,
241 comptime Errors: type,
242 output: fn (@typeOf(context), []const u8) Errors!void,
243) Errors!void {
244 comptime var width: ?usize = null;
245 comptime var float_fmt = 'e';
246 if (fmt.len > 0) {
247 float_fmt = fmt[0];
248 if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
249 }
250
251 switch (float_fmt) {
252 'e' => try formatFloatScientific(value, width, context, Errors, output),
253 '.' => try formatFloatDecimal(value, width, context, Errors, output),
254 else => @compileError("Unknown format character: " ++ []u8{float_fmt}),
255 }
256}
257
258pub fn formatText(
259 bytes: []const u8,
260 comptime fmt: []const u8,
261 context: var,
262 comptime Errors: type,
263 output: fn (@typeOf(context), []const u8) Errors!void,
264) Errors!void {
265 if (fmt.len > 0) {
266 if (fmt[0] == 's') {
267 comptime var width = 0;
268 if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
269 return formatBuf(bytes, width, context, Errors, output);
270 } else @compileError("Unknown format character: " ++ []u8{fmt[0]});
271 }
272 return output(context, bytes);
273}
274
275pub fn formatAsciiChar(
276 c: u8,
277 context: var,
278 comptime Errors: type,
279 output: fn (@typeOf(context), []const u8) Errors!void,
280) Errors!void {
281 return output(context, (*[1]u8)(&c)[0..]);
322282}
323283
324pub fn formatBuf(buf: []const u8, width: usize, context: var, comptime Errors: type, output: fn (@typeOf(context), []const u8) Errors!void) Errors!void {
284pub fn formatBuf(
285 buf: []const u8,
286 width: usize,
287 context: var,
288 comptime Errors: type,
289 output: fn (@typeOf(context), []const u8) Errors!void,
290) Errors!void {
325291 try output(context, buf);
326292
327293 var leftover_padding = if (width > buf.len) (width - buf.len) else return;
......@@ -334,7 +300,13 @@ pub fn formatBuf(buf: []const u8, width: usize, context: var, comptime Errors: t
334300// Print a float in scientific notation to the specified precision. Null uses full precision.
335301// It should be the case that every full precision, printed value can be re-parsed back to the
336302// same type unambiguously.
337pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var, comptime Errors: type, output: fn (@typeOf(context), []const u8) Errors!void) Errors!void {
303pub fn formatFloatScientific(
304 value: var,
305 maybe_precision: ?usize,
306 context: var,
307 comptime Errors: type,
308 output: fn (@typeOf(context), []const u8) Errors!void,
309) Errors!void {
338310 var x = f64(value);
339311
340312 // Errol doesn't handle these special cases.
......@@ -423,7 +395,13 @@ pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var,
423395
424396// Print a float of the format x.yyyyy where the number of y is specified by the precision argument.
425397// By default floats are printed at full precision (no rounding).
426pub fn formatFloatDecimal(value: var, maybe_precision: ?usize, context: var, comptime Errors: type, output: fn (@typeOf(context), []const u8) Errors!void) Errors!void {
398pub fn formatFloatDecimal(
399 value: var,
400 maybe_precision: ?usize,
401 context: var,
402 comptime Errors: type,
403 output: fn (@typeOf(context), []const u8) Errors!void,
404) Errors!void {
427405 var x = f64(value);
428406
429407 // Errol doesn't handle these special cases.
......@@ -613,11 +591,19 @@ pub fn formatInt(
613591 }
614592}
615593
616fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, context: var, comptime Errors: type, output: fn (@typeOf(context), []const u8) Errors!void) Errors!void {
594fn formatIntSigned(
595 value: var,
596 base: u8,
597 uppercase: bool,
598 width: usize,
599 context: var,
600 comptime Errors: type,
601 output: fn (@typeOf(context), []const u8) Errors!void,
602) Errors!void {
617603 const uint = @IntType(false, @typeOf(value).bit_count);
618604 if (value < 0) {
619605 const minus_sign: u8 = '-';
620 try output(context, (&minus_sign)[0..1]);
606 try output(context, (*[1]u8)(&minus_sign)[0..]);
621607 const new_value = uint(-(value + 1)) + 1;
622608 const new_width = if (width == 0) 0 else (width - 1);
623609 return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
......@@ -625,14 +611,22 @@ fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, context:
625611 return formatIntUnsigned(uint(value), base, uppercase, width, context, Errors, output);
626612 } else {
627613 const plus_sign: u8 = '+';
628 try output(context, (&plus_sign)[0..1]);
614 try output(context, (*[1]u8)(&plus_sign)[0..]);
629615 const new_value = uint(value);
630616 const new_width = if (width == 0) 0 else (width - 1);
631617 return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
632618 }
633619}
634620
635fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize, context: var, comptime Errors: type, output: fn (@typeOf(context), []const u8) Errors!void) Errors!void {
621fn formatIntUnsigned(
622 value: var,
623 base: u8,
624 uppercase: bool,
625 width: usize,
626 context: var,
627 comptime Errors: type,
628 output: fn (@typeOf(context), []const u8) Errors!void,
629) Errors!void {
636630 // max_int_digits accounts for the minus sign. when printing an unsigned
637631 // number we don't need to do that.
638632 var buf: [max_int_digits - 1]u8 = undefined;
......@@ -654,7 +648,7 @@ fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize, contex
654648 const zero_byte: u8 = '0';
655649 var leftover_padding = padding - index;
656650 while (true) {
657 try output(context, (&zero_byte)[0..1]);
651 try output(context, (*[1]u8)(&zero_byte)[0..]);
658652 leftover_padding -= 1;
659653 if (leftover_padding == 0) break;
660654 }
......@@ -831,6 +825,10 @@ test "fmt.format" {
831825 const value: u3 = 0b101;
832826 try testFmt("u3: 5\n", "u3: {}\n", value);
833827 }
828 {
829 const value: u8 = 'a';
830 try testFmt("u8: a\n", "u8: {c}\n", value);
831 }
834832 try testFmt("file size: 63MiB\n", "file size: {Bi}\n", usize(63 * 1024 * 1024));
835833 try testFmt("file size: 66.06MB\n", "file size: {B2}\n", usize(63 * 1024 * 1024));
836834 {
......@@ -1048,6 +1046,42 @@ test "fmt.format" {
10481046 const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
10491047 assert(mem.eql(u8, result, "f64: 18014400656965630.00000\n"));
10501048 }
1049 //custom type format
1050 {
1051 const Vec2 = struct {
1052 const SelfType = this;
1053 x: f32,
1054 y: f32,
1055
1056 pub fn format(
1057 self: *SelfType,
1058 comptime fmt: []const u8,
1059 context: var,
1060 comptime Errors: type,
1061 output: fn (@typeOf(context), []const u8) Errors!void,
1062 ) Errors!void {
1063 if (fmt.len > 0) {
1064 if (fmt.len > 1) unreachable;
1065 switch (fmt[0]) {
1066 //point format
1067 'p' => return std.fmt.format(context, Errors, output, "({.3},{.3})", self.x, self.y),
1068 //dimension format
1069 'd' => return std.fmt.format(context, Errors, output, "{.3}x{.3}", self.x, self.y),
1070 else => unreachable,
1071 }
1072 }
1073 return std.fmt.format(context, Errors, output, "({.3},{.3})", self.x, self.y);
1074 }
1075 };
1076
1077 var buf1: [32]u8 = undefined;
1078 var value = Vec2{
1079 .x = 10.2,
1080 .y = 2.22,
1081 };
1082 try testFmt("point: (10.200,2.220)\n", "point: {}\n", &value);
1083 try testFmt("dim: 10.200x2.220\n", "dim: {d}\n", &value);
1084 }
10511085}
10521086
10531087fn testFmt(expected: []const u8, comptime template: []const u8, args: ...) !void {
std/heap.zig+1-1
......@@ -24,7 +24,7 @@ fn cAlloc(self: *Allocator, n: usize, alignment: u29) ![]u8 {
2424fn cRealloc(self: *Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
2525 const old_ptr = @ptrCast([*]c_void, old_mem.ptr);
2626 if (c.realloc(old_ptr, new_size)) |buf| {
27 return @ptrCast(*u8, buf)[0..new_size];
27 return @ptrCast([*]u8, buf)[0..new_size];
2828 } else if (new_size <= old_mem.len) {
2929 return old_mem[0..new_size];
3030 } else {
std/io.zig+2-2
......@@ -219,12 +219,12 @@ pub fn OutStream(comptime WriteError: type) type {
219219 }
220220
221221 pub fn writeByte(self: *Self, byte: u8) !void {
222 const slice = (&byte)[0..1];
222 const slice = (*[1]u8)(&byte)[0..];
223223 return self.writeFn(self, slice);
224224 }
225225
226226 pub fn writeByteNTimes(self: *Self, byte: u8, n: usize) !void {
227 const slice = (&byte)[0..1];
227 const slice = (*[1]u8)(&byte)[0..];
228228 var i: usize = 0;
229229 while (i < n) : (i += 1) {
230230 try self.writeFn(self, slice);
std/macho.zig+1-1
......@@ -164,7 +164,7 @@ fn readNoEof(in: *io.FileInStream, comptime T: type, result: []T) !void {
164164 return in.stream.readNoEof(([]u8)(result));
165165}
166166fn readOneNoEof(in: *io.FileInStream, comptime T: type, result: *T) !void {
167 return readNoEof(in, T, result[0..1]);
167 return readNoEof(in, T, (*[1]T)(result)[0..]);
168168}
169169
170170fn isSymbol(sym: *const Nlist64) bool {
std/math/ln.zig+2-2
......@@ -14,7 +14,7 @@ const TypeId = builtin.TypeId;
1414pub fn ln(x: var) @typeOf(x) {
1515 const T = @typeOf(x);
1616 switch (@typeId(T)) {
17 TypeId.FloatLiteral => {
17 TypeId.ComptimeFloat => {
1818 return @typeOf(1.0)(ln_64(x));
1919 },
2020 TypeId.Float => {
......@@ -24,7 +24,7 @@ pub fn ln(x: var) @typeOf(x) {
2424 else => @compileError("ln not implemented for " ++ @typeName(T)),
2525 };
2626 },
27 TypeId.IntLiteral => {
27 TypeId.ComptimeInt => {
2828 return @typeOf(1)(math.floor(ln_64(f64(x))));
2929 },
3030 TypeId.Int => {
std/math/log.zig+3-3
......@@ -9,15 +9,15 @@ pub fn log(comptime T: type, base: T, x: T) T {
99 return math.log2(x);
1010 } else if (base == 10) {
1111 return math.log10(x);
12 } else if ((@typeId(T) == TypeId.Float or @typeId(T) == TypeId.FloatLiteral) and base == math.e) {
12 } else if ((@typeId(T) == TypeId.Float or @typeId(T) == TypeId.ComptimeFloat) and base == math.e) {
1313 return math.ln(x);
1414 }
1515
1616 switch (@typeId(T)) {
17 TypeId.FloatLiteral => {
17 TypeId.ComptimeFloat => {
1818 return @typeOf(1.0)(math.ln(f64(x)) / math.ln(f64(base)));
1919 },
20 TypeId.IntLiteral => {
20 TypeId.ComptimeInt => {
2121 return @typeOf(1)(math.floor(math.ln(f64(x)) / math.ln(f64(base))));
2222 },
2323 builtin.TypeId.Int => {
std/math/log10.zig+2-2
......@@ -14,7 +14,7 @@ const TypeId = builtin.TypeId;
1414pub fn log10(x: var) @typeOf(x) {
1515 const T = @typeOf(x);
1616 switch (@typeId(T)) {
17 TypeId.FloatLiteral => {
17 TypeId.ComptimeFloat => {
1818 return @typeOf(1.0)(log10_64(x));
1919 },
2020 TypeId.Float => {
......@@ -24,7 +24,7 @@ pub fn log10(x: var) @typeOf(x) {
2424 else => @compileError("log10 not implemented for " ++ @typeName(T)),
2525 };
2626 },
27 TypeId.IntLiteral => {
27 TypeId.ComptimeInt => {
2828 return @typeOf(1)(math.floor(log10_64(f64(x))));
2929 },
3030 TypeId.Int => {
std/math/log2.zig+2-2
......@@ -14,7 +14,7 @@ const TypeId = builtin.TypeId;
1414pub fn log2(x: var) @typeOf(x) {
1515 const T = @typeOf(x);
1616 switch (@typeId(T)) {
17 TypeId.FloatLiteral => {
17 TypeId.ComptimeFloat => {
1818 return @typeOf(1.0)(log2_64(x));
1919 },
2020 TypeId.Float => {
......@@ -24,7 +24,7 @@ pub fn log2(x: var) @typeOf(x) {
2424 else => @compileError("log2 not implemented for " ++ @typeName(T)),
2525 };
2626 },
27 TypeId.IntLiteral => comptime {
27 TypeId.ComptimeInt => comptime {
2828 var result = 0;
2929 var x_shifted = x;
3030 while (b: {
std/math/sqrt.zig+2-2
......@@ -14,9 +14,9 @@ const TypeId = builtin.TypeId;
1414pub fn sqrt(x: var) (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @typeOf(x).bit_count / 2) else @typeOf(x)) {
1515 const T = @typeOf(x);
1616 switch (@typeId(T)) {
17 TypeId.FloatLiteral => return T(@sqrt(f64, x)), // TODO upgrade to f128
17 TypeId.ComptimeFloat => return T(@sqrt(f64, x)), // TODO upgrade to f128
1818 TypeId.Float => return @sqrt(T, x),
19 TypeId.IntLiteral => comptime {
19 TypeId.ComptimeInt => comptime {
2020 if (x > @maxValue(u128)) {
2121 @compileError("sqrt not implemented for comptime_int greater than 128 bits");
2222 }
std/mem.zig+19-15
......@@ -31,14 +31,16 @@ pub const Allocator = struct {
3131 /// Guaranteed: `old_mem.len` is the same as what was returned from `allocFn` or `reallocFn`
3232 freeFn: fn (self: *Allocator, old_mem: []u8) void,
3333
34 fn create(self: *Allocator, comptime T: type) !*T {
34 /// Call destroy with the result
35 pub fn create(self: *Allocator, comptime T: type) !*T {
3536 if (@sizeOf(T) == 0) return *{};
3637 const slice = try self.alloc(T, 1);
3738 return &slice[0];
3839 }
3940
40 // TODO once #733 is solved, this will replace create
41 fn construct(self: *Allocator, init: var) t: {
41 /// Call destroy with the result
42 /// TODO once #733 is solved, this will replace create
43 pub fn construct(self: *Allocator, init: var) t: {
4244 // TODO this is a workaround for type getting parsed as Error!&const T
4345 const T = @typeOf(init).Child;
4446 break :t Error!*T;
......@@ -51,17 +53,19 @@ pub const Allocator = struct {
5153 return ptr;
5254 }
5355
54 fn destroy(self: *Allocator, ptr: var) void {
55 self.free(ptr[0..1]);
56 /// `ptr` should be the return value of `construct` or `create`
57 pub fn destroy(self: *Allocator, ptr: var) void {
58 const non_const_ptr = @intToPtr([*]u8, @ptrToInt(ptr));
59 self.freeFn(self, non_const_ptr[0..@sizeOf(@typeOf(ptr).Child)]);
5660 }
5761
58 fn alloc(self: *Allocator, comptime T: type, n: usize) ![]T {
62 pub fn alloc(self: *Allocator, comptime T: type, n: usize) ![]T {
5963 return self.alignedAlloc(T, @alignOf(T), n);
6064 }
6165
62 fn alignedAlloc(self: *Allocator, comptime T: type, comptime alignment: u29, n: usize) ![]align(alignment) T {
66 pub fn alignedAlloc(self: *Allocator, comptime T: type, comptime alignment: u29, n: usize) ![]align(alignment) T {
6367 if (n == 0) {
64 return (*align(alignment) T)(undefined)[0..0];
68 return ([*]align(alignment) T)(undefined)[0..0];
6569 }
6670 const byte_count = math.mul(usize, @sizeOf(T), n) catch return Error.OutOfMemory;
6771 const byte_slice = try self.allocFn(self, byte_count, alignment);
......@@ -73,17 +77,17 @@ pub const Allocator = struct {
7377 return ([]align(alignment) T)(@alignCast(alignment, byte_slice));
7478 }
7579
76 fn realloc(self: *Allocator, comptime T: type, old_mem: []T, n: usize) ![]T {
80 pub fn realloc(self: *Allocator, comptime T: type, old_mem: []T, n: usize) ![]T {
7781 return self.alignedRealloc(T, @alignOf(T), @alignCast(@alignOf(T), old_mem), n);
7882 }
7983
80 fn alignedRealloc(self: *Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) ![]align(alignment) T {
84 pub fn alignedRealloc(self: *Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) ![]align(alignment) T {
8185 if (old_mem.len == 0) {
8286 return self.alloc(T, n);
8387 }
8488 if (n == 0) {
8589 self.free(old_mem);
86 return (*align(alignment) T)(undefined)[0..0];
90 return ([*]align(alignment) T)(undefined)[0..0];
8791 }
8892
8993 const old_byte_slice = ([]u8)(old_mem);
......@@ -102,11 +106,11 @@ pub const Allocator = struct {
102106 /// Reallocate, but `n` must be less than or equal to `old_mem.len`.
103107 /// Unlike `realloc`, this function cannot fail.
104108 /// Shrinking to 0 is the same as calling `free`.
105 fn shrink(self: *Allocator, comptime T: type, old_mem: []T, n: usize) []T {
109 pub fn shrink(self: *Allocator, comptime T: type, old_mem: []T, n: usize) []T {
106110 return self.alignedShrink(T, @alignOf(T), @alignCast(@alignOf(T), old_mem), n);
107111 }
108112
109 fn alignedShrink(self: *Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) []align(alignment) T {
113 pub fn alignedShrink(self: *Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) []align(alignment) T {
110114 if (n == 0) {
111115 self.free(old_mem);
112116 return old_mem[0..0];
......@@ -123,10 +127,10 @@ pub const Allocator = struct {
123127 return ([]align(alignment) T)(@alignCast(alignment, byte_slice));
124128 }
125129
126 fn free(self: *Allocator, memory: var) void {
130 pub fn free(self: *Allocator, memory: var) void {
127131 const bytes = ([]const u8)(memory);
128132 if (bytes.len == 0) return;
129 const non_const_ptr = @intToPtr(*u8, @ptrToInt(bytes.ptr));
133 const non_const_ptr = @intToPtr([*]u8, @ptrToInt(bytes.ptr));
130134 self.freeFn(self, non_const_ptr[0..bytes.len]);
131135 }
132136};
std/net.zig+1-1
......@@ -68,7 +68,7 @@ pub const Address = struct {
6868
6969pub fn parseIp4(buf: []const u8) !u32 {
7070 var result: u32 = undefined;
71 const out_ptr = ([]u8)((&result)[0..1]);
71 const out_ptr = ([]u8)((*[1]u32)(&result)[0..]);
7272
7373 var x: u8 = 0;
7474 var index: u8 = 0;
std/os/index.zig+2-2
......@@ -1240,7 +1240,7 @@ pub const Dir = struct {
12401240 const next_index = self.index + darwin_entry.d_reclen;
12411241 self.index = next_index;
12421242
1243 const name = (&darwin_entry.d_name)[0..darwin_entry.d_namlen];
1243 const name = @ptrCast([*]u8, &darwin_entry.d_name)[0..darwin_entry.d_namlen];
12441244
12451245 // skip . and .. entries
12461246 if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) {
......@@ -1704,7 +1704,7 @@ pub fn argsFree(allocator: *mem.Allocator, args_alloc: []const []u8) void {
17041704 for (args_alloc) |arg| {
17051705 total_bytes += @sizeOf([]u8) + arg.len;
17061706 }
1707 const unaligned_allocated_buf = @ptrCast(*const u8, args_alloc.ptr)[0..total_bytes];
1707 const unaligned_allocated_buf = @ptrCast([*]const u8, args_alloc.ptr)[0..total_bytes];
17081708 const aligned_allocated_buf = @alignCast(@alignOf([]u8), unaligned_allocated_buf);
17091709 return allocator.free(aligned_allocated_buf);
17101710}
std/os/time.zig+1-1
......@@ -266,7 +266,7 @@ test "os.time.timestamp" {
266266
267267test "os.time.Timer" {
268268 const ns_per_ms = (ns_per_s / ms_per_s);
269 const margin = ns_per_ms * 50;
269 const margin = ns_per_ms * 150;
270270
271271 var timer = try Timer.start();
272272 sleep(0, 10 * ns_per_ms);
std/special/compiler_rt/udivmoddi4_test.zig+2
......@@ -1,3 +1,5 @@
1// Disable formatting to avoid unnecessary source repository bloat.
2// zig fmt: off
13const __udivmoddi4 = @import("udivmoddi4.zig").__udivmoddi4;
24const assert = @import("std").debug.assert;
35
std/special/compiler_rt/udivmodti4_test.zig+2
......@@ -1,3 +1,5 @@
1// Disable formatting to avoid unnecessary source repository bloat.
2// zig fmt: off
13const __udivmodti4 = @import("udivmodti4.zig").__udivmodti4;
24const assert = @import("std").debug.assert;
35
std/zig/parser_test.zig+26
......@@ -1,3 +1,29 @@
1test "zig fmt: comment to disable/enable zig fmt first" {
2 try testCanonical(
3 \\// Test trailing comma syntax
4 \\// zig fmt: off
5 \\
6 \\const struct_trailing_comma = struct { x: i32, y: i32, };
7 );
8}
9
10test "zig fmt: comment to disable/enable zig fmt" {
11 try testTransform(
12 \\const a = b;
13 \\// zig fmt: off
14 \\const c = d;
15 \\// zig fmt: on
16 \\const e = f;
17 ,
18 \\const a = b;
19 \\// zig fmt: off
20 \\const c = d;
21 \\// zig fmt: on
22 \\const e = f;
23 \\
24 );
25}
26
127test "zig fmt: pointer of unknown length" {
228 try testCanonical(
329 \\fn foo(ptr: [*]u8) void {}
std/zig/render.zig+39-2
......@@ -82,8 +82,45 @@ fn renderRoot(
8282
8383 var start_col: usize = 0;
8484 var it = tree.root_node.decls.iterator(0);
85 while (it.next()) |decl| {
86 try renderTopLevelDecl(allocator, stream, tree, 0, &start_col, decl.*);
85 while (true) {
86 var decl = (it.next() ?? return).*;
87 // look for zig fmt: off comment
88 var start_token_index = decl.firstToken();
89 zig_fmt_loop: while (start_token_index != 0) {
90 start_token_index -= 1;
91 const start_token = tree.tokens.at(start_token_index);
92 switch (start_token.id) {
93 Token.Id.LineComment => {},
94 Token.Id.DocComment => continue,
95 else => break,
96 }
97 if (mem.eql(u8, mem.trim(u8, tree.tokenSlicePtr(start_token)[2..], " "), "zig fmt: off")) {
98 var end_token_index = start_token_index;
99 while (true) {
100 end_token_index += 1;
101 const end_token = tree.tokens.at(end_token_index);
102 switch (end_token.id) {
103 Token.Id.LineComment => {},
104 Token.Id.Eof => {
105 const start = tree.tokens.at(start_token_index + 1).start;
106 try stream.write(tree.source[start..]);
107 return;
108 },
109 else => continue,
110 }
111 if (mem.eql(u8, mem.trim(u8, tree.tokenSlicePtr(end_token)[2..], " "), "zig fmt: on")) {
112 const start = tree.tokens.at(start_token_index + 1).start;
113 try stream.print("{}\n", tree.source[start..end_token.end]);
114 while (tree.tokens.at(decl.firstToken()).start < end_token.end) {
115 decl = (it.next() ?? return).*;
116 }
117 break :zig_fmt_loop;
118 }
119 }
120 }
121 }
122
123 try renderTopLevelDecl(allocator, stream, tree, 0, &start_col, decl);
87124 if (it.peek()) |next_decl| {
88125 try renderExtraNewline(tree, stream, &start_col, next_decl.*);
89126 }
test/cases/align.zig+2-2
......@@ -6,7 +6,7 @@ var foo: u8 align(4) = 100;
66test "global variable alignment" {
77 assert(@typeOf(&foo).alignment == 4);
88 assert(@typeOf(&foo) == *align(4) u8);
9 const slice = (&foo)[0..1];
9 const slice = (*[1]u8)(&foo)[0..];
1010 assert(@typeOf(slice) == []align(4) u8);
1111}
1212
......@@ -60,7 +60,7 @@ fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 {
6060test "implicitly decreasing slice alignment" {
6161 const a: u32 align(4) = 3;
6262 const b: u32 align(8) = 4;
63 assert(addUnalignedSlice((&a)[0..1], (&b)[0..1]) == 7);
63 assert(addUnalignedSlice((*[1]u32)(&a)[0..], (*[1]u32)(&b)[0..]) == 7);
6464}
6565fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 {
6666 return a[0] + b[0];
test/cases/array.zig+29
......@@ -115,3 +115,32 @@ test "array len property" {
115115 var x: [5]i32 = undefined;
116116 assert(@typeOf(x).len == 5);
117117}
118
119test "single-item pointer to array indexing and slicing" {
120 testSingleItemPtrArrayIndexSlice();
121 comptime testSingleItemPtrArrayIndexSlice();
122}
123
124fn testSingleItemPtrArrayIndexSlice() void {
125 var array = "aaaa";
126 doSomeMangling(&array);
127 assert(mem.eql(u8, "azya", array));
128}
129
130fn doSomeMangling(array: *[4]u8) void {
131 array[1] = 'z';
132 array[2..3][0] = 'y';
133}
134
135test "implicit cast single-item pointer" {
136 testImplicitCastSingleItemPtr();
137 comptime testImplicitCastSingleItemPtr();
138}
139
140fn testImplicitCastSingleItemPtr() void {
141 var byte: u8 = 100;
142 const slice = (*[1]u8)(&byte)[0..];
143 slice[0] += 1;
144 assert(byte == 101);
145}
146
test/cases/eval.zig+3-3
......@@ -418,9 +418,9 @@ test "string literal used as comptime slice is memoized" {
418418}
419419
420420test "comptime slice of undefined pointer of length 0" {
421 const slice1 = (*i32)(undefined)[0..0];
421 const slice1 = ([*]i32)(undefined)[0..0];
422422 assert(slice1.len == 0);
423 const slice2 = (*i32)(undefined)[100..100];
423 const slice2 = ([*]i32)(undefined)[100..100];
424424 assert(slice2.len == 0);
425425}
426426
......@@ -508,7 +508,7 @@ test "comptime slice of slice preserves comptime var" {
508508test "comptime slice of pointer preserves comptime var" {
509509 comptime {
510510 var buff: [10]u8 = undefined;
511 var a = &buff[0];
511 var a = buff[0..].ptr;
512512 a[0..1][0] = 1;
513513 assert(buff[0..][0..][0] == 1);
514514 }
test/cases/math.zig+18-6
......@@ -329,14 +329,14 @@ fn testShrExact(x: u8) void {
329329 assert(shifted == 0b00101101);
330330}
331331
332test "big number addition" {
332test "comptime_int addition" {
333333 comptime {
334334 assert(35361831660712422535336160538497375248 + 101752735581729509668353361206450473702 == 137114567242441932203689521744947848950);
335335 assert(594491908217841670578297176641415611445982232488944558774612 + 390603545391089362063884922208143568023166603618446395589768 == 985095453608931032642182098849559179469148836107390954364380);
336336 }
337337}
338338
339test "big number multiplication" {
339test "comptime_int multiplication" {
340340 comptime {
341341 assert(
342342 45960427431263824329884196484953148229 * 128339149605334697009938835852565949723 == 5898522172026096622534201617172456926982464453350084962781392314016180490567,
......@@ -347,13 +347,13 @@ test "big number multiplication" {
347347 }
348348}
349349
350test "big number shifting" {
350test "comptime_int shifting" {
351351 comptime {
352352 assert((u128(1) << 127) == 0x80000000000000000000000000000000);
353353 }
354354}
355355
356test "big number multi-limb shift and mask" {
356test "comptime_int multi-limb shift and mask" {
357357 comptime {
358358 var a = 0xefffffffa0000001eeeeeeefaaaaaaab;
359359
......@@ -370,7 +370,7 @@ test "big number multi-limb shift and mask" {
370370 }
371371}
372372
373test "big number multi-limb partial shift right" {
373test "comptime_int multi-limb partial shift right" {
374374 comptime {
375375 var a = 0x1ffffffffeeeeeeee;
376376 a >>= 16;
......@@ -391,7 +391,7 @@ fn test_xor() void {
391391 assert(0xFF ^ 0xFF == 0x00);
392392}
393393
394test "big number xor" {
394test "comptime_int xor" {
395395 comptime {
396396 assert(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0x00000000000000000000000000000000 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
397397 assert(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0x0000000000000000FFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
......@@ -449,3 +449,15 @@ test "@sqrt" {
449449fn testSqrt(comptime T: type, x: T) void {
450450 assert(@sqrt(T, x * x) == x);
451451}
452
453test "comptime_int param and return" {
454 const a = comptimeAdd(35361831660712422535336160538497375248, 101752735581729509668353361206450473702);
455 assert(a == 137114567242441932203689521744947848950);
456
457 const b = comptimeAdd(594491908217841670578297176641415611445982232488944558774612, 390603545391089362063884922208143568023166603618446395589768);
458 assert(b == 985095453608931032642182098849559179469148836107390954364380);
459}
460
461fn comptimeAdd(comptime a: comptime_int, comptime b: comptime_int) comptime_int {
462 return a + b;
463}
test/cases/misc.zig+6-6
......@@ -274,7 +274,7 @@ test "generic malloc free" {
274274}
275275var some_mem: [100]u8 = undefined;
276276fn memAlloc(comptime T: type, n: usize) error![]T {
277 return @ptrCast(*T, &some_mem[0])[0..n];
277 return @ptrCast([*]T, &some_mem[0])[0..n];
278278}
279279fn memFree(comptime T: type, memory: []T) void {}
280280
......@@ -501,10 +501,10 @@ test "@typeId" {
501501 assert(@typeId(*f32) == Tid.Pointer);
502502 assert(@typeId([2]u8) == Tid.Array);
503503 assert(@typeId(AStruct) == Tid.Struct);
504 assert(@typeId(@typeOf(1)) == Tid.IntLiteral);
505 assert(@typeId(@typeOf(1.0)) == Tid.FloatLiteral);
506 assert(@typeId(@typeOf(undefined)) == Tid.UndefinedLiteral);
507 assert(@typeId(@typeOf(null)) == Tid.NullLiteral);
504 assert(@typeId(@typeOf(1)) == Tid.ComptimeInt);
505 assert(@typeId(@typeOf(1.0)) == Tid.ComptimeFloat);
506 assert(@typeId(@typeOf(undefined)) == Tid.Undefined);
507 assert(@typeId(@typeOf(null)) == Tid.Null);
508508 assert(@typeId(?i32) == Tid.Nullable);
509509 assert(@typeId(error!i32) == Tid.ErrorUnion);
510510 assert(@typeId(error) == Tid.ErrorSet);
......@@ -588,7 +588,7 @@ var global_ptr = &gdt[0];
588588
589589// can't really run this test but we can make sure it has no compile error
590590// and generates code
591const vram = @intToPtr(*volatile u8, 0x20000000)[0..0x8000];
591const vram = @intToPtr([*]volatile u8, 0x20000000)[0..0x8000];
592592export fn writeToVRam() void {
593593 vram[0] = 'X';
594594}
test/cases/slice.zig+1-1
......@@ -1,7 +1,7 @@
11const assert = @import("std").debug.assert;
22const mem = @import("std").mem;
33
4const x = @intToPtr(*i32, 0x1000)[0..0x500];
4const x = @intToPtr([*]i32, 0x1000)[0..0x500];
55const y = x[0x100..];
66test "compile time slice of pointer to hard coded address" {
77 assert(@ptrToInt(x.ptr) == 0x1000);
test/cases/syntax.zig+1
......@@ -1,4 +1,5 @@
11// Test trailing comma syntax
2// zig fmt: off
23
34const struct_trailing_comma = struct { x: i32, y: i32, };
45const struct_no_comma = struct { x: i32, y: i32 };
test/compile_errors.zig+22-11
......@@ -1,13 +1,22 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "slicing single-item pointer",
6 \\export fn entry(ptr: *i32) void {
7 \\ const slice = ptr[0..2];
8 \\}
9 ,
10 ".tmp_source.zig:2:22: error: slice of single-item pointer",
11 );
12
413 cases.add(
514 "indexing single-item pointer",
615 \\export fn entry(ptr: *i32) i32 {
716 \\ return ptr[1];
817 \\}
918 ,
10 ".tmp_source.zig:2:15: error: indexing not allowed on pointer to single item",
19 ".tmp_source.zig:2:15: error: index of single-item pointer",
1120 );
1221
1322 cases.add(
......@@ -144,10 +153,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
144153 cases.add(
145154 "comptime slice of undefined pointer non-zero len",
146155 \\export fn entry() void {
147 \\ const slice = (*i32)(undefined)[0..1];
156 \\ const slice = ([*]i32)(undefined)[0..1];
148157 \\}
149158 ,
150 ".tmp_source.zig:2:36: error: non-zero length slice of undefined pointer",
159 ".tmp_source.zig:2:38: error: non-zero length slice of undefined pointer",
151160 );
152161
153162 cases.add(
......@@ -1530,7 +1539,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
15301539 \\fn foo() *const i32 { return y; }
15311540 \\export fn entry() usize { return @sizeOf(@typeOf(foo)); }
15321541 ,
1533 ".tmp_source.zig:3:30: error: expected type '*const i32', found '*const (integer literal)'",
1542 ".tmp_source.zig:3:30: error: expected type '*const i32', found '*const comptime_int'",
15341543 );
15351544
15361545 cases.add(
......@@ -1546,7 +1555,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
15461555 \\const x = 2 == 2.0;
15471556 \\export fn entry() usize { return @sizeOf(@typeOf(x)); }
15481557 ,
1549 ".tmp_source.zig:1:11: error: integer value 2 cannot be implicitly casted to type '(float literal)'",
1558 ".tmp_source.zig:1:11: error: integer value 2 cannot be implicitly casted to type 'comptime_float'",
15501559 );
15511560
15521561 cases.add(
......@@ -2180,7 +2189,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
21802189 \\
21812190 \\export fn entry() usize { return @sizeOf(@typeOf(block_aligned_stuff)); }
21822191 ,
2183 ".tmp_source.zig:3:60: error: unable to perform binary not operation on type '(integer literal)'",
2192 ".tmp_source.zig:3:60: error: unable to perform binary not operation on type 'comptime_int'",
21842193 );
21852194
21862195 cases.addCase(x: {
......@@ -3129,14 +3138,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
31293138 \\export fn entry() void {
31303139 \\ var foo = Foo { .a = 1, .b = 10 };
31313140 \\ foo.b += 1;
3132 \\ bar((&foo.b)[0..1]);
3141 \\ bar((*[1]u32)(&foo.b)[0..]);
31333142 \\}
31343143 \\
31353144 \\fn bar(x: []u32) void {
31363145 \\ x[0] += 1;
31373146 \\}
31383147 ,
3139 ".tmp_source.zig:9:17: error: expected type '[]u32', found '[]align(1) u32'",
3148 ".tmp_source.zig:9:18: error: cast increases pointer alignment",
3149 ".tmp_source.zig:9:23: note: '*align(1) u32' has alignment 1",
3150 ".tmp_source.zig:9:18: note: '*[1]u32' has alignment 4",
31403151 );
31413152
31423153 cases.add(
......@@ -3258,10 +3269,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
32583269 \\ fn bar(self: *const Foo) void {}
32593270 \\};
32603271 ,
3261 ".tmp_source.zig:4:4: error: variable of type '*(integer literal)' must be const or comptime",
3272 ".tmp_source.zig:4:4: error: variable of type '*comptime_int' must be const or comptime",
32623273 ".tmp_source.zig:7:4: error: variable of type '(undefined)' must be const or comptime",
3263 ".tmp_source.zig:8:4: error: variable of type '(integer literal)' must be const or comptime",
3264 ".tmp_source.zig:9:4: error: variable of type '(float literal)' must be const or comptime",
3274 ".tmp_source.zig:8:4: error: variable of type 'comptime_int' must be const or comptime",
3275 ".tmp_source.zig:9:4: error: variable of type 'comptime_float' must be const or comptime",
32653276 ".tmp_source.zig:10:4: error: variable of type '(block)' must be const or comptime",
32663277 ".tmp_source.zig:11:4: error: variable of type '(null)' must be const or comptime",
32673278 ".tmp_source.zig:12:4: error: variable of type 'Opaque' must be const or comptime",