Fix OOB when enum field out of order in different file

This commit is contained in:
David Rubin 2024-02-03 11:52:05 -08:00 committed by GitHub
parent eb4024036d
commit 122387943b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 7 additions and 5 deletions

View File

@ -37125,7 +37125,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
const msg = msg: {
const ty_src = mod.fieldSrcLoc(union_type.decl, .{
.index = field_i,
.range = .type,
.range = .name,
}).lazy;
const msg = try sema.errMsg(&block_scope, ty_src, "no field named '{}' in enum '{}'", .{
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
};
return sema.failWithOwnedErrorMsg(&block_scope, msg);
};
// No check for duplicate because the check already happened in order
// to create the enum type in the first place.
assert(!explicit_tags_seen[enum_index]);
@ -37146,14 +37147,15 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
const msg = msg: {
const ty_src = mod.fieldSrcLoc(union_type.decl, .{
.index = field_i,
.range = .type,
.range = .name,
}).lazy;
const enum_field_src = mod.fieldSrcLoc(tag_info.decl, .{ .index = enum_index }).lazy;
const msg = try sema.errMsg(&block_scope, ty_src, "union field '{}' ordered differently than corresponding enum field", .{
field_name.fmt(ip),
});
errdefer msg.destroy(sema.gpa);
try sema.errNote(&block_scope, enum_field_src, msg, "enum field here", .{});
const decl_ptr = mod.declPtr(tag_info.decl);
try mod.errNoteNonLazy(enum_field_src.toSrcLoc(decl_ptr, mod), msg, "enum field here", .{});
break :msg msg;
};
return sema.failWithOwnedErrorMsg(&block_scope, msg);

View File

@ -18,5 +18,5 @@ export fn entry() void {
// backend=stage2
// target=native
//
// :10:8: error: no field named 'D' in enum 'tmp.Letter'
// :10:5: error: no field named 'D' in enum 'tmp.Letter'
// :1:16: note: enum declared here

View File

@ -16,5 +16,5 @@ export fn entry() usize {
// error
// target=native
//
// :10:8: error: no field named 'd' in enum 'tmp.E'
// :10:5: error: no field named 'd' in enum 'tmp.E'
// :1:11: note: enum declared here