From c9e1360cdba2bc0c20dc04a3d22fbc0002bcd70b Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Mon, 11 Jul 2022 14:17:22 +0300 Subject: Sema: add "cannot convert to payload type" error notes --- ...ls_with_a_function_that_returns_an_optional.zig | 23 ++++++++++++++++++++++ .../compile_errors/discarding_error_value.zig | 3 ++- .../ignored_deferred_function_call.zig | 3 ++- .../ignored_expression_in_while_continuation.zig | 9 ++++++--- ...f_optional_anyopaque_to_anyopaque_must_fail.zig | 14 +++++++++++++ ...erly_when_assigning_a_value_within_a_struct.zig | 23 ++++++++++++++++++++++ ...n_incompatibility_mismatching_handle_is_ptr.zig | 20 +++++++++++++++++++ ...lity_mismatching_handle_is_ptr_generic_call.zig | 20 +++++++++++++++++++ ...ls_with_a_function_that_returns_an_optional.zig | 21 -------------------- ...f_optional_anyopaque_to_anyopaque_must_fail.zig | 11 ----------- ...erly_when_assigning_a_value_within_a_struct.zig | 21 -------------------- ...n_incompatibility_mismatching_handle_is_ptr.zig | 18 ----------------- ...lity_mismatching_handle_is_ptr_generic_call.zig | 18 ----------------- 13 files changed, 110 insertions(+), 94 deletions(-) create mode 100644 test/cases/compile_errors/assigning_to_struct_or_union_fields_that_are_not_optionals_with_a_function_that_returns_an_optional.zig create mode 100644 test/cases/compile_errors/issue_5618_coercion_of_optional_anyopaque_to_anyopaque_must_fail.zig create mode 100644 test/cases/compile_errors/regression_test_2980_base_type_u32_is_not_type_checked_properly_when_assigning_a_value_within_a_struct.zig create mode 100644 test/cases/compile_errors/result_location_incompatibility_mismatching_handle_is_ptr.zig create mode 100644 test/cases/compile_errors/result_location_incompatibility_mismatching_handle_is_ptr_generic_call.zig delete mode 100644 test/cases/compile_errors/stage1/obj/assigning_to_struct_or_union_fields_that_are_not_optionals_with_a_function_that_returns_an_optional.zig delete mode 100644 test/cases/compile_errors/stage1/obj/issue_5618_coercion_of_optional_anyopaque_to_anyopaque_must_fail.zig delete mode 100644 test/cases/compile_errors/stage1/obj/regression_test_2980_base_type_u32_is_not_type_checked_properly_when_assigning_a_value_within_a_struct.zig delete mode 100644 test/cases/compile_errors/stage1/obj/result_location_incompatibility_mismatching_handle_is_ptr.zig delete mode 100644 test/cases/compile_errors/stage1/obj/result_location_incompatibility_mismatching_handle_is_ptr_generic_call.zig (limited to 'test/cases/compile_errors') diff --git a/test/cases/compile_errors/assigning_to_struct_or_union_fields_that_are_not_optionals_with_a_function_that_returns_an_optional.zig b/test/cases/compile_errors/assigning_to_struct_or_union_fields_that_are_not_optionals_with_a_function_that_returns_an_optional.zig new file mode 100644 index 0000000000..762eb284f2 --- /dev/null +++ b/test/cases/compile_errors/assigning_to_struct_or_union_fields_that_are_not_optionals_with_a_function_that_returns_an_optional.zig @@ -0,0 +1,23 @@ +fn maybe(is: bool) ?u8 { + if (is) return @as(u8, 10) else return null; +} +const U = union { + Ye: u8, +}; +const S = struct { + num: u8, +}; +export fn entry() void { + var u = U{ .Ye = maybe(false) }; + var s = S{ .num = maybe(false) }; + _ = u; + _ = s; +} + +// error +// backend=stage2 +// target=native +// +// :11:27: error: expected type 'u8', found '?u8' +// :11:27: note: cannot convert optional to payload type +// :11:27: note: consider using `.?`, `orelse`, or `if` diff --git a/test/cases/compile_errors/discarding_error_value.zig b/test/cases/compile_errors/discarding_error_value.zig index f74fc4ea29..6dfe0be231 100644 --- a/test/cases/compile_errors/discarding_error_value.zig +++ b/test/cases/compile_errors/discarding_error_value.zig @@ -9,4 +9,5 @@ fn foo() !void { // backend=stage2 // target=native // -// :2:12: error: error is discarded. consider using `try`, `catch`, or `if` +// :2:12: error: error is discarded +// :2:12: note: consider using `try`, `catch`, or `if` diff --git a/test/cases/compile_errors/ignored_deferred_function_call.zig b/test/cases/compile_errors/ignored_deferred_function_call.zig index 69df8b0498..05c4373705 100644 --- a/test/cases/compile_errors/ignored_deferred_function_call.zig +++ b/test/cases/compile_errors/ignored_deferred_function_call.zig @@ -7,4 +7,5 @@ fn bar() anyerror!i32 { return 0; } // backend=stage2 // target=native // -// :2:14: error: error is ignored. consider using `try`, `catch`, or `if` +// :2:14: error: error is ignored +// :2:14: note: consider using `try`, `catch`, or `if` diff --git a/test/cases/compile_errors/ignored_expression_in_while_continuation.zig b/test/cases/compile_errors/ignored_expression_in_while_continuation.zig index d295d476ab..d7de0aac57 100644 --- a/test/cases/compile_errors/ignored_expression_in_while_continuation.zig +++ b/test/cases/compile_errors/ignored_expression_in_while_continuation.zig @@ -17,6 +17,9 @@ fn bad() anyerror!void { // backend=stage2 // target=native // -// :2:24: error: error is ignored. consider using `try`, `catch`, or `if` -// :6:25: error: error is ignored. consider using `try`, `catch`, or `if` -// :10:25: error: error is ignored. consider using `try`, `catch`, or `if` +// :2:24: error: error is ignored +// :2:24: note: consider using `try`, `catch`, or `if` +// :6:25: error: error is ignored +// :6:25: note: consider using `try`, `catch`, or `if` +// :10:25: error: error is ignored +// :10:25: note: consider using `try`, `catch`, or `if` diff --git a/test/cases/compile_errors/issue_5618_coercion_of_optional_anyopaque_to_anyopaque_must_fail.zig b/test/cases/compile_errors/issue_5618_coercion_of_optional_anyopaque_to_anyopaque_must_fail.zig new file mode 100644 index 0000000000..f4716bc24d --- /dev/null +++ b/test/cases/compile_errors/issue_5618_coercion_of_optional_anyopaque_to_anyopaque_must_fail.zig @@ -0,0 +1,14 @@ +export fn foo() void { + var u: ?*anyopaque = null; + var v: *anyopaque = undefined; + v = u; +} + +// error +// backend=stage2 +// target=native +// +// :4:9: error: expected type '*anyopaque', found '?*anyopaque' +// :4:9: note: cannot convert optional to payload type +// :4:9: note: consider using `.?`, `orelse`, or `if` +// :4:9: note: '?*anyopaque' could have null values which are illegal in type '*anyopaque' diff --git a/test/cases/compile_errors/regression_test_2980_base_type_u32_is_not_type_checked_properly_when_assigning_a_value_within_a_struct.zig b/test/cases/compile_errors/regression_test_2980_base_type_u32_is_not_type_checked_properly_when_assigning_a_value_within_a_struct.zig new file mode 100644 index 0000000000..09c496211a --- /dev/null +++ b/test/cases/compile_errors/regression_test_2980_base_type_u32_is_not_type_checked_properly_when_assigning_a_value_within_a_struct.zig @@ -0,0 +1,23 @@ +const Foo = struct { + ptr: ?*usize, + uval: u32, +}; +fn get_uval(x: u32) !u32 { + _ = x; + return error.NotFound; +} +export fn entry() void { + const afoo = Foo{ + .ptr = null, + .uval = get_uval(42), + }; + _ = afoo; +} + +// error +// backend=stage2 +// target=native +// +// :12:25: error: expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(tmp.get_uval)).Fn.return_type.?).ErrorUnion.error_set!u32' +// :12:25: note: cannot convert error union to payload type +// :12:25: note: consider using `try`, `catch`, or `if` diff --git a/test/cases/compile_errors/result_location_incompatibility_mismatching_handle_is_ptr.zig b/test/cases/compile_errors/result_location_incompatibility_mismatching_handle_is_ptr.zig new file mode 100644 index 0000000000..cc1d2c976a --- /dev/null +++ b/test/cases/compile_errors/result_location_incompatibility_mismatching_handle_is_ptr.zig @@ -0,0 +1,20 @@ +export fn entry() void { + var damn = Container{ + .not_optional = getOptional(), + }; + _ = damn; +} +pub fn getOptional() ?i32 { + return 0; +} +pub const Container = struct { + not_optional: i32, +}; + +// error +// backend=stage2 +// target=native +// +// :3:36: error: expected type 'i32', found '?i32' +// :3:36: note: cannot convert optional to payload type +// :3:36: note: consider using `.?`, `orelse`, or `if` diff --git a/test/cases/compile_errors/result_location_incompatibility_mismatching_handle_is_ptr_generic_call.zig b/test/cases/compile_errors/result_location_incompatibility_mismatching_handle_is_ptr_generic_call.zig new file mode 100644 index 0000000000..897675d448 --- /dev/null +++ b/test/cases/compile_errors/result_location_incompatibility_mismatching_handle_is_ptr_generic_call.zig @@ -0,0 +1,20 @@ +export fn entry() void { + var damn = Container{ + .not_optional = getOptional(i32), + }; + _ = damn; +} +pub fn getOptional(comptime T: type) ?T { + return 0; +} +pub const Container = struct { + not_optional: i32, +}; + +// error +// backend=stage2 +// target=native +// +// :3:36: error: expected type 'i32', found '?i32' +// :3:36: note: cannot convert optional to payload type +// :3:36: note: consider using `.?`, `orelse`, or `if` diff --git a/test/cases/compile_errors/stage1/obj/assigning_to_struct_or_union_fields_that_are_not_optionals_with_a_function_that_returns_an_optional.zig b/test/cases/compile_errors/stage1/obj/assigning_to_struct_or_union_fields_that_are_not_optionals_with_a_function_that_returns_an_optional.zig deleted file mode 100644 index 8285991c05..0000000000 --- a/test/cases/compile_errors/stage1/obj/assigning_to_struct_or_union_fields_that_are_not_optionals_with_a_function_that_returns_an_optional.zig +++ /dev/null @@ -1,21 +0,0 @@ -fn maybe(is: bool) ?u8 { - if (is) return @as(u8, 10) else return null; -} -const U = union { - Ye: u8, -}; -const S = struct { - num: u8, -}; -export fn entry() void { - var u = U{ .Ye = maybe(false) }; - var s = S{ .num = maybe(false) }; - _ = u; - _ = s; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:11:27: error: cannot convert optional to payload type. consider using `.?`, `orelse`, or `if`. expected type 'u8', found '?u8' diff --git a/test/cases/compile_errors/stage1/obj/issue_5618_coercion_of_optional_anyopaque_to_anyopaque_must_fail.zig b/test/cases/compile_errors/stage1/obj/issue_5618_coercion_of_optional_anyopaque_to_anyopaque_must_fail.zig deleted file mode 100644 index 9ba9910838..0000000000 --- a/test/cases/compile_errors/stage1/obj/issue_5618_coercion_of_optional_anyopaque_to_anyopaque_must_fail.zig +++ /dev/null @@ -1,11 +0,0 @@ -export fn foo() void { - var u: ?*anyopaque = null; - var v: *anyopaque = undefined; - v = u; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:4:9: error: cannot convert optional to payload type. consider using `.?`, `orelse`, or `if`. expected type '*anyopaque', found '?*anyopaque' diff --git a/test/cases/compile_errors/stage1/obj/regression_test_2980_base_type_u32_is_not_type_checked_properly_when_assigning_a_value_within_a_struct.zig b/test/cases/compile_errors/stage1/obj/regression_test_2980_base_type_u32_is_not_type_checked_properly_when_assigning_a_value_within_a_struct.zig deleted file mode 100644 index 8be44fbe05..0000000000 --- a/test/cases/compile_errors/stage1/obj/regression_test_2980_base_type_u32_is_not_type_checked_properly_when_assigning_a_value_within_a_struct.zig +++ /dev/null @@ -1,21 +0,0 @@ -const Foo = struct { - ptr: ?*usize, - uval: u32, -}; -fn get_uval(x: u32) !u32 { - _ = x; - return error.NotFound; -} -export fn entry() void { - const afoo = Foo{ - .ptr = null, - .uval = get_uval(42), - }; - _ = afoo; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:12:25: error: cannot convert error union to payload type. consider using `try`, `catch`, or `if`. expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(get_uval)).Fn.return_type.?).ErrorUnion.error_set!u32' diff --git a/test/cases/compile_errors/stage1/obj/result_location_incompatibility_mismatching_handle_is_ptr.zig b/test/cases/compile_errors/stage1/obj/result_location_incompatibility_mismatching_handle_is_ptr.zig deleted file mode 100644 index 18dd6382fc..0000000000 --- a/test/cases/compile_errors/stage1/obj/result_location_incompatibility_mismatching_handle_is_ptr.zig +++ /dev/null @@ -1,18 +0,0 @@ -export fn entry() void { - var damn = Container{ - .not_optional = getOptional(), - }; - _ = damn; -} -pub fn getOptional() ?i32 { - return 0; -} -pub const Container = struct { - not_optional: i32, -}; - -// error -// backend=stage1 -// target=native -// -// tmp.zig:3:36: error: cannot convert optional to payload type. consider using `.?`, `orelse`, or `if`. expected type 'i32', found '?i32' diff --git a/test/cases/compile_errors/stage1/obj/result_location_incompatibility_mismatching_handle_is_ptr_generic_call.zig b/test/cases/compile_errors/stage1/obj/result_location_incompatibility_mismatching_handle_is_ptr_generic_call.zig deleted file mode 100644 index 75811c58e1..0000000000 --- a/test/cases/compile_errors/stage1/obj/result_location_incompatibility_mismatching_handle_is_ptr_generic_call.zig +++ /dev/null @@ -1,18 +0,0 @@ -export fn entry() void { - var damn = Container{ - .not_optional = getOptional(i32), - }; - _ = damn; -} -pub fn getOptional(comptime T: type) ?T { - return 0; -} -pub const Container = struct { - not_optional: i32, -}; - -// error -// backend=stage1 -// target=native -// -// tmp.zig:3:36: error: cannot convert optional to payload type. consider using `.?`, `orelse`, or `if`. expected type 'i32', found '?i32' -- cgit v1.2.3