summary refs log tree commit diff stats
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2024-09-30 18:34:49 +0300
committerGitHub <noreply@github.com>2024-09-30 17:34:49 +0200
commit2a48182288b1c6fbc01070e5e1ffb7653b3599e3 (patch)
treeabc22306201b420fb5550830231e0bdf2a39a38c
parentfebc58e0365f683df0eee973e7fa183d8cfaddbc (diff)
downloadNim-2a48182288b1c6fbc01070e5e1ffb7653b3599e3.tar.gz
remove `prev == nil` requirement for typedesc params as type nodes (#24206)
fixes #24203

`semTypeNode` is called twice for RHS'es of type sections,
[here](https://github.com/nim-lang/Nim/blob/b0e6d28782ce8c3d8e0b4a64e01f21d6f900648f/compiler/semstmts.nim#L1612)
and
[here](https://github.com/nim-lang/Nim/blob/b0e6d28782ce8c3d8e0b4a64e01f21d6f900648f/compiler/semstmts.nim#L1646).
Each time `prev` is `s.typ`, but the assertion expects `prev == nil`
which is false since `s.typ` is not nil the second time. To fix this,
the `prev == nil` part of the assertion is removed.

The reason this only happens for types like `seq[int]`, `(int, int)` etc
is because they don't have syms: `semTypeIdent` attempts to directly
[replace the typedesc param
itself](https://github.com/nim-lang/Nim/blob/b0e6d28782ce8c3d8e0b4a64e01f21d6f900648f/compiler/semtypes.nim#L1916)
with the sym of the base type of the resolved typedesc type if it
exists, which means `semTypeNode` doesn't receive the typedesc param sym
to perform the assert.
-rw-r--r--compiler/semtypes.nim4
-rw-r--r--tests/metatype/tmetatype_issues.nim9
2 files changed, 11 insertions, 2 deletions
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index 857521232..9af71ba8f 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -2167,7 +2167,7 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
       if s.kind != skError: localError(c.config, n.info, errTypeExpected)
       result = newOrPrevType(tyError, prev, c)
     elif s.kind == skParam and s.typ.kind == tyTypeDesc:
-      internalAssert c.config, s.typ.base.kind != tyNone and prev == nil
+      internalAssert c.config, s.typ.base.kind != tyNone
       result = s.typ.base
     elif prev == nil:
       result = s.typ
@@ -2191,7 +2191,7 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
         if s.kind == skType:
           s.typ
         else:
-          internalAssert c.config, s.typ.base.kind != tyNone and prev == nil
+          internalAssert c.config, s.typ.base.kind != tyNone
           s.typ.base
       let alias = maybeAliasType(c, t, prev)
       if alias != nil:
diff --git a/tests/metatype/tmetatype_issues.nim b/tests/metatype/tmetatype_issues.nim
index 21c5c02f1..d33d8dd31 100644
--- a/tests/metatype/tmetatype_issues.nim
+++ b/tests/metatype/tmetatype_issues.nim
@@ -157,3 +157,12 @@ block t3338:
   var t2 = Bar[int32]()
   t2.add()
   doAssert t2.x == 5
+
+block: # issue #24203
+  proc b(G: typedesc) =
+    type U = G
+  template s(h: untyped) = h
+  s(b(typeof (0, 0)))
+  b(seq[int])
+  b((int, int))
+  b(typeof (0, 0))