From d9ee377517fe22e307592f4d6c019388600b5e57 Mon Sep 17 00:00:00 2001 From: Vindaar Date: Wed, 23 Jan 2019 11:17:32 +0100 Subject: [PATCH] fix #10339 by returning type attached to nkEmpty (#10370) * fix #10339 by checking for nkObjConstr * revert check for nkObjConstr, return type from nkEmpty node The correct type needed in `semObjConstr` to fix #10339 is indeed available, but attached to an `nkEmpty` node. These were previously discarded in `semTypeNode`, which is used to extract the type for the object. * simplify return of PType from `nkEmpty` * also fixes #9866, add test case --- compiler/semtypes.nim | 2 +- tests/macros/tquotedo.nim | 15 +++++++++++++++ tests/statictypes/tstatictypes.nim | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index c28902b1f2..e4f7dc1479 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -1474,7 +1474,7 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType = if c.config.cmd == cmdIdeTools: suggestExpr(c, n) case n.kind - of nkEmpty: discard + of nkEmpty: result = n.typ of nkTypeOfExpr: # for ``type(countup(1,3))``, see ``tests/ttoseq``. checkSonsLen(n, 1, c.config) diff --git a/tests/macros/tquotedo.nim b/tests/macros/tquotedo.nim index 0aae87bf04..6acb8ef4eb 100644 --- a/tests/macros/tquotedo.nim +++ b/tests/macros/tquotedo.nim @@ -4,6 +4,7 @@ output: ''' Hallo Welt Hallo Welt 1 +() ''' """ @@ -34,3 +35,17 @@ macro t(): untyped = t() echo tp() + + +# https://github.com/nim-lang/Nim/issues/9866 +type + # Foo = int # works + Foo = object # fails + +macro dispatchGen(): untyped = + var shOpt: Foo + result = quote do: + let baz = `shOpt` + echo `shOpt` + +dispatchGen() diff --git a/tests/statictypes/tstatictypes.nim b/tests/statictypes/tstatictypes.nim index b7cde61247..2a4ab0c637 100644 --- a/tests/statictypes/tstatictypes.nim +++ b/tests/statictypes/tstatictypes.nim @@ -137,3 +137,20 @@ block: type Coord[N: static[int]] = tuple[col, row: range[0'i8 .. (N.int8-1)]] Point[N: static[int]] = range[0'i16 .. N.int16 * N.int16 - 1] + +# https://github.com/nim-lang/Nim/issues/10339 +block: + type + MicroKernel = object + a: float + b: int + + macro extractA(ukernel: static MicroKernel): untyped = + result = newLit ukernel.a + + proc tFunc[ukernel: static MicroKernel]() = + const x = ukernel.extractA + doAssert x == 5.5 + + const uk = MicroKernel(a: 5.5, b: 1) + tFunc[uk]()