Skip to content

Commit 3a027d7

Browse files
authored
fix erroring invoke type (#1161)
fixes #1160
1 parent 9ae31a1 commit 3a027d7

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

src/nimony/sem.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3066,11 +3066,17 @@ proc semObjConstr(c: var SemContext, it: var Item) =
30663066
if decl.kind != TypeY:
30673067
# includes typevar case
30683068
c.buildErr info, "expected type for object constructor"
3069+
skipToEnd it.n
30693070
return
30703071
objType = decl.objBody
30713072
if objType.typeKind != ObjectT:
30723073
c.buildErr info, "expected object type for object constructor"
3074+
skipToEnd it.n
30733075
return
3076+
else:
3077+
c.buildErr info, "expected type symbol for object constructor"
3078+
skipToEnd it.n
3079+
return
30743080
# build bindings for invoked object type to get proper types for fields:
30753081
let bindings = bindInvokeArgs(decl, invokeArgs)
30763082
var fieldBuf = createTokenBuf(16)

src/nimony/semtypes.nim

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ proc semInvoke(c: var SemContext; n: var Cursor) =
299299
decl = getTypeSection(headId)
300300
if decl.kind != TypeY:
301301
c.buildErr info, "cannot attempt to instantiate a non-type"
302-
if decl.typevars.substructureKind != TypevarsU:
302+
elif decl.typevars.substructureKind != TypevarsU:
303303
c.buildErr info, "cannot attempt to instantiate a concrete type"
304304
else:
305305
ok = true
@@ -316,8 +316,10 @@ proc semInvoke(c: var SemContext; n: var Cursor) =
316316
else:
317317
c.buildErr info, "cannot attempt to instantiate a non-type"
318318

319-
var params = decl.typevars
320-
inc params
319+
var params = default(Cursor)
320+
if decl.kind == TypeY:
321+
params = decl.typevars
322+
inc params
321323
var paramCount = 0
322324
var argCount = 0
323325
var m = createMatch(addr c)
@@ -332,7 +334,7 @@ proc semInvoke(c: var SemContext; n: var Cursor) =
332334
semLocalTypeImpl c, n, AllowValues
333335
swap c.dest, argBuf
334336
var addArg = true
335-
if params.kind == ParRi:
337+
if cursorIsNil(params) or params.kind == ParRi:
336338
# will error later from param/arg count not matching
337339
discard
338340
else:
@@ -349,7 +351,7 @@ proc semInvoke(c: var SemContext; n: var Cursor) =
349351
c.dest.add argBuf
350352
swap c.usedTypevars, genericArgs
351353
takeParRi c, n
352-
if paramCount != argCount:
354+
if ok and paramCount != argCount:
353355
c.dest.shrink typeStart
354356
c.buildErr info, "wrong amount of generic parameters for type " & pool.syms[headId] &
355357
", expected " & $paramCount & " but got " & $argCount
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tests/nimony/errmsgs/twronginvoke.nim(9, 13) Error: undeclared identifier
2+
tests/nimony/errmsgs/twronginvoke.nim(9, 18) Error: cannot attempt to instantiate a non-type
3+
tests/nimony/errmsgs/twronginvoke.nim(9, 26) Error: expected type symbol for object constructor
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type
2+
GObj[T] = ref object
3+
data: T
4+
5+
AObj = ref object
6+
data: string
7+
8+
proc main =
9+
var obj = BGObj[string](data: "abc")
10+
11+
var a = AObj(data: "abc")
12+
13+
var objB = GObj[int](data: 456)
14+
15+
main()

0 commit comments

Comments
 (0)