mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-04 10:54:42 +00:00
fixes 'lent T' inside object constructor [backport] (#18911)
* fixes 'lent T' inside object constructor [backport] * progress
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user