Fix nkImportAs regression (#8796)

This commit is contained in:
Oscar Nihlgård 2018-08-28 11:35:52 +02:00 • committed by Andreas Rumpf
commit 96363ecaf3
2 changed files with 19 additions and 9 deletions

View file

@ -164,7 +164,16 @@ proc myImportModule(c: PContext, n: PNode; importStmtResult: PNode): PSym =
suggestSym(c.config, n.info, result, c.graph.usageSym, false) suggestSym(c.config, n.info, result, c.graph.usageSym, false)
importStmtResult.add newStrNode(toFullPath(c.config, f), n.info) importStmtResult.add newStrNode(toFullPath(c.config, f), n.info)
proc transformImportAs(n: PNode): PNode =
if n.kind == nkInfix and n.sons[0].ident.s == "as":
result = newNodeI(nkImportAs, n.info)
result.add n.sons[1]
result.add n.sons[2]
else:
result = n
proc impMod(c: PContext; it: PNode; importStmtResult: PNode) = proc impMod(c: PContext; it: PNode; importStmtResult: PNode) =
let it = transformImportAs(it)
let m = myImportModule(c, it, importStmtResult) let m = myImportModule(c, it, importStmtResult)
if m != nil: if m != nil:
var emptySet: IntSet var emptySet: IntSet
@ -185,27 +194,22 @@ proc evalImport(c: PContext, n: PNode): PNode =
imp.add dir imp.add dir
imp.add sep # dummy entry, replaced in the loop imp.add sep # dummy entry, replaced in the loop
for x in it[2]: for x in it[2]:
# transform `a/b/[c as d]` to `/a/b/c as d`
if x.kind == nkInfix and x.sons[0].ident.s == "as": if x.kind == nkInfix and x.sons[0].ident.s == "as":
let impAs = copyTree(x)
imp.sons[2] = x.sons[1] imp.sons[2] = x.sons[1]
let impAs = newNodeI(nkImportAs, it.info) impAs.sons[1] = imp
impAs.add imp
impAs.add x.sons[2]
imp = impAs
impMod(c, imp, result) impMod(c, imp, result)
else: else:
imp.sons[2] = x imp.sons[2] = x
impMod(c, imp, result) impMod(c, imp, result)
elif it.kind == nkInfix and it.sons[0].ident.s == "as":
let imp = newNodeI(nkImportAs, it.info)
imp.add it.sons[1]
imp.add it.sons[2]
impMod(c, imp, result)
else: else:
impMod(c, it, result) impMod(c, it, result)
proc evalFrom(c: PContext, n: PNode): PNode = proc evalFrom(c: PContext, n: PNode): PNode =
result = newNodeI(nkImportStmt, n.info) result = newNodeI(nkImportStmt, n.info)
checkMinSonsLen(n, 2, c.config) checkMinSonsLen(n, 2, c.config)
n.sons[0] = transformImportAs(n.sons[0])
var m = myImportModule(c, n.sons[0], result) var m = myImportModule(c, n.sons[0], result)
if m != nil: if m != nil:
n.sons[0] = newSymNode(m) n.sons[0] = newSymNode(m)
@ -217,6 +221,7 @@ proc evalFrom(c: PContext, n: PNode): PNode =
proc evalImportExcept*(c: PContext, n: PNode): PNode = proc evalImportExcept*(c: PContext, n: PNode): PNode =
result = newNodeI(nkImportStmt, n.info) result = newNodeI(nkImportStmt, n.info)
checkMinSonsLen(n, 2, c.config) checkMinSonsLen(n, 2, c.config)
n.sons[0] = transformImportAs(n.sons[0])
var m = myImportModule(c, n.sons[0], result) var m = myImportModule(c, n.sons[0], result)
if m != nil: if m != nil:
n.sons[0] = newSymNode(m) n.sons[0] = newSymNode(m)

View file

@ -3,9 +3,14 @@ discard """
""" """
import .. / modules / [definitions as foo] import .. / modules / [definitions as foo]
import .. / modules / definitions as foo
import std / times as bar import std / times as bar
from times as bar2 import nil
import times as bar3 except convert
import definitions as baz import definitions as baz
discard foo.v discard foo.v
discard bar.now discard bar.now
discard bar2.now
discard bar3.now
discard baz.v discard baz.v