Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Booleans Work Wrong in Compile-time #10886

Closed
loloiccl opened this issue Mar 22, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@loloiccl
Copy link
Contributor

commented Mar 22, 2019

sets.containsOrIncl and or in one-line work wrong in compile-time

Example

import sets

proc add[T](hs: HashSet[T], tl: openArray[T]): HashSet[T] =
  result = hs
  var
    flag = true
    i = 0
  while flag and i < tl.len:
    flag = result.containsOrIncl(tl[i]) or flag
    inc(i)

const hs = ["a", "b", "c"].toSet
const testC = hs.add(@["b", "c", "d", "e", "f"])
let testL = hs.add(@["b", "c", "d", "e", "f"])

echo testC
echo testL

Current Output

{"c", "a", "b", "d"}
{"e", "a", "d", "c", "b", "f"}

Expected Output

{"e", "a", "d", "c", "b", "f"}
{"e", "a", "d", "c", "b", "f"}

Additional Information

$ nim -v
nim -v
Nim Compiler Version 0.19.4 [Linux: amd64]
Compiled at 2019-03-21
Copyright (c) 2006-2018 by Andreas Rumpf

git hash: b6d96cafc8bcad1f3d32f2910b25cd11a93f7751
active boot switches: -d:release
@narimiran

This comment has been minimized.

Copy link
Member

commented Mar 22, 2019

  1. You don't need flag and or in your example, this will do the same thing:
proc add[T](hs: HashSet[T], tl: openArray[T]): HashSet[T] =
  result = hs
  var i = 0
  while i < tl.len:
    discard result.containsOrIncl(tl[i])
    inc i
  1. I tried several variants and this has nothing to do with containsOrIncl, but how bool works on compile time. Here is the shortest possible example:
proc foo(): bool =
  result = true
  result = false or result

const ct = foo()
let rt = foo()

echo ct # => false, wrong
echo rt # => true

Interestingly, changing result = false or result to the other way around: result = result or false produces correct result (true).

@mratsim mratsim added Stdlib VM labels Mar 22, 2019

@loloiccl

This comment has been minimized.

Copy link
Contributor Author

commented Mar 22, 2019

@narimiran

  1. Thinks but I know that. This is an example in which I try to reproduce the bug.
  2. Thanks for finding an easier example!

@narimiran narimiran changed the title "sets.containsOrIncl" and "or" Used in One-line Work Wrong in Compile-time Booleans Work Wrong in Compile-time Mar 22, 2019

@narimiran

This comment has been minimized.

Copy link
Member

commented Mar 22, 2019

Another wrong one at compile time:

proc foo(): bool =
  result = false
  result = true and result

Araq added a commit that referenced this issue Mar 23, 2019

@Araq Araq closed this in 0b2a3f6 Mar 23, 2019

narimiran added a commit that referenced this issue Mar 24, 2019

fixes #10886 [backport] (#10897)
(cherry picked from commit 0b2a3f6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.