make the 'canimport' template work
This commit is contained in:
parent
077ff83b6e
commit
eeea000582
2 changed files with 27 additions and 1 deletions
|
|
@ -2376,7 +2376,14 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
|
||||||
of nkMacroDef: result = semMacroDef(c, n)
|
of nkMacroDef: result = semMacroDef(c, n)
|
||||||
of nkTemplateDef: result = semTemplateDef(c, n)
|
of nkTemplateDef: result = semTemplateDef(c, n)
|
||||||
of nkImportStmt:
|
of nkImportStmt:
|
||||||
if not isTopLevel(c): localError(n.info, errXOnlyAtModuleScope, "import")
|
# this particular way allows 'import' in a 'compiles' context so that
|
||||||
|
# template canImport(x): bool =
|
||||||
|
# compiles:
|
||||||
|
# import x
|
||||||
|
#
|
||||||
|
# works:
|
||||||
|
if c.currentScope.depthLevel > 2 + c.compilesContextId:
|
||||||
|
localError(n.info, errXOnlyAtModuleScope, "import")
|
||||||
result = evalImport(c, n)
|
result = evalImport(c, n)
|
||||||
of nkImportExceptStmt:
|
of nkImportExceptStmt:
|
||||||
if not isTopLevel(c): localError(n.info, errXOnlyAtModuleScope, "import")
|
if not isTopLevel(c): localError(n.info, errXOnlyAtModuleScope, "import")
|
||||||
|
|
|
||||||
19
tests/modules/tcanimport.nim
Normal file
19
tests/modules/tcanimport.nim
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
discard """
|
||||||
|
output: '''ABC
|
||||||
|
nope'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
template canImport(x): bool =
|
||||||
|
compiles:
|
||||||
|
import x
|
||||||
|
|
||||||
|
when canImport(strutils):
|
||||||
|
import strutils
|
||||||
|
echo "abc".toUpperAscii
|
||||||
|
else:
|
||||||
|
echo "meh"
|
||||||
|
|
||||||
|
when canImport(none):
|
||||||
|
echo "what"
|
||||||
|
else:
|
||||||
|
echo "nope"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue