fixes 'lent T' inside object constructor [backport] (#18911)

* fixes 'lent T' inside object constructor [backport]

* progress
This commit is contained in:
Andreas Rumpf
2021-09-27 22:23:31 +02:00
committed by GitHub
parent cdf9ac675b
commit 576fece909
4 changed files with 21 additions and 3 deletions

View File

@@ -107,12 +107,13 @@ proc fitNode(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode =
else:
result = fitNodePostMatch(c, formal, result)
proc fitNodeForLocalVar(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode =
proc fitNodeConsiderViewType(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode =
let a = fitNode(c, formal, arg, info)
if formal.kind in {tyVar, tyLent}:
#classifyViewType(formal) != noView:
result = newNodeIT(nkHiddenAddr, a.info, formal)
result.add a
formal.flags.incl tfVarIsPtr
else:
result = a

View File

@@ -79,7 +79,7 @@ proc semConstrField(c: PContext, flags: TExprFlags,
var initValue = semExprFlagDispatched(c, assignment[1], flags)
if initValue != nil:
initValue = fitNode(c, field.typ, initValue, assignment.info)
initValue = fitNodeConsiderViewType(c, field.typ, initValue, assignment.info)
assignment[0] = newSymNode(field)
assignment[1] = initValue
assignment.flags.incl nfSem

View File

@@ -545,7 +545,7 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
else:
# BUGFIX: ``fitNode`` is needed here!
# check type compatibility between def.typ and typ
def = fitNodeForLocalVar(c, typ, def, def.info)
def = fitNodeConsiderViewType(c, typ, def, def.info)
#changeType(def.skipConv, typ, check=true)
else:
typ = def.typ.skipTypes({tyStatic, tySink}).skipIntLit(c.idgen)

View File

@@ -49,3 +49,20 @@ type
let s1 = @[1,3,4,5,6]
var test = F(oa: toOpenArray(s1, 0, 2))
echo test
type
Foo = object
x: string
y: seq[int]
data: array[10000, byte]
View[T] = object
x: lent T
proc mainB =
let f = Foo(y: @[1, 2, 3])
let foo = View[Foo](x: f)
assert foo.x.x == ""
assert foo.x.y == @[1, 2, 3]
mainB()