Skip to content

Commit 2afce84

Browse files
bung87Varriount
andauthored
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 <Varriount@users.noreply.github.com> * Update tests/errmsgs/t19882_2.nim Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
1 parent de089d7 commit 2afce84

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

compiler/semobjconstr.nim

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,12 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType
398398
# multiple times as long as they don't have closures.
399399
result.typ.flags.incl tfHasOwned
400400
if t.kind != tyObject:
401-
return localErrorNode(c, result,
402-
"object constructor needs an object type".dup(addDeclaredLoc(c.config, t)))
401+
return localErrorNode(c, result, if t.kind != tyGenericBody:
402+
"object constructor needs an object type".dup(addDeclaredLoc(c.config, t))
403+
else: "cannot instantiate: '" &
404+
typeToString(t, preferDesc) &
405+
"'; the object's generic parameters cannot be inferred and must be explicitly given"
406+
)
403407

404408
# Check if the object is fully initialized by recursively testing each
405409
# field (if this is a case object, initialized fields in two different

tests/errmsgs/t19882_2.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
discard """
2+
errormsg: "cannot instantiate: 'A[T]'; the object's generic parameters cannot be inferred and must be explicitly given"
3+
"""
4+
type A[T] = object
5+
var a = A()

0 commit comments

Comments
 (0)