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...@@ -1939,6 +1939,9 @@ static void resolve_var_decl(CodeGen *g, ImportTableEntry *import, AstNode *node
1939 {1939 {
1940 add_node_error(g, node, buf_sprintf("unable to infer variable type"));1940 add_node_error(g, node, buf_sprintf("unable to infer variable type"));
1941 implicit_type = g->builtin_types.entry_invalid;1941 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;
1942 } else if (implicit_type->id == TypeTableEntryIdMetaType && !is_const) {1945 } else if (implicit_type->id == TypeTableEntryIdMetaType && !is_const) {
1943 add_node_error(g, node, buf_sprintf("variable of type 'type' must be constant"));1946 add_node_error(g, node, buf_sprintf("variable of type 'type' must be constant"));
1944 implicit_type = g->builtin_types.entry_invalid;1947 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) {...@@ -701,6 +701,11 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
701 }701 }
702 break;702 break;
703 }703 }
704 case NodeTypeNullLiteral:
705 {
706 fprintf(ar->f, "null");
707 break;
708 }
704 case NodeTypeFnDecl:709 case NodeTypeFnDecl:
705 case NodeTypeParamDecl:710 case NodeTypeParamDecl:
706 case NodeTypeErrorValueDecl:711 case NodeTypeErrorValueDecl:
...@@ -709,7 +714,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {...@@ -709,7 +714,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
709 case NodeTypeStructField:714 case NodeTypeStructField:
710 case NodeTypeStructValueField:715 case NodeTypeStructValueField:
711 case NodeTypeUse:716 case NodeTypeUse:
712 case NodeTypeNullLiteral:
713 case NodeTypeZeroesLiteral:717 case NodeTypeZeroesLiteral:
714 case NodeTypeIfVarExpr:718 case NodeTypeIfVarExpr:
715 case NodeTypeForExpr:719 case NodeTypeForExpr:
src/ir.cpp+17-2
...@@ -329,6 +329,13 @@ static IrInstruction *ir_build_const_bignum(IrBuilder *irb, AstNode *source_node...@@ -329,6 +329,13 @@ static IrInstruction *ir_build_const_bignum(IrBuilder *irb, AstNode *source_node
329 return &const_instruction->base;329 return &const_instruction->base;
330}330}
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
332static IrInstruction *ir_build_const_usize(IrBuilder *irb, AstNode *source_node, uint64_t value) {339static IrInstruction *ir_build_const_usize(IrBuilder *irb, AstNode *source_node, uint64_t value) {
333 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);340 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
334 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_usize;341 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) {...@@ -1138,6 +1145,13 @@ static IrInstruction *ir_gen_num_lit(IrBuilder *irb, AstNode *node) {
1138 return ir_build_const_bignum(irb, node, node->data.number_literal.bignum);1145 return ir_build_const_bignum(irb, node, node->data.number_literal.bignum);
1139}1146}
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
1141static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, AstNode *decl_node,1155static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, AstNode *decl_node,
1142 LValPurpose lval, BlockContext *scope)1156 LValPurpose lval, BlockContext *scope)
1143{1157{
...@@ -1895,6 +1909,8 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont...@@ -1895,6 +1909,8 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
1895 return ir_gen_undefined_literal(irb, node);1909 return ir_gen_undefined_literal(irb, node);
1896 case NodeTypeAsmExpr:1910 case NodeTypeAsmExpr:
1897 return ir_gen_asm_expr(irb, node);1911 return ir_gen_asm_expr(irb, node);
1912 case NodeTypeNullLiteral:
1913 return ir_gen_null_literal(irb, node);
1898 case NodeTypeUnwrapErrorExpr:1914 case NodeTypeUnwrapErrorExpr:
1899 case NodeTypeDefer:1915 case NodeTypeDefer:
1900 case NodeTypeSliceExpr:1916 case NodeTypeSliceExpr:
...@@ -1905,7 +1921,6 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont...@@ -1905,7 +1921,6 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
1905 case NodeTypeLabel:1921 case NodeTypeLabel:
1906 case NodeTypeSwitchExpr:1922 case NodeTypeSwitchExpr:
1907 case NodeTypeCharLiteral:1923 case NodeTypeCharLiteral:
1908 case NodeTypeNullLiteral:
1909 case NodeTypeZeroesLiteral:1924 case NodeTypeZeroesLiteral:
1910 case NodeTypeErrorType:1925 case NodeTypeErrorType:
1911 case NodeTypeTypeLiteral:1926 case NodeTypeTypeLiteral:
...@@ -3127,6 +3142,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -3127,6 +3142,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
3127 case TypeTableEntryIdUnreachable:3142 case TypeTableEntryIdUnreachable:
3128 case TypeTableEntryIdVar:3143 case TypeTableEntryIdVar:
3129 case TypeTableEntryIdBlock:3144 case TypeTableEntryIdBlock:
3145 case TypeTableEntryIdNullLit:
3130 add_node_error(ira->codegen, var_type->source_node,3146 add_node_error(ira->codegen, var_type->source_node,
3131 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name)));3147 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name)));
3132 result_type = ira->codegen->builtin_types.entry_invalid;3148 result_type = ira->codegen->builtin_types.entry_invalid;
...@@ -3140,7 +3156,6 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -3140,7 +3156,6 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
3140 }3156 }
3141 break;3157 break;
3142 case TypeTableEntryIdUndefLit:3158 case TypeTableEntryIdUndefLit:
3143 case TypeTableEntryIdNullLit:
3144 case TypeTableEntryIdVoid:3159 case TypeTableEntryIdVoid:
3145 case TypeTableEntryIdBool:3160 case TypeTableEntryIdBool:
3146 case TypeTableEntryIdInt:3161 case TypeTableEntryIdInt:
src/ir_print.cpp+5-1
...@@ -100,11 +100,15 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const...@@ -100,11 +100,15 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
100 fprintf(irp->f, "}");100 fprintf(irp->f, "}");
101 break;101 break;
102 }102 }
103 case TypeTableEntryIdNullLit:
104 {
105 fprintf(irp->f, "null");
106 break;
107 }
103 case TypeTableEntryIdVar:108 case TypeTableEntryIdVar:
104 case TypeTableEntryIdFloat:109 case TypeTableEntryIdFloat:
105 case TypeTableEntryIdStruct:110 case TypeTableEntryIdStruct:
106 case TypeTableEntryIdUndefLit:111 case TypeTableEntryIdUndefLit:
107 case TypeTableEntryIdNullLit:
108 case TypeTableEntryIdMaybe:112 case TypeTableEntryIdMaybe:
109 case TypeTableEntryIdErrorUnion:113 case TypeTableEntryIdErrorUnion:
110 case TypeTableEntryIdPureError:114 case TypeTableEntryIdPureError: