diff options
author | Araq <rumpf_a@web.de> | 2019-04-22 08:05:16 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2019-04-22 08:05:16 +0200 |
commit | 665fcb12dd0eef8637fb573b92a791021ba34740 (patch) | |
tree | 985a60fb57627589f7d99dbf0d2a1da833a89508 | |
parent | fabc2a7086bb83afc2abfd6d5841f25c8387609f (diff) | |
download | Nim-665fcb12dd0eef8637fb573b92a791021ba34740.tar.gz |
fixes #11073
-rw-r--r-- | compiler/semexprs.nim | 6 | ||||
-rw-r--r-- | tests/destructor/tdont_return_unowned_from_owned.nim | 35 |
2 files changed, 39 insertions, 2 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 10a058ff0..bd7ee1ab3 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1528,8 +1528,10 @@ proc asgnToResultVar(c: PContext, n, le, ri: PNode) {.inline.} = proc asgnToResult(c: PContext, n, le, ri: PNode) = # Special typing rule: do not allow to pass 'owned T' to 'T' in 'result = x': - if ri.typ != nil and ri.typ.skipTypes(abstractInst).kind == tyOwned and - le.typ != nil and le.typ.skipTypes(abstractInst).kind != tyOwned and ri.kind in nkCallKinds: + const absInst = abstractInst - {tyOwned} + if ri.typ != nil and ri.typ.skipTypes(absInst).kind == tyOwned and + le.typ != nil and le.typ.skipTypes(absInst).kind != tyOwned and + ri.kind in nkCallKinds+{nkObjConstr}: localError(c.config, n.info, "cannot return an owned pointer as an unowned pointer; " & "use 'owned(" & typeToString(le.typ) & ")' as the return type") diff --git a/tests/destructor/tdont_return_unowned_from_owned.nim b/tests/destructor/tdont_return_unowned_from_owned.nim new file mode 100644 index 000000000..490134ee9 --- /dev/null +++ b/tests/destructor/tdont_return_unowned_from_owned.nim @@ -0,0 +1,35 @@ +discard """ + cmd: "nim check --newruntime --hints:off $file" + nimout: '''tdont_return_unowned_from_owned.nim(24, 10) Error: cannot return an owned pointer as an unowned pointer; use 'owned(Obj)' as the return type +tdont_return_unowned_from_owned.nim(27, 10) Error: cannot return an owned pointer as an unowned pointer; use 'owned(Obj)' as the return type +tdont_return_unowned_from_owned.nim(30, 6) Error: type mismatch: got <Obj> +but expected one of: +proc new[T](a: var ref T; finalizer: proc (x: ref T) {.nimcall.}) +2 other mismatching symbols have been suppressed; compile with --showAllMismatches:on to see them + +expression: new(result) +tdont_return_unowned_from_owned.nim(30, 6) Error: illformed AST: +''' + errormsg: "illformed AST:" + line: 30 +""" + + + +# bug #11073 +type + Obj = ref object + +proc newObjA(): Obj = + result = new Obj + +proc newObjB(): Obj = + result = Obj() + +proc newObjC(): Obj = + new(result) + +let a = newObjA() +let b = newObjB() +let c = newObjC() + |