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

Incorrect type inference on destructor injection #24374

Open
darkestpigeon opened this issue Oct 28, 2024 · 0 comments
Open

Incorrect type inference on destructor injection #24374

darkestpigeon opened this issue Oct 28, 2024 · 0 comments

Comments

@darkestpigeon
Copy link

Description

Here is a toy example:

type SimpleType[T] = object
  value: int
  # dummyField: T

proc initSimpleType[T](value: int): SimpleType[T] =
  doAssert value >= 0
  echo "created " & $SimpleType[T] & " with value " & $value
  result = SimpleType[T](value: value)

proc `=wasMoved`[T](x: var SimpleType[T]) =
  x.value = -1

proc `=destroy`[T](x: SimpleType[T]) =
  if x.value >= 0:
    echo "destroyed " & $SimpleType[T] & " with value " & $x.value


type Composite = object
  a: SimpleType[int]
  b: SimpleType[float]

type AnotherComposite = object
  c: SimpleType[string]
  d: SimpleType[byte]


block:
  let x = Composite(a: initSimpleType[int](1), b: initSimpleType[float](2))

block:
  let x = AnotherComposite(c: initSimpleType[string](3), d: initSimpleType[byte](4))

Nim Version

Nim Compiler Version 2.2.0 [Linux: amd64]
Compiled at 2024-10-02
Copyright (c) 2006-2024 by Andreas Rumpf

git hash: 78983f1876726a49c69d65629ab433ea1310ece1
active boot switches: -d:release
Nim Compiler Version 2.2.1 [Linux: amd64]
Compiled at 2024-10-27
Copyright (c) 2006-2024 by Andreas Rumpf

git hash: 24aa92c14f2301d1ef63f59bc4ae2ec31a64f9c5
active boot switches: -d:release

Current Output

created SimpleType[system.int] with value 1
created SimpleType[system.float] with value 2
destroyed SimpleType[system.int] with value 1
destroyed SimpleType[system.int] with value 2
created SimpleType[system.string] with value 3
created SimpleType[system.byte] with value 4
destroyed SimpleType[system.int] with value 3
destroyed SimpleType[system.int] with value 4

Expected Output

created SimpleType[system.int] with value 1
created SimpleType[system.float] with value 2
destroyed SimpleType[system.int] with value 1
destroyed SimpleType[system.float] with value 2
created SimpleType[system.string] with value 3
created SimpleType[system.byte] with value 4
destroyed SimpleType[system.string] with value 3
destroyed SimpleType[system.byte] with value 4

Known Workarounds

The problem disappears if the field dummyField: T is uncommented.

Additional Information

No response

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

No branches or pull requests

1 participant