commit e66ed0f2e230ad3aed9a4207997df0ab7c99c2d6 (tree)
parent 707131e37b2ee384d46c26ada83f3c83edf7278d
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Sat, 9 Apr 2016 17:35:26 -0700
add error for assigning null to non-nullable pointer
closes #133
Diffstat:
2 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/src/analyze.cpp b/src/analyze.cpp
@@ -3391,12 +3391,15 @@ static TypeTableEntry *analyze_null_literal_expr(CodeGen *g, ImportTableEntry *i
assert(node->type == NodeTypeNullLiteral);
if (!expected_type) {
- add_node_error(g, node,
- buf_sprintf("unable to determine null type"));
+ add_node_error(g, node, buf_sprintf("unable to determine null type"));
return g->builtin_types.entry_invalid;
}
- assert(expected_type->id == TypeTableEntryIdMaybe);
+ if (expected_type->id != TypeTableEntryIdMaybe) {
+ add_node_error(g, node,
+ buf_sprintf("expected maybe type, got '%s'", buf_ptr(&expected_type->name)));
+ return g->builtin_types.entry_invalid;
+ }
node->data.null_literal.resolved_struct_val_expr.type_entry = expected_type;
node->data.null_literal.resolved_struct_val_expr.source_node = node;
diff --git a/test/run_tests.cpp b/test/run_tests.cpp
@@ -1810,6 +1810,10 @@ fn derp(){}
)SOURCE", 2,
".tmp_source.zig:3:12: error: no label in scope named 'label'",
".tmp_source.zig:5:1: error: label 'label' defined but not used");
+
+ add_compile_fail_case("assign null to non-nullable pointer", R"SOURCE(
+const a: &u8 = null;
+ )SOURCE", 1, ".tmp_source.zig:2:16: error: expected maybe type, got '&u8'");
}
//////////////////////////////////////////////////////////////////////////////