Regression from 1.6 to 2.0/devel with templates with `gensym` `proc`
### Description

```nim
template r(body: untyped) =
  proc x() {.gensym.} =
    body

template g() =
  r:
    let y = 0

  r:
    proc y() = discard
    y()

g()
```

### Nim Version

Builds in Nim 1.6:
```
Nim Compiler Version 1.6.21 [Linux: amd64]
Compiled at 2024-07-09
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 786bbff1ae0c1f124fc0848948c0fd7dfd30225c
active boot switches: -d:release
```

Does not build in 2.0 and devel:
```
Nim Compiler Version 2.0.8 [Linux: amd64]
Compiled at 2024-07-08
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 5935c3bfa9fec6505394867b23510eb5cbab3dbf
active boot switches: -d:release
```

```
Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2024-07-09
Copyright (c) 2006-2024 by Andreas Rumpf

git hash: 732f7752a954bb33a92e839304006ba5d865400a
active boot switches: -d:release
```

### Current Output

```text
/tmp/m.nim(7, 9) Hint: 'y`gensym0' is declared but not used [XDeclaredButNotUsed]
/tmp/m.nim(13, 2) template/generic instantiation of `g` from here
/tmp/m.nim(11, 5) Error: undeclared identifier: 'y`gensym0'; if declared in a template, this identifier may be inconsistently marked inject or gensym
candidates (edit distance, scope distance); see '--spellSuggest': 
 (2, 2): 'x`gensym1'
 (2, 2): 'x`gensym2'
```


### Expected Output

```text
Builds
```


### Possible Solution

_No response_

### Additional Information

_No response_