typed/untyped return type is invalid for everything except templates and macros (#10275)
This commit is contained in:
parent
c8ba9ffdba
commit
8922063bd8
3 changed files with 17 additions and 5 deletions
|
|
@ -1253,9 +1253,6 @@ proc semTypeSection(c: PContext, n: PNode): PNode =
|
||||||
|
|
||||||
proc semParamList(c: PContext, n, genericParams: PNode, s: PSym) =
|
proc semParamList(c: PContext, n, genericParams: PNode, s: PSym) =
|
||||||
s.typ = semProcTypeNode(c, n, genericParams, nil, s.kind)
|
s.typ = semProcTypeNode(c, n, genericParams, nil, s.kind)
|
||||||
if s.kind notin {skMacro, skTemplate}:
|
|
||||||
if s.typ.sons[0] != nil and s.typ.sons[0].kind == tyStmt:
|
|
||||||
localError(c.config, n.info, "invalid return type: 'stmt'")
|
|
||||||
|
|
||||||
proc addParams(c: PContext, n: PNode, kind: TSymKind) =
|
proc addParams(c: PContext, n: PNode, kind: TSymKind) =
|
||||||
for i in countup(1, sonsLen(n)-1):
|
for i in countup(1, sonsLen(n)-1):
|
||||||
|
|
|
||||||
|
|
@ -1158,12 +1158,15 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
|
||||||
# turn explicit 'void' return type into 'nil' because the rest of the
|
# turn explicit 'void' return type into 'nil' because the rest of the
|
||||||
# compiler only checks for 'nil':
|
# compiler only checks for 'nil':
|
||||||
if skipTypes(r, {tyGenericInst, tyAlias, tySink}).kind != tyVoid:
|
if skipTypes(r, {tyGenericInst, tyAlias, tySink}).kind != tyVoid:
|
||||||
|
if kind notin {skMacro, skTemplate} and r.kind in {tyStmt, tyExpr}:
|
||||||
|
localError(c.config, n.sons[0].info, "return type '" & typeToString(r) &
|
||||||
|
"' is only valid for macros and templates")
|
||||||
# 'auto' as a return type does not imply a generic:
|
# 'auto' as a return type does not imply a generic:
|
||||||
if r.kind == tyAnything:
|
elif r.kind == tyAnything:
|
||||||
# 'p(): auto' and 'p(): expr' are equivalent, but the rest of the
|
# 'p(): auto' and 'p(): expr' are equivalent, but the rest of the
|
||||||
# compiler is hardly aware of 'auto':
|
# compiler is hardly aware of 'auto':
|
||||||
r = newTypeS(tyExpr, c)
|
r = newTypeS(tyExpr, c)
|
||||||
elif r.kind != tyExpr:
|
else:
|
||||||
if r.sym == nil or sfAnon notin r.sym.flags:
|
if r.sym == nil or sfAnon notin r.sym.flags:
|
||||||
let lifted = liftParamType(c, kind, genericParams, r, "result",
|
let lifted = liftParamType(c, kind, genericParams, r, "result",
|
||||||
n.sons[0].info)
|
n.sons[0].info)
|
||||||
|
|
|
||||||
12
tests/proc/tillegalreturntype.nim
Normal file
12
tests/proc/tillegalreturntype.nim
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
discard """
|
||||||
|
cmd: "nim check $file"
|
||||||
|
errmsg: ""
|
||||||
|
nimout: '''tillegalreturntype.nim(8, 11) Error: return type 'typed' is only valid for macros and templates
|
||||||
|
tillegalreturntype.nim(11, 11) Error: return type 'untyped' is only valid for macros and templates'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
proc x(): typed =
|
||||||
|
discard
|
||||||
|
|
||||||
|
proc y(): untyped =
|
||||||
|
discard
|
||||||
Loading…
Add table
Add a link
Reference in a new issue