authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-26 19:52:35-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-26 19:52:35-04:00
logee64a22045ccbc39773779d4e386e25f563c8a90
tree95263984be9a72a1c9cc102b55e715a83a38b8eb
parent018a89c7a1b2763a50375f6d6d168dfa1f877f6a
signaturelock-open Commit is signed but in an unrecognized format.

add the `anyframe` and `anyframe->T` types


19 files changed, 337 insertions(+), 50 deletions(-)

BRANCH_TODO+3-3
......@@ -1,6 +1,4 @@
1 * reimplement @frameSize with Prefix Data
2 * reimplement with function splitting rather than switch
3 * add the `anyframe` type and `anyframe->T`
1 * make the anyframe type and anyframe->T type work with resume
42 * await
53 * await of a non async function
64 * await in single-threaded mode
......@@ -12,3 +10,5 @@
1210 * implicit cast of normal function to async function should be allowed when it is inferred to be async
1311 * go over the commented out tests
1412 * revive std.event.Loop
13 * reimplement with function splitting rather than switch
14 * @typeInfo for @Frame(func)
src/all_types.hpp+21
......@@ -479,6 +479,7 @@ enum NodeType {
479479 NodeTypeResume,
480480 NodeTypeAwaitExpr,
481481 NodeTypeSuspend,
482 NodeTypeAnyFrameType,
482483 NodeTypeEnumLiteral,
483484};
484485
......@@ -936,6 +937,10 @@ struct AstNodeSuspend {
936937 AstNode *block;
937938};
938939
940struct AstNodeAnyFrameType {
941 AstNode *payload_type; // can be NULL
942};
943
939944struct AstNodeEnumLiteral {
940945 Token *period;
941946 Token *identifier;
......@@ -1001,6 +1006,7 @@ struct AstNode {
10011006 AstNodeResumeExpr resume_expr;
10021007 AstNodeAwaitExpr await_expr;
10031008 AstNodeSuspend suspend;
1009 AstNodeAnyFrameType anyframe_type;
10041010 AstNodeEnumLiteral enum_literal;
10051011 } data;
10061012};
......@@ -1253,6 +1259,7 @@ enum ZigTypeId {
12531259 ZigTypeIdArgTuple,
12541260 ZigTypeIdOpaque,
12551261 ZigTypeIdCoroFrame,
1262 ZigTypeIdAnyFrame,
12561263 ZigTypeIdVector,
12571264 ZigTypeIdEnumLiteral,
12581265};
......@@ -1272,6 +1279,10 @@ struct ZigTypeCoroFrame {
12721279 ZigType *locals_struct;
12731280};
12741281
1282struct ZigTypeAnyFrame {
1283 ZigType *result_type; // null if `anyframe` instead of `anyframe->T`
1284};
1285
12751286struct ZigType {
12761287 ZigTypeId id;
12771288 Buf name;
......@@ -1298,11 +1309,13 @@ struct ZigType {
12981309 ZigTypeVector vector;
12991310 ZigTypeOpaque opaque;
13001311 ZigTypeCoroFrame frame;
1312 ZigTypeAnyFrame any_frame;
13011313 } data;
13021314
13031315 // use these fields to make sure we don't duplicate type table entries for the same type
13041316 ZigType *pointer_parent[2]; // [0 - mut, 1 - const]
13051317 ZigType *optional_parent;
1318 ZigType *any_frame_parent;
13061319 // If we generate a constant name value for this type, we memoize it here.
13071320 // The type of this is array
13081321 ConstExprValue *cached_const_name_val;
......@@ -1781,6 +1794,7 @@ struct CodeGen {
17811794 ZigType *entry_arg_tuple;
17821795 ZigType *entry_enum_literal;
17831796 ZigType *entry_frame_header;
1797 ZigType *entry_any_frame;
17841798 } builtin_types;
17851799 ZigType *align_amt_type;
17861800 ZigType *stack_trace_type;
......@@ -2208,6 +2222,7 @@ enum IrInstructionId {
22082222 IrInstructionIdSetRuntimeSafety,
22092223 IrInstructionIdSetFloatMode,
22102224 IrInstructionIdArrayType,
2225 IrInstructionIdAnyFrameType,
22112226 IrInstructionIdSliceType,
22122227 IrInstructionIdGlobalAsm,
22132228 IrInstructionIdAsm,
......@@ -2709,6 +2724,12 @@ struct IrInstructionPtrType {
27092724 bool is_allow_zero;
27102725};
27112726
2727struct IrInstructionAnyFrameType {
2728 IrInstruction base;
2729
2730 IrInstruction *payload_type;
2731};
2732
27122733struct IrInstructionSliceType {
27132734 IrInstruction base;
27142735
src/analyze.cpp+108-3
......@@ -256,6 +256,7 @@ AstNode *type_decl_node(ZigType *type_entry) {
256256 case ZigTypeIdBoundFn:
257257 case ZigTypeIdArgTuple:
258258 case ZigTypeIdVector:
259 case ZigTypeIdAnyFrame:
259260 return nullptr;
260261 }
261262 zig_unreachable();
......@@ -322,6 +323,7 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
322323 case ZigTypeIdBoundFn:
323324 case ZigTypeIdArgTuple:
324325 case ZigTypeIdVector:
326 case ZigTypeIdAnyFrame:
325327 return true;
326328 }
327329 zig_unreachable();
......@@ -354,6 +356,31 @@ ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x) {
354356 return get_int_type(g, false, bits_needed_for_unsigned(x));
355357}
356358
359ZigType *get_any_frame_type(CodeGen *g, ZigType *result_type) {
360 if (result_type != nullptr && result_type->any_frame_parent != nullptr) {
361 return result_type->any_frame_parent;
362 } else if (result_type == nullptr && g->builtin_types.entry_any_frame != nullptr) {
363 return g->builtin_types.entry_any_frame;
364 }
365
366 ZigType *entry = new_type_table_entry(ZigTypeIdAnyFrame);
367 entry->abi_size = g->builtin_types.entry_usize->abi_size;
368 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
369 entry->abi_align = g->builtin_types.entry_usize->abi_align;
370 entry->data.any_frame.result_type = result_type;
371 buf_init_from_str(&entry->name, "anyframe");
372 if (result_type != nullptr) {
373 buf_appendf(&entry->name, "->%s", buf_ptr(&result_type->name));
374 }
375
376 if (result_type != nullptr) {
377 result_type->any_frame_parent = entry;
378 } else if (result_type == nullptr) {
379 g->builtin_types.entry_any_frame = entry;
380 }
381 return entry;
382}
383
357384static const char *ptr_len_to_star_str(PtrLen ptr_len) {
358385 switch (ptr_len) {
359386 case PtrLenSingle:
......@@ -1080,6 +1107,7 @@ static Error emit_error_unless_type_allowed_in_packed_struct(CodeGen *g, ZigType
10801107 case ZigTypeIdArgTuple:
10811108 case ZigTypeIdOpaque:
10821109 case ZigTypeIdCoroFrame:
1110 case ZigTypeIdAnyFrame:
10831111 add_node_error(g, source_node,
10841112 buf_sprintf("type '%s' not allowed in packed struct; no guaranteed in-memory representation",
10851113 buf_ptr(&type_entry->name)));
......@@ -1169,6 +1197,7 @@ bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry) {
11691197 case ZigTypeIdArgTuple:
11701198 case ZigTypeIdVoid:
11711199 case ZigTypeIdCoroFrame:
1200 case ZigTypeIdAnyFrame:
11721201 return false;
11731202 case ZigTypeIdOpaque:
11741203 case ZigTypeIdUnreachable:
......@@ -1340,6 +1369,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
13401369 case ZigTypeIdFn:
13411370 case ZigTypeIdVector:
13421371 case ZigTypeIdCoroFrame:
1372 case ZigTypeIdAnyFrame:
13431373 switch (type_requires_comptime(g, type_entry)) {
13441374 case ReqCompTimeNo:
13451375 break;
......@@ -1436,6 +1466,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
14361466 case ZigTypeIdFn:
14371467 case ZigTypeIdVector:
14381468 case ZigTypeIdCoroFrame:
1469 case ZigTypeIdAnyFrame:
14391470 switch (type_requires_comptime(g, fn_type_id.return_type)) {
14401471 case ReqCompTimeInvalid:
14411472 return g->builtin_types.entry_invalid;
......@@ -2997,6 +3028,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
29973028 case NodeTypeAwaitExpr:
29983029 case NodeTypeSuspend:
29993030 case NodeTypeEnumLiteral:
3031 case NodeTypeAnyFrameType:
30003032 zig_unreachable();
30013033 }
30023034}
......@@ -3049,6 +3081,7 @@ ZigType *validate_var_type(CodeGen *g, AstNode *source_node, ZigType *type_entry
30493081 case ZigTypeIdBoundFn:
30503082 case ZigTypeIdVector:
30513083 case ZigTypeIdCoroFrame:
3084 case ZigTypeIdAnyFrame:
30523085 return type_entry;
30533086 }
30543087 zig_unreachable();
......@@ -3550,6 +3583,7 @@ bool is_container(ZigType *type_entry) {
35503583 case ZigTypeIdOpaque:
35513584 case ZigTypeIdVector:
35523585 case ZigTypeIdCoroFrame:
3586 case ZigTypeIdAnyFrame:
35533587 return false;
35543588 }
35553589 zig_unreachable();
......@@ -3607,6 +3641,7 @@ Error resolve_container_type(CodeGen *g, ZigType *type_entry) {
36073641 case ZigTypeIdOpaque:
36083642 case ZigTypeIdVector:
36093643 case ZigTypeIdCoroFrame:
3644 case ZigTypeIdAnyFrame:
36103645 zig_unreachable();
36113646 }
36123647 zig_unreachable();
......@@ -3615,11 +3650,13 @@ Error resolve_container_type(CodeGen *g, ZigType *type_entry) {
36153650ZigType *get_src_ptr_type(ZigType *type) {
36163651 if (type->id == ZigTypeIdPointer) return type;
36173652 if (type->id == ZigTypeIdFn) return type;
3653 if (type->id == ZigTypeIdAnyFrame) return type;
36183654 if (type->id == ZigTypeIdOptional) {
36193655 if (type->data.maybe.child_type->id == ZigTypeIdPointer) {
36203656 return type->data.maybe.child_type->data.pointer.allow_zero ? nullptr : type->data.maybe.child_type;
36213657 }
36223658 if (type->data.maybe.child_type->id == ZigTypeIdFn) return type->data.maybe.child_type;
3659 if (type->data.maybe.child_type->id == ZigTypeIdAnyFrame) return type->data.maybe.child_type;
36233660 }
36243661 return nullptr;
36253662}
......@@ -3635,6 +3672,13 @@ bool type_is_nonnull_ptr(ZigType *type) {
36353672 return get_codegen_ptr_type(type) == type && !ptr_allows_addr_zero(type);
36363673}
36373674
3675static uint32_t get_coro_frame_align_bytes(CodeGen *g) {
3676 uint32_t a = g->pointer_size_bytes * 2;
3677 // promises have at least alignment 8 so that we can have 3 extra bits when doing atomicrmw
3678 if (a < 8) a = 8;
3679 return a;
3680}
3681
36383682uint32_t get_ptr_align(CodeGen *g, ZigType *type) {
36393683 ZigType *ptr_type = get_src_ptr_type(type);
36403684 if (ptr_type->id == ZigTypeIdPointer) {
......@@ -3646,6 +3690,8 @@ uint32_t get_ptr_align(CodeGen *g, ZigType *type) {
36463690 // when getting the alignment of `?extern fn() void`.
36473691 // See http://lists.llvm.org/pipermail/llvm-dev/2018-September/126142.html
36483692 return (ptr_type->data.fn.fn_type_id.alignment == 0) ? 1 : ptr_type->data.fn.fn_type_id.alignment;
3693 } else if (ptr_type->id == ZigTypeIdAnyFrame) {
3694 return get_coro_frame_align_bytes(g);
36493695 } else {
36503696 zig_unreachable();
36513697 }
......@@ -3657,6 +3703,8 @@ bool get_ptr_const(ZigType *type) {
36573703 return ptr_type->data.pointer.is_const;
36583704 } else if (ptr_type->id == ZigTypeIdFn) {
36593705 return true;
3706 } else if (ptr_type->id == ZigTypeIdAnyFrame) {
3707 return true;
36603708 } else {
36613709 zig_unreachable();
36623710 }
......@@ -4153,6 +4201,7 @@ bool handle_is_ptr(ZigType *type_entry) {
41534201 case ZigTypeIdFn:
41544202 case ZigTypeIdEnum:
41554203 case ZigTypeIdVector:
4204 case ZigTypeIdAnyFrame:
41564205 return false;
41574206 case ZigTypeIdArray:
41584207 case ZigTypeIdStruct:
......@@ -4404,6 +4453,9 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
44044453 case ZigTypeIdCoroFrame:
44054454 // TODO better hashing algorithm
44064455 return 675741936;
4456 case ZigTypeIdAnyFrame:
4457 // TODO better hashing algorithm
4458 return 3747294894;
44074459 case ZigTypeIdBoundFn:
44084460 case ZigTypeIdInvalid:
44094461 case ZigTypeIdUnreachable:
......@@ -4469,6 +4521,7 @@ static bool can_mutate_comptime_var_state(ConstExprValue *value) {
44694521 case ZigTypeIdErrorSet:
44704522 case ZigTypeIdEnum:
44714523 case ZigTypeIdCoroFrame:
4524 case ZigTypeIdAnyFrame:
44724525 return false;
44734526
44744527 case ZigTypeIdPointer:
......@@ -4541,6 +4594,7 @@ static bool return_type_is_cacheable(ZigType *return_type) {
45414594 case ZigTypeIdPointer:
45424595 case ZigTypeIdVector:
45434596 case ZigTypeIdCoroFrame:
4597 case ZigTypeIdAnyFrame:
45444598 return true;
45454599
45464600 case ZigTypeIdArray:
......@@ -4673,6 +4727,7 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
46734727 case ZigTypeIdFloat:
46744728 case ZigTypeIdErrorUnion:
46754729 case ZigTypeIdCoroFrame:
4730 case ZigTypeIdAnyFrame:
46764731 return OnePossibleValueNo;
46774732 case ZigTypeIdUndefined:
46784733 case ZigTypeIdNull:
......@@ -4761,6 +4816,7 @@ ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry) {
47614816 case ZigTypeIdVoid:
47624817 case ZigTypeIdUnreachable:
47634818 case ZigTypeIdCoroFrame:
4819 case ZigTypeIdAnyFrame:
47644820 return ReqCompTimeNo;
47654821 }
47664822 zig_unreachable();
......@@ -5433,6 +5489,8 @@ bool const_values_equal(CodeGen *g, ConstExprValue *a, ConstExprValue *b) {
54335489 return true;
54345490 case ZigTypeIdCoroFrame:
54355491 zig_panic("TODO");
5492 case ZigTypeIdAnyFrame:
5493 zig_panic("TODO");
54365494 case ZigTypeIdUndefined:
54375495 zig_panic("TODO");
54385496 case ZigTypeIdNull:
......@@ -5786,7 +5844,11 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
57865844 return;
57875845 }
57885846 case ZigTypeIdCoroFrame:
5789 buf_appendf(buf, "(TODO: coroutine frame value)");
5847 buf_appendf(buf, "(TODO: async function frame value)");
5848 return;
5849
5850 case ZigTypeIdAnyFrame:
5851 buf_appendf(buf, "(TODO: anyframe value)");
57905852 return;
57915853
57925854 }
......@@ -5836,6 +5898,7 @@ uint32_t type_id_hash(TypeId x) {
58365898 case ZigTypeIdBoundFn:
58375899 case ZigTypeIdArgTuple:
58385900 case ZigTypeIdCoroFrame:
5901 case ZigTypeIdAnyFrame:
58395902 zig_unreachable();
58405903 case ZigTypeIdErrorUnion:
58415904 return hash_ptr(x.data.error_union.err_set_type) ^ hash_ptr(x.data.error_union.payload_type);
......@@ -5885,6 +5948,7 @@ bool type_id_eql(TypeId a, TypeId b) {
58855948 case ZigTypeIdArgTuple:
58865949 case ZigTypeIdOpaque:
58875950 case ZigTypeIdCoroFrame:
5951 case ZigTypeIdAnyFrame:
58885952 zig_unreachable();
58895953 case ZigTypeIdErrorUnion:
58905954 return a.data.error_union.err_set_type == b.data.error_union.err_set_type &&
......@@ -6051,6 +6115,7 @@ static const ZigTypeId all_type_ids[] = {
60516115 ZigTypeIdArgTuple,
60526116 ZigTypeIdOpaque,
60536117 ZigTypeIdCoroFrame,
6118 ZigTypeIdAnyFrame,
60546119 ZigTypeIdVector,
60556120 ZigTypeIdEnumLiteral,
60566121};
......@@ -6116,10 +6181,12 @@ size_t type_id_index(ZigType *entry) {
61166181 return 21;
61176182 case ZigTypeIdCoroFrame:
61186183 return 22;
6119 case ZigTypeIdVector:
6184 case ZigTypeIdAnyFrame:
61206185 return 23;
6121 case ZigTypeIdEnumLiteral:
6186 case ZigTypeIdVector:
61226187 return 24;
6188 case ZigTypeIdEnumLiteral:
6189 return 25;
61236190 }
61246191 zig_unreachable();
61256192}
......@@ -6178,6 +6245,8 @@ const char *type_id_name(ZigTypeId id) {
61786245 return "Vector";
61796246 case ZigTypeIdCoroFrame:
61806247 return "Frame";
6248 case ZigTypeIdAnyFrame:
6249 return "AnyFrame";
61816250 }
61826251 zig_unreachable();
61836252}
......@@ -7398,6 +7467,40 @@ static void resolve_llvm_types_coro_frame(CodeGen *g, ZigType *frame_type, Resol
73987467 frame_type->llvm_di_type = frame_type->data.frame.locals_struct->llvm_di_type;
73997468}
74007469
7470static void resolve_llvm_types_any_frame(CodeGen *g, ZigType *any_frame_type, ResolveStatus wanted_resolve_status) {
7471 if (any_frame_type->llvm_di_type != nullptr) return;
7472
7473 ZigType *result_type = any_frame_type->data.any_frame.result_type;
7474 Buf *name = buf_sprintf("(%s header)", buf_ptr(&any_frame_type->name));
7475
7476 ZigType *frame_header_type;
7477 if (result_type == nullptr || !type_has_bits(result_type)) {
7478 const char *field_names[] = {"resume_index", "fn_ptr", "awaiter"};
7479 ZigType *field_types[] = {
7480 g->builtin_types.entry_usize,
7481 g->builtin_types.entry_usize,
7482 g->builtin_types.entry_usize,
7483 };
7484 frame_header_type = get_struct_type(g, buf_ptr(name), field_names, field_types, 3);
7485 } else {
7486 ZigType *ptr_result_type = get_pointer_to_type(g, result_type, false);
7487
7488 const char *field_names[] = {"resume_index", "fn_ptr", "awaiter", "result_ptr", "result"};
7489 ZigType *field_types[] = {
7490 g->builtin_types.entry_usize,
7491 g->builtin_types.entry_usize,
7492 g->builtin_types.entry_usize,
7493 ptr_result_type,
7494 result_type,
7495 };
7496 frame_header_type = get_struct_type(g, buf_ptr(name), field_names, field_types, 5);
7497 }
7498
7499 ZigType *ptr_type = get_pointer_to_type(g, frame_header_type, false);
7500 any_frame_type->llvm_type = get_llvm_type(g, ptr_type);
7501 any_frame_type->llvm_di_type = get_llvm_di_type(g, ptr_type);
7502}
7503
74017504static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status) {
74027505 assert(type->id == ZigTypeIdOpaque || type_is_resolved(type, ResolveStatusSizeKnown));
74037506 assert(wanted_resolve_status > ResolveStatusSizeKnown);
......@@ -7460,6 +7563,8 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_r
74607563 }
74617564 case ZigTypeIdCoroFrame:
74627565 return resolve_llvm_types_coro_frame(g, type, wanted_resolve_status);
7566 case ZigTypeIdAnyFrame:
7567 return resolve_llvm_types_any_frame(g, type, wanted_resolve_status);
74637568 }
74647569 zig_unreachable();
74657570}
src/analyze.hpp+1
......@@ -41,6 +41,7 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c
4141ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],
4242 ZigType *field_types[], size_t field_count);
4343ZigType *get_test_fn_type(CodeGen *g);
44ZigType *get_any_frame_type(CodeGen *g, ZigType *result_type);
4445bool handle_is_ptr(ZigType *type_entry);
4546
4647bool type_has_bits(ZigType *type_entry);
src/ast_render.cpp+10
......@@ -259,6 +259,8 @@ static const char *node_type_str(NodeType node_type) {
259259 return "Suspend";
260260 case NodeTypePointerType:
261261 return "PointerType";
262 case NodeTypeAnyFrameType:
263 return "AnyFrameType";
262264 case NodeTypeEnumLiteral:
263265 return "EnumLiteral";
264266 }
......@@ -847,6 +849,14 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
847849 render_node_ungrouped(ar, node->data.inferred_array_type.child_type);
848850 break;
849851 }
852 case NodeTypeAnyFrameType: {
853 fprintf(ar->f, "anyframe");
854 if (node->data.anyframe_type.payload_type != nullptr) {
855 fprintf(ar->f, "->");
856 render_node_grouped(ar, node->data.anyframe_type.payload_type);
857 }
858 break;
859 }
850860 case NodeTypeErrorType:
851861 fprintf(ar->f, "anyerror");
852862 break;
src/codegen.cpp+15-1
......@@ -4947,6 +4947,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
49474947 case IrInstructionIdSetRuntimeSafety:
49484948 case IrInstructionIdSetFloatMode:
49494949 case IrInstructionIdArrayType:
4950 case IrInstructionIdAnyFrameType:
49504951 case IrInstructionIdSliceType:
49514952 case IrInstructionIdSizeOf:
49524953 case IrInstructionIdSwitchTarget:
......@@ -5438,7 +5439,9 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
54385439 return val;
54395440 }
54405441 case ZigTypeIdCoroFrame:
5441 zig_panic("TODO bit pack a coroutine frame");
5442 zig_panic("TODO bit pack an async function frame");
5443 case ZigTypeIdAnyFrame:
5444 zig_panic("TODO bit pack an anyframe");
54425445 }
54435446 zig_unreachable();
54445447}
......@@ -5961,6 +5964,8 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
59615964 zig_unreachable();
59625965 case ZigTypeIdCoroFrame:
59635966 zig_panic("TODO");
5967 case ZigTypeIdAnyFrame:
5968 zig_panic("TODO");
59645969 }
59655970 zig_unreachable();
59665971}
......@@ -7176,6 +7181,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
71767181 " ArgTuple: void,\n"
71777182 " Opaque: void,\n"
71787183 " Frame: void,\n"
7184 " AnyFrame: AnyFrame,\n"
71797185 " Vector: Vector,\n"
71807186 " EnumLiteral: void,\n"
71817187 "\n\n"
......@@ -7291,6 +7297,10 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
72917297 " args: []FnArg,\n"
72927298 " };\n"
72937299 "\n"
7300 " pub const AnyFrame = struct {\n"
7301 " child: ?type,\n"
7302 " };\n"
7303 "\n"
72947304 " pub const Vector = struct {\n"
72957305 " len: comptime_int,\n"
72967306 " child: type,\n"
......@@ -8448,6 +8458,7 @@ static void prepend_c_type_to_decl_list(CodeGen *g, GenH *gen_h, ZigType *type_e
84488458 case ZigTypeIdErrorUnion:
84498459 case ZigTypeIdErrorSet:
84508460 case ZigTypeIdCoroFrame:
8461 case ZigTypeIdAnyFrame:
84518462 zig_unreachable();
84528463 case ZigTypeIdVoid:
84538464 case ZigTypeIdUnreachable:
......@@ -8632,6 +8643,7 @@ static void get_c_type(CodeGen *g, GenH *gen_h, ZigType *type_entry, Buf *out_bu
86328643 case ZigTypeIdNull:
86338644 case ZigTypeIdArgTuple:
86348645 case ZigTypeIdCoroFrame:
8646 case ZigTypeIdAnyFrame:
86358647 zig_unreachable();
86368648 }
86378649}
......@@ -8800,7 +8812,9 @@ static void gen_h_file(CodeGen *g) {
88008812 case ZigTypeIdFn:
88018813 case ZigTypeIdVector:
88028814 case ZigTypeIdCoroFrame:
8815 case ZigTypeIdAnyFrame:
88038816 zig_unreachable();
8817
88048818 case ZigTypeIdEnum:
88058819 if (type_entry->data.enumeration.layout == ContainerLayoutExtern) {
88068820 fprintf(out_h, "enum %s {\n", buf_ptr(type_h_name(type_entry)));
src/ir.cpp+83-3
......@@ -303,6 +303,7 @@ static bool types_have_same_zig_comptime_repr(ZigType *a, ZigType *b) {
303303 case ZigTypeIdBoundFn:
304304 case ZigTypeIdErrorSet:
305305 case ZigTypeIdOpaque:
306 case ZigTypeIdAnyFrame:
306307 return true;
307308 case ZigTypeIdFloat:
308309 return a->data.floating.bit_count == b->data.floating.bit_count;
......@@ -563,6 +564,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionArrayType *) {
563564 return IrInstructionIdArrayType;
564565}
565566
567static constexpr IrInstructionId ir_instruction_id(IrInstructionAnyFrameType *) {
568 return IrInstructionIdAnyFrameType;
569}
570
566571static constexpr IrInstructionId ir_instruction_id(IrInstructionSliceType *) {
567572 return IrInstructionIdSliceType;
568573}
......@@ -1696,6 +1701,16 @@ static IrInstruction *ir_build_array_type(IrBuilder *irb, Scope *scope, AstNode
16961701 return &instruction->base;
16971702}
16981703
1704static IrInstruction *ir_build_anyframe_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
1705 IrInstruction *payload_type)
1706{
1707 IrInstructionAnyFrameType *instruction = ir_build_instruction<IrInstructionAnyFrameType>(irb, scope, source_node);
1708 instruction->payload_type = payload_type;
1709
1710 if (payload_type != nullptr) ir_ref_instruction(payload_type, irb->current_basic_block);
1711
1712 return &instruction->base;
1713}
16991714static IrInstruction *ir_build_slice_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
17001715 IrInstruction *child_type, bool is_const, bool is_volatile, IrInstruction *align_value, bool is_allow_zero)
17011716{
......@@ -6515,6 +6530,22 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
65156530 }
65166531}
65176532
6533static IrInstruction *ir_gen_anyframe_type(IrBuilder *irb, Scope *scope, AstNode *node) {
6534 assert(node->type == NodeTypeAnyFrameType);
6535
6536 AstNode *payload_type_node = node->data.anyframe_type.payload_type;
6537 IrInstruction *payload_type_value = nullptr;
6538
6539 if (payload_type_node != nullptr) {
6540 payload_type_value = ir_gen_node(irb, payload_type_node, scope);
6541 if (payload_type_value == irb->codegen->invalid_instruction)
6542 return payload_type_value;
6543
6544 }
6545
6546 return ir_build_anyframe_type(irb, scope, node, payload_type_value);
6547}
6548
65186549static IrInstruction *ir_gen_undefined_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
65196550 assert(node->type == NodeTypeUndefinedLiteral);
65206551 return ir_build_const_undefined(irb, scope, node);
......@@ -7884,6 +7915,8 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
78847915 return ir_lval_wrap(irb, scope, ir_gen_array_type(irb, scope, node), lval, result_loc);
78857916 case NodeTypePointerType:
78867917 return ir_lval_wrap(irb, scope, ir_gen_pointer_type(irb, scope, node), lval, result_loc);
7918 case NodeTypeAnyFrameType:
7919 return ir_lval_wrap(irb, scope, ir_gen_anyframe_type(irb, scope, node), lval, result_loc);
78877920 case NodeTypeStringLiteral:
78887921 return ir_lval_wrap(irb, scope, ir_gen_string_literal(irb, scope, node), lval, result_loc);
78897922 case NodeTypeUndefinedLiteral:
......@@ -12775,6 +12808,7 @@ static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *
1277512808 case ZigTypeIdArgTuple:
1277612809 case ZigTypeIdEnum:
1277712810 case ZigTypeIdEnumLiteral:
12811 case ZigTypeIdAnyFrame:
1277812812 operator_allowed = is_equality_cmp;
1277912813 break;
1278012814
......@@ -14155,6 +14189,7 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
1415514189 case ZigTypeIdArgTuple:
1415614190 case ZigTypeIdOpaque:
1415714191 case ZigTypeIdCoroFrame:
14192 case ZigTypeIdAnyFrame:
1415814193 ir_add_error(ira, target,
1415914194 buf_sprintf("invalid export target '%s'", buf_ptr(&type_value->name)));
1416014195 break;
......@@ -14180,6 +14215,7 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
1418014215 case ZigTypeIdOpaque:
1418114216 case ZigTypeIdEnumLiteral:
1418214217 case ZigTypeIdCoroFrame:
14218 case ZigTypeIdAnyFrame:
1418314219 ir_add_error(ira, target,
1418414220 buf_sprintf("invalid export target type '%s'", buf_ptr(&target->value.type->name)));
1418514221 break;
......@@ -15720,7 +15756,9 @@ static IrInstruction *ir_analyze_optional_type(IrAnalyze *ira, IrInstructionUnOp
1572015756 case ZigTypeIdBoundFn:
1572115757 case ZigTypeIdArgTuple:
1572215758 case ZigTypeIdCoroFrame:
15759 case ZigTypeIdAnyFrame:
1572315760 return ir_const_type(ira, &un_op_instruction->base, get_optional_type(ira->codegen, type_entry));
15761
1572415762 case ZigTypeIdUnreachable:
1572515763 case ZigTypeIdOpaque:
1572615764 ir_add_error_node(ira, un_op_instruction->base.source_node,
......@@ -17443,6 +17481,20 @@ static IrInstruction *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,
1744317481 return ir_const_void(ira, &instruction->base);
1744417482}
1744517483
17484static IrInstruction *ir_analyze_instruction_any_frame_type(IrAnalyze *ira,
17485 IrInstructionAnyFrameType *instruction)
17486{
17487 ZigType *payload_type = nullptr;
17488 if (instruction->payload_type != nullptr) {
17489 payload_type = ir_resolve_type(ira, instruction->payload_type->child);
17490 if (type_is_invalid(payload_type))
17491 return ira->codegen->invalid_instruction;
17492 }
17493
17494 ZigType *any_frame_type = get_any_frame_type(ira->codegen, payload_type);
17495 return ir_const_type(ira, &instruction->base, any_frame_type);
17496}
17497
1744617498static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1744717499 IrInstructionSliceType *slice_type_instruction)
1744817500{
......@@ -17492,6 +17544,7 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1749217544 case ZigTypeIdBoundFn:
1749317545 case ZigTypeIdVector:
1749417546 case ZigTypeIdCoroFrame:
17547 case ZigTypeIdAnyFrame:
1749517548 {
1749617549 ResolveStatus needed_status = (align_bytes == 0) ?
1749717550 ResolveStatusZeroBitsKnown : ResolveStatusAlignmentKnown;
......@@ -17607,6 +17660,7 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
1760717660 case ZigTypeIdBoundFn:
1760817661 case ZigTypeIdVector:
1760917662 case ZigTypeIdCoroFrame:
17663 case ZigTypeIdAnyFrame:
1761017664 {
1761117665 if ((err = ensure_complete_type(ira->codegen, child_type)))
1761217666 return ira->codegen->invalid_instruction;
......@@ -17658,6 +17712,7 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira,
1765817712 case ZigTypeIdFn:
1765917713 case ZigTypeIdVector:
1766017714 case ZigTypeIdCoroFrame:
17715 case ZigTypeIdAnyFrame:
1766117716 {
1766217717 uint64_t size_in_bytes = type_size(ira->codegen, type_entry);
1766317718 return ir_const_unsigned(ira, &size_of_instruction->base, size_in_bytes);
......@@ -18222,6 +18277,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1822218277 case ZigTypeIdOpaque:
1822318278 case ZigTypeIdVector:
1822418279 case ZigTypeIdCoroFrame:
18280 case ZigTypeIdAnyFrame:
1822518281 ir_add_error(ira, &switch_target_instruction->base,
1822618282 buf_sprintf("invalid switch target type '%s'", buf_ptr(&target_type->name)));
1822718283 return ira->codegen->invalid_instruction;
......@@ -19656,6 +19712,22 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1965619712
1965719713 break;
1965819714 }
19715 case ZigTypeIdAnyFrame: {
19716 result = create_const_vals(1);
19717 result->special = ConstValSpecialStatic;
19718 result->type = ir_type_info_get_type(ira, "AnyFrame", nullptr);
19719
19720 ConstExprValue *fields = create_const_vals(1);
19721 result->data.x_struct.fields = fields;
19722
19723 // child: ?type
19724 ensure_field_index(result->type, "child", 0);
19725 fields[0].special = ConstValSpecialStatic;
19726 fields[0].type = get_optional_type(ira->codegen, ira->codegen->builtin_types.entry_type);
19727 fields[0].data.x_optional = (type_entry->data.any_frame.result_type == nullptr) ? nullptr :
19728 create_const_type(ira->codegen, type_entry->data.any_frame.result_type);
19729 break;
19730 }
1965919731 case ZigTypeIdEnum:
1966019732 {
1966119733 result = create_const_vals(1);
......@@ -20062,7 +20134,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2006220134 break;
2006320135 }
2006420136 case ZigTypeIdCoroFrame:
20065 zig_panic("TODO @typeInfo for coro frames");
20137 zig_panic("TODO @typeInfo for async function frames");
2006620138 }
2006720139
2006820140 assert(result != nullptr);
......@@ -21852,6 +21924,7 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct
2185221924 case ZigTypeIdFn:
2185321925 case ZigTypeIdVector:
2185421926 case ZigTypeIdCoroFrame:
21927 case ZigTypeIdAnyFrame:
2185521928 {
2185621929 uint64_t align_in_bytes = get_abi_alignment(ira->codegen, type_entry);
2185721930 return ir_const_unsigned(ira, &instruction->base, align_in_bytes);
......@@ -23004,7 +23077,9 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2300423077 case ZigTypeIdUnion:
2300523078 zig_panic("TODO buf_write_value_bytes union type");
2300623079 case ZigTypeIdCoroFrame:
23007 zig_panic("TODO buf_write_value_bytes coro frame type");
23080 zig_panic("TODO buf_write_value_bytes async fn frame type");
23081 case ZigTypeIdAnyFrame:
23082 zig_panic("TODO buf_write_value_bytes anyframe type");
2300823083 }
2300923084 zig_unreachable();
2301023085}
......@@ -23185,7 +23260,9 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2318523260 case ZigTypeIdUnion:
2318623261 zig_panic("TODO buf_read_value_bytes union type");
2318723262 case ZigTypeIdCoroFrame:
23188 zig_panic("TODO buf_read_value_bytes coro frame type");
23263 zig_panic("TODO buf_read_value_bytes async fn frame type");
23264 case ZigTypeIdAnyFrame:
23265 zig_panic("TODO buf_read_value_bytes anyframe type");
2318923266 }
2319023267 zig_unreachable();
2319123268}
......@@ -24327,6 +24404,8 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2432724404 return ir_analyze_instruction_set_runtime_safety(ira, (IrInstructionSetRuntimeSafety *)instruction);
2432824405 case IrInstructionIdSetFloatMode:
2432924406 return ir_analyze_instruction_set_float_mode(ira, (IrInstructionSetFloatMode *)instruction);
24407 case IrInstructionIdAnyFrameType:
24408 return ir_analyze_instruction_any_frame_type(ira, (IrInstructionAnyFrameType *)instruction);
2433024409 case IrInstructionIdSliceType:
2433124410 return ir_analyze_instruction_slice_type(ira, (IrInstructionSliceType *)instruction);
2433224411 case IrInstructionIdGlobalAsm:
......@@ -24707,6 +24786,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2470724786 case IrInstructionIdStructFieldPtr:
2470824787 case IrInstructionIdArrayType:
2470924788 case IrInstructionIdSliceType:
24789 case IrInstructionIdAnyFrameType:
2471024790 case IrInstructionIdSizeOf:
2471124791 case IrInstructionIdTestNonNull:
2471224792 case IrInstructionIdOptionalUnwrapPtr:
src/ir_print.cpp+12
......@@ -471,6 +471,15 @@ static void ir_print_slice_type(IrPrint *irp, IrInstructionSliceType *instructio
471471 ir_print_other_instruction(irp, instruction->child_type);
472472}
473473
474static void ir_print_any_frame_type(IrPrint *irp, IrInstructionAnyFrameType *instruction) {
475 if (instruction->payload_type == nullptr) {
476 fprintf(irp->f, "anyframe");
477 } else {
478 fprintf(irp->f, "anyframe->");
479 ir_print_other_instruction(irp, instruction->payload_type);
480 }
481}
482
474483static void ir_print_global_asm(IrPrint *irp, IrInstructionGlobalAsm *instruction) {
475484 fprintf(irp->f, "asm(\"%s\")", buf_ptr(instruction->asm_code));
476485}
......@@ -1629,6 +1638,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
16291638 case IrInstructionIdSliceType:
16301639 ir_print_slice_type(irp, (IrInstructionSliceType *)instruction);
16311640 break;
1641 case IrInstructionIdAnyFrameType:
1642 ir_print_any_frame_type(irp, (IrInstructionAnyFrameType *)instruction);
1643 break;
16321644 case IrInstructionIdGlobalAsm:
16331645 ir_print_global_asm(irp, (IrInstructionGlobalAsm *)instruction);
16341646 break;
src/parser.cpp+21-1
......@@ -282,6 +282,9 @@ static AstNode *ast_parse_prefix_op_expr(
282282 case NodeTypeAwaitExpr:
283283 right = &prefix->data.await_expr.expr;
284284 break;
285 case NodeTypeAnyFrameType:
286 right = &prefix->data.anyframe_type.payload_type;
287 break;
285288 case NodeTypeArrayType:
286289 right = &prefix->data.array_type.child_type;
287290 break;
......@@ -1640,6 +1643,10 @@ static AstNode *ast_parse_primary_type_expr(ParseContext *pc) {
16401643 if (null != nullptr)
16411644 return ast_create_node(pc, NodeTypeNullLiteral, null);
16421645
1646 Token *anyframe = eat_token_if(pc, TokenIdKeywordAnyFrame);
1647 if (anyframe != nullptr)
1648 return ast_create_node(pc, NodeTypeAnyFrameType, anyframe);
1649
16431650 Token *true_token = eat_token_if(pc, TokenIdKeywordTrue);
16441651 if (true_token != nullptr) {
16451652 AstNode *res = ast_create_node(pc, NodeTypeBoolLiteral, true_token);
......@@ -2510,7 +2517,7 @@ static AstNode *ast_parse_prefix_op(ParseContext *pc) {
25102517
25112518// PrefixTypeOp
25122519// <- QUESTIONMARK
2513// / KEYWORD_promise MINUSRARROW
2520// / KEYWORD_anyframe MINUSRARROW
25142521// / ArrayTypeStart (ByteAlign / KEYWORD_const / KEYWORD_volatile)*
25152522// / PtrTypeStart (KEYWORD_align LPAREN Expr (COLON INTEGER COLON INTEGER)? RPAREN / KEYWORD_const / KEYWORD_volatile)*
25162523static AstNode *ast_parse_prefix_type_op(ParseContext *pc) {
......@@ -2521,6 +2528,16 @@ static AstNode *ast_parse_prefix_type_op(ParseContext *pc) {
25212528 return res;
25222529 }
25232530
2531 Token *anyframe = eat_token_if(pc, TokenIdKeywordAnyFrame);
2532 if (anyframe != nullptr) {
2533 if (eat_token_if(pc, TokenIdArrow) != nullptr) {
2534 AstNode *res = ast_create_node(pc, NodeTypeAnyFrameType, anyframe);
2535 return res;
2536 }
2537
2538 put_back_token(pc);
2539 }
2540
25242541 AstNode *array = ast_parse_array_type_start(pc);
25252542 if (array != nullptr) {
25262543 assert(array->type == NodeTypeArrayType);
......@@ -3005,6 +3022,9 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
30053022 case NodeTypeInferredArrayType:
30063023 visit_field(&node->data.array_type.child_type, visit, context);
30073024 break;
3025 case NodeTypeAnyFrameType:
3026 visit_field(&node->data.anyframe_type.payload_type, visit, context);
3027 break;
30083028 case NodeTypeErrorType:
30093029 // none
30103030 break;
src/tokenizer.cpp+2
......@@ -109,6 +109,7 @@ static const struct ZigKeyword zig_keywords[] = {
109109 {"align", TokenIdKeywordAlign},
110110 {"allowzero", TokenIdKeywordAllowZero},
111111 {"and", TokenIdKeywordAnd},
112 {"anyframe", TokenIdKeywordAnyFrame},
112113 {"asm", TokenIdKeywordAsm},
113114 {"async", TokenIdKeywordAsync},
114115 {"await", TokenIdKeywordAwait},
......@@ -1533,6 +1534,7 @@ const char * token_name(TokenId id) {
15331534 case TokenIdKeywordCancel: return "cancel";
15341535 case TokenIdKeywordAlign: return "align";
15351536 case TokenIdKeywordAnd: return "and";
1537 case TokenIdKeywordAnyFrame: return "anyframe";
15361538 case TokenIdKeywordAsm: return "asm";
15371539 case TokenIdKeywordBreak: return "break";
15381540 case TokenIdKeywordCatch: return "catch";
src/tokenizer.hpp+1
......@@ -53,6 +53,7 @@ enum TokenId {
5353 TokenIdKeywordAlign,
5454 TokenIdKeywordAllowZero,
5555 TokenIdKeywordAnd,
56 TokenIdKeywordAnyFrame,
5657 TokenIdKeywordAsm,
5758 TokenIdKeywordAsync,
5859 TokenIdKeywordAwait,
std/hash_map.zig+1
......@@ -540,6 +540,7 @@ pub fn autoHash(key: var, comptime rng: *std.rand.Random, comptime HashInt: type
540540 .Undefined,
541541 .ArgTuple,
542542 .Frame,
543 .AnyFrame,
543544 => @compileError("cannot hash this type"),
544545
545546 .Void,
std/testing.zig+1
......@@ -30,6 +30,7 @@ pub fn expectEqual(expected: var, actual: @typeOf(expected)) void {
3030 .ArgTuple,
3131 .Opaque,
3232 .Frame,
33 .AnyFrame,
3334 => @compileError("value of type " ++ @typeName(@typeOf(actual)) ++ " encountered"),
3435
3536 .Undefined,
std/zig/ast.zig+8-8
......@@ -400,7 +400,7 @@ pub const Node = struct {
400400 VarType,
401401 ErrorType,
402402 FnProto,
403 PromiseType,
403 AnyFrameType,
404404
405405 // Primary expressions
406406 IntegerLiteral,
......@@ -952,9 +952,9 @@ pub const Node = struct {
952952 }
953953 };
954954
955 pub const PromiseType = struct {
955 pub const AnyFrameType = struct {
956956 base: Node,
957 promise_token: TokenIndex,
957 anyframe_token: TokenIndex,
958958 result: ?Result,
959959
960960 pub const Result = struct {
......@@ -962,7 +962,7 @@ pub const Node = struct {
962962 return_type: *Node,
963963 };
964964
965 pub fn iterate(self: *PromiseType, index: usize) ?*Node {
965 pub fn iterate(self: *AnyFrameType, index: usize) ?*Node {
966966 var i = index;
967967
968968 if (self.result) |result| {
......@@ -973,13 +973,13 @@ pub const Node = struct {
973973 return null;
974974 }
975975
976 pub fn firstToken(self: *const PromiseType) TokenIndex {
977 return self.promise_token;
976 pub fn firstToken(self: *const AnyFrameType) TokenIndex {
977 return self.anyframe_token;
978978 }
979979
980 pub fn lastToken(self: *const PromiseType) TokenIndex {
980 pub fn lastToken(self: *const AnyFrameType) TokenIndex {
981981 if (self.result) |result| return result.return_type.lastToken();
982 return self.promise_token;
982 return self.anyframe_token;
983983 }
984984 };
985985
std/zig/parse.zig+20-20
......@@ -1201,7 +1201,7 @@ fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
12011201/// / KEYWORD_error DOT IDENTIFIER
12021202/// / KEYWORD_false
12031203/// / KEYWORD_null
1204/// / KEYWORD_promise
1204/// / KEYWORD_anyframe
12051205/// / KEYWORD_true
12061206/// / KEYWORD_undefined
12071207/// / KEYWORD_unreachable
......@@ -1256,11 +1256,11 @@ fn parsePrimaryTypeExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*N
12561256 }
12571257 if (eatToken(it, .Keyword_false)) |token| return createLiteral(arena, Node.BoolLiteral, token);
12581258 if (eatToken(it, .Keyword_null)) |token| return createLiteral(arena, Node.NullLiteral, token);
1259 if (eatToken(it, .Keyword_promise)) |token| {
1260 const node = try arena.create(Node.PromiseType);
1261 node.* = Node.PromiseType{
1262 .base = Node{ .id = .PromiseType },
1263 .promise_token = token,
1259 if (eatToken(it, .Keyword_anyframe)) |token| {
1260 const node = try arena.create(Node.AnyFrameType);
1261 node.* = Node.AnyFrameType{
1262 .base = Node{ .id = .AnyFrameType },
1263 .anyframe_token = token,
12641264 .result = null,
12651265 };
12661266 return &node.base;
......@@ -2194,7 +2194,7 @@ fn parsePrefixOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
21942194
21952195/// PrefixTypeOp
21962196/// <- QUESTIONMARK
2197/// / KEYWORD_promise MINUSRARROW
2197/// / KEYWORD_anyframe MINUSRARROW
21982198/// / ArrayTypeStart (ByteAlign / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)*
21992199/// / PtrTypeStart (KEYWORD_align LPAREN Expr (COLON INTEGER COLON INTEGER)? RPAREN / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)*
22002200fn parsePrefixTypeOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
......@@ -2209,20 +2209,20 @@ fn parsePrefixTypeOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
22092209 return &node.base;
22102210 }
22112211
2212 // TODO: Returning a PromiseType instead of PrefixOp makes casting and setting .rhs or
2212 // TODO: Returning a AnyFrameType instead of PrefixOp makes casting and setting .rhs or
22132213 // .return_type more difficult for the caller (see parsePrefixOpExpr helper).
2214 // Consider making the PromiseType a member of PrefixOp and add a
2215 // PrefixOp.PromiseType variant?
2216 if (eatToken(it, .Keyword_promise)) |token| {
2214 // Consider making the AnyFrameType a member of PrefixOp and add a
2215 // PrefixOp.AnyFrameType variant?
2216 if (eatToken(it, .Keyword_anyframe)) |token| {
22172217 const arrow = eatToken(it, .Arrow) orelse {
22182218 putBackToken(it, token);
22192219 return null;
22202220 };
2221 const node = try arena.create(Node.PromiseType);
2222 node.* = Node.PromiseType{
2223 .base = Node{ .id = .PromiseType },
2224 .promise_token = token,
2225 .result = Node.PromiseType.Result{
2221 const node = try arena.create(Node.AnyFrameType);
2222 node.* = Node.AnyFrameType{
2223 .base = Node{ .id = .AnyFrameType },
2224 .anyframe_token = token,
2225 .result = Node.AnyFrameType.Result{
22262226 .arrow_token = arrow,
22272227 .return_type = undefined, // set by caller
22282228 },
......@@ -2903,8 +2903,8 @@ fn parsePrefixOpExpr(
29032903 rightmost_op = rhs;
29042904 } else break;
29052905 },
2906 .PromiseType => {
2907 const prom = rightmost_op.cast(Node.PromiseType).?;
2906 .AnyFrameType => {
2907 const prom = rightmost_op.cast(Node.AnyFrameType).?;
29082908 if (try opParseFn(arena, it, tree)) |rhs| {
29092909 prom.result.?.return_type = rhs;
29102910 rightmost_op = rhs;
......@@ -2922,8 +2922,8 @@ fn parsePrefixOpExpr(
29222922 .InvalidToken = AstError.InvalidToken{ .token = it.index },
29232923 });
29242924 },
2925 .PromiseType => {
2926 const prom = rightmost_op.cast(Node.PromiseType).?;
2925 .AnyFrameType => {
2926 const prom = rightmost_op.cast(Node.AnyFrameType).?;
29272927 prom.result.?.return_type = try expectNode(arena, it, tree, childParseFn, AstError{
29282928 .InvalidToken = AstError.InvalidToken{ .token = it.index },
29292929 });
std/zig/parser_test.zig+2-2
......@@ -2111,12 +2111,12 @@ test "zig fmt: coroutines" {
21112111 \\ suspend;
21122112 \\ x += 1;
21132113 \\ suspend;
2114 \\ const p: promise->void = async simpleAsyncFn() catch unreachable;
2114 \\ const p: anyframe->void = async simpleAsyncFn() catch unreachable;
21152115 \\ await p;
21162116 \\}
21172117 \\
21182118 \\test "coroutine suspend, resume, cancel" {
2119 \\ const p: promise = try async<std.debug.global_allocator> testAsyncSeq();
2119 \\ const p: anyframe = try async<std.debug.global_allocator> testAsyncSeq();
21202120 \\ resume p;
21212121 \\ cancel p;
21222122 \\}
std/zig/render.zig+5-5
......@@ -1205,15 +1205,15 @@ fn renderExpression(
12051205 }
12061206 },
12071207
1208 ast.Node.Id.PromiseType => {
1209 const promise_type = @fieldParentPtr(ast.Node.PromiseType, "base", base);
1208 ast.Node.Id.AnyFrameType => {
1209 const anyframe_type = @fieldParentPtr(ast.Node.AnyFrameType, "base", base);
12101210
1211 if (promise_type.result) |result| {
1212 try renderToken(tree, stream, promise_type.promise_token, indent, start_col, Space.None); // promise
1211 if (anyframe_type.result) |result| {
1212 try renderToken(tree, stream, anyframe_type.anyframe_token, indent, start_col, Space.None); // anyframe
12131213 try renderToken(tree, stream, result.arrow_token, indent, start_col, Space.None); // ->
12141214 return renderExpression(allocator, stream, tree, indent, start_col, result.return_type, space);
12151215 } else {
1216 return renderToken(tree, stream, promise_type.promise_token, indent, start_col, space); // promise
1216 return renderToken(tree, stream, anyframe_type.anyframe_token, indent, start_col, space); // anyframe
12171217 }
12181218 },
12191219
std/zig/tokenizer.zig+2-2
......@@ -15,6 +15,7 @@ pub const Token = struct {
1515 Keyword{ .bytes = "align", .id = Id.Keyword_align },
1616 Keyword{ .bytes = "allowzero", .id = Id.Keyword_allowzero },
1717 Keyword{ .bytes = "and", .id = Id.Keyword_and },
18 Keyword{ .bytes = "anyframe", .id = Id.Keyword_anyframe },
1819 Keyword{ .bytes = "asm", .id = Id.Keyword_asm },
1920 Keyword{ .bytes = "async", .id = Id.Keyword_async },
2021 Keyword{ .bytes = "await", .id = Id.Keyword_await },
......@@ -42,7 +43,6 @@ pub const Token = struct {
4243 Keyword{ .bytes = "or", .id = Id.Keyword_or },
4344 Keyword{ .bytes = "orelse", .id = Id.Keyword_orelse },
4445 Keyword{ .bytes = "packed", .id = Id.Keyword_packed },
45 Keyword{ .bytes = "promise", .id = Id.Keyword_promise },
4646 Keyword{ .bytes = "pub", .id = Id.Keyword_pub },
4747 Keyword{ .bytes = "resume", .id = Id.Keyword_resume },
4848 Keyword{ .bytes = "return", .id = Id.Keyword_return },
......@@ -174,7 +174,7 @@ pub const Token = struct {
174174 Keyword_or,
175175 Keyword_orelse,
176176 Keyword_packed,
177 Keyword_promise,
177 Keyword_anyframe,
178178 Keyword_pub,
179179 Keyword_resume,
180180 Keyword_return,
test/stage1/behavior/type_info.zig+21-2
......@@ -177,11 +177,11 @@ fn testUnion() void {
177177 expect(TypeId(typeinfo_info) == TypeId.Union);
178178 expect(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto);
179179 expect(typeinfo_info.Union.tag_type.? == TypeId);
180 expect(typeinfo_info.Union.fields.len == 25);
180 expect(typeinfo_info.Union.fields.len == 26);
181181 expect(typeinfo_info.Union.fields[4].enum_field != null);
182182 expect(typeinfo_info.Union.fields[4].enum_field.?.value == 4);
183183 expect(typeinfo_info.Union.fields[4].field_type == @typeOf(@typeInfo(u8).Int));
184 expect(typeinfo_info.Union.decls.len == 20);
184 expect(typeinfo_info.Union.decls.len == 21);
185185
186186 const TestNoTagUnion = union {
187187 Foo: void,
......@@ -280,6 +280,25 @@ fn testVector() void {
280280 expect(vec_info.Vector.child == i32);
281281}
282282
283test "type info: anyframe and anyframe->T" {
284 testAnyFrame();
285 comptime testAnyFrame();
286}
287
288fn testAnyFrame() void {
289 {
290 const anyframe_info = @typeInfo(anyframe->i32);
291 expect(TypeId(anyframe_info) == .AnyFrame);
292 expect(anyframe_info.AnyFrame.child.? == i32);
293 }
294
295 {
296 const anyframe_info = @typeInfo(anyframe);
297 expect(TypeId(anyframe_info) == .AnyFrame);
298 expect(anyframe_info.AnyFrame.child == null);
299 }
300}
301
283302test "type info: optional field unwrapping" {
284303 const Struct = struct {
285304 cdOffset: u32,