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

Unexpected behaviour when constructing with `result` #11525

Closed
dom96 opened this issue Jun 17, 2019 · 6 comments

Comments

@dom96
Copy link
Member

commented Jun 17, 2019

type
  Point[T] = object
    x, y: T

proc adjustPos[T](width, height: int, pos: Point[T]): Point[T] =
  result = pos

  result = Point[T](
    x: pos.x - (width / 2),
    y: pos.y - (height / 2)
  )
  
proc adjustPos2[T](width, height: int, pos: Point[T]): Point[T] =
  result = pos

  result = Point[T](
    x: result.x - (width / 2),
    y: result.y - (height / 2)
  )

proc adjustPos3(width, height: int, pos: Point): Point =
  result = pos

  result = Point(
    x: result.x - (width / 2),
    y: result.y - (height / 2)
  )

echo adjustPos(200, 200, Point[float](x: 1000, y: 1000))
echo adjustPos2(200, 200, Point[float](x: 1000, y: 1000))
echo adjustPos3(200, 200, Point[float](x: 1000, y: 1000))

Result:

(x: 900.0, y: 900.0)
(x: -100.0, y: -100.0)
(x: -100.0, y: -100.0)

Perhaps this behaviour is expected. But I swear I had adjustPos3 in the JS backend and it worked (I didn't re-test this), so there is at least 3 different behaviours here (!)

@krux02

This comment has been minimized.

Copy link
Contributor

commented Jun 17, 2019

No, this behavior is not expected. It is a bug. It should either complain at compile time that you read a value that you are writing, or it should do it properly.

@jangko

This comment has been minimized.

Copy link
Contributor

commented Jun 18, 2019

looks like this bug is not exclusively belong to result, local variable also suffer from this syndrome.

type
  MyObject = object
    x, y: int
    
proc test() =
  var a = MyObject(x: 100, y: 200)
  a = MyObject(x: a.x - 10, y: a.y - 10)
  echo a # should be (x: 90, y: 190)

test()

Output:

(x: -10, y: -10)

wow, after all these years nobody ever notice this bug exists.

@pgkos

This comment has been minimized.

Copy link
Contributor

commented Jun 18, 2019

The result is correct on v0.17.2, broken from v0.18.0.

@krux02

This comment has been minimized.

Copy link
Contributor

commented Jun 18, 2019

@pgkos then I guess this is a regression.

@krux02 krux02 added the Regression label Jun 18, 2019

@pgkos

This comment has been minimized.

Copy link
Contributor

commented Jun 18, 2019

bisect shows that d0a9fac is the first bad commit.

@Araq

This comment has been minimized.

Copy link
Member

commented Jun 19, 2019

Should be easy to fix.

@Araq Araq closed this in 19b1424 Jun 26, 2019

narimiran added a commit that referenced this issue Jul 2, 2019

fixes #11525
(cherry picked from commit 19b1424)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.