Commit dff1ac1089

Vexu <git@vexu.eu>
2020-07-11 11:08:29
check for invalid sentinel when creating pointer with `@Type`
1 parent ef17af1
Changed files (2)
src/ir.cpp
@@ -25915,6 +25915,11 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
                 {
                     return ira->codegen->invalid_inst_gen->value->type;
                 }
+                if (sentinel != nullptr && (size_enum_index == BuiltinPtrSizeOne || size_enum_index == BuiltinPtrSizeC)) {
+                    ir_add_error(ira, source_instr,
+                        buf_sprintf("sentinels are only allowed on slices and unknown-length pointers"));
+                    return ira->codegen->invalid_inst_gen->value->type;
+                }
                 BigInt *bi = get_const_field_lit_int(ira, source_instr->source_node, payload, "alignment", 3);
                 if (bi == nullptr)
                     return ira->codegen->invalid_inst_gen->value->type;
@@ -25948,7 +25953,7 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
                     0, // host_int_bytes
                     is_allowzero,
                     VECTOR_INDEX_NONE, nullptr, sentinel);
-                if (size_enum_index != 2)
+                if (size_enum_index != BuiltinPtrSizeSlice)
                     return ptr_type;
                 return get_slice_type(ira->codegen, ptr_type);
             }
test/compile_errors.zig
@@ -2,6 +2,22 @@ const tests = @import("tests.zig");
 const std = @import("std");
 
 pub fn addCases(cases: *tests.CompileErrorContext) void {
+    cases.addTest("invalid pointer with @Type",
+        \\export fn entry() void {
+        \\    _ = @Type(.{ .Pointer = .{
+        \\        .size = .One,
+        \\        .is_const = false,
+        \\        .is_volatile = false,
+        \\        .alignment = 1,
+        \\        .child = u8,
+        \\        .is_allowzero = false,
+        \\        .sentinel = 0,
+        \\    }});
+        \\}
+    , &[_][]const u8{
+        "tmp.zig:2:16: error: sentinels are only allowed on slices and unknown-length pointers",
+    });
+
     cases.addTest("int/float conversion to comptime_int/float",
         \\export fn foo() void {
         \\    var a: f32 = 2;