From e730172e47749db8a9d3be5949231bde95343b39 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 7 Apr 2021 22:02:45 -0700 Subject: 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. --- src/Module.zig | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/Module.zig') 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 -- cgit v1.2.3