authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-11-22 02:38:28+01:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-11-22 03:04:55+01:00
log83a0329c92dc8302b7098f718ceed2ed00e9fda2
tree6db9b5cd2959cce11fce5b852f721371dfa8f162
parent41b37712ea0af660365b1638b9b7f742990c175a

sema: move error set coercion to coerceInMemoryAlloed


1 files changed, 33 insertions(+), 46 deletions(-)

src/Sema.zig+33-46
......@@ -12358,31 +12358,6 @@ fn coerce(
1235812358 // T to E!T or E to E!T
1235912359 return sema.wrapErrorUnion(block, dest_ty, inst, inst_src);
1236012360 },
12361 .ErrorSet => switch (inst_ty.zigTypeTag()) {
12362 .ErrorSet => {
12363 // Coercion to `anyerror`. Note that this check can return false positives
12364 // in case the error sets did not get resolved.
12365 if (dest_ty.isAnyError()) {
12366 return sema.coerceCompatibleErrorSets(block, inst, inst_src);
12367 }
12368 // If both are inferred error sets of functions, and
12369 // the dest includes the source function, the coercion is OK.
12370 // This check is important because it works without forcing a full resolution
12371 // of inferred error sets.
12372 if (inst_ty.castTag(.error_set_inferred)) |src_payload| {
12373 if (dest_ty.castTag(.error_set_inferred)) |dst_payload| {
12374 const src_func = src_payload.data.func;
12375 const dst_func = dst_payload.data.func;
12376
12377 if (src_func == dst_func or dst_payload.data.functions.contains(src_func)) {
12378 return sema.coerceCompatibleErrorSets(block, inst, inst_src);
12379 }
12380 }
12381 }
12382 // TODO full error set resolution and compare sets by names.
12383 },
12384 else => {},
12385 },
1238612361 .Union => switch (inst_ty.zigTypeTag()) {
1238712362 .Enum, .EnumLiteral => return sema.coerceEnumToUnion(block, dest_ty, dest_ty_src, inst, inst_src),
1238812363 else => {},
......@@ -12440,15 +12415,47 @@ fn coerceInMemoryAllowed(dest_ty: Type, src_ty: Type, dest_is_mut: bool, target:
1244012415 return coerceInMemoryAllowedFns(dest_ty, src_ty, target);
1244112416 }
1244212417
12418 // Error Sets
12419 if (dest_ty.zigTypeTag() == .ErrorSet and src_ty.zigTypeTag() == .ErrorSet) {
12420 return coerceInMemoryAllowedErrorSets(dest_ty, src_ty);
12421 }
12422
1244312423 // TODO: arrays
1244412424 // TODO: non-pointer-like optionals
1244512425 // TODO: error unions
12446 // TODO: error sets
1244712426 // TODO: vectors
1244812427
1244912428 return .no_match;
1245012429}
1245112430
12431fn coerceInMemoryAllowedErrorSets(
12432 dest_ty: Type,
12433 src_ty: Type,
12434) InMemoryCoercionResult {
12435 // Coercion to `anyerror`. Note that this check can return false positives
12436 // in case the error sets did not get resolved.
12437 if (dest_ty.isAnyError()) {
12438 return .ok;
12439 }
12440 // If both are inferred error sets of functions, and
12441 // the dest includes the source function, the coercion is OK.
12442 // This check is important because it works without forcing a full resolution
12443 // of inferred error sets.
12444 if (src_ty.castTag(.error_set_inferred)) |src_payload| {
12445 if (dest_ty.castTag(.error_set_inferred)) |dst_payload| {
12446 const src_func = src_payload.data.func;
12447 const dst_func = dst_payload.data.func;
12448
12449 if (src_func == dst_func or dst_payload.data.functions.contains(src_func)) {
12450 return .ok;
12451 }
12452 }
12453 }
12454
12455 // TODO full error set resolution and compare sets by names.
12456 return .no_match;
12457}
12458
1245212459fn coerceInMemoryAllowedFns(
1245312460 dest_ty: Type,
1245412461 src_ty: Type,
......@@ -13224,26 +13231,6 @@ fn coerceVectorInMemory(
1322413231 return block.addBitCast(dest_ty, inst);
1322513232}
1322613233
13227fn coerceCompatibleErrorSets(
13228 sema: *Sema,
13229 block: *Block,
13230 err_set: Air.Inst.Ref,
13231 err_set_src: LazySrcLoc,
13232) !Air.Inst.Ref {
13233 if (try sema.resolveDefinedValue(block, err_set_src, err_set)) |err_set_val| {
13234 // Same representation works.
13235 return sema.addConstant(Type.anyerror, err_set_val);
13236 }
13237 try sema.requireRuntimeBlock(block, err_set_src);
13238 return block.addInst(.{
13239 .tag = .bitcast,
13240 .data = .{ .ty_op = .{
13241 .ty = Air.Inst.Ref.anyerror_type,
13242 .operand = err_set,
13243 } },
13244 });
13245}
13246
1324713234fn analyzeDeclVal(
1324813235 sema: *Sema,
1324913236 block: *Block,