fixes 'lent T' inside object constructor [backport] (#18911)
* fixes 'lent T' inside object constructor [backport] * progress
This commit is contained in:
parent
cdf9ac675b
commit
576fece909
4 changed files with 21 additions and 3 deletions
|
|
@ -107,12 +107,13 @@ proc fitNode(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode =
|
||||||
else:
|
else:
|
||||||
result = fitNodePostMatch(c, formal, result)
|
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)
|
let a = fitNode(c, formal, arg, info)
|
||||||
if formal.kind in {tyVar, tyLent}:
|
if formal.kind in {tyVar, tyLent}:
|
||||||
#classifyViewType(formal) != noView:
|
#classifyViewType(formal) != noView:
|
||||||
result = newNodeIT(nkHiddenAddr, a.info, formal)
|
result = newNodeIT(nkHiddenAddr, a.info, formal)
|
||||||
result.add a
|
result.add a
|
||||||
|
formal.flags.incl tfVarIsPtr
|
||||||
else:
|
else:
|
||||||
result = a
|
result = a
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ proc semConstrField(c: PContext, flags: TExprFlags,
|
||||||
|
|
||||||
var initValue = semExprFlagDispatched(c, assignment[1], flags)
|
var initValue = semExprFlagDispatched(c, assignment[1], flags)
|
||||||
if initValue != nil:
|
if initValue != nil:
|
||||||
initValue = fitNode(c, field.typ, initValue, assignment.info)
|
initValue = fitNodeConsiderViewType(c, field.typ, initValue, assignment.info)
|
||||||
assignment[0] = newSymNode(field)
|
assignment[0] = newSymNode(field)
|
||||||
assignment[1] = initValue
|
assignment[1] = initValue
|
||||||
assignment.flags.incl nfSem
|
assignment.flags.incl nfSem
|
||||||
|
|
|
||||||
|
|
@ -545,7 +545,7 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
|
||||||
else:
|
else:
|
||||||
# BUGFIX: ``fitNode`` is needed here!
|
# BUGFIX: ``fitNode`` is needed here!
|
||||||
# check type compatibility between def.typ and typ
|
# 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)
|
#changeType(def.skipConv, typ, check=true)
|
||||||
else:
|
else:
|
||||||
typ = def.typ.skipTypes({tyStatic, tySink}).skipIntLit(c.idgen)
|
typ = def.typ.skipTypes({tyStatic, tySink}).skipIntLit(c.idgen)
|
||||||
|
|
|
||||||
|
|
@ -49,3 +49,20 @@ type
|
||||||
let s1 = @[1,3,4,5,6]
|
let s1 = @[1,3,4,5,6]
|
||||||
var test = F(oa: toOpenArray(s1, 0, 2))
|
var test = F(oa: toOpenArray(s1, 0, 2))
|
||||||
echo test
|
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()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue