authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-17 12:50:36+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-17 17:39:54+02:00
logc9dde10f8629c3ad2234f6220990b1dd70bac807
treef3f0a40024f282b8fae69dbc663c04dfbdc5a784
parent35e989235b29c5d921c8cf053a1d2f92fa0ce57a

stage1: improve error message when casting tuples


3 files changed, 40 insertions(+), 12 deletions(-)

doc/langref.html.in+1-1
......@@ -10405,7 +10405,7 @@ pub fn main() !void {
1040510405 <p>String literals such as {#syntax#}"foo"{#endsyntax#} are in the global constant data section.
1040610406 This is why it is an error to pass a string literal to a mutable slice, like this:
1040710407 </p>
10408 {#code_begin|test_err|expected type '[]u8'#}
10408 {#code_begin|test_err|cannot cast pointer to array literal to slice type '[]u8'#}
1040910409fn foo(s: []u8) void {
1041010410 _ = s;
1041110411}
src/stage1/ir.cpp+27-4
......@@ -7843,7 +7843,7 @@ static Stage1AirInst *ir_analyze_cast(IrAnalyze *ira, Scope *scope, AstNode *sou
78437843 bool const_ok = (slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0
78447844 || !actual_type->data.pointer.is_const);
78457845
7846 if (const_ok && types_match_const_cast_only(ira, slice_ptr_type->data.pointer.child_type,
7846 if (types_match_const_cast_only(ira, slice_ptr_type->data.pointer.child_type,
78477847 array_type->data.array.child_type, source_node,
78487848 !slice_ptr_type->data.pointer.is_const).id == ConstCastResultIdOk &&
78497849 (slice_ptr_type->data.pointer.sentinel == nullptr ||
......@@ -7851,6 +7851,14 @@ static Stage1AirInst *ir_analyze_cast(IrAnalyze *ira, Scope *scope, AstNode *sou
78517851 const_values_equal(ira->codegen, array_type->data.array.sentinel,
78527852 slice_ptr_type->data.pointer.sentinel))))
78537853 {
7854 if (!const_ok) {
7855 ErrorMsg *msg = ir_add_error_node(ira, source_node,
7856 buf_sprintf("cannot cast pointer to array literal to slice type '%s'",
7857 buf_ptr(&wanted_type->name)));
7858 add_error_note(ira->codegen, msg, source_node,
7859 buf_sprintf("cast discards const qualifier"));
7860 return ira->codegen->invalid_inst_gen;
7861 }
78547862 // If the pointers both have ABI align, it works.
78557863 // Or if the array length is 0, alignment doesn't matter.
78567864 bool ok_align = array_type->data.array.len == 0 ||
......@@ -8208,8 +8216,16 @@ static Stage1AirInst *ir_analyze_cast(IrAnalyze *ira, Scope *scope, AstNode *sou
82088216 ZigType *wanted_child = wanted_type->data.pointer.child_type;
82098217 bool const_ok = (!actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const);
82108218 if (wanted_child->id == ZigTypeIdArray && (is_array_init || field_count == 0) &&
8211 wanted_child->data.array.len == field_count && (const_ok || field_count == 0))
8219 wanted_child->data.array.len == field_count)
82128220 {
8221 if (!const_ok && field_count != 0) {
8222 ErrorMsg *msg = ir_add_error_node(ira, source_node,
8223 buf_sprintf("cannot cast pointer to array literal to '%s'",
8224 buf_ptr(&wanted_type->name)));
8225 add_error_note(ira->codegen, msg, source_node,
8226 buf_sprintf("cast discards const qualifier"));
8227 return ira->codegen->invalid_inst_gen;
8228 }
82138229 Stage1AirInst *res = ir_analyze_struct_literal_to_array(ira, scope, source_node, value, anon_type, wanted_child);
82148230 if (res->value->type->id == ZigTypeIdPointer)
82158231 return res;
......@@ -8241,6 +8257,13 @@ static Stage1AirInst *ir_analyze_cast(IrAnalyze *ira, Scope *scope, AstNode *sou
82418257 res = ir_get_ref(ira, scope, source_node, res, actual_type->data.pointer.is_const, actual_type->data.pointer.is_volatile);
82428258
82438259 return ir_resolve_ptr_of_array_to_slice(ira, scope, source_node, res, wanted_type, nullptr);
8260 } else if (!slice_type->data.pointer.is_const && actual_type->data.pointer.is_const && field_count != 0) {
8261 ErrorMsg *msg = ir_add_error_node(ira, source_node,
8262 buf_sprintf("cannot cast pointer to array literal to slice type '%s'",
8263 buf_ptr(&wanted_type->name)));
8264 add_error_note(ira->codegen, msg, source_node,
8265 buf_sprintf("cast discards const qualifier"));
8266 return ira->codegen->invalid_inst_gen;
82448267 }
82458268 }
82468269 }
......@@ -15068,7 +15091,7 @@ static Stage1AirInst *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, Stage1ZirI
1506815091 return ira->codegen->invalid_inst_gen;
1506915092 if (actual_array_type->id != ZigTypeIdArray) {
1507015093 ir_add_error_node(ira, elem_ptr_instruction->init_array_type_source_node,
15071 buf_sprintf("array literal requires address-of operator to coerce to slice type '%s'",
15094 buf_sprintf("array literal requires address-of operator (&) to coerce to slice type '%s'",
1507215095 buf_ptr(&actual_array_type->name)));
1507315096 return ira->codegen->invalid_inst_gen;
1507415097 }
......@@ -17473,7 +17496,7 @@ static Stage1AirInst *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1747317496
1747417497 if (is_slice(container_type)) {
1747517498 ir_add_error_node(ira, instruction->init_array_type_source_node,
17476 buf_sprintf("array literal requires address-of operator to coerce to slice type '%s'",
17499 buf_sprintf("array literal requires address-of operator (&) to coerce to slice type '%s'",
1747717500 buf_ptr(&container_type->name)));
1747817501 return ira->codegen->invalid_inst_gen;
1747917502 }
test/compile_errors.zig+12-7
......@@ -86,9 +86,12 @@ pub fn addCases(ctx: *TestContext) !void {
8686 \\ _ = c;
8787 \\}
8888 , &[_][]const u8{
89 "tmp.zig:2:31: error: expected type '[][]const u8', found '*const struct:2:31'",
90 "tmp.zig:6:33: error: expected type '*[2][]const u8', found '*const struct:6:33'",
89 "tmp.zig:2:31: error: cannot cast pointer to array literal to slice type '[][]const u8'",
90 "tmp.zig:2:31: note: cast discards const qualifier",
91 "tmp.zig:6:33: error: cannot cast pointer to array literal to '*[2][]const u8'",
92 "tmp.zig:6:33: note: cast discards const qualifier",
9193 "tmp.zig:11:21: error: expected type '*S', found '*const struct:11:21'",
94 "tmp.zig:11:21: note: cast discards const qualifier",
9295 });
9396
9497 ctx.objErrStage1("@Type() union payload is undefined",
......@@ -1962,7 +1965,7 @@ pub fn addCases(ctx: *TestContext) !void {
19621965 \\ _ = geo_data;
19631966 \\}
19641967 , &[_][]const u8{
1965 "tmp.zig:4:30: error: array literal requires address-of operator to coerce to slice type '[][2]f32'",
1968 "tmp.zig:4:30: error: array literal requires address-of operator (&) to coerce to slice type '[][2]f32'",
19661969 });
19671970
19681971 ctx.objErrStage1("slicing of global undefined pointer",
......@@ -2537,7 +2540,7 @@ pub fn addCases(ctx: *TestContext) !void {
25372540 \\ _ = x;
25382541 \\}
25392542 , &[_][]const u8{
2540 "tmp.zig:2:15: error: array literal requires address-of operator to coerce to slice type '[]u8'",
2543 "tmp.zig:2:15: error: array literal requires address-of operator (&) to coerce to slice type '[]u8'",
25412544 });
25422545
25432546 ctx.objErrStage1("slice passed as array init type",
......@@ -2546,7 +2549,7 @@ pub fn addCases(ctx: *TestContext) !void {
25462549 \\ _ = x;
25472550 \\}
25482551 , &[_][]const u8{
2549 "tmp.zig:2:15: error: array literal requires address-of operator to coerce to slice type '[]u8'",
2552 "tmp.zig:2:15: error: array literal requires address-of operator (&) to coerce to slice type '[]u8'",
25502553 });
25512554
25522555 ctx.objErrStage1("inferred array size invalid here",
......@@ -3493,7 +3496,8 @@ pub fn addCases(ctx: *TestContext) !void {
34933496 \\ _ = sliceA;
34943497 \\}
34953498 , &[_][]const u8{
3496 "tmp.zig:3:27: error: expected type '[]u8', found '*const [1]u8'",
3499 "tmp.zig:3:27: error: cannot cast pointer to array literal to slice type '[]u8'",
3500 "tmp.zig:3:27: note: cast discards const qualifier",
34973501 });
34983502
34993503 ctx.objErrStage1("deref slice and get len field",
......@@ -8717,7 +8721,8 @@ pub fn addCases(ctx: *TestContext) !void {
87178721 \\ comptime ignore(@typeInfo(MyStruct).Struct.fields[0]);
87188722 \\}
87198723 , &[_][]const u8{
8720 ":5:28: error: expected type '[]u8', found '*const [3:0]u8'",
8724 ":5:28: error: cannot cast pointer to array literal to slice type '[]u8'",
8725 ":5:28: note: cast discards const qualifier",
87218726 });
87228727
87238728 ctx.objErrStage1("integer underflow error",