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...@@ -37125,7 +37125,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
37125 const msg = msg: {37125 const msg = msg: {
37126 const ty_src = mod.fieldSrcLoc(union_type.decl, .{37126 const ty_src = mod.fieldSrcLoc(union_type.decl, .{
37127 .index = field_i,37127 .index = field_i,
37128 .range = .type,37128 .range = .name,
37129 }).lazy;37129 }).lazy;
37130 const msg = try sema.errMsg(&block_scope, ty_src, "no field named '{}' in enum '{}'", .{37130 const msg = try sema.errMsg(&block_scope, ty_src, "no field named '{}' in enum '{}'", .{
37131 field_name.fmt(ip), Type.fromInterned(union_type.tagTypePtr(ip).*).fmt(mod),37131 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...@@ -37136,6 +37136,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
37136 };37136 };
37137 return sema.failWithOwnedErrorMsg(&block_scope, msg);37137 return sema.failWithOwnedErrorMsg(&block_scope, msg);
37138 };37138 };
37139
37139 // No check for duplicate because the check already happened in order37140 // No check for duplicate because the check already happened in order
37140 // to create the enum type in the first place.37141 // to create the enum type in the first place.
37141 assert(!explicit_tags_seen[enum_index]);37142 assert(!explicit_tags_seen[enum_index]);
...@@ -37146,14 +37147,15 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un...@@ -37146,14 +37147,15 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
37146 const msg = msg: {37147 const msg = msg: {
37147 const ty_src = mod.fieldSrcLoc(union_type.decl, .{37148 const ty_src = mod.fieldSrcLoc(union_type.decl, .{
37148 .index = field_i,37149 .index = field_i,
37149 .range = .type,37150 .range = .name,
37150 }).lazy;37151 }).lazy;
37151 const enum_field_src = mod.fieldSrcLoc(tag_info.decl, .{ .index = enum_index }).lazy;37152 const enum_field_src = mod.fieldSrcLoc(tag_info.decl, .{ .index = enum_index }).lazy;
37152 const msg = try sema.errMsg(&block_scope, ty_src, "union field '{}' ordered differently than corresponding enum field", .{37153 const msg = try sema.errMsg(&block_scope, ty_src, "union field '{}' ordered differently than corresponding enum field", .{
37153 field_name.fmt(ip),37154 field_name.fmt(ip),
37154 });37155 });
37155 errdefer msg.destroy(sema.gpa);37156 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", .{});
37157 break :msg msg;37159 break :msg msg;
37158 };37160 };
37159 return sema.failWithOwnedErrorMsg(&block_scope, msg);37161 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 {...@@ -18,5 +18,5 @@ export fn entry() void {
18// backend=stage218// backend=stage2
19// target=native19// target=native
20//20//
21// :10:8: error: no field named 'D' in enum 'tmp.Letter'21// :10:5: error: no field named 'D' in enum 'tmp.Letter'
22// :1:16: note: enum declared here22// :1:16: note: enum declared here
test/cases/compile_errors/union_extra_field.zig+1-1
...@@ -16,5 +16,5 @@ export fn entry() usize {...@@ -16,5 +16,5 @@ export fn entry() usize {
16// error16// error
17// target=native17// target=native
18//18//
19// :10:8: error: no field named 'd' in enum 'tmp.E'19// :10:5: error: no field named 'd' in enum 'tmp.E'
20// :1:11: note: enum declared here20// :1:11: note: enum declared here