You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
whentrue:
type A =object
w: range[2..8]
procfn(n: int): seq[A] =# for i in 0..<n: result.add A(w: 2) # no ProveInit warning but inefficient, refs https://github.com/nim-lang/Nim/pull/13808#discussion_r401327323
{.cast(ProveInit).}:
result=newSeq[A](n)
for i in0..<n: result[i] =A(w: 2)
procfn2(n: int): seq[A] =result=newSeq[A](n) # would correctly trigger `ProveInit` here unless silenced by another {.cast(ProveInit).}:
example 3
similar to Example 2 but for UnsafeSetLen; user code should be able to locally silence Warning: setLen can potentially expand the sequence, but the element type 'A' doesn't have a valid default value [UnsafeSetLen]
whentrue:
type A =object
a0: range[2..8]
procfn(n: int): seq[A] =
{.cast(UnsafeSetLen).}:
result.setLen n
for ai inmitems(result): ai.w =2
example 4
would fix nim-lang/Nim#3608 or at least provide a suitable workaround
whentrue:
type A =refobject
w: range[2..6]
proccreateABC(): A =# result = abc(w: 2) # works but may be undesirable
{.cast(ProveInit).}:
new(result)
result.w =2
would provide a suitable workaround for nim-lang/Nim#9466 (issue is getting closed with nim-lang/Nim#17538 as the error disappeared but the warning is still there)
alternatives considered
{.pushhint[Performance]:off.}
code
{.pop.}
# or:
{.hint[Performance]:off.}:
code
# or:
{.hint[Performance]:off.}
code
{.hint[Performance]:on.} # bad: would set to on even if wasn't set to on before setting to off
these have following downsides:
has global effect
push/pop is not lexically scoped and easy to get wrong, especially given fact that pop is IIRC optional
it's subject to the distinction of foreign package vs main package notes
it doesn't actually work depending on the hint/warning
eg, there's no way AFAIK to make it work with current pragmas ({.hint[Performance]:off.} or push etc)
whentrue:
type A =object
w: range[2..8]
procfn(n: int): seq[A] =# for i in 0..<n: result.add A(w: 2) # no ProveInit warning but inefficient, refs https://github.com/nim-lang/Nim/pull/13808#discussion_r401327323# {.push warning[ProveInit]:off.}
{.warning[ProveInit]:off.}:
result=newSeq[A](n)
for i in0..<n: result[i] =A(w: 2)
# {.warning[ProveInit]:off.}# {.pop.}procfn2(n: int): seq[A] =# this should give `ProveInit` warning but does notresult=newSeq[A](n)
The text was updated successfully, but these errors were encountered:
Good idea but cast(Performance) is not unsafe while the others are. Maybe we need a different pragma name instead. However, the performance hint can also be avoided by an explicit copy call. (Was planned, not sure if it has been added already.)
proposal
extend {.cast.} to more use cases
right now it allows
{.cast(noSideEffect).}
,{.cast(gcsafe).}
eg as follows:this proposal extends this) concept to these:
to disable hint/warning/error in a scope
example 1
example 2
would give a good solution to the classic NotNil initialization problem, refs nim-lang/Nim#13808 (comment)
example 3
similar to Example 2 but for
UnsafeSetLen
; user code should be able to locally silenceWarning: setLen can potentially expand the sequence, but the element type 'A' doesn't have a valid default value [UnsafeSetLen]
example 4
would fix nim-lang/Nim#3608 or at least provide a suitable workaround
likewise, would provide a suitable workaround for nim-lang/Nim#16735
example 5
would provide a suitable workaround for nim-lang/Nim#9466 (issue is getting closed with nim-lang/Nim#17538 as the error disappeared but the warning is still there)
alternatives considered
these have following downsides:
eg, there's no way AFAIK to make it work with current pragmas (
{.hint[Performance]:off.}
orpush
etc)The text was updated successfully, but these errors were encountered: