diff --git a/src/analyze.cpp b/src/analyze.cpp index b988d663f..fb6894eba 100644 --- 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 index 431452a23..266720ccb 100644 --- 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'"); } //////////////////////////////////////////////////////////////////////////////