disable the undocumented 'import ' feature; allow import std / strutils; fixes #2819; refs #6700

This commit is contained in:
Andreas Rumpf 2018-02-12 16:36:19 +01:00
commit cddc389784
3 changed files with 119 additions and 96 deletions

View file

@ -251,3 +251,12 @@ bar()
- The ``securehash`` module is now deprecated. Instead import ``std / sha1``. - The ``securehash`` module is now deprecated. Instead import ``std / sha1``.
- ``db_mysql`` module: ``DbConn`` is now a ``distinct`` type that doesn't expose the - ``db_mysql`` module: ``DbConn`` is now a ``distinct`` type that doesn't expose the
details of the underlying ``PMySQL`` type. details of the underlying ``PMySQL`` type.
- Standard library modules can now also be imported via the ``std`` pseudo-directory.
This is useful in order to distinguish between standard library and nimble package
imports:
```nim
import std / [strutils, os, osproc]
import someNimblePackage / [strutils, os]
```

View file

@ -274,9 +274,10 @@ proc testCompileOption*(switch: string, info: TLineInfo): bool =
proc processPath(path: string, info: TLineInfo, proc processPath(path: string, info: TLineInfo,
notRelativeToProj = false): string = notRelativeToProj = false): string =
let p = if notRelativeToProj or os.isAbsolute(path) or let p = if os.isAbsolute(path) or '$' in path:
'$' in path:
path path
elif notRelativeToProj:
getCurrentDir() / path
else: else:
options.gProjectPath / path options.gProjectPath / path
try: try:

View file

@ -11,11 +11,12 @@ import ast, renderer, strutils, msgs, options, idents, os
import nimblecmd import nimblecmd
const when false:
const
considerParentDirs = not defined(noParentProjects) considerParentDirs = not defined(noParentProjects)
considerNimbleDirs = not defined(noNimbleDirs) considerNimbleDirs = not defined(noNimbleDirs)
proc findInNimbleDir(pkg, subdir, dir: string): string = proc findInNimbleDir(pkg, subdir, dir: string): string =
var best = "" var best = ""
var bestv = "" var bestv = ""
for k, p in os.walkDir(dir, relative=true): for k, p in os.walkDir(dir, relative=true):
@ -51,7 +52,8 @@ const stdlibDirs = [
"wrappers", "wrappers/linenoise", "wrappers", "wrappers/linenoise",
"windows", "posix", "js"] "windows", "posix", "js"]
proc resolveDollar(project, source, pkg, subdir: string; info: TLineInfo): string = when false:
proc resolveDollar(project, source, pkg, subdir: string; info: TLineInfo): string =
template attempt(a) = template attempt(a) =
let x = addFileExt(a, "nim") let x = addFileExt(a, "nim")
if fileExists(x): return x if fileExists(x): return x
@ -96,11 +98,11 @@ proc resolveDollar(project, source, pkg, subdir: string; info: TLineInfo): strin
result = findInNimbleDir(pkg, subdir, "/opt/nimble/pkgs") result = findInNimbleDir(pkg, subdir, "/opt/nimble/pkgs")
if result.len > 0: return result if result.len > 0: return result
proc scriptableImport(pkg, sub: string; info: TLineInfo): string = proc scriptableImport(pkg, sub: string; info: TLineInfo): string =
result = resolveDollar(gProjectFull, info.toFullPath(), pkg, sub, info) result = resolveDollar(gProjectFull, info.toFullPath(), pkg, sub, info)
if result.isNil: result = "" if result.isNil: result = ""
proc lookupPackage(pkg, subdir: PNode): string = proc lookupPackage(pkg, subdir: PNode): string =
let sub = if subdir != nil: renderTree(subdir, {renderNoComments}).replace(" ") else: "" let sub = if subdir != nil: renderTree(subdir, {renderNoComments}).replace(" ") else: ""
case pkg.kind case pkg.kind
of nkStrLit, nkRStrLit, nkTripleStrLit: of nkStrLit, nkRStrLit, nkTripleStrLit:
@ -136,6 +138,7 @@ proc getModuleName*(n: PNode): string =
n.sons[1] = n.sons[2] n.sons[1] = n.sons[2]
n.sons.setLen(2) n.sons.setLen(2)
return getModuleName(n.sons[0]) return getModuleName(n.sons[0])
when false:
if n1.kind == nkPrefix and n1[0].kind == nkIdent and n1[0].ident.s == "$": if n1.kind == nkPrefix and n1[0].kind == nkIdent and n1[0].ident.s == "$":
if n0.kind == nkIdent and n0.ident.s == "/": if n0.kind == nkIdent and n0.ident.s == "/":
result = lookupPackage(n1[1], n[2]) result = lookupPackage(n1[1], n[2])
@ -143,14 +146,24 @@ proc getModuleName*(n: PNode): string =
localError(n.info, "only '/' supported with $package notation") localError(n.info, "only '/' supported with $package notation")
result = "" result = ""
else: else:
let modname = getModuleName(n[2])
if $n1 == "std":
template attempt(a) =
let x = addFileExt(a, "nim")
if fileExists(x): return x
for candidate in stdlibDirs:
attempt(options.libpath / candidate / modname)
# hacky way to implement 'x / y /../ z': # hacky way to implement 'x / y /../ z':
result = getModuleName(n1) result = getModuleName(n1)
result.add renderTree(n0, {renderNoComments}) result.add renderTree(n0, {renderNoComments})
result.add getModuleName(n[2]) result.add modname
of nkPrefix: of nkPrefix:
when false:
if n.sons[0].kind == nkIdent and n.sons[0].ident.s == "$": if n.sons[0].kind == nkIdent and n.sons[0].ident.s == "$":
result = lookupPackage(n[1], nil) result = lookupPackage(n[1], nil)
else: else:
discard
# hacky way to implement 'x / y /../ z': # hacky way to implement 'x / y /../ z':
result = renderTree(n, {renderNoComments}).replace(" ") result = renderTree(n, {renderNoComments}).replace(" ")
of nkDotExpr: of nkDotExpr: