Commit 6cf7fb1177

Jimmi Holst Christensen <jimmiholstchristensen@gmail.com>
2019-05-11 20:51:59
fixes #2235
1 parent ba3d18a
Changed files (2)
src
test
stage1
behavior
src/parser.cpp
@@ -1158,10 +1158,6 @@ static AstNode *ast_parse_prefix_expr(ParseContext *pc) {
 //      / Block
 //      / CurlySuffixExpr
 static AstNode *ast_parse_primary_expr(ParseContext *pc) {
-    AstNode *enum_lit = ast_parse_enum_lit(pc);
-    if (enum_lit != nullptr)
-        return enum_lit;
-
     AstNode *asm_expr = ast_parse_asm_expr(pc);
     if (asm_expr != nullptr)
         return asm_expr;
@@ -1496,6 +1492,7 @@ static AstNode *ast_parse_suffix_expr(ParseContext *pc) {
 //     <- BUILTINIDENTIFIER FnCallArguments
 //      / CHAR_LITERAL
 //      / ContainerDecl
+//     / DOT IDENTIFIER
 //      / ErrorSetDecl
 //      / FLOAT
 //      / FnProto
@@ -1556,6 +1553,10 @@ static AstNode *ast_parse_primary_type_expr(ParseContext *pc) {
     if (container_decl != nullptr)
         return container_decl;
 
+    AstNode *enum_lit = ast_parse_enum_lit(pc);
+    if (enum_lit != nullptr)
+        return enum_lit;
+
     AstNode *error_set_decl = ast_parse_error_set_decl(pc);
     if (error_set_decl != nullptr)
         return error_set_decl;
@@ -1958,7 +1959,14 @@ static AstNode *ast_parse_field_init(ParseContext *pc) {
         return nullptr;
 
     Token *name = expect_token(pc, TokenIdSymbol);
-    expect_token(pc, TokenIdEq);
+    if (eat_token_if(pc, TokenIdEq) == nullptr) {
+        // Because ".Name" can also be intepreted as an enum literal, we should put back
+        // those two tokens again so that the parser can try to parse them as the enum
+        // literal later.
+        put_back_token(pc);
+        put_back_token(pc);
+        return nullptr;
+    }
     AstNode *expr = ast_expect(pc, ast_parse_expr);
 
     AstNode *res = ast_create_node(pc, NodeTypeStructValueField, first);
test/stage1/behavior/enum.zig
@@ -923,3 +923,18 @@ test "peer type resolution with enum literal" {
     expect(Items.two == .two);
     expect(.two == Items.two);
 }
+
+test "enum literal in array literal" {
+    const Items = enum {
+        one,
+        two,
+    };
+
+    const array = []Items {
+      .one,
+      .two,
+    };
+
+    expect(array[0] == .one);
+    expect(array[1] == .two);
+}