authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-03-06 18:24:49-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-03-06 18:31:31-05:00
logeff3530dfab5ecb4e480e0516ed57a8f564543f5
tree6d407f8f334e1910e0ae6ea16190aa9c3e5de016
parent44ae891bd79cc8b2f9040a39c176317ebf4a4ef8

var is no longer a pseudo-type, it is syntax

closes #779

8 files changed, 116 insertions(+), 154 deletions(-)

doc/langref.html.in+3-3
......@@ -5733,19 +5733,19 @@ UseDecl = "use" Expression ";"
57335733
57345734ExternDecl = "extern" option(String) (FnProto | VariableDeclaration) ";"
57355735
5736FnProto = option("nakedcc" | "stdcallcc" | "extern" | ("async" option("(" Expression ")"))) "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr
5736FnProto = option("nakedcc" | "stdcallcc" | "extern" | ("async" option("(" Expression ")"))) "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") (TypeExpr | "var")
57375737
57385738FnDef = option("inline" | "export") FnProto Block
57395739
57405740ParamDeclList = "(" list(ParamDecl, ",") ")"
57415741
5742ParamDecl = option("noalias" | "comptime") option(Symbol ":") (TypeExpr | "...")
5742ParamDecl = option("noalias" | "comptime") option(Symbol ":") (TypeExpr | "var" | "...")
57435743
57445744Block = option(Symbol ":") "{" many(Statement) "}"
57455745
57465746Statement = LocalVarDecl ";" | Defer(Block) | Defer(Expression) ";" | BlockExpression(Block) | Expression ";" | ";"
57475747
5748TypeExpr = ErrorSetExpr | "var"
5748TypeExpr = ErrorSetExpr
57495749
57505750ErrorSetExpr = (PrefixOpExpression "!" PrefixOpExpression) | PrefixOpExpression
57515751
src/all_types.hpp+3-6
......@@ -16,6 +16,7 @@
1616#include "bigint.hpp"
1717#include "bigfloat.hpp"
1818#include "target.hpp"
19#include "tokenizer.hpp"
1920
2021struct AstNode;
2122struct ImportTableEntry;
......@@ -399,7 +400,6 @@ enum NodeType {
399400 NodeTypeStructValueField,
400401 NodeTypeArrayType,
401402 NodeTypeErrorType,
402 NodeTypeVarLiteral,
403403 NodeTypeIfErrorExpr,
404404 NodeTypeTestExpr,
405405 NodeTypeErrorSetDecl,
......@@ -427,6 +427,7 @@ struct AstNodeFnProto {
427427 Buf *name;
428428 ZigList<AstNode *> params;
429429 AstNode *return_type;
430 Token *return_var_token;
430431 bool is_var_args;
431432 bool is_extern;
432433 bool is_export;
......@@ -456,6 +457,7 @@ struct AstNodeFnDecl {
456457struct AstNodeParamDecl {
457458 Buf *name;
458459 AstNode *type;
460 Token *var_token;
459461 bool is_noalias;
460462 bool is_inline;
461463 bool is_var_args;
......@@ -866,9 +868,6 @@ struct AstNodeUnreachableExpr {
866868struct AstNodeErrorType {
867869};
868870
869struct AstNodeVarLiteral {
870};
871
872871struct AstNodeAwaitExpr {
873872 AstNode *expr;
874873};
......@@ -933,7 +932,6 @@ struct AstNode {
933932 AstNodeUnreachableExpr unreachable_expr;
934933 AstNodeArrayType array_type;
935934 AstNodeErrorType error_type;
936 AstNodeVarLiteral var_literal;
937935 AstNodeErrorSetDecl err_set_decl;
938936 AstNodeCancelExpr cancel_expr;
939937 AstNodeResumeExpr resume_expr;
......@@ -1134,7 +1132,6 @@ struct TypeTableEntryPromise {
11341132
11351133enum TypeTableEntryId {
11361134 TypeTableEntryIdInvalid,
1137 TypeTableEntryIdVar,
11381135 TypeTableEntryIdMetaType,
11391136 TypeTableEntryIdVoid,
11401137 TypeTableEntryIdBool,
src/analyze.cpp+18-28
......@@ -200,7 +200,6 @@ static uint8_t bits_needed_for_unsigned(uint64_t x) {
200200bool type_is_complete(TypeTableEntry *type_entry) {
201201 switch (type_entry->id) {
202202 case TypeTableEntryIdInvalid:
203 case TypeTableEntryIdVar:
204203 zig_unreachable();
205204 case TypeTableEntryIdStruct:
206205 return type_entry->data.structure.complete;
......@@ -239,7 +238,6 @@ bool type_is_complete(TypeTableEntry *type_entry) {
239238bool type_has_zero_bits_known(TypeTableEntry *type_entry) {
240239 switch (type_entry->id) {
241240 case TypeTableEntryIdInvalid:
242 case TypeTableEntryIdVar:
243241 zig_unreachable();
244242 case TypeTableEntryIdStruct:
245243 return type_entry->data.structure.zero_bits_known;
......@@ -1281,7 +1279,6 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf **
12811279static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {
12821280 switch (type_entry->id) {
12831281 case TypeTableEntryIdInvalid:
1284 case TypeTableEntryIdVar:
12851282 zig_unreachable();
12861283 case TypeTableEntryIdMetaType:
12871284 case TypeTableEntryIdUnreachable:
......@@ -1324,7 +1321,6 @@ static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {
13241321static bool type_allowed_in_extern(CodeGen *g, TypeTableEntry *type_entry) {
13251322 switch (type_entry->id) {
13261323 case TypeTableEntryIdInvalid:
1327 case TypeTableEntryIdVar:
13281324 zig_unreachable();
13291325 case TypeTableEntryIdMetaType:
13301326 case TypeTableEntryIdNumLitFloat:
......@@ -1428,6 +1424,14 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
14281424 calling_convention_name(fn_type_id.cc)));
14291425 return g->builtin_types.entry_invalid;
14301426 }
1427 } else if (param_node->data.param_decl.var_token != nullptr) {
1428 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
1429 add_node_error(g, param_node->data.param_decl.type,
1430 buf_sprintf("parameter of type 'var' not allowed in function with calling convention '%s'",
1431 calling_convention_name(fn_type_id.cc)));
1432 return g->builtin_types.entry_invalid;
1433 }
1434 return get_generic_fn_type(g, &fn_type_id);
14311435 }
14321436
14331437 TypeTableEntry *type_entry = analyze_type_expr(g, child_scope, param_node->data.param_decl.type);
......@@ -1463,14 +1467,6 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
14631467 add_node_error(g, param_node->data.param_decl.type,
14641468 buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&type_entry->name)));
14651469 return g->builtin_types.entry_invalid;
1466 case TypeTableEntryIdVar:
1467 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
1468 add_node_error(g, param_node->data.param_decl.type,
1469 buf_sprintf("parameter of type 'var' not allowed in function with calling convention '%s'",
1470 calling_convention_name(fn_type_id.cc)));
1471 return g->builtin_types.entry_invalid;
1472 }
1473 return get_generic_fn_type(g, &fn_type_id);
14741470 case TypeTableEntryIdNumLitFloat:
14751471 case TypeTableEntryIdNumLitInt:
14761472 case TypeTableEntryIdNamespace:
......@@ -1527,6 +1523,16 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
15271523 fn_type_id.return_type = specified_return_type;
15281524 }
15291525
1526 if (fn_proto->return_var_token != nullptr) {
1527 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
1528 add_node_error(g, fn_proto->return_type,
1529 buf_sprintf("return type 'var' not allowed in function with calling convention '%s'",
1530 calling_convention_name(fn_type_id.cc)));
1531 return g->builtin_types.entry_invalid;
1532 }
1533 return get_generic_fn_type(g, &fn_type_id);
1534 }
1535
15301536 if (!calling_convention_allows_zig_types(fn_type_id.cc) && !type_allowed_in_extern(g, fn_type_id.return_type)) {
15311537 add_node_error(g, fn_proto->return_type,
15321538 buf_sprintf("return type '%s' not allowed in function with calling convention '%s'",
......@@ -1552,7 +1558,6 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
15521558 case TypeTableEntryIdNamespace:
15531559 case TypeTableEntryIdBlock:
15541560 case TypeTableEntryIdBoundFn:
1555 case TypeTableEntryIdVar:
15561561 case TypeTableEntryIdMetaType:
15571562 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
15581563 add_node_error(g, fn_proto->return_type,
......@@ -3226,7 +3231,6 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
32263231 case NodeTypeStructValueField:
32273232 case NodeTypeArrayType:
32283233 case NodeTypeErrorType:
3229 case NodeTypeVarLiteral:
32303234 case NodeTypeIfErrorExpr:
32313235 case NodeTypeTestExpr:
32323236 case NodeTypeErrorSetDecl:
......@@ -3262,7 +3266,6 @@ TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEnt
32623266 case TypeTableEntryIdInvalid:
32633267 return g->builtin_types.entry_invalid;
32643268 case TypeTableEntryIdUnreachable:
3265 case TypeTableEntryIdVar:
32663269 case TypeTableEntryIdNumLitFloat:
32673270 case TypeTableEntryIdNumLitInt:
32683271 case TypeTableEntryIdUndefLit:
......@@ -3641,7 +3644,6 @@ TypeEnumField *find_enum_field_by_tag(TypeTableEntry *enum_type, const BigInt *t
36413644static bool is_container(TypeTableEntry *type_entry) {
36423645 switch (type_entry->id) {
36433646 case TypeTableEntryIdInvalid:
3644 case TypeTableEntryIdVar:
36453647 zig_unreachable();
36463648 case TypeTableEntryIdStruct:
36473649 case TypeTableEntryIdEnum:
......@@ -3716,7 +3718,6 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) {
37163718 case TypeTableEntryIdBlock:
37173719 case TypeTableEntryIdBoundFn:
37183720 case TypeTableEntryIdInvalid:
3719 case TypeTableEntryIdVar:
37203721 case TypeTableEntryIdArgTuple:
37213722 case TypeTableEntryIdOpaque:
37223723 case TypeTableEntryIdPromise:
......@@ -4216,7 +4217,6 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
42164217 case TypeTableEntryIdNamespace:
42174218 case TypeTableEntryIdBlock:
42184219 case TypeTableEntryIdBoundFn:
4219 case TypeTableEntryIdVar:
42204220 case TypeTableEntryIdArgTuple:
42214221 case TypeTableEntryIdOpaque:
42224222 zig_unreachable();
......@@ -4515,7 +4515,6 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
45154515 case TypeTableEntryIdBoundFn:
45164516 case TypeTableEntryIdInvalid:
45174517 case TypeTableEntryIdUnreachable:
4518 case TypeTableEntryIdVar:
45194518 zig_unreachable();
45204519 }
45214520 zig_unreachable();
......@@ -4613,7 +4612,6 @@ bool type_has_bits(TypeTableEntry *type_entry) {
46134612bool type_requires_comptime(TypeTableEntry *type_entry) {
46144613 switch (type_entry->id) {
46154614 case TypeTableEntryIdInvalid:
4616 case TypeTableEntryIdVar:
46174615 case TypeTableEntryIdOpaque:
46184616 zig_unreachable();
46194617 case TypeTableEntryIdNumLitFloat:
......@@ -5109,7 +5107,6 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
51095107 case TypeTableEntryIdBoundFn:
51105108 case TypeTableEntryIdInvalid:
51115109 case TypeTableEntryIdUnreachable:
5112 case TypeTableEntryIdVar:
51135110 case TypeTableEntryIdPromise:
51145111 zig_unreachable();
51155112 }
......@@ -5189,9 +5186,6 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
51895186 case TypeTableEntryIdInvalid:
51905187 buf_appendf(buf, "(invalid)");
51915188 return;
5192 case TypeTableEntryIdVar:
5193 buf_appendf(buf, "(var)");
5194 return;
51955189 case TypeTableEntryIdVoid:
51965190 buf_appendf(buf, "{}");
51975191 return;
......@@ -5427,7 +5421,6 @@ TypeTableEntry *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits)
54275421uint32_t type_id_hash(TypeId x) {
54285422 switch (x.id) {
54295423 case TypeTableEntryIdInvalid:
5430 case TypeTableEntryIdVar:
54315424 case TypeTableEntryIdOpaque:
54325425 case TypeTableEntryIdMetaType:
54335426 case TypeTableEntryIdVoid:
......@@ -5474,7 +5467,6 @@ bool type_id_eql(TypeId a, TypeId b) {
54745467 return false;
54755468 switch (a.id) {
54765469 case TypeTableEntryIdInvalid:
5477 case TypeTableEntryIdVar:
54785470 case TypeTableEntryIdMetaType:
54795471 case TypeTableEntryIdVoid:
54805472 case TypeTableEntryIdBool:
......@@ -5629,7 +5621,6 @@ size_t type_id_len() {
56295621size_t type_id_index(TypeTableEntryId id) {
56305622 switch (id) {
56315623 case TypeTableEntryIdInvalid:
5632 case TypeTableEntryIdVar:
56335624 zig_unreachable();
56345625 case TypeTableEntryIdMetaType:
56355626 return 0;
......@@ -5688,7 +5679,6 @@ size_t type_id_index(TypeTableEntryId id) {
56885679const char *type_id_name(TypeTableEntryId id) {
56895680 switch (id) {
56905681 case TypeTableEntryIdInvalid:
5691 case TypeTableEntryIdVar:
56925682 zig_unreachable();
56935683 case TypeTableEntryIdMetaType:
56945684 return "Type";
src/ast_render.cpp+12-11
......@@ -236,8 +236,6 @@ static const char *node_type_str(NodeType node_type) {
236236 return "ArrayType";
237237 case NodeTypeErrorType:
238238 return "ErrorType";
239 case NodeTypeVarLiteral:
240 return "VarLiteral";
241239 case NodeTypeIfErrorExpr:
242240 return "IfErrorExpr";
243241 case NodeTypeTestExpr:
......@@ -436,6 +434,8 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
436434 }
437435 if (param_decl->data.param_decl.is_var_args) {
438436 fprintf(ar->f, "...");
437 } else if (param_decl->data.param_decl.var_token != nullptr) {
438 fprintf(ar->f, "var");
439439 } else {
440440 render_node_grouped(ar, param_decl->data.param_decl.type);
441441 }
......@@ -456,13 +456,17 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
456456 fprintf(ar->f, ")");
457457 }
458458
459 AstNode *return_type_node = node->data.fn_proto.return_type;
460 assert(return_type_node != nullptr);
461 fprintf(ar->f, " ");
462 if (node->data.fn_proto.auto_err_set) {
463 fprintf(ar->f, "!");
459 if (node->data.fn_proto.return_var_token != nullptr) {
460 fprintf(ar->f, "var");
461 } else {
462 AstNode *return_type_node = node->data.fn_proto.return_type;
463 assert(return_type_node != nullptr);
464 fprintf(ar->f, " ");
465 if (node->data.fn_proto.auto_err_set) {
466 fprintf(ar->f, "!");
467 }
468 render_node_grouped(ar, return_type_node);
464469 }
465 render_node_grouped(ar, return_type_node);
466470 break;
467471 }
468472 case NodeTypeFnDef:
......@@ -768,9 +772,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
768772 case NodeTypeErrorType:
769773 fprintf(ar->f, "error");
770774 break;
771 case NodeTypeVarLiteral:
772 fprintf(ar->f, "var");
773 break;
774775 case NodeTypeAsmExpr:
775776 {
776777 AstNodeAsmExpr *asm_expr = &node->data.asm_expr;
src/codegen.cpp-10
......@@ -4508,7 +4508,6 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
45084508 assert(!type_entry->zero_bits);
45094509 switch (type_entry->id) {
45104510 case TypeTableEntryIdInvalid:
4511 case TypeTableEntryIdVar:
45124511 case TypeTableEntryIdMetaType:
45134512 case TypeTableEntryIdUnreachable:
45144513 case TypeTableEntryIdNumLitFloat:
......@@ -4960,7 +4959,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
49604959 case TypeTableEntryIdNamespace:
49614960 case TypeTableEntryIdBlock:
49624961 case TypeTableEntryIdBoundFn:
4963 case TypeTableEntryIdVar:
49644962 case TypeTableEntryIdArgTuple:
49654963 case TypeTableEntryIdOpaque:
49664964 case TypeTableEntryIdPromise:
......@@ -5611,11 +5609,6 @@ static void define_builtin_types(CodeGen *g) {
56115609 entry->zero_bits = true;
56125610 g->builtin_types.entry_null = entry;
56135611 }
5614 {
5615 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdVar);
5616 buf_init_from_str(&entry->name, "(var)");
5617 g->builtin_types.entry_var = entry;
5618 }
56195612 {
56205613 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdArgTuple);
56215614 buf_init_from_str(&entry->name, "(args)");
......@@ -6444,7 +6437,6 @@ static void prepend_c_type_to_decl_list(CodeGen *g, GenH *gen_h, TypeTableEntry
64446437
64456438 switch (type_entry->id) {
64466439 case TypeTableEntryIdInvalid:
6447 case TypeTableEntryIdVar:
64486440 case TypeTableEntryIdMetaType:
64496441 case TypeTableEntryIdNumLitFloat:
64506442 case TypeTableEntryIdNumLitInt:
......@@ -6639,7 +6631,6 @@ static void get_c_type(CodeGen *g, GenH *gen_h, TypeTableEntry *type_entry, Buf
66396631 case TypeTableEntryIdNumLitInt:
66406632 case TypeTableEntryIdUndefLit:
66416633 case TypeTableEntryIdNullLit:
6642 case TypeTableEntryIdVar:
66436634 case TypeTableEntryIdArgTuple:
66446635 case TypeTableEntryIdPromise:
66456636 zig_unreachable();
......@@ -6781,7 +6772,6 @@ static void gen_h_file(CodeGen *g) {
67816772 TypeTableEntry *type_entry = gen_h->types_to_declare.at(type_i);
67826773 switch (type_entry->id) {
67836774 case TypeTableEntryIdInvalid:
6784 case TypeTableEntryIdVar:
67856775 case TypeTableEntryIdMetaType:
67866776 case TypeTableEntryIdVoid:
67876777 case TypeTableEntryIdBool:
src/ir.cpp+50-72
......@@ -2147,7 +2147,7 @@ static IrInstruction *ir_build_fn_proto(IrBuilder *irb, Scope *scope, AstNode *s
21472147 size_t param_count = source_node->data.fn_proto.params.length;
21482148 if (is_var_args) param_count -= 1;
21492149 for (size_t i = 0; i < param_count; i += 1) {
2150 ir_ref_instruction(param_types[i], irb->current_basic_block);
2150 if (param_types[i] != nullptr) ir_ref_instruction(param_types[i], irb->current_basic_block);
21512151 }
21522152 if (align_value != nullptr) ir_ref_instruction(align_value, irb->current_basic_block);
21532153 ir_ref_instruction(return_type, irb->current_basic_block);
......@@ -3305,12 +3305,6 @@ static IrInstruction *ir_gen_null_literal(IrBuilder *irb, Scope *scope, AstNode
33053305 return ir_build_const_null(irb, scope, node);
33063306}
33073307
3308static IrInstruction *ir_gen_var_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
3309 assert(node->type == NodeTypeVarLiteral);
3310
3311 return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_var);
3312}
3313
33143308static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
33153309 assert(node->type == NodeTypeSymbol);
33163310
......@@ -5916,11 +5910,15 @@ static IrInstruction *ir_gen_fn_proto(IrBuilder *irb, Scope *parent_scope, AstNo
59165910 is_var_args = true;
59175911 break;
59185912 }
5919 AstNode *type_node = param_node->data.param_decl.type;
5920 IrInstruction *type_value = ir_gen_node(irb, type_node, parent_scope);
5921 if (type_value == irb->codegen->invalid_instruction)
5922 return irb->codegen->invalid_instruction;
5923 param_types[i] = type_value;
5913 if (param_node->data.param_decl.var_token == nullptr) {
5914 AstNode *type_node = param_node->data.param_decl.type;
5915 IrInstruction *type_value = ir_gen_node(irb, type_node, parent_scope);
5916 if (type_value == irb->codegen->invalid_instruction)
5917 return irb->codegen->invalid_instruction;
5918 param_types[i] = type_value;
5919 } else {
5920 param_types[i] = nullptr;
5921 }
59245922 }
59255923
59265924 IrInstruction *align_value = nullptr;
......@@ -5931,12 +5929,16 @@ static IrInstruction *ir_gen_fn_proto(IrBuilder *irb, Scope *parent_scope, AstNo
59315929 }
59325930
59335931 IrInstruction *return_type;
5934 if (node->data.fn_proto.return_type == nullptr) {
5935 return_type = ir_build_const_type(irb, parent_scope, node, irb->codegen->builtin_types.entry_void);
5932 if (node->data.fn_proto.return_var_token == nullptr) {
5933 if (node->data.fn_proto.return_type == nullptr) {
5934 return_type = ir_build_const_type(irb, parent_scope, node, irb->codegen->builtin_types.entry_void);
5935 } else {
5936 return_type = ir_gen_node(irb, node->data.fn_proto.return_type, parent_scope);
5937 if (return_type == irb->codegen->invalid_instruction)
5938 return irb->codegen->invalid_instruction;
5939 }
59365940 } else {
5937 return_type = ir_gen_node(irb, node->data.fn_proto.return_type, parent_scope);
5938 if (return_type == irb->codegen->invalid_instruction)
5939 return irb->codegen->invalid_instruction;
5941 return_type = nullptr;
59405942 }
59415943
59425944 return ir_build_fn_proto(irb, parent_scope, node, param_types, align_value, return_type, is_var_args);
......@@ -6189,8 +6191,6 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
61896191 return ir_lval_wrap(irb, scope, ir_gen_asm_expr(irb, scope, node), lval);
61906192 case NodeTypeNullLiteral:
61916193 return ir_lval_wrap(irb, scope, ir_gen_null_literal(irb, scope, node), lval);
6192 case NodeTypeVarLiteral:
6193 return ir_lval_wrap(irb, scope, ir_gen_var_literal(irb, scope, node), lval);
61946194 case NodeTypeIfErrorExpr:
61956195 return ir_lval_wrap(irb, scope, ir_gen_if_err_expr(irb, scope, node), lval);
61966196 case NodeTypeTestExpr:
......@@ -7515,11 +7515,6 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
75157515 return ImplicitCastMatchResultReportedError;
75167516 }
75177517
7518 // implicit conversion from anything to var
7519 if (expected_type->id == TypeTableEntryIdVar) {
7520 return ImplicitCastMatchResultYes;
7521 }
7522
75237518 // implicit conversion from non maybe type to maybe type
75247519 if (expected_type->id == TypeTableEntryIdMaybe &&
75257520 ir_types_match_with_implicit_cast(ira, expected_type->data.maybe.child_type, actual_type, value))
......@@ -9341,9 +9336,6 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
93419336 return ira->codegen->invalid_instruction;
93429337 }
93439338
9344 if (wanted_type->id == TypeTableEntryIdVar)
9345 return value;
9346
93479339 // explicit match or non-const to const
93489340 if (types_match_const_cast_only(ira, wanted_type, actual_type, source_node).id == ConstCastResultIdOk) {
93499341 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpNoop, false);
......@@ -10311,9 +10303,6 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
1031110303 ir_add_error_node(ira, source_node,
1031210304 buf_sprintf("operator not allowed for type '%s'", buf_ptr(&resolved_type->name)));
1031310305 return ira->codegen->builtin_types.entry_invalid;
10314
10315 case TypeTableEntryIdVar:
10316 zig_unreachable();
1031710306 }
1031810307
1031910308 IrInstruction *casted_op1 = ir_implicit_cast(ira, op1, resolved_type);
......@@ -11106,7 +11095,6 @@ static VarClassRequired get_var_class_required(TypeTableEntry *type_entry) {
1110611095 case TypeTableEntryIdInvalid:
1110711096 zig_unreachable();
1110811097 case TypeTableEntryIdUnreachable:
11109 case TypeTableEntryIdVar:
1111011098 return VarClassRequiredIllegal;
1111111099 case TypeTableEntryIdBool:
1111211100 case TypeTableEntryIdInt:
......@@ -11279,7 +11267,6 @@ static TypeTableEntry *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructi
1127911267
1128011268 switch (target->value.type->id) {
1128111269 case TypeTableEntryIdInvalid:
11282 case TypeTableEntryIdVar:
1128311270 case TypeTableEntryIdUnreachable:
1128411271 zig_unreachable();
1128511272 case TypeTableEntryIdFn: {
......@@ -11332,7 +11319,6 @@ static TypeTableEntry *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructi
1133211319 TypeTableEntry *type_value = target->value.data.x_type;
1133311320 switch (type_value->id) {
1133411321 case TypeTableEntryIdInvalid:
11335 case TypeTableEntryIdVar:
1133611322 zig_unreachable();
1133711323 case TypeTableEntryIdStruct:
1133811324 if (is_slice(type_value)) {
......@@ -11543,14 +11529,20 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node
1154311529{
1154411530 AstNode *param_decl_node = fn_proto_node->data.fn_proto.params.at(*next_proto_i);
1154511531 assert(param_decl_node->type == NodeTypeParamDecl);
11546 AstNode *param_type_node = param_decl_node->data.param_decl.type;
11547 TypeTableEntry *param_type = analyze_type_expr(ira->codegen, *exec_scope, param_type_node);
11548 if (type_is_invalid(param_type))
11549 return false;
1155011532
11551 IrInstruction *casted_arg = ir_implicit_cast(ira, arg, param_type);
11552 if (type_is_invalid(casted_arg->value.type))
11553 return false;
11533 IrInstruction *casted_arg;
11534 if (param_decl_node->data.param_decl.var_token == nullptr) {
11535 AstNode *param_type_node = param_decl_node->data.param_decl.type;
11536 TypeTableEntry *param_type = analyze_type_expr(ira->codegen, *exec_scope, param_type_node);
11537 if (type_is_invalid(param_type))
11538 return false;
11539
11540 casted_arg = ir_implicit_cast(ira, arg, param_type);
11541 if (type_is_invalid(casted_arg->value.type))
11542 return false;
11543 } else {
11544 casted_arg = arg;
11545 }
1155411546
1155511547 ConstExprValue *arg_val = ir_resolve_const(ira, casted_arg, UndefBad);
1155611548 if (!arg_val)
......@@ -11579,19 +11571,18 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1157911571 arg_part_of_generic_id = true;
1158011572 casted_arg = ir_implicit_byval_const_ref_cast(ira, arg);
1158111573 } else {
11582 AstNode *param_type_node = param_decl_node->data.param_decl.type;
11583 TypeTableEntry *param_type = analyze_type_expr(ira->codegen, *child_scope, param_type_node);
11584 if (type_is_invalid(param_type))
11585 return false;
11574 if (param_decl_node->data.param_decl.var_token == nullptr) {
11575 AstNode *param_type_node = param_decl_node->data.param_decl.type;
11576 TypeTableEntry *param_type = analyze_type_expr(ira->codegen, *child_scope, param_type_node);
11577 if (type_is_invalid(param_type))
11578 return false;
1158611579
11587 bool is_var_type = (param_type->id == TypeTableEntryIdVar);
11588 if (is_var_type) {
11589 arg_part_of_generic_id = true;
11590 casted_arg = ir_implicit_byval_const_ref_cast(ira, arg);
11591 } else {
1159211580 casted_arg = ir_implicit_cast(ira, arg, param_type);
1159311581 if (type_is_invalid(casted_arg->value.type))
1159411582 return false;
11583 } else {
11584 arg_part_of_generic_id = true;
11585 casted_arg = ir_implicit_byval_const_ref_cast(ira, arg);
1159511586 }
1159611587 }
1159711588
......@@ -12304,7 +12295,6 @@ static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op
1230412295 return ira->codegen->builtin_types.entry_invalid;
1230512296 switch (type_entry->id) {
1230612297 case TypeTableEntryIdInvalid:
12307 case TypeTableEntryIdVar:
1230812298 zig_unreachable();
1230912299 case TypeTableEntryIdMetaType:
1231012300 case TypeTableEntryIdVoid:
......@@ -13539,10 +13529,6 @@ static TypeTableEntry *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructi
1353913529 switch (type_entry->id) {
1354013530 case TypeTableEntryIdInvalid:
1354113531 zig_unreachable(); // handled above
13542 case TypeTableEntryIdVar:
13543 ir_add_error_node(ira, expr_value->source_node,
13544 buf_sprintf("type '%s' not eligible for @typeOf", buf_ptr(&type_entry->name)));
13545 return ira->codegen->builtin_types.entry_invalid;
1354613532 case TypeTableEntryIdNumLitFloat:
1354713533 case TypeTableEntryIdNumLitInt:
1354813534 case TypeTableEntryIdUndefLit:
......@@ -13807,7 +13793,6 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1380713793 switch (child_type->id) {
1380813794 case TypeTableEntryIdInvalid: // handled above
1380913795 zig_unreachable();
13810 case TypeTableEntryIdVar:
1381113796 case TypeTableEntryIdUnreachable:
1381213797 case TypeTableEntryIdUndefLit:
1381313798 case TypeTableEntryIdNullLit:
......@@ -13916,7 +13901,6 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,
1391613901 switch (child_type->id) {
1391713902 case TypeTableEntryIdInvalid: // handled above
1391813903 zig_unreachable();
13919 case TypeTableEntryIdVar:
1392013904 case TypeTableEntryIdUnreachable:
1392113905 case TypeTableEntryIdUndefLit:
1392213906 case TypeTableEntryIdNullLit:
......@@ -13968,7 +13952,6 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
1396813952 switch (type_entry->id) {
1396913953 case TypeTableEntryIdInvalid: // handled above
1397013954 zig_unreachable();
13971 case TypeTableEntryIdVar:
1397213955 case TypeTableEntryIdUnreachable:
1397313956 case TypeTableEntryIdUndefLit:
1397413957 case TypeTableEntryIdNullLit:
......@@ -14316,7 +14299,6 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1431614299
1431714300 switch (target_type->id) {
1431814301 case TypeTableEntryIdInvalid:
14319 case TypeTableEntryIdVar:
1432014302 zig_unreachable();
1432114303 case TypeTableEntryIdMetaType:
1432214304 case TypeTableEntryIdVoid:
......@@ -14911,7 +14893,6 @@ static TypeTableEntry *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_
1491114893 }
1491214894 case TypeTableEntryIdEnum:
1491314895 zig_panic("TODO min/max value for enum type");
14914 case TypeTableEntryIdVar:
1491514896 case TypeTableEntryIdMetaType:
1491614897 case TypeTableEntryIdUnreachable:
1491714898 case TypeTableEntryIdPointer:
......@@ -16159,7 +16140,6 @@ static TypeTableEntry *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruc
1615916140
1616016141 switch (type_entry->id) {
1616116142 case TypeTableEntryIdInvalid:
16162 case TypeTableEntryIdVar:
1616316143 zig_unreachable();
1616416144 case TypeTableEntryIdMetaType:
1616516145 case TypeTableEntryIdUnreachable:
......@@ -16461,21 +16441,23 @@ static TypeTableEntry *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruc
1646116441 zig_unreachable();
1646216442 }
1646316443 }
16464 IrInstruction *param_type_value = instruction->param_types[fn_type_id.next_param_index]->other;
16465 if (type_is_invalid(param_type_value->value.type))
16466 return ira->codegen->builtin_types.entry_invalid;
16467
1646816444 FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index];
1646916445 param_info->is_noalias = param_node->data.param_decl.is_noalias;
16470 param_info->type = ir_resolve_type(ira, param_type_value);
16471 if (type_is_invalid(param_info->type))
16472 return ira->codegen->builtin_types.entry_invalid;
1647316446
16474 if (param_info->type->id == TypeTableEntryIdVar) {
16447 if (instruction->param_types[fn_type_id.next_param_index] == nullptr) {
16448 param_info->type = nullptr;
1647516449 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1647616450 out_val->data.x_type = get_generic_fn_type(ira->codegen, &fn_type_id);
1647716451 return ira->codegen->builtin_types.entry_type;
16452 } else {
16453 IrInstruction *param_type_value = instruction->param_types[fn_type_id.next_param_index]->other;
16454 if (type_is_invalid(param_type_value->value.type))
16455 return ira->codegen->builtin_types.entry_invalid;
16456 param_info->type = ir_resolve_type(ira, param_type_value);
16457 if (type_is_invalid(param_info->type))
16458 return ira->codegen->builtin_types.entry_invalid;
1647816459 }
16460
1647916461 }
1648016462
1648116463 if (instruction->align_value != nullptr) {
......@@ -16869,7 +16851,6 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
1686916851 assert(val->special == ConstValSpecialStatic);
1687016852 switch (val->type->id) {
1687116853 case TypeTableEntryIdInvalid:
16872 case TypeTableEntryIdVar:
1687316854 case TypeTableEntryIdMetaType:
1687416855 case TypeTableEntryIdOpaque:
1687516856 case TypeTableEntryIdBoundFn:
......@@ -16937,7 +16918,6 @@ static void buf_read_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
1693716918 assert(val->special == ConstValSpecialStatic);
1693816919 switch (val->type->id) {
1693916920 case TypeTableEntryIdInvalid:
16940 case TypeTableEntryIdVar:
1694116921 case TypeTableEntryIdMetaType:
1694216922 case TypeTableEntryIdOpaque:
1694316923 case TypeTableEntryIdBoundFn:
......@@ -17014,7 +16994,6 @@ static TypeTableEntry *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstruc
1701416994
1701516995 switch (src_type->id) {
1701616996 case TypeTableEntryIdInvalid:
17017 case TypeTableEntryIdVar:
1701816997 case TypeTableEntryIdMetaType:
1701916998 case TypeTableEntryIdOpaque:
1702016999 case TypeTableEntryIdBoundFn:
......@@ -17041,7 +17020,6 @@ static TypeTableEntry *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstruc
1704117020
1704217021 switch (dest_type->id) {
1704317022 case TypeTableEntryIdInvalid:
17044 case TypeTableEntryIdVar:
1704517023 case TypeTableEntryIdMetaType:
1704617024 case TypeTableEntryIdOpaque:
1704717025 case TypeTableEntryIdBoundFn:
src/parser.cpp+23-24
......@@ -263,21 +263,14 @@ static AstNode *ast_parse_error_set_expr(ParseContext *pc, size_t *token_index,
263263}
264264
265265/*
266TypeExpr = ErrorSetExpr | "var"
266TypeExpr = ErrorSetExpr
267267*/
268268static AstNode *ast_parse_type_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
269 Token *token = &pc->tokens->at(*token_index);
270 if (token->id == TokenIdKeywordVar) {
271 AstNode *node = ast_create_node(pc, NodeTypeVarLiteral, token);
272 *token_index += 1;
273 return node;
274 } else {
275 return ast_parse_error_set_expr(pc, token_index, mandatory);
276 }
269 return ast_parse_error_set_expr(pc, token_index, mandatory);
277270}
278271
279272/*
280ParamDecl = option("noalias" | "comptime") option(Symbol ":") (TypeExpr | "...")
273ParamDecl = option("noalias" | "comptime") option(Symbol ":") (TypeExpr | "var" | "...")
281274*/
282275static AstNode *ast_parse_param_decl(ParseContext *pc, size_t *token_index) {
283276 Token *token = &pc->tokens->at(*token_index);
......@@ -308,6 +301,9 @@ static AstNode *ast_parse_param_decl(ParseContext *pc, size_t *token_index) {
308301 if (ellipsis_tok->id == TokenIdEllipsis3) {
309302 *token_index += 1;
310303 node->data.param_decl.is_var_args = true;
304 } else if (ellipsis_tok->id == TokenIdKeywordVar) {
305 *token_index += 1;
306 node->data.param_decl.var_token = ellipsis_tok;
311307 } else {
312308 node->data.param_decl.type = ast_parse_type_expr(pc, token_index, true);
313309 }
......@@ -2421,7 +2417,7 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand
24212417}
24222418
24232419/*
2424FnProto = option("nakedcc" | "stdcallcc" | "extern" | ("async" option("(" Expression ")"))) "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr
2420FnProto = option("nakedcc" | "stdcallcc" | "extern" | ("async" option("(" Expression ")"))) "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") (TypeExpr | "var")
24252421*/
24262422static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool mandatory, VisibMod visib_mod) {
24272423 Token *first_token = &pc->tokens->at(*token_index);
......@@ -2507,19 +2503,25 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool m
25072503 ast_eat_token(pc, token_index, TokenIdRParen);
25082504 next_token = &pc->tokens->at(*token_index);
25092505 }
2510 if (next_token->id == TokenIdKeywordError) {
2511 Token *maybe_lbrace_tok = &pc->tokens->at(*token_index + 1);
2512 if (maybe_lbrace_tok->id == TokenIdLBrace) {
2513 *token_index += 1;
2514 node->data.fn_proto.return_type = ast_create_node(pc, NodeTypeErrorType, next_token);
2515 return node;
2516 }
2517 } else if (next_token->id == TokenIdBang) {
2506 if (next_token->id == TokenIdKeywordVar) {
2507 node->data.fn_proto.return_var_token = next_token;
25182508 *token_index += 1;
2519 node->data.fn_proto.auto_err_set = true;
25202509 next_token = &pc->tokens->at(*token_index);
2510 } else {
2511 if (next_token->id == TokenIdKeywordError) {
2512 Token *maybe_lbrace_tok = &pc->tokens->at(*token_index + 1);
2513 if (maybe_lbrace_tok->id == TokenIdLBrace) {
2514 *token_index += 1;
2515 node->data.fn_proto.return_type = ast_create_node(pc, NodeTypeErrorType, next_token);
2516 return node;
2517 }
2518 } else if (next_token->id == TokenIdBang) {
2519 *token_index += 1;
2520 node->data.fn_proto.auto_err_set = true;
2521 next_token = &pc->tokens->at(*token_index);
2522 }
2523 node->data.fn_proto.return_type = ast_parse_type_expr(pc, token_index, true);
25212524 }
2522 node->data.fn_proto.return_type = ast_parse_type_expr(pc, token_index, true);
25232525
25242526 return node;
25252527}
......@@ -3069,9 +3071,6 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
30693071 case NodeTypeErrorType:
30703072 // none
30713073 break;
3072 case NodeTypeVarLiteral:
3073 // none
3074 break;
30753074 case NodeTypeAddrOfExpr:
30763075 visit_field(&node->data.addr_of_expr.align_expr, visit, context);
30773076 visit_field(&node->data.addr_of_expr.op_expr, visit, context);
test/compile_errors.zig+7
......@@ -1,6 +1,13 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: &tests.CompileErrorContext) void {
4 cases.add("var not allowed in structs",
5 \\export fn entry() void {
6 \\ var s = (struct{v: var}){.v=i32(10)};
7 \\}
8 ,
9 ".tmp_source.zig:2:23: error: invalid token: 'var'");
10
411 cases.add("@ptrCast discards const qualifier",
512 \\export fn entry() void {
613 \\ const x: i32 = 1234;