authorgravatar for will.lillis24@gmail.comWill Lillis <will.lillis24@gmail.com> 2024-07-16 12:42:13-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-07-16 16:42:13+00:00
loga9d544575d5bd2a939b09b8f088bee5d8ff7b0d9
tree0b8f7a3dc25261d88ea412399104ca291060ec9f
parent9356cb1475606a7afd2e722af60f87ce2b39f9f8
signaturebadge-check Signed by PGP key B5690EEEBB952194

Sema: add error note for failed coercions to optional types and error unions


4 files changed, 87 insertions(+), 29 deletions(-)

src/Sema.zig+61-28
......@@ -11312,7 +11312,7 @@ const SwitchProngAnalysis = struct {
1131211312 const first_non_imc = in_mem: {
1131311313 for (field_indices, 0..) |field_idx, i| {
1131411314 const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_idx]);
11315 if (.ok != try sema.coerceInMemoryAllowed(block, capture_ty, field_ty, false, zcu.getTarget(), LazySrcLoc.unneeded, LazySrcLoc.unneeded)) {
11315 if (.ok != try sema.coerceInMemoryAllowed(block, capture_ty, field_ty, false, zcu.getTarget(), LazySrcLoc.unneeded, LazySrcLoc.unneeded, null)) {
1131611316 break :in_mem i;
1131711317 }
1131811318 }
......@@ -11335,7 +11335,7 @@ const SwitchProngAnalysis = struct {
1133511335 const next = first_non_imc + 1;
1133611336 for (field_indices[next..], next..) |field_idx, i| {
1133711337 const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_idx]);
11338 if (.ok != try sema.coerceInMemoryAllowed(block, capture_ty, field_ty, false, zcu.getTarget(), LazySrcLoc.unneeded, LazySrcLoc.unneeded)) {
11338 if (.ok != try sema.coerceInMemoryAllowed(block, capture_ty, field_ty, false, zcu.getTarget(), LazySrcLoc.unneeded, LazySrcLoc.unneeded, null)) {
1133911339 in_mem_coercible.unset(i);
1134011340 }
1134111341 }
......@@ -23162,6 +23162,7 @@ fn ptrCastFull(
2316223162 mod.getTarget(),
2316323163 src,
2316423164 operand_src,
23165 null,
2316523166 );
2316623167 if (imc_res == .ok) break :check_child;
2316723168 return sema.failWithOwnedErrorMsg(block, msg: {
......@@ -25772,7 +25773,7 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2577225773
2577325774 const dest_elem_ty = dest_ty.elemType2(mod);
2577425775 const src_elem_ty = src_ty.elemType2(mod);
25775 if (.ok != try sema.coerceInMemoryAllowed(block, dest_elem_ty, src_elem_ty, true, target, dest_src, src_src)) {
25776 if (.ok != try sema.coerceInMemoryAllowed(block, dest_elem_ty, src_elem_ty, true, target, dest_src, src_src, null)) {
2577625777 return sema.fail(block, src, "TODO: lower @memcpy to a for loop because the element types have different ABI sizes", .{});
2577725778 }
2577825779
......@@ -29188,7 +29189,7 @@ fn coerceExtra(
2918829189
2918929190 const maybe_inst_val = try sema.resolveValue(inst);
2919029191
29191 var in_memory_result = try sema.coerceInMemoryAllowed(block, dest_ty, inst_ty, false, target, dest_ty_src, inst_src);
29192 var in_memory_result = try sema.coerceInMemoryAllowed(block, dest_ty, inst_ty, false, target, dest_ty_src, inst_src, maybe_inst_val);
2919229193 if (in_memory_result == .ok) {
2919329194 if (maybe_inst_val) |val| {
2919429195 return sema.coerceInMemory(val, dest_ty);
......@@ -29243,7 +29244,7 @@ fn coerceExtra(
2924329244 error.NotCoercible => {
2924429245 if (in_memory_result == .no_match) {
2924529246 // Try to give more useful notes
29246 in_memory_result = try sema.coerceInMemoryAllowed(block, child_type, inst_ty, false, target, dest_ty_src, inst_src);
29247 in_memory_result = try sema.coerceInMemoryAllowed(block, child_type, inst_ty, false, target, dest_ty_src, inst_src, maybe_inst_val);
2924729248 }
2924829249 break :optional;
2924929250 },
......@@ -29273,7 +29274,7 @@ fn coerceExtra(
2927329274 const array_elem_ty = array_ty.childType(zcu);
2927429275 if (array_ty.arrayLen(zcu) != 1) break :single_item;
2927529276 const dest_is_mut = !dest_info.flags.is_const;
29276 switch (try sema.coerceInMemoryAllowed(block, array_elem_ty, ptr_elem_ty, dest_is_mut, target, dest_ty_src, inst_src)) {
29277 switch (try sema.coerceInMemoryAllowed(block, array_elem_ty, ptr_elem_ty, dest_is_mut, target, dest_ty_src, inst_src, maybe_inst_val)) {
2927729278 .ok => {},
2927829279 else => break :single_item,
2927929280 }
......@@ -29290,7 +29291,7 @@ fn coerceExtra(
2929029291 const dest_is_mut = !dest_info.flags.is_const;
2929129292
2929229293 const dst_elem_type = Type.fromInterned(dest_info.child);
29293 const elem_res = try sema.coerceInMemoryAllowed(block, dst_elem_type, array_elem_type, dest_is_mut, target, dest_ty_src, inst_src);
29294 const elem_res = try sema.coerceInMemoryAllowed(block, dst_elem_type, array_elem_type, dest_is_mut, target, dest_ty_src, inst_src, maybe_inst_val);
2929429295 switch (elem_res) {
2929529296 .ok => {},
2929629297 else => {
......@@ -29351,7 +29352,7 @@ fn coerceExtra(
2935129352 const src_elem_ty = inst_ty.childType(zcu);
2935229353 const dest_is_mut = !dest_info.flags.is_const;
2935329354 const dst_elem_type = Type.fromInterned(dest_info.child);
29354 switch (try sema.coerceInMemoryAllowed(block, dst_elem_type, src_elem_ty, dest_is_mut, target, dest_ty_src, inst_src)) {
29355 switch (try sema.coerceInMemoryAllowed(block, dst_elem_type, src_elem_ty, dest_is_mut, target, dest_ty_src, inst_src, maybe_inst_val)) {
2935529356 .ok => {},
2935629357 else => break :src_c_ptr,
2935729358 }
......@@ -29404,7 +29405,7 @@ fn coerceExtra(
2940429405 const addr = sema.coerceExtra(block, ptr_size_ty, inst, inst_src, .{ .report_err = false }) catch |err| switch (err) {
2940529406 error.NotCoercible => {
2940629407 // Try to give more useful notes
29407 in_memory_result = try sema.coerceInMemoryAllowed(block, ptr_size_ty, inst_ty, false, target, dest_ty_src, inst_src);
29408 in_memory_result = try sema.coerceInMemoryAllowed(block, ptr_size_ty, inst_ty, false, target, dest_ty_src, inst_src, maybe_inst_val);
2940829409 break :pointer;
2940929410 },
2941029411 else => |e| return e,
......@@ -29422,6 +29423,7 @@ fn coerceExtra(
2942229423 target,
2942329424 dest_ty_src,
2942429425 inst_src,
29426 maybe_inst_val,
2942529427 )) {
2942629428 .ok => {},
2942729429 else => break :p,
......@@ -29526,6 +29528,7 @@ fn coerceExtra(
2952629528 target,
2952729529 dest_ty_src,
2952829530 inst_src,
29531 maybe_inst_val,
2952929532 )) {
2953029533 .ok => {},
2953129534 else => break :p,
......@@ -29705,7 +29708,14 @@ fn coerceExtra(
2970529708 else => eu: {
2970629709 // T to E!T
2970729710 return sema.wrapErrorUnionPayload(block, dest_ty, inst, inst_src) catch |err| switch (err) {
29708 error.NotCoercible => break :eu,
29711 error.NotCoercible => {
29712 if (in_memory_result == .no_match) {
29713 const payload_type = dest_ty.errorUnionPayload(zcu);
29714 // Try to give more useful notes
29715 in_memory_result = try sema.coerceInMemoryAllowed(block, payload_type, inst_ty, false, target, dest_ty_src, inst_src, maybe_inst_val);
29716 }
29717 break :eu;
29718 },
2970929719 else => |e| return e,
2971029720 };
2971129721 },
......@@ -29730,6 +29740,7 @@ fn coerceExtra(
2973029740 target,
2973129741 dest_ty_src,
2973229742 inst_src,
29743 maybe_inst_val,
2973329744 )) {
2973429745 break :array_to_array;
2973529746 }
......@@ -29805,7 +29816,7 @@ fn coerceExtra(
2980529816
2980629817 // E!T to T
2980729818 if (inst_ty.zigTypeTag(zcu) == .ErrorUnion and
29808 (try sema.coerceInMemoryAllowed(block, inst_ty.errorUnionPayload(zcu), dest_ty, false, target, dest_ty_src, inst_src)) == .ok)
29819 (try sema.coerceInMemoryAllowed(block, inst_ty.errorUnionPayload(zcu), dest_ty, false, target, dest_ty_src, inst_src, maybe_inst_val)) == .ok)
2980929820 {
2981029821 try sema.errNote(inst_src, msg, "cannot convert error union to payload type", .{});
2981129822 try sema.errNote(inst_src, msg, "consider using 'try', 'catch', or 'if'", .{});
......@@ -29813,7 +29824,7 @@ fn coerceExtra(
2981329824
2981429825 // ?T to T
2981529826 if (inst_ty.zigTypeTag(zcu) == .Optional and
29816 (try sema.coerceInMemoryAllowed(block, inst_ty.optionalChild(zcu), dest_ty, false, target, dest_ty_src, inst_src)) == .ok)
29827 (try sema.coerceInMemoryAllowed(block, inst_ty.optionalChild(zcu), dest_ty, false, target, dest_ty_src, inst_src, maybe_inst_val)) == .ok)
2981729828 {
2981829829 try sema.errNote(inst_src, msg, "cannot convert optional to payload type", .{});
2981929830 try sema.errNote(inst_src, msg, "consider using '.?', 'orelse', or 'if'", .{});
......@@ -29859,6 +29870,7 @@ const InMemoryCoercionResult = union(enum) {
2985929870 ok,
2986029871 no_match: Pair,
2986129872 int_not_coercible: Int,
29873 comptime_int_not_coercible: TypeValuePair,
2986229874 error_union_payload: PairAndChild,
2986329875 array_len: IntPair,
2986429876 array_sentinel: Sentinel,
......@@ -29895,6 +29907,11 @@ const InMemoryCoercionResult = union(enum) {
2989529907 wanted: Type,
2989629908 };
2989729909
29910 const TypeValuePair = struct {
29911 actual: Value,
29912 wanted: Type,
29913 };
29914
2989829915 const PairAndChild = struct {
2989929916 child: *InMemoryCoercionResult,
2990029917 actual: Type,
......@@ -29988,6 +30005,12 @@ const InMemoryCoercionResult = union(enum) {
2998830005 });
2998930006 break;
2999030007 },
30008 .comptime_int_not_coercible => |int| {
30009 try sema.errNote(src, msg, "type '{}' cannot represent value '{}'", .{
30010 int.wanted.fmt(pt), int.actual.fmtValue(pt, sema),
30011 });
30012 break;
30013 },
2999130014 .error_union_payload => |pair| {
2999230015 try sema.errNote(src, msg, "error union payload '{}' cannot cast into error union payload '{}'", .{
2999330016 pair.actual.fmt(pt), pair.wanted.fmt(pt),
......@@ -30229,6 +30252,7 @@ pub fn coerceInMemoryAllowed(
3022930252 target: std.Target,
3023030253 dest_src: LazySrcLoc,
3023130254 src_src: LazySrcLoc,
30255 src_val: ?Value,
3023230256) CompileError!InMemoryCoercionResult {
3023330257 const pt = sema.pt;
3023430258 const mod = pt.zcu;
......@@ -30264,6 +30288,15 @@ pub fn coerceInMemoryAllowed(
3026430288 }
3026530289 }
3026630290
30291 // Comptime int to regular int.
30292 if (dest_tag == .Int and src_tag == .ComptimeInt) {
30293 if (src_val) |val| {
30294 if (!(try sema.intFitsInType(val, dest_ty, null))) {
30295 return .{ .comptime_int_not_coercible = .{ .wanted = dest_ty, .actual = val } };
30296 }
30297 }
30298 }
30299
3026730300 // Differently-named floats with the same number of bits.
3026830301 if (dest_tag == .Float and src_tag == .Float) {
3026930302 const dest_bits = dest_ty.floatBits(target);
......@@ -30296,7 +30329,7 @@ pub fn coerceInMemoryAllowed(
3029630329 if (dest_tag == .ErrorUnion and src_tag == .ErrorUnion) {
3029730330 const dest_payload = dest_ty.errorUnionPayload(mod);
3029830331 const src_payload = src_ty.errorUnionPayload(mod);
30299 const child = try sema.coerceInMemoryAllowed(block, dest_payload, src_payload, dest_is_mut, target, dest_src, src_src);
30332 const child = try sema.coerceInMemoryAllowed(block, dest_payload, src_payload, dest_is_mut, target, dest_src, src_src, null);
3030030333 if (child != .ok) {
3030130334 return InMemoryCoercionResult{ .error_union_payload = .{
3030230335 .child = try child.dupe(sema.arena),
......@@ -30304,7 +30337,7 @@ pub fn coerceInMemoryAllowed(
3030430337 .wanted = dest_payload,
3030530338 } };
3030630339 }
30307 return try sema.coerceInMemoryAllowed(block, dest_ty.errorUnionSet(mod), src_ty.errorUnionSet(mod), dest_is_mut, target, dest_src, src_src);
30340 return try sema.coerceInMemoryAllowed(block, dest_ty.errorUnionSet(mod), src_ty.errorUnionSet(mod), dest_is_mut, target, dest_src, src_src, null);
3030830341 }
3030930342
3031030343 // Error Sets
......@@ -30323,7 +30356,7 @@ pub fn coerceInMemoryAllowed(
3032330356 } };
3032430357 }
3032530358
30326 const child = try sema.coerceInMemoryAllowed(block, dest_info.elem_type, src_info.elem_type, dest_is_mut, target, dest_src, src_src);
30359 const child = try sema.coerceInMemoryAllowed(block, dest_info.elem_type, src_info.elem_type, dest_is_mut, target, dest_src, src_src, null);
3032730360 if (child != .ok) {
3032830361 return InMemoryCoercionResult{ .array_elem = .{
3032930362 .child = try child.dupe(sema.arena),
......@@ -30362,7 +30395,7 @@ pub fn coerceInMemoryAllowed(
3036230395
3036330396 const dest_elem_ty = dest_ty.scalarType(mod);
3036430397 const src_elem_ty = src_ty.scalarType(mod);
30365 const child = try sema.coerceInMemoryAllowed(block, dest_elem_ty, src_elem_ty, dest_is_mut, target, dest_src, src_src);
30398 const child = try sema.coerceInMemoryAllowed(block, dest_elem_ty, src_elem_ty, dest_is_mut, target, dest_src, src_src, null);
3036630399 if (child != .ok) {
3036730400 return InMemoryCoercionResult{ .vector_elem = .{
3036830401 .child = try child.dupe(sema.arena),
......@@ -30389,7 +30422,7 @@ pub fn coerceInMemoryAllowed(
3038930422
3039030423 const dest_elem_ty = dest_ty.childType(mod);
3039130424 const src_elem_ty = src_ty.childType(mod);
30392 const child = try sema.coerceInMemoryAllowed(block, dest_elem_ty, src_elem_ty, dest_is_mut, target, dest_src, src_src);
30425 const child = try sema.coerceInMemoryAllowed(block, dest_elem_ty, src_elem_ty, dest_is_mut, target, dest_src, src_src, null);
3039330426 if (child != .ok) {
3039430427 return InMemoryCoercionResult{ .array_elem = .{
3039530428 .child = try child.dupe(sema.arena),
......@@ -30429,7 +30462,7 @@ pub fn coerceInMemoryAllowed(
3042930462 const dest_child_type = dest_ty.optionalChild(mod);
3043030463 const src_child_type = src_ty.optionalChild(mod);
3043130464
30432 const child = try sema.coerceInMemoryAllowed(block, dest_child_type, src_child_type, dest_is_mut, target, dest_src, src_src);
30465 const child = try sema.coerceInMemoryAllowed(block, dest_child_type, src_child_type, dest_is_mut, target, dest_src, src_src, null);
3043330466 if (child != .ok) {
3043430467 return InMemoryCoercionResult{ .optional_child = .{
3043530468 .child = try child.dupe(sema.arena),
......@@ -30451,7 +30484,7 @@ pub fn coerceInMemoryAllowed(
3045130484 if (dest_ty.structFieldAlign(field_idx, pt) != src_ty.structFieldAlign(field_idx, pt)) break :tuple;
3045230485 const dest_field_ty = dest_ty.structFieldType(field_idx, mod);
3045330486 const src_field_ty = src_ty.structFieldType(field_idx, mod);
30454 const field = try sema.coerceInMemoryAllowed(block, dest_field_ty, src_field_ty, dest_is_mut, target, dest_src, src_src);
30487 const field = try sema.coerceInMemoryAllowed(block, dest_field_ty, src_field_ty, dest_is_mut, target, dest_src, src_src, null);
3045530488 if (field != .ok) break :tuple;
3045630489 }
3045730490 return .ok;
......@@ -30598,7 +30631,7 @@ fn coerceInMemoryAllowedFns(
3059830631 else => {
3059930632 const dest_return_type = Type.fromInterned(dest_info.return_type);
3060030633 const src_return_type = Type.fromInterned(src_info.return_type);
30601 const rt = try sema.coerceInMemoryAllowed(block, dest_return_type, src_return_type, false, target, dest_src, src_src);
30634 const rt = try sema.coerceInMemoryAllowed(block, dest_return_type, src_return_type, false, target, dest_src, src_src, null);
3060230635 if (rt != .ok) {
3060330636 return InMemoryCoercionResult{ .fn_return_type = .{
3060430637 .child = try rt.dupe(sema.arena),
......@@ -30644,7 +30677,7 @@ fn coerceInMemoryAllowedFns(
3064430677 .generic_poison_type => {},
3064530678 else => {
3064630679 // Note: Cast direction is reversed here.
30647 const param = try sema.coerceInMemoryAllowed(block, src_param_ty, dest_param_ty, false, target, dest_src, src_src);
30680 const param = try sema.coerceInMemoryAllowed(block, src_param_ty, dest_param_ty, false, target, dest_src, src_src, null);
3064830681 if (param != .ok) {
3064930682 return InMemoryCoercionResult{ .fn_param = .{
3065030683 .child = try param.dupe(sema.arena),
......@@ -30708,13 +30741,13 @@ fn coerceInMemoryAllowedPtrs(
3070830741
3070930742 const dest_child = Type.fromInterned(dest_info.child);
3071030743 const src_child = Type.fromInterned(src_info.child);
30711 const child = try sema.coerceInMemoryAllowed(block, dest_child, src_child, !dest_info.flags.is_const, target, dest_src, src_src);
30744 const child = try sema.coerceInMemoryAllowed(block, dest_child, src_child, !dest_info.flags.is_const, target, dest_src, src_src, null);
3071230745 if (child != .ok) allow: {
3071330746 // As a special case, we also allow coercing `*[n:s]T` to `*[n]T`, akin to dropping the sentinel from a slice.
3071430747 // `*[n:s]T` cannot coerce in memory to `*[n]T` since they have different sizes.
3071530748 if (src_child.zigTypeTag(zcu) == .Array and dest_child.zigTypeTag(zcu) == .Array and
3071630749 src_child.sentinel(zcu) != null and dest_child.sentinel(zcu) == null and
30717 .ok == try sema.coerceInMemoryAllowed(block, dest_child.childType(zcu), src_child.childType(zcu), !dest_info.flags.is_const, target, dest_src, src_src))
30750 .ok == try sema.coerceInMemoryAllowed(block, dest_child.childType(zcu), src_child.childType(zcu), !dest_info.flags.is_const, target, dest_src, src_src, null))
3071830751 {
3071930752 break :allow;
3072030753 }
......@@ -31593,7 +31626,7 @@ fn coerceArrayLike(
3159331626 const target = mod.getTarget();
3159431627
3159531628 // try coercion of the whole array
31596 const in_memory_result = try sema.coerceInMemoryAllowed(block, dest_ty, inst_ty, false, target, dest_ty_src, inst_src);
31629 const in_memory_result = try sema.coerceInMemoryAllowed(block, dest_ty, inst_ty, false, target, dest_ty_src, inst_src, null);
3159731630 if (in_memory_result == .ok) {
3159831631 if (try sema.resolveValue(inst)) |inst_val| {
3159931632 // These types share the same comptime value representation.
......@@ -34108,13 +34141,13 @@ fn resolvePeerTypesInner(
3410834141 const peer_elem_ty = ty.childType(mod);
3410934142 if (!peer_elem_ty.eql(elem_ty, mod)) coerce: {
3411034143 const peer_elem_coerces_to_elem =
34111 try sema.coerceInMemoryAllowed(block, elem_ty, peer_elem_ty, false, mod.getTarget(), src, src);
34144 try sema.coerceInMemoryAllowed(block, elem_ty, peer_elem_ty, false, mod.getTarget(), src, src, null);
3411234145 if (peer_elem_coerces_to_elem == .ok) {
3411334146 break :coerce;
3411434147 }
3411534148
3411634149 const elem_coerces_to_peer_elem =
34117 try sema.coerceInMemoryAllowed(block, peer_elem_ty, elem_ty, false, mod.getTarget(), src, src);
34150 try sema.coerceInMemoryAllowed(block, peer_elem_ty, elem_ty, false, mod.getTarget(), src, src, null);
3411834151 if (elem_coerces_to_peer_elem == .ok) {
3411934152 elem_ty = peer_elem_ty;
3412034153 break :coerce;
......@@ -35039,12 +35072,12 @@ fn resolvePairInMemoryCoercible(sema: *Sema, block: *Block, src: LazySrcLoc, ty_
3503935072 const target = sema.pt.zcu.getTarget();
3504035073
3504135074 // ty_b -> ty_a
35042 if (.ok == try sema.coerceInMemoryAllowed(block, ty_a, ty_b, true, target, src, src)) {
35075 if (.ok == try sema.coerceInMemoryAllowed(block, ty_a, ty_b, true, target, src, src, null)) {
3504335076 return ty_a;
3504435077 }
3504535078
3504635079 // ty_a -> ty_b
35047 if (.ok == try sema.coerceInMemoryAllowed(block, ty_b, ty_a, true, target, src, src)) {
35080 if (.ok == try sema.coerceInMemoryAllowed(block, ty_b, ty_a, true, target, src, src, null)) {
3504835081 return ty_b;
3504935082 }
3505035083
src/Sema/comptime_ptr_access.zig+4-1
......@@ -321,6 +321,7 @@ fn loadComptimePtrInner(
321321 zcu.getTarget(),
322322 src,
323323 src,
324 null,
324325 )) {
325326 // We already have a value which is IMC to the desired type.
326327 return .{ .success = base_val };
......@@ -353,6 +354,7 @@ fn loadComptimePtrInner(
353354 zcu.getTarget(),
354355 src,
355356 src,
357 null,
356358 )) {
357359 // Changing the length of an array.
358360 const skip_base: u64 = extra_base_index + if (load_ty.zigTypeTag(zcu) == .Array) skip: {
......@@ -721,6 +723,7 @@ fn prepareComptimePtrStore(
721723 zcu.getTarget(),
722724 src,
723725 src,
726 null,
724727 )) {
725728 // The base strategy already gets us a value which the desired type is IMC to.
726729 return base_strat;
......@@ -753,7 +756,7 @@ fn prepareComptimePtrStore(
753756 else => unreachable,
754757 };
755758 const val_one_ty, const val_count = base_val.typeOf(zcu).arrayBase(zcu);
756 if (.ok != try sema.coerceInMemoryAllowed(block, val_one_ty, store_one_ty, true, zcu.getTarget(), src, src)) {
759 if (.ok != try sema.coerceInMemoryAllowed(block, val_one_ty, store_one_ty, true, zcu.getTarget(), src, src, null)) {
757760 break :restructure_array;
758761 }
759762 if (base_elem_offset + extra_base_index + store_count > val_count) return .{ .out_of_bounds = oob_ty };
test/cases/compile_errors/coercion_to_error_union.zig created+11
......@@ -0,0 +1,11 @@
1export fn b() void {
2 const x: anyerror!u8 = 256;
3 _ = x;
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// 2:28: error: expected type 'anyerror!u8', found 'comptime_int'
11// 2:28: note: type 'u8' cannot represent value '256'
test/cases/compile_errors/coercion_to_optional.zig created+11
......@@ -0,0 +1,11 @@
1export fn a() void {
2 const x: ?u8 = 256;
3 _ = x;
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// 2:20: error: expected type '?u8', found 'comptime_int'
11// 2:20: note: type 'u8' cannot represent value '256'