diff --git a/vlib/v/checker/tests/import_middle_err.out b/vlib/v/checker/tests/import_middle_err.out new file mode 100644 index 0000000000..fbca4308db --- /dev/null +++ b/vlib/v/checker/tests/import_middle_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/import_middle_err.v:5:1: error: `import x` can only be declared at the beginning of the file + 3 | println('hello, world') + 4 | } + 5 | import os + | ~~~~~~ + 6 | fn main() { + 7 | println(time.now()) diff --git a/vlib/v/checker/tests/import_middle_err.vv b/vlib/v/checker/tests/import_middle_err.vv new file mode 100644 index 0000000000..f38e462265 --- /dev/null +++ b/vlib/v/checker/tests/import_middle_err.vv @@ -0,0 +1,8 @@ +import time +fn show() { + println('hello, world') +} +import os +fn main() { + println(time.now()) +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 2ae42b795d..195bd8a1de 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -89,18 +89,15 @@ pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.Comme stmt = com stmts << stmt } + // module mut mstmt := ast.Stmt{} module_decl := p.module_decl() mstmt = module_decl stmts << mstmt // imports - /* - mut imports := []ast.Import{} for p.tok.kind == .key_import { - imports << p.import_stmt() + stmts << p.import_stmt() } - */ - // TODO: import only mode for { if p.tok.kind == .eof { if p.pref.is_script && !p.pref.is_test && p.mod == 'main' && !have_fn_main(stmts) { @@ -320,6 +317,7 @@ pub fn (mut p Parser) top_stmt() ast.Stmt { return p.interface_decl() } .key_import { + p.error_with_pos('`import x` can only be declared at the beginning of the file', p.tok.position()) return p.import_stmt() } .key_global {