Sema: add "cannot convert to payload type" error notes

This commit is contained in:
Veikka Tuominen 2022-07-11 14:17:22 +03:00
parent 2a41b1449b
commit c9e1360cdb
10 changed files with 75 additions and 26 deletions

View File

@ -2749,7 +2749,15 @@ fn ensureResultUsed(
const operand_ty = sema.typeOf(operand); const operand_ty = sema.typeOf(operand);
switch (operand_ty.zigTypeTag()) { switch (operand_ty.zigTypeTag()) {
.Void, .NoReturn => return, .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", .{}), 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 src = inst_data.src();
const operand_ty = sema.typeOf(operand); const operand_ty = sema.typeOf(operand);
switch (operand_ty.zigTypeTag()) { 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, 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) }); 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); 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); try in_memory_result.report(sema, block, inst_src, msg);
break :msg msg; break :msg msg;
}; };

View File

@ -15,7 +15,9 @@ export fn entry() void {
} }
// error // error
// backend=stage1 // backend=stage2
// target=native // 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`

View File

@ -9,4 +9,5 @@ fn foo() !void {
// backend=stage2 // backend=stage2
// target=native // 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`

View File

@ -7,4 +7,5 @@ fn bar() anyerror!i32 { return 0; }
// backend=stage2 // backend=stage2
// target=native // 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`

View File

@ -17,6 +17,9 @@ fn bad() anyerror!void {
// backend=stage2 // backend=stage2
// target=native // target=native
// //
// :2:24: error: error is ignored. consider using `try`, `catch`, or `if` // :2:24: error: error is ignored
// :6:25: error: error is ignored. consider using `try`, `catch`, or `if` // :2:24: note: consider using `try`, `catch`, or `if`
// :10:25: error: error is ignored. 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`

View File

@ -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'

View File

@ -15,7 +15,9 @@ export fn entry() void {
} }
// error // error
// backend=stage1 // backend=stage2
// target=native // 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`

View File

@ -12,7 +12,9 @@ pub const Container = struct {
}; };
// error // error
// backend=stage1 // backend=stage2
// target=native // 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`

View File

@ -12,7 +12,9 @@ pub const Container = struct {
}; };
// error // error
// backend=stage1 // backend=stage2
// target=native // 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`

View File

@ -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'