diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 9ff0b5c0cf..e927be417a 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -1379,8 +1379,11 @@ proc semGeneric(c: PContext, n: PNode, s: PSym, prev: PType): PType = if tx.isNil or isTupleRecursive(tx): localError(c.config, n.info, "illegal recursion in type '$1'" % typeToString(result[0])) return errorType(c) - if tx != result and tx.kind == tyObject and tx.sons[0] != nil: - semObjectTypeForInheritedGenericInst(c, n, tx) + if tx != result and tx.kind == tyObject: + if tx.sons[0] != nil: + semObjectTypeForInheritedGenericInst(c, n, tx) + var position = 0 + recomputeFieldPositions(tx, tx.n, position) proc maybeAliasType(c: PContext; typeExpr, prev: PType): PType = if typeExpr.kind in {tyObject, tyEnum, tyDistinct, tyForward} and prev != nil: diff --git a/compiler/semtypinst.nim b/compiler/semtypinst.nim index 9c17307982..950ec8e6b6 100644 --- a/compiler/semtypinst.nim +++ b/compiler/semtypinst.nim @@ -667,6 +667,22 @@ proc replaceTypesForLambda*(p: PContext, pt: TIdTable, n: PNode; result = replaceTypeVarsN(cl, n) popInfoContext(p.config) +proc recomputeFieldPositions*(t: PType; obj: PNode; currPosition: var int) = + if t != nil and t.len > 0 and t.sons[0] != nil: + let b = skipTypes(t.sons[0], skipPtrs) + recomputeFieldPositions(b, b.n, currPosition) + case obj.kind + of nkRecList: + for i in 0 ..< sonsLen(obj): recomputeFieldPositions(nil, obj.sons[i], currPosition) + of nkRecCase: + recomputeFieldPositions(nil, obj.sons[0], currPosition) + for i in 1 ..< sonsLen(obj): + recomputeFieldPositions(nil, lastSon(obj.sons[i]), currPosition) + of nkSym: + obj.sym.position = currPosition + inc currPosition + else: discard "cannot happen" + proc generateTypeInstance*(p: PContext, pt: TIdTable, info: TLineInfo, t: PType): PType = var typeMap = initLayeredTypeMap(pt) @@ -674,6 +690,10 @@ proc generateTypeInstance*(p: PContext, pt: TIdTable, info: TLineInfo, pushInfoContext(p.config, info) result = replaceTypeVarsT(cl, t) popInfoContext(p.config) + let objType = result.skipTypes(abstractInst) + if objType.kind == tyObject: + var position = 0 + recomputeFieldPositions(objType, objType.n, position) proc prepareMetatypeForSigmatch*(p: PContext, pt: TIdTable, info: TLineInfo, t: PType): PType = diff --git a/tests/vm/tcompiletimerange.nim b/tests/vm/tcompiletimerange.nim index 9f41398978..cd675b4a34 100644 --- a/tests/vm/tcompiletimerange.nim +++ b/tests/vm/tcompiletimerange.nim @@ -1,18 +1,17 @@ discard """ - disabled: "true" """ # issue #8199 -const rangesGCHoldEnabled = not defined(rangesDisableGCHold) +const rangesGCHoldEnabled = true # not defined(rangesDisableGCHold) type # A view into immutable array Range*[T] {.shallow.} = object when rangesGCHoldEnabled: - gcHold: seq[T] - start: ptr T - mLen: int32 + gcHold: seq[T] # 0 + start: ptr T # 1 + mLen: int32 # 2 type BytesRange* = Range[byte]