closes #18690; make view types stricter [backport] (#18891)

* closes #18690

* don't allow capturing of view types [backport]
This commit is contained in:
Andreas Rumpf 2021-09-24 16:27:34 +02:00 • committed by GitHub
commit 7e5eab571e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View file

@ -12,7 +12,7 @@
import import
intsets, strutils, options, ast, astalgo, msgs, intsets, strutils, options, ast, astalgo, msgs,
idents, renderer, types, magicsys, lowerings, tables, modulegraphs, lineinfos, idents, renderer, types, magicsys, lowerings, tables, modulegraphs, lineinfos,
transf, liftdestructors transf, liftdestructors, typeallowed
discard """ discard """
The basic approach is that captured vars need to be put on the heap and The basic approach is that captured vars need to be put on the heap and
@ -191,9 +191,7 @@ proc interestingVar(s: PSym): bool {.inline.} =
s.typ.kind notin {tyStatic, tyTypeDesc} s.typ.kind notin {tyStatic, tyTypeDesc}
proc illegalCapture(s: PSym): bool {.inline.} = proc illegalCapture(s: PSym): bool {.inline.} =
result = skipTypes(s.typ, abstractInst).kind in result = classifyViewType(s.typ) != noView or s.kind == skResult
{tyVar, tyOpenArray, tyVarargs, tyLent} or
s.kind == skResult
proc isInnerProc(s: PSym): bool = proc isInnerProc(s: PSym): bool =
if s.kind in {skProc, skFunc, skMethod, skConverter, skIterator} and s.magic == mNone: if s.kind in {skProc, skFunc, skMethod, skConverter, skIterator} and s.magic == mNone:

View file

@ -6,7 +6,8 @@ discard """
2 2
3 3
3 3
15''' 15
(oa: [1, 3, 4])'''
targets: "c cpp" targets: "c cpp"
""" """
@ -38,3 +39,13 @@ var y: var int = foo(x)
y = 15 y = 15
echo foo(x) echo foo(x)
# bug #16132 # bug #16132
# bug #18690
type
F = object
oa: openarray[int]
let s1 = @[1,3,4,5,6]
var test = F(oa: toOpenArray(s1, 0, 2))
echo test