From 0dc51dfe476f4d3ca6e7d5a27396a8ea332bd533 Mon Sep 17 00:00:00 2001 From: Bung Date: Thu, 22 Sep 2022 09:11:39 +0800 Subject: [PATCH] Improve error message when instantiating generics with object constructor (#20358) * Improve error message when instantiating generics with object constructor * follow suggestion * Update compiler/semobjconstr.nim Co-authored-by: Clay Sweetser * Update tests/errmsgs/t19882_2.nim Co-authored-by: Clay Sweetser (cherry picked from commit 2afce84616e1de176c9e76a3e0146fff7ab1de10) --- compiler/semobjconstr.nim | 8 ++++++-- tests/errmsgs/t19882_2.nim | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 tests/errmsgs/t19882_2.nim diff --git a/compiler/semobjconstr.nim b/compiler/semobjconstr.nim index 6d11d321ec..64672c59fe 100644 --- a/compiler/semobjconstr.nim +++ b/compiler/semobjconstr.nim @@ -392,8 +392,12 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags): PNode = # multiple times as long as they don't have closures. result.typ.flags.incl tfHasOwned if t.kind != tyObject: - return localErrorNode(c, result, - "object constructor needs an object type".dup(addDeclaredLoc(c.config, t))) + return localErrorNode(c, result, if t.kind != tyGenericBody: + "object constructor needs an object type".dup(addDeclaredLoc(c.config, t)) + else: "cannot instantiate: '" & + typeToString(t, preferDesc) & + "'; the object's generic parameters cannot be inferred and must be explicitly given" + ) # Check if the object is fully initialized by recursively testing each # field (if this is a case object, initialized fields in two different diff --git a/tests/errmsgs/t19882_2.nim b/tests/errmsgs/t19882_2.nim new file mode 100644 index 0000000000..7f3055a5da --- /dev/null +++ b/tests/errmsgs/t19882_2.nim @@ -0,0 +1,5 @@ +discard """ + errormsg: "cannot instantiate: 'A[T]'; the object's generic parameters cannot be inferred and must be explicitly given" +""" +type A[T] = object +var a = A() \ No newline at end of file