Skip to content

Template return type mutability not respected #25215

@darkestpigeon

Description

@darkestpigeon

Nim Version

Nim Compiler Version 2.3.1 [Linux: amd64]
Compiled at 2025-09-25
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: 9f74712ec6ec346f8f1366c10f752d0a9aa85a70
active boot switches: -d:release
Nim Compiler Version 2.2.4 [Linux: amd64]
Compiled at 2025-04-22
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: f7145dd26efeeeb6eeae6fff649db244d81b212d
active boot switches: -d:release

Description

This should not compile:

proc print(x: int) =
  echo x

proc inc(x: var int) =
  x += 1


type MyObj = object
  data: pointer
  size: int

template view(obj: MyObj): int =
  let data = obj.data
  cast[ptr int](data)[]


let x = @[1, 2, 3]
let y = MyObj(data: addr x[0], size: x.len)

print(y.view)
inc(y.view)
print(y.view)

Current Output

1
2

Expected Output


Known Workarounds

No response

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions