summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBung <crc32@qq.com>2022-10-21 15:26:46 +0800
committerGitHub <noreply@github.com>2022-10-21 09:26:46 +0200
commit04c48e3c5b7a98b065d9f3a96575a304a60a9290 (patch)
tree8709c4e2feac4a735b6f3b00e8bbcdfffba5f215
parent5b195ab0f000d5276ddd9702e51215aaefc3abdb (diff)
downloadNim-04c48e3c5b7a98b065d9f3a96575a304a60a9290.tar.gz
fix #19426 compile error using when/elif/else and typedesc in template (#20550)
-rw-r--r--compiler/semfold.nim1
-rw-r--r--tests/whenstmt/t19426.nim16
2 files changed, 17 insertions, 0 deletions
diff --git a/compiler/semfold.nim b/compiler/semfold.nim
index 94e5fda31..9e45e3b2d 100644
--- a/compiler/semfold.nim
+++ b/compiler/semfold.nim
@@ -457,6 +457,7 @@ proc foldArrayAccess(m: PSym, n: PNode; idgen: IdGenerator; g: ModuleGraph): PNo
 
 proc foldFieldAccess(m: PSym, n: PNode; idgen: IdGenerator; g: ModuleGraph): PNode =
   # a real field access; proc calls have already been transformed
+  if n[1].kind != nkSym: return nil
   var x = getConstExpr(m, n[0], idgen, g)
   if x == nil or x.kind notin {nkObjConstr, nkPar, nkTupleConstr}: return
 
diff --git a/tests/whenstmt/t19426.nim b/tests/whenstmt/t19426.nim
new file mode 100644
index 000000000..95fb54a9e
--- /dev/null
+++ b/tests/whenstmt/t19426.nim
@@ -0,0 +1,16 @@
+type
+  MyInt = object
+    bitWidth: int
+
+template toRealType*(t: MyInt): typedesc =
+  when t.bitWidth == 32: int32
+  elif t.bitWidth == 64: int64
+  else: {.error.}
+
+proc doFail(T: typedesc): T = default(T)
+
+proc test =
+  const myInt = MyInt(bitWidth:32)
+  discard doFail(toRealType(myInt))
+
+test()
\ No newline at end of file