authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-02-03 11:52:05-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-02-03 19:52:05+00:00
log122387943bf1d00c85aba77f37c93072e43140c9
tree9b2d270404d51c0a0d4f1c05b0302dae76270b57
parenteb4024036d3e8e0c4a8b0dcf09da107421f3fa01
signaturebadge-check Signed by PGP key B5690EEEBB952194

Fix OOB when enum field out of order in different file


3 files changed, 7 insertions(+), 5 deletions(-)

src/Sema.zig+5-3
......@@ -37125,7 +37125,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
3712537125 const msg = msg: {
3712637126 const ty_src = mod.fieldSrcLoc(union_type.decl, .{
3712737127 .index = field_i,
37128 .range = .type,
37128 .range = .name,
3712937129 }).lazy;
3713037130 const msg = try sema.errMsg(&block_scope, ty_src, "no field named '{}' in enum '{}'", .{
3713137131 field_name.fmt(ip), Type.fromInterned(union_type.tagTypePtr(ip).*).fmt(mod),
......@@ -37136,6 +37136,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
3713637136 };
3713737137 return sema.failWithOwnedErrorMsg(&block_scope, msg);
3713837138 };
37139
3713937140 // No check for duplicate because the check already happened in order
3714037141 // to create the enum type in the first place.
3714137142 assert(!explicit_tags_seen[enum_index]);
......@@ -37146,14 +37147,15 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
3714637147 const msg = msg: {
3714737148 const ty_src = mod.fieldSrcLoc(union_type.decl, .{
3714837149 .index = field_i,
37149 .range = .type,
37150 .range = .name,
3715037151 }).lazy;
3715137152 const enum_field_src = mod.fieldSrcLoc(tag_info.decl, .{ .index = enum_index }).lazy;
3715237153 const msg = try sema.errMsg(&block_scope, ty_src, "union field '{}' ordered differently than corresponding enum field", .{
3715337154 field_name.fmt(ip),
3715437155 });
3715537156 errdefer msg.destroy(sema.gpa);
37156 try sema.errNote(&block_scope, enum_field_src, msg, "enum field here", .{});
37157 const decl_ptr = mod.declPtr(tag_info.decl);
37158 try mod.errNoteNonLazy(enum_field_src.toSrcLoc(decl_ptr, mod), msg, "enum field here", .{});
3715737159 break :msg msg;
3715837160 };
3715937161 return sema.failWithOwnedErrorMsg(&block_scope, msg);
test/cases/compile_errors/union_enum_field_does_not_match_enum.zig+1-1
......@@ -18,5 +18,5 @@ export fn entry() void {
1818// backend=stage2
1919// target=native
2020//
21// :10:8: error: no field named 'D' in enum 'tmp.Letter'
21// :10:5: error: no field named 'D' in enum 'tmp.Letter'
2222// :1:16: note: enum declared here
test/cases/compile_errors/union_extra_field.zig+1-1
......@@ -16,5 +16,5 @@ export fn entry() usize {
1616// error
1717// target=native
1818//
19// :10:8: error: no field named 'd' in enum 'tmp.E'
19// :10:5: error: no field named 'd' in enum 'tmp.E'
2020// :1:11: note: enum declared here