diff --git a/compiler/semobjconstr.nim b/compiler/semobjconstr.nim index e90e0bc810744..1e4bb90ebca93 100644 --- a/compiler/semobjconstr.nim +++ b/compiler/semobjconstr.nim @@ -465,11 +465,14 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType if t == nil: return localErrorNode(c, result, "object constructor needs an object type") - if t.skipTypes({tyGenericInst, - tyAlias, tySink, tyOwned, tyRef}).kind != tyObject and - expectedType != nil and expectedType.skipTypes({tyGenericInst, - tyAlias, tySink, tyOwned, tyRef}).kind == tyObject: - t = expectedType + when false: + # attempted type inference for generic object types, + # doesn't work since n[0] isn't set and seems underspecified + if t.skipTypes({tyGenericInst, + tyAlias, tySink, tyOwned, tyRef}).kind != tyObject and + expectedType != nil and expectedType.skipTypes({tyGenericInst, + tyAlias, tySink, tyOwned, tyRef}).kind == tyObject: + t = expectedType t = skipTypes(t, {tyGenericInst, tyAlias, tySink, tyOwned}) if t.kind == tyRef: diff --git a/tests/errmsgs/tuninstobjconstr.nim b/tests/errmsgs/tuninstobjconstr.nim new file mode 100644 index 0000000000000..5d6cac304f479 --- /dev/null +++ b/tests/errmsgs/tuninstobjconstr.nim @@ -0,0 +1,11 @@ +# issue #24372 + +type + Foo[T] = object + x: string + +proc initFoo(): Foo[string] = + Foo(x: "hello") #[tt.Error + ^ cannot instantiate: 'Foo[T]'; the object's generic parameters cannot be inferred and must be explicitly given]# + +discard initFoo()