authorgravatar for emekankurumeh@outlook.comemekoi <emekankurumeh@outlook.com> 2019-03-31 14:19:13-05:00
committergravatar for emekankurumeh@outlook.comemekoi <emekankurumeh@outlook.com> 2019-03-31 16:47:34-05:00
log4a64f266272c2bce832d971b8082455becf5e478
treef560e2233541719d71fe6e891786879c85750679
parenta4afacd1822cea75196b27a8a5fe30bd252eccae

added error for implicit cast from *const T to *[1]T. credit: @kristate


2 files changed, 29 insertions(+), 1 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)))
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}