From 36d3fd64da21fe128f92ea49943c9dd834aa6848 Mon Sep 17 00:00:00 2001 From: Araq Date: Sat, 11 Feb 2017 00:42:12 +0100 Subject: [PATCH] fixes #5354 --- compiler/semexprs.nim | 10 +++++----- tests/metatype/tfieldaccessor.nim | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 tests/metatype/tfieldaccessor.nim diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 57674735a0..9972585c7b 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1093,11 +1093,11 @@ proc builtinFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode = return readTypeParameter(c, ty, i, n.info) of tyObject, tyTuple: if ty.n != nil and ty.n.kind == nkRecList: - for field in ty.n: - if field.sym.name == i: - n.typ = newTypeWithSons(c, tyFieldAccessor, @[ty, field.sym.typ]) - n.typ.n = copyTree(n) - return n + let field = lookupInRecord(ty.n, i) + if field != nil: + n.typ = newTypeWithSons(c, tyFieldAccessor, @[ty, field.typ]) + n.typ.n = copyTree(n) + return n else: # echo "TYPE FIELD ACCESS" # debug ty diff --git a/tests/metatype/tfieldaccessor.nim b/tests/metatype/tfieldaccessor.nim new file mode 100644 index 0000000000..7054dd22b9 --- /dev/null +++ b/tests/metatype/tfieldaccessor.nim @@ -0,0 +1,17 @@ +type + Test = object + x: int + case p: bool + of true: + a: int + else: + case q: bool + of true: + b: int + else: + discard + +proc f[T](t: typedesc[T]): int = + 1 + +assert Test.f == 1