From 129528723e18c783ff129a1cc20a803b3fa6ad5a Mon Sep 17 00:00:00 2001 From: Saem Ghani Date: Sat, 24 Aug 2024 17:21:01 -0700 Subject: [PATCH] fix: import current dir relative modules (#1432) ## Summary Attempts to import modules using relative import with multiple module syntax like `import ./[foo, bar]` no longer result in an error. Fixes: https://github.com/nim-works/nimskull/issues/1430 ## Details `./some_dir/[foo]` worked because the AST resulted in an `infix` node for the second directory separator `/` , but a `prefix` node when using `./[foo]` , `importEval` now takes this into account. A test has been included along with the fix. Along with this change some preexisting import statement tests were adjusted away from using `when false` to flag known issues to the builtin testament functionality of the `knownissue` field. --------- Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com> --- compiler/modules/importer.nim | 14 ++++++++- .../importstmt/timport_relative.nim | 9 ++++++ .../importstmt/ttyped_import_nimskull757.nim | 21 +------------ .../ttyped_import_nimskull757_knownissues.nim | 31 +++++++++++++++++++ 4 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 tests/lang_stmts/importstmt/timport_relative.nim create mode 100644 tests/lang_stmts/importstmt/ttyped_import_nimskull757_knownissues.nim diff --git a/compiler/modules/importer.nim b/compiler/modules/importer.nim index 8a5c76566e0..396214a3c4f 100644 --- a/compiler/modules/importer.nim +++ b/compiler/modules/importer.nim @@ -377,7 +377,19 @@ proc evalImport*(c: PContext, n: PNode): PNode = var hasError = false for it in n.sons: - if it.kind == nkInfix and it.len == 3 and it[2].kind == nkBracket: + if it.kind == nkPrefix and it.len == 2 and it[1].kind == nkBracket: + for x in it[1].items: + let imp = newTreeI(nkPrefix, it.info, + it[0], + # transform `./[a as b] to `./a as b` + if x.kind == nkInfix and x[0].ident.s == "as": + x[1] + else: + x + ) + + hasError = impMod(c, imp, x.info, result).kind == nkError or hasError + elif it.kind == nkInfix and it.len == 3 and it[2].kind == nkBracket: let sep = it[0] dir = it[1] diff --git a/tests/lang_stmts/importstmt/timport_relative.nim b/tests/lang_stmts/importstmt/timport_relative.nim new file mode 100644 index 00000000000..d850993f37e --- /dev/null +++ b/tests/lang_stmts/importstmt/timport_relative.nim @@ -0,0 +1,9 @@ +discard """ + description: "Regression test for relative imports" +""" + +# relative to current directory +import ./[mbar] +import ./[mbaz, mfoo] +import ./[mqux1 as mqux] +import ./mqux2 \ No newline at end of file diff --git a/tests/lang_stmts/importstmt/ttyped_import_nimskull757.nim b/tests/lang_stmts/importstmt/ttyped_import_nimskull757.nim index 9c438428fda..4f9327d583d 100644 --- a/tests/lang_stmts/importstmt/ttyped_import_nimskull757.nim +++ b/tests/lang_stmts/importstmt/ttyped_import_nimskull757.nim @@ -39,23 +39,4 @@ outputTyped: import mqux3 except bar3 import mqux4 except bar4, baz4 # import as output might have issues, see below - import mqux5 as mqux6 - -when false: # knownIssue: `import except` doesn't preserve exclusion info - # test should check for output: - # ImportExceptStmt - # Sym "mqux3" - # Sym "bar3" - # ImportExceptStmt - # Sym "mqux4" - # Sym "bar4" - # Sym "baz4" - outputTyped: - import mqux3 except bar3 - import mqux4 except bar4, baz4 - -when false: # knownIssue: `Import as` doesn't preserve original identifier info - # not quite sure what the test should be yet, current behaviour might be - # acceptable, because the symbol decl can be introspected? - outputTyped: - import mqux5 as mqux6 \ No newline at end of file + import mqux5 as mqux6 \ No newline at end of file diff --git a/tests/lang_stmts/importstmt/ttyped_import_nimskull757_knownissues.nim b/tests/lang_stmts/importstmt/ttyped_import_nimskull757_knownissues.nim new file mode 100644 index 00000000000..033a0a08c47 --- /dev/null +++ b/tests/lang_stmts/importstmt/ttyped_import_nimskull757_knownissues.nim @@ -0,0 +1,31 @@ +discard """ + description: "Test import statements are properly analysed" + knownissue: "`import except` doesn't preserve exclusion info" + output: ''' +StmtList + ImportExceptStmt + Sym "mqux3" + Sym "bar3" + ImportExceptStmt + Sym "mqux4" + Sym "bar4" + Sym "baz4" +''' +""" + +import std/macros + +macro outputTyped(n: typed) = + let output = treeRepr n + quote: + echo `output` + +outputTyped: + import mqux3 except bar3 + import mqux4 except bar4, baz4 + +when false: # knownIssue: `Import as` doesn't preserve original identifier info + # not quite sure what the test should be yet, current behaviour might be + # acceptable, because the symbol decl can be introspected? + outputTyped: + import mqux5 as mqux6 \ No newline at end of file