diff --git a/compiler/semobjconstr.nim b/compiler/semobjconstr.nim index d9317c3320..1ea9cb37f4 100644 --- a/compiler/semobjconstr.nim +++ b/compiler/semobjconstr.nim @@ -486,6 +486,11 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType # we have to watch out, there are also 'owned proc' types that can be used # multiple times as long as they don't have closures. result.typ.incl tfHasOwned + if t.kind == tyForward and n.len == 1 and efDetermineType in flags: + # a forward object type does not error during determine-type analysis; + # it now stays unresolved long enough for the existing delayed field-default pass to resolve it after the type section finishes. + result.typ = t + return result if t.kind != tyObject: return localErrorNode(c, result, if t.kind != tyGenericBody: "object constructor needs an object type".dup(addTypeNodeDeclaredLoc(c.config, t)) diff --git a/tests/objects/mobject_default_value.nim b/tests/objects/mobject_default_value.nim index 2245495015..e8b8de3844 100644 --- a/tests/objects/mobject_default_value.nim +++ b/tests/objects/mobject_default_value.nim @@ -13,3 +13,25 @@ type pendingTasks*: range[0'i32 .. high(int32)] head: T tail: T + +block: + type + Foo = object + x = Bar() + + Bar = object + x: int + + var f = Foo() + doassert f.x.x == 0 + +block: + type + Bar = object + x: int + + Foo = object + x = Bar() + + var f = Foo() + doassert f.x.x == 0