authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-01 11:35:03-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-04-01 11:35:03-04:00
log3c27d9c25a836a3d28f05509a7a77cb33d428dbd
tree96030724b5b3298e8f734ada4199c7ef684fb9b4
parent3199792ade2dfcb88f6668849c54352e51fc1ae0
parentb7aa289ae45eb281968a48f21d3ecaf1fb86d830
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2147 from emekoi/fix1940

added error for implicit cast from *const T to *[1]T.

6 files changed, 45 insertions(+), 17 deletions(-)

src/ir.cpp+6-1
......@@ -11090,6 +11090,7 @@ static IrInstruction *ir_analyze_ptr_to_array(IrAnalyze *ira, IrInstruction *sou
1109011090 Error err;
1109111091 if ((err = type_resolve(ira->codegen, target->value.type->data.pointer.child_type, ResolveStatusAlignmentKnown)))
1109211092 return ira->codegen->invalid_instruction;
11093 assert((wanted_type->data.pointer.is_const && target->value.type->data.pointer.is_const) || !target->value.type->data.pointer.is_const);
1109311094 wanted_type = adjust_ptr_align(ira->codegen, wanted_type, get_ptr_align(ira->codegen, target->value.type));
1109411095 ZigType *array_type = wanted_type->data.pointer.child_type;
1109511096 assert(array_type->id == ZigTypeIdArray);
......@@ -11651,7 +11652,11 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1165111652 if (array_type->id == ZigTypeIdArray && array_type->data.array.len == 1 &&
1165211653 types_match_const_cast_only(ira, array_type->data.array.child_type,
1165311654 actual_type->data.pointer.child_type, source_node,
11654 !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
11655 !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk &&
11656 // This should be the job of `types_match_const_cast_only`
11657 // but `types_match_const_cast_only` only gets info for child_types
11658 ((wanted_type->data.pointer.is_const && actual_type->data.pointer.is_const) ||
11659 !actual_type->data.pointer.is_const))
1165511660 {
1165611661 if ((err = type_resolve(ira->codegen, wanted_type->data.pointer.child_type,
1165711662 ResolveStatusAlignmentKnown)))
std/event/loop.zig+8-8
......@@ -263,7 +263,7 @@ pub const Loop = struct {
263263 .next = undefined,
264264 };
265265 self.available_eventfd_resume_nodes.push(eventfd_node);
266 const kevent_array = (*[1]posix.Kevent)(&eventfd_node.data.kevent);
266 const kevent_array = (*const [1]posix.Kevent)(&eventfd_node.data.kevent);
267267 _ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null);
268268 eventfd_node.data.kevent.flags = posix.EV_CLEAR | posix.EV_ENABLE;
269269 eventfd_node.data.kevent.fflags = posix.NOTE_TRIGGER;
......@@ -279,7 +279,7 @@ pub const Loop = struct {
279279 .data = 0,
280280 .udata = @ptrToInt(&self.final_resume_node),
281281 };
282 const final_kev_arr = (*[1]posix.Kevent)(&self.os_data.final_kevent);
282 const final_kev_arr = (*const [1]posix.Kevent)(&self.os_data.final_kevent);
283283 _ = try os.bsdKEvent(self.os_data.kqfd, final_kev_arr, empty_kevs, null);
284284 self.os_data.final_kevent.flags = posix.EV_ENABLE;
285285 self.os_data.final_kevent.fflags = posix.NOTE_TRIGGER;
......@@ -472,7 +472,7 @@ pub const Loop = struct {
472472 .data = 0,
473473 .udata = @ptrToInt(&resume_node.base),
474474 };
475 const kevent_array = (*[1]posix.Kevent)(&kev);
475 const kevent_array = (*const [1]posix.Kevent)(&kev);
476476 const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
477477 _ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null);
478478 }
......@@ -486,7 +486,7 @@ pub const Loop = struct {
486486 .data = 0,
487487 .udata = 0,
488488 };
489 const kevent_array = (*[1]posix.Kevent)(&kev);
489 const kevent_array = (*const [1]posix.Kevent)(&kev);
490490 const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
491491 _ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch undefined;
492492 self.finishOneEvent();
......@@ -502,7 +502,7 @@ pub const Loop = struct {
502502 eventfd_node.base.handle = next_tick_node.data;
503503 switch (builtin.os) {
504504 builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
505 const kevent_array = (*[1]posix.Kevent)(&eventfd_node.kevent);
505 const kevent_array = (*const [1]posix.Kevent)(&eventfd_node.kevent);
506506 const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
507507 _ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch {
508508 self.next_tick_queue.unget(next_tick_node);
......@@ -631,7 +631,7 @@ pub const Loop = struct {
631631 },
632632 builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
633633 self.posixFsRequest(&self.os_data.fs_end_request);
634 const final_kevent = (*[1]posix.Kevent)(&self.os_data.final_kevent);
634 const final_kevent = (*const [1]posix.Kevent)(&self.os_data.final_kevent);
635635 const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
636636 // cannot fail because we already added it and this just enables it
637637 _ = os.bsdKEvent(self.os_data.kqfd, final_kevent, empty_kevs, null) catch unreachable;
......@@ -751,7 +751,7 @@ pub const Loop = struct {
751751 self.os_data.fs_queue.put(request_node);
752752 switch (builtin.os) {
753753 builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
754 const fs_kevs = (*[1]posix.Kevent)(&self.os_data.fs_kevent_wake);
754 const fs_kevs = (*const [1]posix.Kevent)(&self.os_data.fs_kevent_wake);
755755 const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
756756 _ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, empty_kevs, null) catch unreachable;
757757 },
......@@ -821,7 +821,7 @@ pub const Loop = struct {
821821 }
822822 },
823823 builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
824 const fs_kevs = (*[1]posix.Kevent)(&self.os_data.fs_kevent_wait);
824 const fs_kevs = (*const [1]posix.Kevent)(&self.os_data.fs_kevent_wait);
825825 var out_kevs: [1]posix.Kevent = undefined;
826826 _ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, out_kevs[0..], null) catch unreachable;
827827 },
std/fmt.zig+5-5
......@@ -375,7 +375,7 @@ pub fn formatAsciiChar(
375375 comptime Errors: type,
376376 output: fn (@typeOf(context), []const u8) Errors!void,
377377) Errors!void {
378 return output(context, (*[1]u8)(&c)[0..]);
378 return output(context, (*const [1]u8)(&c)[0..]);
379379}
380380
381381pub fn formatBuf(
......@@ -390,7 +390,7 @@ pub fn formatBuf(
390390 var leftover_padding = if (width > buf.len) (width - buf.len) else return;
391391 const pad_byte: u8 = ' ';
392392 while (leftover_padding > 0) : (leftover_padding -= 1) {
393 try output(context, (*[1]u8)(&pad_byte)[0..1]);
393 try output(context, (*const [1]u8)(&pad_byte)[0..1]);
394394 }
395395}
396396
......@@ -705,7 +705,7 @@ fn formatIntSigned(
705705 const uint = @IntType(false, @typeOf(value).bit_count);
706706 if (value < 0) {
707707 const minus_sign: u8 = '-';
708 try output(context, (*[1]u8)(&minus_sign)[0..]);
708 try output(context, (*const [1]u8)(&minus_sign)[0..]);
709709 const new_value = @intCast(uint, -(value + 1)) + 1;
710710 const new_width = if (width == 0) 0 else (width - 1);
711711 return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
......@@ -713,7 +713,7 @@ fn formatIntSigned(
713713 return formatIntUnsigned(@intCast(uint, value), base, uppercase, width, context, Errors, output);
714714 } else {
715715 const plus_sign: u8 = '+';
716 try output(context, (*[1]u8)(&plus_sign)[0..]);
716 try output(context, (*const [1]u8)(&plus_sign)[0..]);
717717 const new_value = @intCast(uint, value);
718718 const new_width = if (width == 0) 0 else (width - 1);
719719 return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
......@@ -751,7 +751,7 @@ fn formatIntUnsigned(
751751 const zero_byte: u8 = '0';
752752 var leftover_padding = padding - index;
753753 while (true) {
754 try output(context, (*[1]u8)(&zero_byte)[0..]);
754 try output(context, (*const [1]u8)(&zero_byte)[0..]);
755755 leftover_padding -= 1;
756756 if (leftover_padding == 0) break;
757757 }
std/io.zig+2-2
......@@ -227,12 +227,12 @@ pub fn OutStream(comptime WriteError: type) type {
227227 }
228228
229229 pub fn writeByte(self: *Self, byte: u8) Error!void {
230 const slice = (*[1]u8)(&byte)[0..];
230 const slice = (*const [1]u8)(&byte)[0..];
231231 return self.writeFn(self, slice);
232232 }
233233
234234 pub fn writeByteNTimes(self: *Self, byte: u8, n: usize) Error!void {
235 const slice = (*[1]u8)(&byte)[0..];
235 const slice = (*const [1]u8)(&byte)[0..];
236236 var i: usize = 0;
237237 while (i < n) : (i += 1) {
238238 try self.writeFn(self, slice);
test/compile_errors.zig+23
......@@ -5869,4 +5869,27 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
58695869 \\ comptime testCompileLog(Bar{.X = 123});
58705870 \\}
58715871 , "tmp.zig:6:5: error: found compile log statement");
5872
5873 cases.add(
5874 "attempted implicit cast from *const T to *[1]T",
5875 \\export fn entry(byte: u8) void {
5876 \\ const w: i32 = 1234;
5877 \\ var x: *const i32 = &w;
5878 \\ var y: *[1]i32 = x;
5879 \\ y[0] += 1;
5880 \\}
5881 ,
5882 "tmp.zig:4:22: error: expected type '*[1]i32', found '*const i32'",
5883 "tmp.zig:4:22: note: pointer type child 'i32' cannot cast into pointer type child '[1]i32'",
5884 );
5885
5886 cases.add(
5887 "attempted implicit cast from *const T to []T",
5888 \\export fn entry() void {
5889 \\ const u: u32 = 42;
5890 \\ const x: []u32 = &u;
5891 \\}
5892 ,
5893 "tmp.zig:3:23: error: expected type '[]u32', found '*const u32'",
5894 );
58725895}
test/stage1/behavior/align.zig+1-1
......@@ -60,7 +60,7 @@ fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 {
6060test "implicitly decreasing slice alignment" {
6161 const a: u32 align(4) = 3;
6262 const b: u32 align(8) = 4;
63 expect(addUnalignedSlice((*[1]u32)(&a)[0..], (*[1]u32)(&b)[0..]) == 7);
63 expect(addUnalignedSlice((*const [1]u32)(&a)[0..], (*const [1]u32)(&b)[0..]) == 7);
6464}
6565fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 {
6666 return a[0] + b[0];