Commit 2f8dd46174

Andrew Kelley <superjoe30@gmail.com>
2016-11-19 07:53:14
IR: error for uncasted null lit variable
1 parent 8a81f8a
src/analyze.cpp
@@ -1939,6 +1939,9 @@ static void resolve_var_decl(CodeGen *g, ImportTableEntry *import, AstNode *node
         {
             add_node_error(g, node, buf_sprintf("unable to infer variable type"));
             implicit_type = g->builtin_types.entry_invalid;
+        } else if (implicit_type->id == TypeTableEntryIdNullLit) {
+            add_node_error(g, node, buf_sprintf("unable to infer variable type"));
+            implicit_type = g->builtin_types.entry_invalid;
         } else if (implicit_type->id == TypeTableEntryIdMetaType && !is_const) {
             add_node_error(g, node, buf_sprintf("variable of type 'type' must be constant"));
             implicit_type = g->builtin_types.entry_invalid;
src/ast_render.cpp
@@ -701,6 +701,11 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
                 }
                 break;
             }
+        case NodeTypeNullLiteral:
+            {
+                fprintf(ar->f, "null");
+                break;
+            }
         case NodeTypeFnDecl:
         case NodeTypeParamDecl:
         case NodeTypeErrorValueDecl:
@@ -709,7 +714,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
         case NodeTypeStructField:
         case NodeTypeStructValueField:
         case NodeTypeUse:
-        case NodeTypeNullLiteral:
         case NodeTypeZeroesLiteral:
         case NodeTypeIfVarExpr:
         case NodeTypeForExpr:
src/ir.cpp
@@ -329,6 +329,13 @@ static IrInstruction *ir_build_const_bignum(IrBuilder *irb, AstNode *source_node
     return &const_instruction->base;
 }
 
+static IrInstruction *ir_build_const_null(IrBuilder *irb, AstNode *source_node) {
+    IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
+    const_instruction->base.type_entry = irb->codegen->builtin_types.entry_null;
+    const_instruction->base.static_value.special = ConstValSpecialStatic;
+    return &const_instruction->base;
+}
+
 static IrInstruction *ir_build_const_usize(IrBuilder *irb, AstNode *source_node, uint64_t value) {
     IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
     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) {
     return ir_build_const_bignum(irb, node, node->data.number_literal.bignum);
 }
 
+static IrInstruction *ir_gen_null_literal(IrBuilder *irb, AstNode *node) {
+    assert(node->type == NodeTypeNullLiteral);
+
+    return ir_build_const_null(irb, node);
+}
+
+
 static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, AstNode *decl_node,
         LValPurpose lval, BlockContext *scope)
 {
@@ -1895,6 +1909,8 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
             return ir_gen_undefined_literal(irb, node);
         case NodeTypeAsmExpr:
             return ir_gen_asm_expr(irb, node);
+        case NodeTypeNullLiteral:
+            return ir_gen_null_literal(irb, node);
         case NodeTypeUnwrapErrorExpr:
         case NodeTypeDefer:
         case NodeTypeSliceExpr:
@@ -1905,7 +1921,6 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
         case NodeTypeLabel:
         case NodeTypeSwitchExpr:
         case NodeTypeCharLiteral:
-        case NodeTypeNullLiteral:
         case NodeTypeZeroesLiteral:
         case NodeTypeErrorType:
         case NodeTypeTypeLiteral:
@@ -3127,6 +3142,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
         case TypeTableEntryIdUnreachable:
         case TypeTableEntryIdVar:
         case TypeTableEntryIdBlock:
+        case TypeTableEntryIdNullLit:
             add_node_error(ira->codegen, var_type->source_node,
                 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name)));
             result_type = ira->codegen->builtin_types.entry_invalid;
@@ -3140,7 +3156,6 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
             }
             break;
         case TypeTableEntryIdUndefLit:
-        case TypeTableEntryIdNullLit:
         case TypeTableEntryIdVoid:
         case TypeTableEntryIdBool:
         case TypeTableEntryIdInt:
src/ir_print.cpp
@@ -100,11 +100,15 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
                 fprintf(irp->f, "}");
                 break;
             }
+        case TypeTableEntryIdNullLit:
+            {
+                fprintf(irp->f, "null");
+                break;
+            }
         case TypeTableEntryIdVar:
         case TypeTableEntryIdFloat:
         case TypeTableEntryIdStruct:
         case TypeTableEntryIdUndefLit:
-        case TypeTableEntryIdNullLit:
         case TypeTableEntryIdMaybe:
         case TypeTableEntryIdErrorUnion:
         case TypeTableEntryIdPureError: