Skip to content

Commit

Permalink
fix: import current dir relative modules (#1432)
Browse files Browse the repository at this point in the history
## Summary

Attempts to import modules using relative import with multiple module
syntax like  `import ./[foo, bar]`  no longer result in an error.

Fixes: #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 <[email protected]>
  • Loading branch information
saem and zerbina authored Aug 25, 2024
1 parent 89b45fc commit 1295287
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
14 changes: 13 additions & 1 deletion compiler/modules/importer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 9 additions & 0 deletions tests/lang_stmts/importstmt/timport_relative.nim
Original file line number Diff line number Diff line change
@@ -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
21 changes: 1 addition & 20 deletions tests/lang_stmts/importstmt/ttyped_import_nimskull757.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
import mqux5 as mqux6
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1295287

Please sign in to comment.