Segfault when checking if you can yield a ref object
### Description

Original bug popped up in async code that looked like this
```nim
import std/asyncdispatch
proc foo() =
  await someProc()
```
which segfaulted when compiling instead of telling me I needed to make the proc async.
I minimised the issue to this
```nim
type
  SomeObj = ref object # Doesn't error if you make SomeObj be non-ref

template foo = yield SomeObj()
when compiles(foo): discard
```


This is the stacktrace when running with `nim_dbg`
```
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/nim.nim(169) nim
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/nim.nim(124) handleCmdLine
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/main.nim(404) mainCommand
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/main.nim(96) commandCheck
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/pipelines.nim(312) compilePipelineProject
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/pipelines.nim(232) compilePipelineModule
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/pipelines.nim(178) processPipelineModule
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/sem.nim(836) semWithPContext
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/sem.nim(793) semStmtAndGenerateGenerics
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/semstmts.nim(2899) semStmt
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/semexprs.nim(1259) semExprNoType
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/semexprs.nim(3499) semExpr /home/jake/Documents/projects/Nim/t.nim(3, 3) nkStmtList
/home/jake/.choosenim/toolchains/nim-hashdevel/lib/system.nim(918) semStmtList
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/semexprs.nim(3430) semExpr /home/jake/Documents/projects/Nim/t.nim(6, 1) nkWhenStmt
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/semexprs.nim(2719) semWhen
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/sem.nim(385) semConstExpr
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/semexprs.nim(83) semExprWithType
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/semexprs.nim(67) semExprCheck
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/semexprs.nim(3403) semExpr /home/jake/Documents/projects/Nim/t.nim(6, 14) nkCall
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/semexprs.nim(2558) semMagic
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/semexprs.nim(2487) semCompiles
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/semexprs.nim(2460) tryExpr
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/sempass2.nim(1752) trackStmt
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/sempass2.nim(1488) track
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/sempass2.nim(1234) track
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/sempass2.nim(1105) trackCall
/home/jake/.choosenim/toolchains/nim-hashdevel/compiler/ast.nim(2013) track
```

### Nim Version

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

git hash: 22d2cf217597468ace8ba540d6990b1f6d8a816a
active boot switches: -d:release


### Current Output

```text
Compiler segfaults
```


### Expected Output

```text
Compiler doesn't segfault
```


### Known Workarounds

_No response_

### Additional Information

_No response_