summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2021-09-27 22:23:31 +0200
committerGitHub <noreply@github.com>2021-09-27 22:23:31 +0200
commit576fece90967ed2a6209c18ca839f30c751f4332 (patch)
tree2a76def9f4da47d51a780519b1dd4c8a052981aa
parentcdf9ac675b4348a7b5239186637c54920bb92619 (diff)
downloadNim-576fece90967ed2a6209c18ca839f30c751f4332.tar.gz
fixes 'lent T' inside object constructor [backport] (#18911)
* fixes 'lent T' inside object constructor [backport]

* progress
-rw-r--r--compiler/sem.nim3
-rw-r--r--compiler/semobjconstr.nim2
-rw-r--r--compiler/semstmts.nim2
-rw-r--r--tests/views/tviews1.nim17
4 files changed, 21 insertions, 3 deletions
diff --git a/compiler/sem.nim b/compiler/sem.nim
index bdecbe602..70c57864c 100644
--- a/compiler/sem.nim
+++ b/compiler/sem.nim
@@ -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
 
diff --git a/compiler/semobjconstr.nim b/compiler/semobjconstr.nim
index 1563e5c16..001956ede 100644
--- a/compiler/semobjconstr.nim
+++ b/compiler/semobjconstr.nim
@@ -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
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim
index 3b699f6c8..4607e857a 100644
--- a/compiler/semstmts.nim
+++ b/compiler/semstmts.nim
@@ -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)
diff --git a/tests/views/tviews1.nim b/tests/views/tviews1.nim
index 3dbf664fc..ced487ce8 100644
--- a/tests/views/tviews1.nim
+++ b/tests/views/tviews1.nim
@@ -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()