From 8088f462c95b3facd6a089529d70a97b8570d312 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 6 Jan 2022 18:02:52 +0200 Subject: [PATCH] parser: fix parsers producing codegen statements (for `[flag] enum MyEnum{}`) with mixed scope --- vlib/v/parser/parser.v | 10 ++++++++-- ...ptime_conditional_in_the_same_scope_test.v | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/enum_bitfield_works_with_comptime_conditional_in_the_same_scope_test.v diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index e0eb138a6f..4f314fc93b 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1,6 +1,7 @@ // Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. +[has_globals] module parser import v.scanner @@ -89,6 +90,8 @@ mut: codegen_text string } +__global codegen_files = []&ast.File{} + // for tests pub fn parse_stmt(text string, table &ast.Table, scope &ast.Scope) ast.Stmt { mut p := Parser{ @@ -312,8 +315,7 @@ pub fn (mut p Parser) parse() &ast.File { // codegen if p.codegen_text.len > 0 && !p.pref.is_fmt { ptext := 'module ' + p.mod.all_after_last('.') + p.codegen_text - codegen_file := parse_text(ptext, p.file_name, p.table, p.comments_mode, p.pref) - stmts << codegen_file.stmts + codegen_files << parse_text(ptext, p.file_name, p.table, p.comments_mode, p.pref) } return &ast.File{ @@ -404,6 +406,10 @@ pub fn parse_files(paths []string, table &ast.Table, pref &pref.Preferences) []& files << parse_file(path, table, .skip_comments, pref) timers.show('parse_file $path') } + if codegen_files.len > 0 { + files << codegen_files + codegen_files.clear() + } return files } diff --git a/vlib/v/tests/enum_bitfield_works_with_comptime_conditional_in_the_same_scope_test.v b/vlib/v/tests/enum_bitfield_works_with_comptime_conditional_in_the_same_scope_test.v new file mode 100644 index 0000000000..d244f81ee2 --- /dev/null +++ b/vlib/v/tests/enum_bitfield_works_with_comptime_conditional_in_the_same_scope_test.v @@ -0,0 +1,20 @@ +[flag] +pub enum Fill { + crash +} + +fn font_path() string { + $if dragonfly { + fonts := ['test', 'test/2'] + if true { + return fonts[0] + } + } + return '' +} + +fn test_compilation() { + a := Fill.crash + println(a) + assert true +}