Fix array's high & low return type for empty arrays (#17705)
* fix array.high/low return type * Add test for empty array low return type Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
This commit is contained in:
parent
51a40c1f38
commit
363467fc00
3 changed files with 17 additions and 0 deletions
|
|
@ -379,6 +379,8 @@ proc semLowHigh(c: PContext, n: PNode, m: TMagic): PNode =
|
||||||
n.typ = getSysType(c.graph, n.info, tyInt)
|
n.typ = getSysType(c.graph, n.info, tyInt)
|
||||||
of tyArray:
|
of tyArray:
|
||||||
n.typ = typ[0] # indextype
|
n.typ = typ[0] # indextype
|
||||||
|
if n.typ.kind == tyRange and emptyRange(n.typ.n[0], n.typ.n[1]): #Invalid range
|
||||||
|
n.typ = getSysType(c.graph, n.info, tyInt)
|
||||||
of tyInt..tyInt64, tyChar, tyBool, tyEnum, tyUInt..tyUInt64, tyFloat..tyFloat64:
|
of tyInt..tyInt64, tyChar, tyBool, tyEnum, tyUInt..tyUInt64, tyFloat..tyFloat64:
|
||||||
n.typ = n[1].typ.skipTypes({tyTypeDesc})
|
n.typ = n[1].typ.skipTypes({tyTypeDesc})
|
||||||
of tyGenericParam:
|
of tyGenericParam:
|
||||||
|
|
|
||||||
|
|
@ -348,6 +348,8 @@ proc high*[T](x: openArray[T]): int {.magic: "High", noSideEffect.}
|
||||||
proc high*[I, T](x: array[I, T]): I {.magic: "High", noSideEffect.}
|
proc high*[I, T](x: array[I, T]): I {.magic: "High", noSideEffect.}
|
||||||
## Returns the highest possible index of an array `x`.
|
## Returns the highest possible index of an array `x`.
|
||||||
##
|
##
|
||||||
|
## For empty arrays, the return type is `int`.
|
||||||
|
##
|
||||||
## See also:
|
## See also:
|
||||||
## * `low(array) <#low,array[I,T]>`_
|
## * `low(array) <#low,array[I,T]>`_
|
||||||
##
|
##
|
||||||
|
|
@ -360,6 +362,8 @@ proc high*[I, T](x: array[I, T]): I {.magic: "High", noSideEffect.}
|
||||||
proc high*[I, T](x: typedesc[array[I, T]]): I {.magic: "High", noSideEffect.}
|
proc high*[I, T](x: typedesc[array[I, T]]): I {.magic: "High", noSideEffect.}
|
||||||
## Returns the highest possible index of an array type.
|
## Returns the highest possible index of an array type.
|
||||||
##
|
##
|
||||||
|
## For empty arrays, the return type is `int`.
|
||||||
|
##
|
||||||
## See also:
|
## See also:
|
||||||
## * `low(typedesc[array]) <#low,typedesc[array[I,T]]>`_
|
## * `low(typedesc[array]) <#low,typedesc[array[I,T]]>`_
|
||||||
##
|
##
|
||||||
|
|
@ -420,6 +424,8 @@ proc low*[T](x: openArray[T]): int {.magic: "Low", noSideEffect.}
|
||||||
proc low*[I, T](x: array[I, T]): I {.magic: "Low", noSideEffect.}
|
proc low*[I, T](x: array[I, T]): I {.magic: "Low", noSideEffect.}
|
||||||
## Returns the lowest possible index of an array `x`.
|
## Returns the lowest possible index of an array `x`.
|
||||||
##
|
##
|
||||||
|
## For empty arrays, the return type is `int`.
|
||||||
|
##
|
||||||
## See also:
|
## See also:
|
||||||
## * `high(array) <#high,array[I,T]>`_
|
## * `high(array) <#high,array[I,T]>`_
|
||||||
##
|
##
|
||||||
|
|
@ -432,6 +438,8 @@ proc low*[I, T](x: array[I, T]): I {.magic: "Low", noSideEffect.}
|
||||||
proc low*[I, T](x: typedesc[array[I, T]]): I {.magic: "Low", noSideEffect.}
|
proc low*[I, T](x: typedesc[array[I, T]]): I {.magic: "Low", noSideEffect.}
|
||||||
## Returns the lowest possible index of an array type.
|
## Returns the lowest possible index of an array type.
|
||||||
##
|
##
|
||||||
|
## For empty arrays, the return type is `int`.
|
||||||
|
##
|
||||||
## See also:
|
## See also:
|
||||||
## * `high(typedesc[array]) <#high,typedesc[array[I,T]]>`_
|
## * `high(typedesc[array]) <#high,typedesc[array[I,T]]>`_
|
||||||
##
|
##
|
||||||
|
|
|
||||||
|
|
@ -587,3 +587,10 @@ block t12466:
|
||||||
a[0'u16 + i] = i
|
a[0'u16 + i] = i
|
||||||
for i in 0'u16 ..< 8'u16:
|
for i in 0'u16 ..< 8'u16:
|
||||||
a[0'u16 + i] = i
|
a[0'u16 + i] = i
|
||||||
|
|
||||||
|
block t17705:
|
||||||
|
# https://github.com/nim-lang/Nim/pull/17705
|
||||||
|
var a = array[0, int].low
|
||||||
|
a = int(a)
|
||||||
|
var b = array[0, int].high
|
||||||
|
b = int(b)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue