nim-works / nimskull

An in development statically typed systems programming language; with sustainability at its core. We, the community of users, maintain it.
https://nim-works.github.io/nimskull/index.html
Other
275 stars 39 forks source link

borrows of parameters below `byref` threshold ends up leasing the copied parameter #1457

Closed alaviss closed 3 weeks ago

alaviss commented 3 weeks ago

Example

{.experimental: "views".}
type
  Mint = object
    data: int

  Option = object
    value: lent Mint

proc `=copy`(dst: var Mint, src: Mint) {.error.}

proc some(x: Mint): Option =
  Option(value: x)

proc main() =
  var x = Mint(data: 10)
  let optlent = some(x)

main()

Actual Output

C Code for some(x):

N_LIB_PRIVATE N_NIMCALL(_L6Option_2_M4test, some__test_8)(_L4Mint_1_M4test x) {
    _L6Option_2_M4test result;
    _L4Mint_1_M4test* _2;
    nimfr_("some", "/tmp/test.nim");
    nimln_(12, "/tmp/test.nim");
    _2 = (&x);
    nimZeroMem((void*)(&result), sizeof(_L6Option_2_M4test));
    result.value = _2;
    popFrame();
    return result;
}

Expected Output

The x parameter should be a pointer, not a stack copy.

Additional information