diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-04-07 22:02:45 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-04-07 22:02:45 -0700 |
| commit | e730172e47749db8a9d3be5949231bde95343b39 (patch) | |
| tree | d77ee85e8f3c99a6a13979a22e8650063677b75a /src | |
| parent | b67378fb08fb3129b66385479a278a93940f09f5 (diff) | |
| download | zig-e730172e47749db8a9d3be5949231bde95343b39.tar.gz zig-e730172e47749db8a9d3be5949231bde95343b39.zip | |
stage2: fix switch validation of handling all enum values
There were several problems, all fixed:
* AstGen was storing field names as references to the original
source code bytes. However, that data would be destroyed when the
source file is updated. Now, it correctly stores the field names in
the Decl arena for the enum. The same fix applies to error set field
names.
* Sema was missing a memset inside `analyzeSwitch`, leaving the "seen
enum fields" array with undefined memory. Now that they are all
properly set to null, the validation works.
* Moved the "enum declared here" note to the end. It looked weird
interrupting the notes for which enum values were missing.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Module.zig | 21 | ||||
| -rw-r--r-- | src/Sema.zig | 16 |
2 files changed, 25 insertions, 12 deletions
diff --git a/src/Module.zig b/src/Module.zig index ccf6996cae..933917d948 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -4582,28 +4582,39 @@ pub fn optimizeMode(mod: Module) std.builtin.Mode { /// Otherwise, returns a reference to the source code bytes directly. /// See also `appendIdentStr` and `parseStrLit`. pub fn identifierTokenString(mod: *Module, scope: *Scope, token: ast.TokenIndex) InnerError![]const u8 { - return mod.identifierTokenStringTreeArena(scope, token, scope.tree(), scope.arena()); + const tree = scope.tree(); + const token_tags = tree.tokens.items(.tag); + assert(token_tags[token] == .identifier); + const ident_name = tree.tokenSlice(token); + if (!mem.startsWith(u8, ident_name, "@")) { + return ident_name; + } + var buf: ArrayListUnmanaged(u8) = .{}; + defer buf.deinit(mod.gpa); + try parseStrLit(mod, scope, token, &buf, ident_name, 1); + const duped = try scope.arena().dupe(u8, buf.items); + return duped; } /// `scope` is only used for error reporting. +/// The string is stored in `arena` regardless of whether it uses @"" syntax. pub fn identifierTokenStringTreeArena( mod: *Module, scope: *Scope, token: ast.TokenIndex, tree: *const ast.Tree, arena: *Allocator, -) InnerError![]const u8 { +) InnerError![]u8 { const token_tags = tree.tokens.items(.tag); assert(token_tags[token] == .identifier); const ident_name = tree.tokenSlice(token); if (!mem.startsWith(u8, ident_name, "@")) { - return ident_name; + return arena.dupe(u8, ident_name); } var buf: ArrayListUnmanaged(u8) = .{}; defer buf.deinit(mod.gpa); try parseStrLit(mod, scope, token, &buf, ident_name, 1); - const duped = try arena.dupe(u8, buf.items); - return duped; + return arena.dupe(u8, buf.items); } /// Given an identifier token, obtain the string for it (possibly parsing as a string diff --git a/src/Sema.zig b/src/Sema.zig index 134e569948..a0be08ed71 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2789,6 +2789,8 @@ fn analyzeSwitch( var seen_fields = try gpa.alloc(?AstGen.SwitchProngSrc, operand.ty.enumFieldCount()); defer gpa.free(seen_fields); + mem.set(?AstGen.SwitchProngSrc, seen_fields, null); + var extra_index: usize = special.end; { var scalar_i: u32 = 0; @@ -2849,12 +2851,6 @@ fn analyzeSwitch( .{}, ); errdefer msg.destroy(sema.gpa); - try mod.errNoteNonLazy( - operand.ty.declSrcLoc(), - msg, - "enum '{}' declared here", - .{operand.ty}, - ); for (seen_fields) |seen_src, i| { if (seen_src != null) continue; @@ -2865,10 +2861,16 @@ fn analyzeSwitch( &block.base, src, msg, - "unhandled enumeration value: '{s}", + "unhandled enumeration value: '{s}'", .{field_name}, ); } + try mod.errNoteNonLazy( + operand.ty.declSrcLoc(), + msg, + "enum '{}' declared here", + .{operand.ty}, + ); break :msg msg; }; return mod.failWithOwnedErrorMsg(&block.base, msg); |
