mirror of
https://github.com/ziglang/zig.git
synced 2025-12-16 03:03:09 +00:00
Sema: do not allow coercing undefined to opaque types
This commit is contained in:
parent
af89bb05d3
commit
b5d3db5fc6
21
src/Sema.zig
21
src/Sema.zig
@ -30675,6 +30675,18 @@ fn coerceExtra(
|
||||
else => {},
|
||||
}
|
||||
|
||||
const can_coerce_to = switch (dest_ty.zigTypeTag(zcu)) {
|
||||
.noreturn, .@"opaque" => false,
|
||||
else => true,
|
||||
};
|
||||
|
||||
if (can_coerce_to) {
|
||||
// undefined to anything. We do this after the big switch above so that
|
||||
// special logic has a chance to run first, such as `*[N]T` to `[]T` which
|
||||
// should initialize the length field of the slice.
|
||||
if (maybe_inst_val) |val| if (val.toIntern() == .undef) return pt.undefRef(dest_ty);
|
||||
}
|
||||
|
||||
if (!opts.report_err) return error.NotCoercible;
|
||||
|
||||
if (opts.is_ret and dest_ty.zigTypeTag(zcu) == .noreturn) {
|
||||
@ -30692,15 +30704,14 @@ fn coerceExtra(
|
||||
return sema.failWithOwnedErrorMsg(block, msg);
|
||||
}
|
||||
|
||||
// undefined to anything. We do this after the big switch above so that
|
||||
// special logic has a chance to run first, such as `*[N]T` to `[]T` which
|
||||
// should initialize the length field of the slice.
|
||||
if (maybe_inst_val) |val| if (val.toIntern() == .undef) return pt.undefRef(dest_ty);
|
||||
|
||||
const msg = msg: {
|
||||
const msg = try sema.errMsg(inst_src, "expected type '{}', found '{}'", .{ dest_ty.fmt(pt), inst_ty.fmt(pt) });
|
||||
errdefer msg.destroy(sema.gpa);
|
||||
|
||||
if (!can_coerce_to) {
|
||||
try sema.errNote(inst_src, msg, "cannot coerce to '{}'", .{dest_ty.fmt(pt)});
|
||||
}
|
||||
|
||||
// E!T to T
|
||||
if (inst_ty.zigTypeTag(zcu) == .error_union and
|
||||
(try sema.coerceInMemoryAllowed(block, inst_ty.errorUnionPayload(zcu), dest_ty, false, target, dest_ty_src, inst_src, maybe_inst_val)) == .ok)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user