From e78d9f4c76156121aeb176c7b3edeaf987e9e9e5 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 26 Dec 2020 22:29:02 +0800 Subject: [PATCH 1/3] fix nim js cmp fails at CT --- lib/system.nim | 10 ++-------- lib/system/jssys.nim | 7 ++++++- tests/misc/tstrtabs.nim | 20 ++++++++++++++++++++ tests/stdlib/tstring.nim | 36 +++++++++++++++++++++--------------- 4 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 tests/misc/tstrtabs.nim diff --git a/lib/system.nim b/lib/system.nim index 85ef15e08f480..fb008dc452fd0 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2394,14 +2394,8 @@ when notJSnotNims: """.} when defined(js): - when not defined(nimscript): - include "system/jssys" - include "system/reprjs" - else: - proc cmp(x, y: string): int = - if x == y: return 0 - if x < y: return -1 - return 1 + include "system/jssys" + include "system/reprjs" when defined(js) or defined(nimscript): proc addInt*(result: var string; x: int64) = diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index 8865558fe5539..7848a435e38a3 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -341,7 +341,12 @@ proc cmpStrings(a, b: string): int {.asmNoStackFrame, compilerproc.} = """ proc cmp(x, y: string): int = - return cmpStrings(x, y) + when nimvm: + if x == y: result = 0 + elif x < y: result = -1 + else: result = 1 + else: + result = cmpStrings(x, y) proc eqStrings(a, b: string): bool {.asmNoStackFrame, compilerproc.} = asm """ diff --git a/tests/misc/tstrtabs.nim b/tests/misc/tstrtabs.nim new file mode 100644 index 0000000000000..2f7eda9f7ae92 --- /dev/null +++ b/tests/misc/tstrtabs.nim @@ -0,0 +1,20 @@ +discard """ + targets: "c cpp js" +""" + +import std/strtabs + +proc fun()= + let ret = newStringTable(modeCaseSensitive) + ret["foo"] = "bar" + + doAssert $ret == "{foo: bar}" + + let b = ret["foo"] + doAssert b == "bar" + +proc main()= + static: fun() + fun() + +main() diff --git a/tests/stdlib/tstring.nim b/tests/stdlib/tstring.nim index ff3d41b49287b..4d5a15940e9c6 100644 --- a/tests/stdlib/tstring.nim +++ b/tests/stdlib/tstring.nim @@ -1,16 +1,14 @@ discard """ - output: '''OK -@[@[], @[], @[], @[], @[]] -''' + targets: "c cpp js" """ + const characters = "abcdefghijklmnopqrstuvwxyz" const numbers = "1234567890" -var s: string - proc test_string_slice() = # test "slice of length == len(characters)": # replace characters completely by numbers + var s: string s = characters s[0..^1] = numbers doAssert s == numbers @@ -51,11 +49,13 @@ proc test_string_slice() = s[2..0] = numbers doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz" - # bug #6223 - doAssertRaises(IndexDefect): - discard s[0..999] + when nimvm: + discard + else: + # bug #6223 + doAssertRaises(IndexDefect): + discard s[0..999] - echo("OK") proc test_string_cmp() = let world = "hello\0world" @@ -76,9 +76,6 @@ proc test_string_cmp() = doAssert cmp(world, hello) > 0 doAssert cmp(world, goodbye) > 0 -test_string_slice() -test_string_cmp() - #-------------------------- # bug #7816 @@ -87,9 +84,9 @@ import sequtils proc tester[T](x: T) = let test = toSeq(0..4).map(i => newSeq[int]()) - echo test + doAssert $test == "@[@[], @[], @[], @[], @[]]" + -tester(1) # #14497 func reverse*(a: string): string = @@ -97,4 +94,13 @@ func reverse*(a: string): string = for i in 0 ..< a.len div 2: swap(result[i], result[^(i + 1)]) -doAssert reverse("hello") == "olleh" + +proc main() = + test_string_slice() + test_string_cmp() + + tester(1) + doAssert reverse("hello") == "olleh" + +static: main() +main() From b799940f36cdfe0b0a1479d2d30168650f61ecbe Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 27 Mar 2021 22:23:04 +0800 Subject: [PATCH 2/3] add testcase for #9466 --- tests/notnil/tnotnil5.nim | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/notnil/tnotnil5.nim diff --git a/tests/notnil/tnotnil5.nim b/tests/notnil/tnotnil5.nim new file mode 100644 index 0000000000000..f93b1664204c5 --- /dev/null +++ b/tests/notnil/tnotnil5.nim @@ -0,0 +1,29 @@ +discard """ + matrix: "--threads:on" +""" + +{.experimental: "parallel".} +{.experimental: "notnil".} +import threadpool + +type + AO = object + x: int + + A = ref AO not nil + +proc process(a: A): A = + return A(x: a.x+1) + +proc processMany(ayys: openArray[A]): seq[A] = + var newAs: seq[FlowVar[A]] + + parallel: + for a in ayys: + newAs.add(spawn process(a)) + for newAflow in newAs: + let newA = ^newAflow + if isNil(newA): + return @[] + # Error: cannot prove 'newA' is not nil + result.add(newA) From c41deac2749c150a271a97261db0d7ec4bf7d4c6 Mon Sep 17 00:00:00 2001 From: flywind Date: Sun, 28 Mar 2021 14:47:04 +0800 Subject: [PATCH 3/3] Update tests/notnil/tnotnil5.nim --- tests/notnil/tnotnil5.nim | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/notnil/tnotnil5.nim b/tests/notnil/tnotnil5.nim index f93b1664204c5..2dcb7f7c3eac9 100644 --- a/tests/notnil/tnotnil5.nim +++ b/tests/notnil/tnotnil5.nim @@ -25,5 +25,4 @@ proc processMany(ayys: openArray[A]): seq[A] = let newA = ^newAflow if isNil(newA): return @[] - # Error: cannot prove 'newA' is not nil result.add(newA)