diff --git a/src/Sema.zig b/src/Sema.zig index 95b8d8acdf..a10695df1f 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2749,7 +2749,15 @@ fn ensureResultUsed( const operand_ty = sema.typeOf(operand); switch (operand_ty.zigTypeTag()) { .Void, .NoReturn => return, - .ErrorSet, .ErrorUnion => return sema.fail(block, src, "error is ignored. consider using `try`, `catch`, or `if`", .{}), + .ErrorSet, .ErrorUnion => { + const msg = msg: { + const msg = try sema.errMsg(block, src, "error is ignored", .{}); + errdefer msg.destroy(sema.gpa); + try sema.errNote(block, src, msg, "consider using `try`, `catch`, or `if`", .{}); + break :msg msg; + }; + return sema.failWithOwnedErrorMsg(block, msg); + }, else => return sema.fail(block, src, "expression value is ignored", .{}), } } @@ -2763,7 +2771,15 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com const src = inst_data.src(); const operand_ty = sema.typeOf(operand); switch (operand_ty.zigTypeTag()) { - .ErrorSet, .ErrorUnion => return sema.fail(block, src, "error is discarded. consider using `try`, `catch`, or `if`", .{}), + .ErrorSet, .ErrorUnion => { + const msg = msg: { + const msg = try sema.errMsg(block, src, "error is discarded", .{}); + errdefer msg.destroy(sema.gpa); + try sema.errNote(block, src, msg, "consider using `try`, `catch`, or `if`", .{}); + break :msg msg; + }; + return sema.failWithOwnedErrorMsg(block, msg); + }, else => return, } } @@ -20402,6 +20418,23 @@ fn coerceExtra( const msg = try sema.errMsg(block, inst_src, "expected type '{}', found '{}'", .{ dest_ty.fmt(sema.mod), inst_ty.fmt(sema.mod) }); errdefer msg.destroy(sema.gpa); + // E!T to T + if (inst_ty.zigTypeTag() == .ErrorUnion and + (try sema.coerceInMemoryAllowed(block, inst_ty.errorUnionPayload(), dest_ty, false, target, dest_ty_src, inst_src)) == .ok) + { + try sema.errNote(block, inst_src, msg, "cannot convert error union to payload type", .{}); + try sema.errNote(block, inst_src, msg, "consider using `try`, `catch`, or `if`", .{}); + } + + // ?T to T + var buf: Type.Payload.ElemType = undefined; + if (inst_ty.zigTypeTag() == .Optional and + (try sema.coerceInMemoryAllowed(block, inst_ty.optionalChild(&buf), dest_ty, false, target, dest_ty_src, inst_src)) == .ok) + { + try sema.errNote(block, inst_src, msg, "cannot convert optional to payload type", .{}); + try sema.errNote(block, inst_src, msg, "consider using `.?`, `orelse`, or `if`", .{}); + } + try in_memory_result.report(sema, block, inst_src, msg); break :msg msg; }; 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/assigning_to_struct_or_union_fields_that_are_not_optionals_with_a_function_that_returns_an_optional.zig similarity index 62% rename from test/cases/compile_errors/stage1/obj/assigning_to_struct_or_union_fields_that_are_not_optionals_with_a_function_that_returns_an_optional.zig rename to test/cases/compile_errors/assigning_to_struct_or_union_fields_that_are_not_optionals_with_a_function_that_returns_an_optional.zig index 8285991c05..762eb284f2 100644 --- 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/assigning_to_struct_or_union_fields_that_are_not_optionals_with_a_function_that_returns_an_optional.zig @@ -15,7 +15,9 @@ export fn entry() void { } // error -// backend=stage1 +// backend=stage2 // target=native // -// tmp.zig:11:27: error: cannot convert optional to payload type. consider using `.?`, `orelse`, or `if`. expected type 'u8', found '?u8' +// :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/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/regression_test_2980_base_type_u32_is_not_type_checked_properly_when_assigning_a_value_within_a_struct.zig similarity index 50% rename from 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 rename to test/cases/compile_errors/regression_test_2980_base_type_u32_is_not_type_checked_properly_when_assigning_a_value_within_a_struct.zig index 8be44fbe05..09c496211a 100644 --- 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/regression_test_2980_base_type_u32_is_not_type_checked_properly_when_assigning_a_value_within_a_struct.zig @@ -15,7 +15,9 @@ export fn entry() void { } // error -// backend=stage1 +// backend=stage2 // 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' +// :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/stage1/obj/result_location_incompatibility_mismatching_handle_is_ptr.zig b/test/cases/compile_errors/result_location_incompatibility_mismatching_handle_is_ptr.zig similarity index 57% rename from test/cases/compile_errors/stage1/obj/result_location_incompatibility_mismatching_handle_is_ptr.zig rename to test/cases/compile_errors/result_location_incompatibility_mismatching_handle_is_ptr.zig index 18dd6382fc..cc1d2c976a 100644 --- a/test/cases/compile_errors/stage1/obj/result_location_incompatibility_mismatching_handle_is_ptr.zig +++ b/test/cases/compile_errors/result_location_incompatibility_mismatching_handle_is_ptr.zig @@ -12,7 +12,9 @@ pub const Container = struct { }; // error -// backend=stage1 +// backend=stage2 // target=native // -// tmp.zig:3:36: error: cannot convert optional to payload type. consider using `.?`, `orelse`, or `if`. expected type 'i32', found '?i32' +// :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/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 similarity index 59% rename from test/cases/compile_errors/stage1/obj/result_location_incompatibility_mismatching_handle_is_ptr_generic_call.zig rename to test/cases/compile_errors/result_location_incompatibility_mismatching_handle_is_ptr_generic_call.zig index 75811c58e1..897675d448 100644 --- a/test/cases/compile_errors/stage1/obj/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 @@ -12,7 +12,9 @@ pub const Container = struct { }; // error -// backend=stage1 +// backend=stage2 // target=native // -// tmp.zig:3:36: error: cannot convert optional to payload type. consider using `.?`, `orelse`, or `if`. expected type 'i32', found '?i32' +// :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/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'