Nested dirty template in generic proc
### Description

This might be mentioned elsewhere in another form, but this is a self contained example

this does not work:
```nim
template inner(i: int) {.dirty.} =
  let thing = 1

template outer() =
  proc p[T](x: T) =
    inner(5)
    echo thing

outer()
p(0)
```

but this does:
```nim
template inner(i: int) {.dirty.} =
  let thing = 1

template outer() =
  proc p(x: int) =
    inner(5)
    echo thing

outer()
p(0)
```

### Nim Version

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

git hash: c1f91c26a5136b2ad00f7da93b19c2da9b85dd16
active boot switches: -d:releas

### Current Output

```text
Error: undeclared identifier: 'thing'
```


### Expected Output

```text
1
```


### Possible Solution

_No response_

### Additional Information

_No response_