Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: import current dir relative modules #1432

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion compiler/modules/importer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,29 @@ 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:
let
sep = it[0]
impTmpl =
block:
let t = newNodeI(nkPrefix, it.info, 2)
t[0] = sep
t
## `impTmpl` is copied/instanced in a loop below including setting
## the second child

for x in it[1]:
let imp = copyTree(impTmpl)

# transform `./[a as b] to `./a as b`
imp[1] =
if x.kind == nkInfix and x[0].ident.s == "as":
x[1]
else:
x
saem marked this conversation as resolved.
Show resolved Hide resolved

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
7 changes: 7 additions & 0 deletions tests/lang_stmts/importstmt/timport_relative.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
discard """
description: "Regression test for relative imports"
"""

import ./[mbar]
import ./[mbaz, mfoo]
import ./[mqux1 as mqux]
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
Loading