commit dff1ac1089dd8a7cae5c167bdb4e6269c9b84bb6 (tree)
parent ef17af1270155e803a77955c9f37175dd09c43d4
Author: Vexu <git@vexu.eu>
Date: Sat, 11 Jul 2020 12:08:29 +0300
check for invalid sentinel when creating pointer with `@Type`
Diffstat:
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/src/ir.cpp b/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);
}
diff --git a/test/compile_errors.zig b/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;