Merge pull request #2147 from emekoi/fix1940

added error for implicit cast from *const T to *[1]T.
This commit is contained in:
Andrew Kelley 2019-04-01 11:35:03 -04:00 committed by GitHub
commit 3c27d9c25a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 17 deletions

View File

@ -11090,6 +11090,7 @@ static IrInstruction *ir_analyze_ptr_to_array(IrAnalyze *ira, IrInstruction *sou
Error err;
if ((err = type_resolve(ira->codegen, target->value.type->data.pointer.child_type, ResolveStatusAlignmentKnown)))
return ira->codegen->invalid_instruction;
assert((wanted_type->data.pointer.is_const && target->value.type->data.pointer.is_const) || !target->value.type->data.pointer.is_const);
wanted_type = adjust_ptr_align(ira->codegen, wanted_type, get_ptr_align(ira->codegen, target->value.type));
ZigType *array_type = wanted_type->data.pointer.child_type;
assert(array_type->id == ZigTypeIdArray);
@ -11651,7 +11652,11 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
if (array_type->id == ZigTypeIdArray && array_type->data.array.len == 1 &&
types_match_const_cast_only(ira, array_type->data.array.child_type,
actual_type->data.pointer.child_type, source_node,
!wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
!wanted_type->data.pointer.is_const).id == ConstCastResultIdOk &&
// This should be the job of `types_match_const_cast_only`
// but `types_match_const_cast_only` only gets info for child_types
((wanted_type->data.pointer.is_const && actual_type->data.pointer.is_const) ||
!actual_type->data.pointer.is_const))
{
if ((err = type_resolve(ira->codegen, wanted_type->data.pointer.child_type,
ResolveStatusAlignmentKnown)))

View File

@ -263,7 +263,7 @@ pub const Loop = struct {
.next = undefined,
};
self.available_eventfd_resume_nodes.push(eventfd_node);
const kevent_array = (*[1]posix.Kevent)(&eventfd_node.data.kevent);
const kevent_array = (*const [1]posix.Kevent)(&eventfd_node.data.kevent);
_ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null);
eventfd_node.data.kevent.flags = posix.EV_CLEAR | posix.EV_ENABLE;
eventfd_node.data.kevent.fflags = posix.NOTE_TRIGGER;
@ -279,7 +279,7 @@ pub const Loop = struct {
.data = 0,
.udata = @ptrToInt(&self.final_resume_node),
};
const final_kev_arr = (*[1]posix.Kevent)(&self.os_data.final_kevent);
const final_kev_arr = (*const [1]posix.Kevent)(&self.os_data.final_kevent);
_ = try os.bsdKEvent(self.os_data.kqfd, final_kev_arr, empty_kevs, null);
self.os_data.final_kevent.flags = posix.EV_ENABLE;
self.os_data.final_kevent.fflags = posix.NOTE_TRIGGER;
@ -472,7 +472,7 @@ pub const Loop = struct {
.data = 0,
.udata = @ptrToInt(&resume_node.base),
};
const kevent_array = (*[1]posix.Kevent)(&kev);
const kevent_array = (*const [1]posix.Kevent)(&kev);
const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
_ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null);
}
@ -486,7 +486,7 @@ pub const Loop = struct {
.data = 0,
.udata = 0,
};
const kevent_array = (*[1]posix.Kevent)(&kev);
const kevent_array = (*const [1]posix.Kevent)(&kev);
const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
_ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch undefined;
self.finishOneEvent();
@ -502,7 +502,7 @@ pub const Loop = struct {
eventfd_node.base.handle = next_tick_node.data;
switch (builtin.os) {
builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
const kevent_array = (*[1]posix.Kevent)(&eventfd_node.kevent);
const kevent_array = (*const [1]posix.Kevent)(&eventfd_node.kevent);
const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
_ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch {
self.next_tick_queue.unget(next_tick_node);
@ -631,7 +631,7 @@ pub const Loop = struct {
},
builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
self.posixFsRequest(&self.os_data.fs_end_request);
const final_kevent = (*[1]posix.Kevent)(&self.os_data.final_kevent);
const final_kevent = (*const [1]posix.Kevent)(&self.os_data.final_kevent);
const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
// cannot fail because we already added it and this just enables it
_ = os.bsdKEvent(self.os_data.kqfd, final_kevent, empty_kevs, null) catch unreachable;
@ -751,7 +751,7 @@ pub const Loop = struct {
self.os_data.fs_queue.put(request_node);
switch (builtin.os) {
builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
const fs_kevs = (*[1]posix.Kevent)(&self.os_data.fs_kevent_wake);
const fs_kevs = (*const [1]posix.Kevent)(&self.os_data.fs_kevent_wake);
const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
_ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, empty_kevs, null) catch unreachable;
},
@ -821,7 +821,7 @@ pub const Loop = struct {
}
},
builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
const fs_kevs = (*[1]posix.Kevent)(&self.os_data.fs_kevent_wait);
const fs_kevs = (*const [1]posix.Kevent)(&self.os_data.fs_kevent_wait);
var out_kevs: [1]posix.Kevent = undefined;
_ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, out_kevs[0..], null) catch unreachable;
},

