merge devel

This commit is contained in:
cooldome 2018-06-12 21:19:30 +01:00
commit 37aa488f99
136 changed files with 4276 additions and 5649 deletions

View file

@ -1,18 +0,0 @@
discard """
file: "tasynctry2.nim"
errormsg: "\'yield\' cannot be used within \'try\' in a non-inlined iterator"
line: 14
"""
import asyncdispatch
{.experimental: "oldIterTransf".}
proc foo(): Future[bool] {.async.} = discard
proc test5(): Future[int] {.async.} =
try:
discard await foo()
raise newException(ValueError, "Test5")
except:
discard await foo()
result = 0

View file

@ -0,0 +1,3 @@
proc addFloats*(x, y, z: float): float =
discard "implementation overriden by tcompilerapi.nim"

View file

@ -0,0 +1,9 @@
import exposed
echo "top level statements are executed!"
proc hostProgramRunsThis*(a, b: float): float =
result = addFloats(a, b, 1.0)
let hostProgramWantsThis* = "my secret"

View file

@ -0,0 +1,47 @@
discard """
output: '''top level statements are executed!
2.0
my secret
'''
"""
## Example program that demonstrates how to use the
## compiler as an API to embed into your own projects.
import "../../compiler" / [ast, vmdef, vm, nimeval]
import std / [os]
proc main() =
let std = findNimStdLib()
if std.len == 0:
quit "cannot find Nim's standard library"
var intr = createInterpreter("myscript.nim", [std, getAppDir()])
intr.implementRoutine("*", "exposed", "addFloats", proc (a: VmArgs) =
setResult(a, getFloat(a, 0) + getFloat(a, 1) + getFloat(a, 2))
)
intr.evalScript()
let foreignProc = selectRoutine(intr, "hostProgramRunsThis")
if foreignProc == nil:
quit "script does not export a proc of the name: 'hostProgramRunsThis'"
let res = intr.callRoutine(foreignProc, [newFloatNode(nkFloatLit, 0.9),
newFloatNode(nkFloatLit, 0.1)])
if res.kind == nkFloatLit:
echo res.floatVal
else:
echo "bug!"
let foreignValue = selectUniqueSymbol(intr, "hostProgramWantsThis")
if foreignValue == nil:
quit "script does not export a global of the name: hostProgramWantsThis"
let val = intr.getGlobalValue(foreignValue)
if val.kind in {nkStrLit..nkTripleStrLit}:
echo val.strVal
else:
echo "bug!"
destroyInterpreter(intr)
main()

27
tests/concepts/t6770.nim Normal file
View file

@ -0,0 +1,27 @@
discard """
output: '''
10
10
'''
"""
type GA = concept c
c.a is int
type A = object
a: int
type AA = object
case exists: bool
of true:
a: int
else:
discard
proc print(inp: GA) =
echo inp.a
let failing = AA(exists: true, a: 10)
let working = A(a:10)
print(working)
print(failing)

View file

@ -20,3 +20,19 @@ v.doSomething()
var vf = initVector[float]()
vf.doSomething() # Nim uses doSomething[int] here in C++
# Alternative definition:
# https://github.com/nim-lang/Nim/issues/7653
type VectorAlt* {.importcpp: "std::vector", header: "<vector>", nodecl.} [T] = object
proc mkVector*[T]: VectorAlt[T] {.importcpp: "std::vector<'*0>()", header: "<vector>", constructor, nodecl.}
proc foo(): VectorAlt[cint] =
mkVector[cint]()
proc bar(): VectorAlt[cstring] =
mkVector[cstring]()
var x = foo()
var y = bar()

View file

