authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-19 01:53:14-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-19 01:53:14-05:00
log2f8dd46174821a12205f939cda51f0fb4765475f
tree081d54c71464abc69f238540d4d25df2b53af0d4
parent8a81f8aa1388331624e4c073e2534d3a987a7d9a

IR: error for uncasted null lit variable


4 files changed, 30 insertions(+), 4 deletions(-)

src/analyze.cpp+3
......@@ -1939,6 +1939,9 @@ static void resolve_var_decl(CodeGen *g, ImportTableEntry *import, AstNode *node
19391939 {
19401940 add_node_error(g, node, buf_sprintf("unable to infer variable type"));
19411941 implicit_type = g->builtin_types.entry_invalid;
1942 } else if (implicit_type->id == TypeTableEntryIdNullLit) {
1943 add_node_error(g, node, buf_sprintf("unable to infer variable type"));
1944 implicit_type = g->builtin_types.entry_invalid;
19421945 } else if (implicit_type->id == TypeTableEntryIdMetaType && !is_const) {
19431946 add_node_error(g, node, buf_sprintf("variable of type 'type' must be constant"));
19441947 implicit_type = g->builtin_types.entry_invalid;
src/ast_render.cpp+5-1
......@@ -701,6 +701,11 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
701701 }
702702 break;
703703 }
704 case NodeTypeNullLiteral:
705 {
706 fprintf(ar->f, "null");
707 break;
708 }
704709 case NodeTypeFnDecl:
705710 case NodeTypeParamDecl:
706711 case NodeTypeErrorValueDecl:
......@@ -709,7 +714,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
709714 case NodeTypeStructField:
710715 case NodeTypeStructValueField:
711716 case NodeTypeUse:
712 case NodeTypeNullLiteral:
713717 case NodeTypeZeroesLiteral:
714718 case NodeTypeIfVarExpr:
715719 case NodeTypeForExpr:
src/ir.cpp+17-2
......@@ -329,6 +329,13 @@ static IrInstruction *ir_build_const_bignum(IrBuilder *irb, AstNode *source_node
329329 return &const_instruction->base;
330330}
331331
332static IrInstruction *ir_build_const_null(IrBuilder *irb, AstNode *source_node) {
333 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
334 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_null;
335 const_instruction->base.static_value.special = ConstValSpecialStatic;
336 return &const_instruction->base;
337}
338
332339static IrInstruction *ir_build_const_usize(IrBuilder *irb, AstNode *source_node, uint64_t value) {
333340 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
334341 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_usize;
......@@ -1138,6 +1145,13 @@ static IrInstruction *ir_gen_num_lit(IrBuilder *irb, AstNode *node) {
11381145 return ir_build_const_bignum(irb, node, node->data.number_literal.bignum);
11391146}
11401147
1148static IrInstruction *ir_gen_null_literal(IrBuilder *irb, AstNode *node) {
1149 assert(node->type == NodeTypeNullLiteral);
1150
1151 return ir_build_const_null(irb, node);
1152}
1153
1154
11411155static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, AstNode *decl_node,
11421156 LValPurpose lval, BlockContext *scope)
11431157{
......@@ -1895,6 +1909,8 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
18951909 return ir_gen_undefined_literal(irb, node);
18961910 case NodeTypeAsmExpr:
18971911 return ir_gen_asm_expr(irb, node);
1912 case NodeTypeNullLiteral:
1913 return ir_gen_null_literal(irb, node);
18981914 case NodeTypeUnwrapErrorExpr:
18991915 case NodeTypeDefer:
19001916 case NodeTypeSliceExpr:
......@@ -1905,7 +1921,6 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
19051921 case NodeTypeLabel:
19061922 case NodeTypeSwitchExpr:
19071923 case NodeTypeCharLiteral:
1908 case NodeTypeNullLiteral:
19091924 case NodeTypeZeroesLiteral:
19101925 case NodeTypeErrorType:
19111926 case NodeTypeTypeLiteral:
......@@ -3127,6 +3142,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
31273142 case TypeTableEntryIdUnreachable:
31283143 case TypeTableEntryIdVar:
31293144 case TypeTableEntryIdBlock:
3145 case TypeTableEntryIdNullLit:
31303146 add_node_error(ira->codegen, var_type->source_node,
31313147 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name)));
31323148 result_type = ira->codegen->builtin_types.entry_invalid;
......@@ -3140,7 +3156,6 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
31403156 }
31413157 break;
31423158 case TypeTableEntryIdUndefLit:
3143 case TypeTableEntryIdNullLit:
31443159 case TypeTableEntryIdVoid:
31453160 case TypeTableEntryIdBool:
31463161 case TypeTableEntryIdInt:
src/ir_print.cpp+5-1
......@@ -100,11 +100,15 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
100100 fprintf(irp->f, "}");
101101 break;
102102 }
103 case TypeTableEntryIdNullLit:
104 {
105 fprintf(irp->f, "null");
106 break;
107 }
103108 case TypeTableEntryIdVar:
104109 case TypeTableEntryIdFloat:
105110 case TypeTableEntryIdStruct:
106111 case TypeTableEntryIdUndefLit:
107 case TypeTableEntryIdNullLit:
108112 case TypeTableEntryIdMaybe:
109113 case TypeTableEntryIdErrorUnion:
110114 case TypeTableEntryIdPureError: