diff options
author | Araq <rumpf_a@web.de> | 2015-10-27 08:37:56 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2015-10-27 08:37:56 +0100 |
commit | 86e2d6ee907d4573fecfd2faded6e700cf75c8a3 (patch) | |
tree | 97ae5e8cdc8867f230f2355433b18ae2dc868cb9 /tests | |
parent | e94a6ec1f91c855ef1e5a2b54db7a5cbe8d245d4 (diff) | |
download | Nim-86e2d6ee907d4573fecfd2faded6e700cf75c8a3.tar.gz |
fixes #3476
Diffstat (limited to 'tests')
-rw-r--r-- | tests/generics/tvarseq_caching.nim | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/generics/tvarseq_caching.nim b/tests/generics/tvarseq_caching.nim new file mode 100644 index 000000000..f617b9335 --- /dev/null +++ b/tests/generics/tvarseq_caching.nim @@ -0,0 +1,48 @@ +discard """ + output: '''@[1, 2, 3] +@[4.0, 5.0, 6.0] +@[1, 2, 3] +@[4.0, 5.0, 6.0] +@[1, 2, 3] +@[4, 5, 6]''' +""" + +# bug #3476 + +proc foo[T]: var seq[T] = + ## Problem! Bug with generics makes every call to this proc generate + ## a new seq[T] instead of retrieving the `items {.global.}` variable. + var items {.global.}: seq[T] + return items + +proc foo2[T]: ptr seq[T] = + ## Workaround! By returning by `ptr` instead of `var` we can get access to + ## the `items` variable, but that means we have to explicitly deref at callsite. + var items {.global.}: seq[T] + return addr items + +proc bar[T]: var seq[int] = + ## Proof. This proc correctly retrieves the `items` variable. Notice the only thing + ## that's changed from `foo` is that it returns `seq[int]` instead of `seq[T]`. + var items {.global.}: seq[int] + return items + + +foo[int]() = @[1, 2, 3] +foo[float]() = @[4.0, 5.0, 6.0] + +foo2[int]()[] = @[1, 2, 3] +foo2[float]()[] = @[4.0, 5.0, 6.0] + +bar[int]() = @[1, 2, 3] +bar[float]() = @[4, 5, 6] + + +echo foo[int]() # prints 'nil' - BUG! +echo foo[float]() # prints 'nil' - BUG! + +echo foo2[int]()[] # prints '@[1, 2, 3]' +echo foo2[float]()[] # prints '@[4.0, 5.0, 6.0]' + +echo bar[int]() # prints '@[1, 2, 3]' +echo bar[float]() # prints '@[4, 5, 6]' |