@ -1,11 +1,141 @@
discard """
outputsub: '''tproper_stacktrace.nim(7) tproper_stacktrace'''
exitcode: 1
output: '''ok'''
"""
import strscans, strutils
template fuzzy(x) =
echo x[] != 9
proc raiseTestException*() =
raise newException(Exception, "test")
var p: ptr int
fuzzy p
proc matchStackTrace(actualEntries: openarray[StackTraceEntry], expected: string) =
var expectedEntries = newSeq[StackTraceEntry]()
var i = 0
template checkEqual(actual, expected: typed, subject: string) =
if actual != expected:
echo "Unexpected ", subject, " on line ", i
echo "Actual: ", actual
echo "Expected: ", expected
doAssert(false)
for l in splitLines(expected.strip):
var procname, filename: string
var line: int
if not scanf(l, "$s$w.nim($i) $w", filename, line, procname):
doAssert(false, "Wrong expected stack trace")
checkEqual($actualEntries[i].filename, filename & ".nim", "file name")
if line != 0:
checkEqual(actualEntries[i].line, line, "line number")
checkEqual($actualEntries[i].procname, procname, "proc name")
inc i
doAssert(i == actualEntries.len, "Unexpected number of lines in stack trace")
template verifyStackTrace*(expectedStackTrace: string, body: untyped) =
var verified = false
try:
body
except Exception as e:
verified = true
# echo "Stack trace:"
# echo e.getStackTrace
matchStackTrace(e.getStackTraceEntries(), expectedStackTrace)
doAssert(verified, "No exception was raised")
when isMainModule:
# <-- Align with line 70 in the text editor
block:
proc bar() =
raiseTestException()
proc foo() =
bar()
const expectedStackTrace = """
tproper_stacktrace.nim(86) tproper_stacktrace
tproper_stacktrace.nim(76) foo
tproper_stacktrace.nim(73) bar
tproper_stacktrace.nim(7) raiseTestException
"""
verifyStackTrace expectedStackTrace:
foo()
block:
proc bar(x: int) =
raiseTestException()
template foo(x: int) =
bar(x)
const expectedStackTrace = """
tproper_stacktrace.nim(103) tproper_stacktrace
tproper_stacktrace.nim(90) bar
tproper_stacktrace.nim(7) raiseTestException
"""
verifyStackTrace expectedStackTrace:
var x: int
foo(x)
block: #6803
proc bar(x = 500) =
raiseTestException()
proc foo() =
bar()
const expectedStackTrace = """
tproper_stacktrace.nim(120) tproper_stacktrace
tproper_stacktrace.nim(110) foo
tproper_stacktrace.nim(107) bar
tproper_stacktrace.nim(7) raiseTestException
"""
verifyStackTrace expectedStackTrace:
foo()
block:
proc bar() {.stackTrace: off.} =
proc baz() = # Stack trace should be enabled
raiseTestException()
baz()
proc foo() =
bar()
const expectedStackTrace = """
tproper_stacktrace.nim(139) tproper_stacktrace
tproper_stacktrace.nim(129) foo
tproper_stacktrace.nim(125) baz
tproper_stacktrace.nim(7) raiseTestException
"""
verifyStackTrace expectedStackTrace:
foo()
echo "ok"

View file

@ -1,12 +1,14 @@
discard """
output: '''42'''
output: "42\n42"
"""
type
Foo[N: static[int]] = object
proc foo[N](x: Foo[N]) =
let n = N
echo N
echo n
var f1: Foo[42]
f1.foo

View file

@ -12,12 +12,12 @@ macro outterMacro*(n, blck: untyped): untyped =
echo "Using arg ! " & n.repr
result = "Got: '" & $n.kind & "' " & $j
var callNode = n[0]
expectKind(n, TNimrodNodeKind.nnkCall)
if n.len != 3 or n[1].kind != TNimrodNodeKind.nnkIdent:
expectKind(n, NimNodeKind.nnkCall)
if n.len != 3 or n[1].kind != NimNodeKind.nnkIdent:
error("Macro " & callNode.repr &
" requires the ident passed as parameter (eg: " & callNode.repr &
"(the_name_you_want)): statements.")
result = newNimNode(TNimrodNodeKind.nnkStmtList)
result = newNimNode(NimNodeKind.nnkStmtList)
var ass : NimNode = newNimNode(nnkAsgn)
ass.add(newIdentNode(n[1].ident))
ass.add(newStrLitNode(innerProc(4)))

View file

@ -0,0 +1,46 @@
discard """
output: "Valid and not defined"
"""
# test for issue #7997
# checking for `when not defined` in a template for some compile time symbol
# results in a compilation error of:
# Error: obsolete usage of 'defined', use 'declared' instead
# if the symbol is 'overloaded' by some variable or procedure, because in
# that case the argument of `defined` is of kind `nkSym` instead of `nkIdent`
# (for which was checked in `semexprs.semDefined`).
block:
# check whether a proc with the same name as the argument to `defined`
# compiles
proc overloaded() =
discard
template definedCheck(): untyped =
when not defined(overloaded): true
else: false
doAssert definedCheck == true
block:
# check whether a variable with the same name as the argument to `defined`
# compiles
var overloaded: int
template definedCheck(): untyped =
when not defined(overloaded): true
else: false
doAssert definedCheck == true
block:
# check whether a non overloaded when check still works properly
when not defined(validIdentifier):
echo "Valid and not defined"
block:
# now check that invalid identifiers cause a compilation error
# by using reject template.
template reject(b) =
static: doAssert(not compiles(b))
reject:
when defined(123):
echo "Invalid identifier! Will not be echoed"

View file

@ -0,0 +1,27 @@
discard """
output: 10.0
"""
type
MyFloat = object
val: float
converter to_myfloat*(x: float): MyFloat {.inline.} =
MyFloat(val: x)
proc `+`(x1, x2: MyFloat): MyFloat =
MyFloat(val: x1.val + x2.val)
proc `*`(x1, x2: MyFloat): MyFloat =
MyFloat(val: x1.val * x2.val)
template optMul{`*`(a, 2.0)}(a: MyFloat): MyFloat =
a + a
func floatMyFloat(x: MyFloat): MyFloat =
result = x * 2.0
func floatDouble(x: float): float =
result = x * 2.0
echo floatDouble(5)