Wrong result of sort in VM #4065

Closed
yglukhov opened this Issue Apr 8, 2016 · 5 comments

Projects

None yet

3 participants

@yglukhov
Member
yglukhov commented Apr 8, 2016

Code:

import algorithm

proc ctProc(): int =
    var s = @[488780230'i32, 1932284137'i32, 69951615'i32]
    s.sort(proc (a, b: int32): int = a - b)
    echo s

    assert(s[0] < s[1])
    assert(s[1] < s[2])

const a = ctProc()

Compile:

nim c --cpu:i386 test.nim

or

nim js test.nim

Result:

@[69951615, 1932284137, 488780230]
stack trace: (most recent call last)
test.nim(11)
test.nim(9)              ctProc
lib/system.nim(3351)     failedAssertImpl
lib/system.nim(3343)     raiseAssert
lib/system.nim(2529)     sysFatal
lib/system.nim(2529, 7) Error: unhandled exception: s[1] < s[2]
@Araq Araq added the VM label Apr 9, 2016
@yglukhov
Member
yglukhov commented Jul 6, 2016

@Araq, here's a narrowed example, showing that the problem is in VM narrowing logic :)

proc ctProc(): int =
    let a = -1443503907
    let b = 0
    result = a xor b

const a = ctProc()
let b = ctProc()

doAssert(a == b)
@yglukhov yglukhov added a commit to yglukhov/Nim that referenced this issue Jul 8, 2016
@yglukhov yglukhov Fixed #4065 9a4e5e2
@jangko
Contributor
jangko commented Aug 4, 2016

although no PR specifically targetting this issue beside @yglukhov's one, looks like the devel branch not exhibit this behavior anymore.

really strange, could it be not only narrowing problem?

@yglukhov
Member
yglukhov commented Aug 4, 2016

With latest devel I'm observing some strange results. On MacOS nim is 64bit. The following code:

proc ctProc(): int =
    let a = -1443503907
    let b = 0
    result = a xor b

const a = ctProc()
let b = ctProc()

echo a
echo b

doAssert(a == b)
$ nim c -r --cpu:i386 "--passC:-arch i386" "--passL:-arch i386" ~/test.nim
Hint: used config file '/Users/yglukhov/Projects/nim/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: test [Processing]
Hint:  [Link]
Hint: operation successful (10328 lines compiled; 0.206 sec total; 15.504MiB; Debug Build) [SuccessX]
Hint: /Users/yglukhov/test  [Exec]
2851463389
-1443503907

Note that even though echo displays different values, doAssert does not assert! %)

As for JS:

$ nim js -r -d:nodejs  ~/test.nim
Hint: used config file '/Users/yglukhov/Projects/nim/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: test [Processing]
Hint: operation successful (4916 lines compiled; 0.028 sec total; 3.504MiB; Debug Build) [SuccessX]
Hint: /usr/local/bin/../Cellar/node/6.2.0/bin/node /Users/yglukhov/nimcache/test.js  [Exec]
2851463389
-1443503907

/Users/yglukhov/nimcache/test.js:179
throw e_12405;}
^
AssertionError: 50,56,53,49,52,54,51,51,56,57,32,61,61,32,98,32,0
Error: execution of an external program failed: '/usr/local/bin/../Cellar/node/6.2.0/bin/node /Users/yglukhov/nimcache/test.js '

Assert did assert in JS.

@jangko
Contributor
jangko commented Aug 4, 2016

mine is:

Nim Compiler Version 0.14.3 (2016-08-04) [Windows: amd64]
Copyright (c) 2006-2016 by Andreas Rumpf

active boot switches: -d:release

gcc version 5.3.0 (GCC)
@yglukhov
Member
yglukhov commented Aug 4, 2016

@jangko do you compile the sample for 32bits or for js?

@Araq Araq added a commit that closed this issue Aug 4, 2016
@Araq Araq attempt to fix #4065 4a96a3d
@Araq Araq closed this in 4a96a3d Aug 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment