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>
This commit is contained in:
parent
de089d7fdb
commit
2afce84616
2 changed files with 11 additions and 2 deletions
|
|
@ -398,8 +398,12 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType
|
||||||
# multiple times as long as they don't have closures.
|
# multiple times as long as they don't have closures.
|
||||||
result.typ.flags.incl tfHasOwned
|
result.typ.flags.incl tfHasOwned
|
||||||
if t.kind != tyObject:
|
if t.kind != tyObject:
|
||||||
return localErrorNode(c, result,
|
return localErrorNode(c, result, if t.kind != tyGenericBody:
|
||||||
"object constructor needs an object type".dup(addDeclaredLoc(c.config, t)))
|
"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
|
# Check if the object is fully initialized by recursively testing each
|
||||||
# field (if this is a case object, initialized fields in two different
|
# field (if this is a case object, initialized fields in two different
|
||||||
|
|
|
||||||
5
tests/errmsgs/t19882_2.nim
Normal file
5
tests/errmsgs/t19882_2.nim
Normal file
|
|
@ -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()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue