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

document #15618 #15810

Merged
merged 4 commits into from
Nov 5, 2020
Merged

document #15618 #15810

merged 4 commits into from
Nov 5, 2020

Conversation

ringabout
Copy link
Member

No description provided.

@timotheecour
Copy link
Member

Can we fix it instead?

lib/pure/unittest.nim Outdated Show resolved Hide resolved
@ringabout
Copy link
Member Author

I think we should make this work to fix this issue.

var x = 4
doAssert 4.0 == x

@timotheecour
Copy link
Member

I think we should make this work to fix this issue.

no, this can't work.
But check 4.0 == 2 + 2 should work

@ringabout
Copy link
Member Author

check 4.0 == 2 + 2 is converted to this for supporting checkpoints. It seems hard to fix.

var x = 2 + 2    # maybe have side effects
doAsssert 4.0 == x
checkpointes(... & $x)

And

func double*(n: float): float = n * 2
doAssert(double(-2) == -4)
test "double -2": check(double(-2) == -4)

is converted to:

func double*(n: float): float = n * 2

var x = double(-2) # maybe have side effects
doAssert(x == -4)
checkpointes(... & $x)

@timotheecour
Copy link
Member

timotheecour commented Nov 2, 2020

It seems hard to fix.

check lhs == rhs
=>
var rhs2: type(lhs) = rhs

note that in future work {.evalonce.} might be worth considering eg: let rhs2 {.evalonce.}: type(lhs) = expr refs #13750

@ringabout
Copy link
Member Author

ringabout commented Nov 2, 2020

Thanks for hints! I will give a try.

But how about this?

import unittest


check 2.0 + 2.0 == 2 + 2  # works with doAssert
check 2 + 2 == 2.0 + 2.0 # works with doAssert

var x: int = 2.0 + 2.0 # This failed.

or lhs and rhs are both functions.

It may not be hard. But I think It is error-prone.

@timotheecour
Copy link
Member

nnkIntLit is special, its type is determined from context.

here's a working prototype:

when true:
  import macros
  macro commonType(a, b: typed): untyped =
    if a.kind == nnkIntLit:
      result = quote do:
        type(`b`)
    else:
      result = quote do:
        type(`a`)

  macro check2(cond: untyped): untyped =
    let op=cond[0]
    let a1=cond[1]
    let a2=cond[2]
    let s = cond.repr.newLit
    result = quote do:
      type T = commonType(`a1`, `a2`)
      let lhs: T = `a1`
      let rhs: T = `a2`
      if not(`op`(lhs, rhs)):
        doAssert false, $(lhs, rhs, `s`)
  check2(2+2 == 2.0 + 2.0)
  check2(2.0 + 2.0 == 2+2)
  check2(2+3 == 2.0 + 2.0) # this will show the errmsg

Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>
lib/pure/unittest.nim Outdated Show resolved Hide resolved
@Araq Araq merged commit adde5ba into nim-lang:devel Nov 5, 2020
narimiran pushed a commit that referenced this pull request Nov 9, 2020
* document #15618

* Update lib/pure/unittest.nim

* Update lib/pure/unittest.nim

Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>

* Update lib/pure/unittest.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>
(cherry picked from commit adde5ba)
PMunch pushed a commit to PMunch/Nim that referenced this pull request Jan 6, 2021
* document nim-lang#15618

* Update lib/pure/unittest.nim

* Update lib/pure/unittest.nim

Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>

* Update lib/pure/unittest.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>
mildred pushed a commit to mildred/Nim that referenced this pull request Jan 11, 2021
* document nim-lang#15618

* Update lib/pure/unittest.nim

* Update lib/pure/unittest.nim

Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>

* Update lib/pure/unittest.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>
irdassis pushed a commit to irdassis/Nim that referenced this pull request Mar 16, 2021
* document nim-lang#15618

* Update lib/pure/unittest.nim

* Update lib/pure/unittest.nim

Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>

* Update lib/pure/unittest.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>
ardek66 pushed a commit to ardek66/Nim that referenced this pull request Mar 26, 2021
* document nim-lang#15618

* Update lib/pure/unittest.nim

* Update lib/pure/unittest.nim

Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>

* Update lib/pure/unittest.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants