lift destructor for openarray (#12073)

* destroy for sink openarray
This commit is contained in:
cooldome 2019-08-28 11:07:46 +01:00 • committed by Andreas Rumpf
commit c9f49cbc0a
5 changed files with 30 additions and 13 deletions

View file

@ -1496,14 +1496,14 @@ proc propagateToOwner*(owner, elem: PType) =
if tfHasAsgn in elem.flags: if tfHasAsgn in elem.flags:
let o2 = owner.skipTypes({tyGenericInst, tyAlias, tySink}) let o2 = owner.skipTypes({tyGenericInst, tyAlias, tySink})
if o2.kind in {tyTuple, tyObject, tyArray, if o2.kind in {tyTuple, tyObject, tyArray,
tySequence, tyOpt, tySet, tyDistinct}: tySequence, tyOpt, tySet, tyDistinct, tyOpenArray, tyVarargs}:
o2.flags.incl tfHasAsgn o2.flags.incl tfHasAsgn
owner.flags.incl tfHasAsgn owner.flags.incl tfHasAsgn
if tfHasOwned in elem.flags: if tfHasOwned in elem.flags:
let o2 = owner.skipTypes({tyGenericInst, tyAlias, tySink}) let o2 = owner.skipTypes({tyGenericInst, tyAlias, tySink})
if o2.kind in {tyTuple, tyObject, tyArray, if o2.kind in {tyTuple, tyObject, tyArray,
tySequence, tyOpt, tySet, tyDistinct}: tySequence, tyOpt, tySet, tyDistinct, tyOpenArray, tyVarargs}:
o2.flags.incl tfHasOwned o2.flags.incl tfHasOwned
owner.flags.incl tfHasOwned owner.flags.incl tfHasOwned

View file

@ -469,7 +469,7 @@ proc genProcParams(m: BModule, t: PType, rettype, params: var Rope,
add(params, param.loc.r) add(params, param.loc.r)
# declare the len field for open arrays: # declare the len field for open arrays:
var arr = param.typ var arr = param.typ
if arr.kind in {tyVar, tyLent}: arr = arr.lastSon if arr.kind in {tyVar, tyLent, tySink}: arr = arr.lastSon
var j = 0 var j = 0
while arr.kind in {tyOpenArray, tyVarargs}: while arr.kind in {tyOpenArray, tyVarargs}:
# this fixes the 'sort' bug: # this fixes the 'sort' bug:
@ -477,7 +477,7 @@ proc genProcParams(m: BModule, t: PType, rettype, params: var Rope,
# need to pass hidden parameter: # need to pass hidden parameter:
addf(params, ", NI $1Len_$2", [param.loc.r, j.rope]) addf(params, ", NI $1Len_$2", [param.loc.r, j.rope])
inc(j) inc(j)
arr = arr.sons[0] arr = arr.sons[0].skipTypes({tySink})
if t.sons[0] != nil and isInvalidReturnType(m.config, t.sons[0]): if t.sons[0] != nil and isInvalidReturnType(m.config, t.sons[0]):
var arr = t.sons[0] var arr = t.sons[0]
if params != nil: add(params, ", ") if params != nil: add(params, ", ")

View file

@ -506,7 +506,11 @@ proc fillBody(c: var TLiftCtx; t: PType; body, x, y: PNode) =
of tyTuple: of tyTuple:
fillBodyTup(c, t, body, x, y) fillBodyTup(c, t, body, x, y)
of tyVarargs, tyOpenArray: of tyVarargs, tyOpenArray:
localError(c.g.config, c.info, "cannot copy openArray") if c.kind == attachedDestructor:
forallElements(c, t, body, x, y)
else:
discard "cannot copy openArray"
of tyFromExpr, tyProxy, tyBuiltInTypeClass, tyUserTypeClass, of tyFromExpr, tyProxy, tyBuiltInTypeClass, tyUserTypeClass,
tyUserTypeClassInst, tyCompositeTypeClass, tyAnd, tyOr, tyNot, tyAnything, tyUserTypeClassInst, tyCompositeTypeClass, tyAnd, tyOr, tyNot, tyAnything,
tyGenericParam, tyGenericBody, tyNil, tyUntyped, tyTyped, tyGenericParam, tyGenericBody, tyNil, tyUntyped, tyTyped,
@ -626,7 +630,7 @@ proc createTypeBoundOps(g: ModuleGraph; c: PContext; orig: PType; info: TLineInf
var canon = g.canonTypes.getOrDefault(h) var canon = g.canonTypes.getOrDefault(h)
var overwrite = false var overwrite = false
if canon == nil: if canon == nil:
let typ = orig.skipTypes({tyGenericInst, tyAlias}) let typ = orig.skipTypes({tyGenericInst, tyAlias, tySink})
g.canonTypes[h] = typ g.canonTypes[h] = typ
canon = typ canon = typ
elif canon != orig: elif canon != orig:

View file

@ -77,14 +77,12 @@ template semIdeForTemplateOrGeneric(c: PContext; n: PNode;
discard safeSemExpr(c, n) discard safeSemExpr(c, n)
proc fitNodePostMatch(c: PContext, formal: PType, arg: PNode): PNode = proc fitNodePostMatch(c: PContext, formal: PType, arg: PNode): PNode =
result = arg let x = arg.skipConv
let x = result.skipConv
if x.kind in {nkPar, nkTupleConstr, nkCurly} and formal.kind != tyUntyped: if x.kind in {nkPar, nkTupleConstr, nkCurly} and formal.kind != tyUntyped:
changeType(c, x, formal, check=true) changeType(c, x, formal, check=true)
else: result = arg
result = skipHiddenSubConv(result) result = skipHiddenSubConv(result)
#result.typ = takeType(formal, arg.typ)
#echo arg.info, " picked ", result.typ.typeToString
proc fitNode(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode = proc fitNode(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode =
if arg.typ.isNil: if arg.typ.isNil:

View file

@ -176,3 +176,18 @@ proc myfuncLoop(x: int): MySeqNonCopyable =
result = cc result = cc
discard myfuncLoop(3) discard myfuncLoop(3)
#------------------------------------------------------------
# Move into table via openarray
#------------------------------------------------------------
type
TableNonCopyable = object
x: seq[(string, MySeqNonCopyable)]
proc toTable(pairs: sink openArray[(string, MySeqNonCopyable)]): TableNonCopyable =
discard
let mytable = {"a": newMySeq(2, 5.0)}.toTable