summary refs log tree commit diff stats
path: root/tests/generics/tforward_generic.nim
blob: f43e7455fcb7bedcceff916ed2100722c7016d47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
discard """
  output: '''b()
720 120.0
720 120.0'''
"""

# bug #3055
proc b(t: int | string)
proc a(t: int) = b(t)
proc b(t: int | string) = echo "b()"
a(1)

# test recursive generics still work:
proc fac[T](x: T): T =
  if x == 0: return 1
  else: return fac(x-1)*x

echo fac(6), " ", fac(5.0)

# test recursive generic with forwarding:
proc fac2[T](x: T): T

echo fac2(6), " ", fac2(5.0)

proc fac2[T](x: T): T =
  if x == 0: return 1
  else: return fac2(x-1)*x
">m.config): # we gather all thread locals var into a struct; we need to allocate # storage for that somehow, can't use the thread local storage # allocator for it :-( if not containsOrIncl(m.g.nimtvDeclared, s.id): m.g.nimtvDeps.add(s.loc.t) addf(m.g.nimtv, "$1 $2;$n", [getTypeDesc(m, s.loc.t), s.loc.r]) else: if isExtern: add(m.s[cfsVars], "extern ") if optThreads in m.config.globalOptions: add(m.s[cfsVars], "NIM_THREADVAR ") add(m.s[cfsVars], getTypeDesc(m, s.loc.t)) addf(m.s[cfsVars], " $1;$n", [s.loc.r]) proc generateThreadLocalStorage(m: BModule) = if m.g.nimtv != nil and (usesThreadVars in m.flags or sfMainModule in m.module.flags): for t in items(m.g.nimtvDeps): discard getTypeDesc(m, t) addf(m.s[cfsSeqTypes], "typedef struct {$1} NimThreadVars;$n", [m.g.nimtv]) proc generateThreadVarsSize(m: BModule) = if m.g.nimtv != nil: let externc = if m.config.cmd == cmdCompileToCpp or sfCompileToCpp in m.module.flags: "extern \"C\" " else: "" addf(m.s[cfsProcs], "$#NI NimThreadVarsSize(){return (NI)sizeof(NimThreadVars);}$n", [externc.rope])