authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-07-11 12:08:29+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-13 00:29:53+00:00
logdff1ac1089dd8a7cae5c167bdb4e6269c9b84bb6
treeca2a2c57fcc9d948954ba346256f8e8b22139242
parentef17af1270155e803a77955c9f37175dd09c43d4

check for invalid sentinel when creating pointer with `@Type`


2 files changed, 22 insertions(+), 1 deletions(-)

src/ir.cpp+6-1
......@@ -25915,6 +25915,11 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2591525915 {
2591625916 return ira->codegen->invalid_inst_gen->value->type;
2591725917 }
25918 if (sentinel != nullptr && (size_enum_index == BuiltinPtrSizeOne || size_enum_index == BuiltinPtrSizeC)) {
25919 ir_add_error(ira, source_instr,
25920 buf_sprintf("sentinels are only allowed on slices and unknown-length pointers"));
25921 return ira->codegen->invalid_inst_gen->value->type;
25922 }
2591825923 BigInt *bi = get_const_field_lit_int(ira, source_instr->source_node, payload, "alignment", 3);
2591925924 if (bi == nullptr)
2592025925 return ira->codegen->invalid_inst_gen->value->type;
......@@ -25948,7 +25953,7 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
2594825953 0, // host_int_bytes
2594925954 is_allowzero,
2595025955 VECTOR_INDEX_NONE, nullptr, sentinel);
25951 if (size_enum_index != 2)
25956 if (size_enum_index != BuiltinPtrSizeSlice)
2595225957 return ptr_type;
2595325958 return get_slice_type(ira->codegen, ptr_type);
2595425959 }
test/compile_errors.zig+16
......@@ -2,6 +2,22 @@ const tests = @import("tests.zig");
22const std = @import("std");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.addTest("invalid pointer with @Type",
6 \\export fn entry() void {
7 \\ _ = @Type(.{ .Pointer = .{
8 \\ .size = .One,
9 \\ .is_const = false,
10 \\ .is_volatile = false,
11 \\ .alignment = 1,
12 \\ .child = u8,
13 \\ .is_allowzero = false,
14 \\ .sentinel = 0,
15 \\ }});
16 \\}
17 , &[_][]const u8{
18 "tmp.zig:2:16: error: sentinels are only allowed on slices and unknown-length pointers",
19 });
20
521 cases.addTest("int/float conversion to comptime_int/float",
622 \\export fn foo() void {
723 \\ var a: f32 = 2;