diff options
| author | John Schmidt <john.schmidt.h@gmail.com> | 2022-03-18 17:35:30 +0100 |
|---|---|---|
| committer | John Schmidt <john.schmidt.h@gmail.com> | 2022-04-03 13:49:34 +0200 |
| commit | 8f0dac01ef00574167ca99b716d9903c86a96a3d (patch) | |
| tree | d63c99ca1f7997557b19369aeae915516acef13d /src | |
| parent | 91eb1af9177d774158d888484023e3bf0be65412 (diff) | |
| download | zig-8f0dac01ef00574167ca99b716d9903c86a96a3d.tar.gz zig-8f0dac01ef00574167ca99b716d9903c86a96a3d.zip | |
sema: add more info to error message for invalid comptime union field access
Diffstat (limited to 'src')
| -rw-r--r-- | src/Sema.zig | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 65550bec00..5f997f131e 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -17484,9 +17484,15 @@ fn unionFieldVal( if (tag_matches) { return sema.addConstant(field.ty, tag_and_val.val); } else { - // TODO enhance this saying which one was active - // and which one was accessed, and showing where the union was declared. - return sema.fail(block, src, "access of inactive union field", .{}); + const msg = msg: { + const active_index = tag_and_val.tag.castTag(.enum_field_index).?.data; + const active_field_name = union_obj.fields.keys()[active_index]; + const msg = try sema.errMsg(block, src, "access of union field '{s}' while field '{s}' is active", .{ field_name, active_field_name }); + errdefer msg.destroy(sema.gpa); + try sema.addDeclaredHereNote(msg, union_ty); + break :msg msg; + }; + return sema.failWithOwnedErrorMsg(block, msg); } }, .Packed, .Extern => { |