View File

@ -375,7 +375,7 @@ pub fn formatAsciiChar(
comptime Errors: type,
output: fn (@typeOf(context), []const u8) Errors!void,
) Errors!void {
return output(context, (*[1]u8)(&c)[0..]);
return output(context, (*const [1]u8)(&c)[0..]);
}
pub fn formatBuf(
@ -390,7 +390,7 @@ pub fn formatBuf(
var leftover_padding = if (width > buf.len) (width - buf.len) else return;
const pad_byte: u8 = ' ';
while (leftover_padding > 0) : (leftover_padding -= 1) {
try output(context, (*[1]u8)(&pad_byte)[0..1]);
try output(context, (*const [1]u8)(&pad_byte)[0..1]);
}
}
@ -705,7 +705,7 @@ fn formatIntSigned(
const uint = @IntType(false, @typeOf(value).bit_count);
if (value < 0) {
const minus_sign: u8 = '-';
try output(context, (*[1]u8)(&minus_sign)[0..]);
try output(context, (*const [1]u8)(&minus_sign)[0..]);
const new_value = @intCast(uint, -(value + 1)) + 1;
const new_width = if (width == 0) 0 else (width - 1);
return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
@ -713,7 +713,7 @@ fn formatIntSigned(
return formatIntUnsigned(@intCast(uint, value), base, uppercase, width, context, Errors, output);
} else {
const plus_sign: u8 = '+';
try output(context, (*[1]u8)(&plus_sign)[0..]);
try output(context, (*const [1]u8)(&plus_sign)[0..]);
const new_value = @intCast(uint, value);
const new_width = if (width == 0) 0 else (width - 1);
return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
@ -751,7 +751,7 @@ fn formatIntUnsigned(
const zero_byte: u8 = '0';
var leftover_padding = padding - index;
while (true) {
try output(context, (*[1]u8)(&zero_byte)[0..]);
try output(context, (*const [1]u8)(&zero_byte)[0..]);
leftover_padding -= 1;
if (leftover_padding == 0) break;
}

View File

@ -227,12 +227,12 @@ pub fn OutStream(comptime WriteError: type) type {
}
pub fn writeByte(self: *Self, byte: u8) Error!void {
const slice = (*[1]u8)(&byte)[0..];
const slice = (*const [1]u8)(&byte)[0..];
return self.writeFn(self, slice);
}
pub fn writeByteNTimes(self: *Self, byte: u8, n: usize) Error!void {
const slice = (*[1]u8)(&byte)[0..];
const slice = (*const [1]u8)(&byte)[0..];
var i: usize = 0;
while (i < n) : (i += 1) {
try self.writeFn(self, slice);

View File

@ -5869,4 +5869,27 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ comptime testCompileLog(Bar{.X = 123});
\\}
, "tmp.zig:6:5: error: found compile log statement");
cases.add(
"attempted implicit cast from *const T to *[1]T",
\\export fn entry(byte: u8) void {
\\ const w: i32 = 1234;
\\ var x: *const i32 = &w;
\\ var y: *[1]i32 = x;
\\ y[0] += 1;
\\}
,
"tmp.zig:4:22: error: expected type '*[1]i32', found '*const i32'",
"tmp.zig:4:22: note: pointer type child 'i32' cannot cast into pointer type child '[1]i32'",
);
cases.add(
"attempted implicit cast from *const T to []T",
\\export fn entry() void {
\\ const u: u32 = 42;
\\ const x: []u32 = &u;
\\}
,
"tmp.zig:3:23: error: expected type '[]u32', found '*const u32'",
);
}

View File

@ -60,7 +60,7 @@ fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 {
test "implicitly decreasing slice alignment" {
const a: u32 align(4) = 3;
const b: u32 align(8) = 4;
expect(addUnalignedSlice((*[1]u32)(&a)[0..], (*[1]u32)(&b)[0..]) == 7);
expect(addUnalignedSlice((*const [1]u32)(&a)[0..], (*const [1]u32)(&b)[0..]) == 7);
}
fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 {
return a[0] + b[0];