stage1: Prevent the creation of illegal pointer types

Changing the pointer length from Unknown to Single/C now resets the
sentinel value too.

Closes #5134
This commit is contained in:
LemonBoy 2020-04-22 16:54:11 +02:00 committed by Andrew Kelley
parent 173a143dd0
commit b5e72c0148
3 changed files with 10 additions and 3 deletions

View File

@ -578,6 +578,7 @@ ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type, bool is_con
}
switch (ptr_len) {
case PtrLenSingle:
assert(sentinel == nullptr);
buf_appendf(&entry->name, "*");
break;
case PtrLenUnknown:

View File

@ -21090,7 +21090,7 @@ static ZigType *adjust_ptr_len(CodeGen *g, ZigType *ptr_type, PtrLen ptr_len) {
ptr_type->data.pointer.allow_zero,
ptr_type->data.pointer.vector_index,
ptr_type->data.pointer.inferred_struct_field,
ptr_type->data.pointer.sentinel);
(ptr_len != PtrLenUnknown) ? nullptr : ptr_type->data.pointer.sentinel);
}
static ZigType *adjust_ptr_allow_zero(CodeGen *g, ZigType *ptr_type, bool allow_zero) {

View File

@ -1,6 +1,7 @@
const std = @import("std");
const expect = std.testing.expect;
const expectError = std.testing.expectError;
const testing = std.testing;
const expect = testing.expect;
const expectError = testing.expectError;
test "dereference pointer" {
comptime testDerefPtr();
@ -331,3 +332,8 @@ test "@ptrToInt on null optional at comptime" {
comptime expect(0xf00 == @ptrToInt(pointer));
}
}
test "indexing array with sentinel returns correct type" {
var s: [:0]const u8 = "abc";
testing.expectEqualSlices(u8, "*const u8", @typeName(@TypeOf(&s[0])));
}