forbid casting to bare unchecked array (#11186)
* fixes #11180, forbid casting to unchecked array. * allow UncheckedArray as param
This commit is contained in:
parent
a5fb0acf5f
commit
34405db80f
3 changed files with 23 additions and 7 deletions
|
|
@ -1199,9 +1199,9 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind,
|
||||||
case t2.kind
|
case t2.kind
|
||||||
of tyVar, tyLent:
|
of tyVar, tyLent:
|
||||||
if taHeap notin flags: result = t2 # ``var var`` is illegal on the heap
|
if taHeap notin flags: result = t2 # ``var var`` is illegal on the heap
|
||||||
of tyOpenArray:
|
of tyOpenArray, tyUncheckedArray:
|
||||||
if kind != skParam: result = t
|
if kind != skParam: result = t
|
||||||
else: result = typeAllowedAux(marker, t2, kind, flags)
|
else: result = typeAllowedAux(marker, t2.sons[0], skParam, flags)
|
||||||
else:
|
else:
|
||||||
if kind notin {skParam, skResult}: result = t
|
if kind notin {skParam, skResult}: result = t
|
||||||
else: result = typeAllowedAux(marker, t2, kind, flags)
|
else: result = typeAllowedAux(marker, t2, kind, flags)
|
||||||
|
|
@ -1235,14 +1235,21 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind,
|
||||||
result = nil
|
result = nil
|
||||||
of tyOrdinal:
|
of tyOrdinal:
|
||||||
if kind != skParam: result = t
|
if kind != skParam: result = t
|
||||||
of tyGenericInst, tyDistinct, tyAlias, tyInferred, tyUncheckedArray:
|
of tyGenericInst, tyDistinct, tyAlias, tyInferred:
|
||||||
result = typeAllowedAux(marker, lastSon(t), kind, flags)
|
result = typeAllowedAux(marker, lastSon(t), kind, flags)
|
||||||
of tyRange:
|
of tyRange:
|
||||||
if skipTypes(t.sons[0], abstractInst-{tyTypeDesc}).kind notin
|
if skipTypes(t.sons[0], abstractInst-{tyTypeDesc}).kind notin
|
||||||
{tyChar, tyEnum, tyInt..tyFloat128, tyUInt8..tyUInt32}: result = t
|
{tyChar, tyEnum, tyInt..tyFloat128, tyUInt8..tyUInt32}: result = t
|
||||||
of tyOpenArray, tyVarargs, tySink:
|
of tyOpenArray, tyVarargs, tySink:
|
||||||
if kind != skParam: result = t
|
if kind != skParam:
|
||||||
else: result = typeAllowedAux(marker, t.sons[0], skVar, flags)
|
result = t
|
||||||
|
else:
|
||||||
|
result = typeAllowedAux(marker, t.sons[0], skVar, flags)
|
||||||
|
of tyUncheckedArray:
|
||||||
|
if kind != skParam and taHeap notin flags:
|
||||||
|
result = t
|
||||||
|
else:
|
||||||
|
result = typeAllowedAux(marker, lastSon(t), kind, flags)
|
||||||
of tySequence, tyOpt:
|
of tySequence, tyOpt:
|
||||||
if t.sons[0].kind != tyEmpty:
|
if t.sons[0].kind != tyEmpty:
|
||||||
result = typeAllowedAux(marker, t.sons[0], skVar, flags+{taHeap})
|
result = typeAllowedAux(marker, t.sons[0], skVar, flags+{taHeap})
|
||||||
|
|
|
||||||
9
tests/errmsgs/tuncheckedarrayvar.nim
Normal file
9
tests/errmsgs/tuncheckedarrayvar.nim
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
discard """
|
||||||
|
errormsg: '''
|
||||||
|
invalid type: 'UncheckedArray[uint8]' for var
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
var
|
||||||
|
rawMem = alloc0(20)
|
||||||
|
byteUA = cast[UncheckedArray[uint8]](rawMem)
|
||||||
|
|
@ -4,7 +4,7 @@ IntLit 5
|
||||||
proc (x: int): string => typeDesc[proc[string, int]]
|
proc (x: int): string => typeDesc[proc[string, int]]
|
||||||
proc (x: int): void => typeDesc[proc[void, int]]
|
proc (x: int): void => typeDesc[proc[void, int]]
|
||||||
proc (x: int) => typeDesc[proc[void, int]]
|
proc (x: int) => typeDesc[proc[void, int]]
|
||||||
x => UncheckedArray[int]
|
x => seq[int]
|
||||||
a
|
a
|
||||||
s
|
s
|
||||||
d
|
d
|
||||||
|
|
@ -111,7 +111,7 @@ block t2211:
|
||||||
showType(proc(x:int): void)
|
showType(proc(x:int): void)
|
||||||
showType(proc(x:int))
|
showType(proc(x:int))
|
||||||
|
|
||||||
var x: UncheckedArray[int]
|
var x: seq[int]
|
||||||
showType(x)
|
showType(x)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue