diff --git a/compiler/transf.nim b/compiler/transf.nim index 615bff31ee6b..59e4453fd7fb 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -865,9 +865,18 @@ proc transformArrayAccess(c: PTransf, n: PNode): PNode = if n[0].kind == nkSym and n[0].sym.kind == skType: result = n else: - result = newTransNode(n) - for i in 0..= 2 and result[1].kind in {nkChckRange, nkChckRange64} and + n[1].kind in {nkHiddenStdConv, nkHiddenSubConv}: + # implicit conversion, was transformed into range check + # remove in favor of index check if conversion to array index type + # has to be done here because the array index type needs to be relaxed + # i.e. a uint32 index can implicitly convert to range[0..3] but not int + let arr = skipTypes(n[0].typ, abstractVarRange) + if arr.kind == tyArray and + firstOrd(c.graph.config, arr) == getOrdValue(result[1][1]) and + lastOrd(c.graph.config, arr) == getOrdValue(result[1][2]): + result[1] = result[1].skipConv proc getMergeOp(n: PNode): PSym = case n.kind diff --git a/tests/array/tindexconv.nim b/tests/array/tindexconv.nim new file mode 100644 index 000000000000..aa95ed4b672f --- /dev/null +++ b/tests/array/tindexconv.nim @@ -0,0 +1,4 @@ +block: # issue #17958 + var mem: array[uint8, uint8] + let val = 0xffff'u16 + discard mem[uint8 val] # Error: unhandled exception: index 65535 not in 0 .. 255 [IndexDefect] diff --git a/tests/array/tinvalidarrayaccess.nim b/tests/array/tinvalidarrayaccess.nim index f8bce45ef365..5656deb781ff 100644 --- a/tests/array/tinvalidarrayaccess.nim +++ b/tests/array/tinvalidarrayaccess.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "index 2 not in 0 .. 1" + errormsg: "conversion from int literal(2) to range 0..1(int) is invalid" line: 18 """ block: diff --git a/tests/array/tinvalidarrayaccess2.nim b/tests/array/tinvalidarrayaccess2.nim index 0a07038344a8..1d8d0cfff586 100644 --- a/tests/array/tinvalidarrayaccess2.nim +++ b/tests/array/tinvalidarrayaccess2.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "index 3 not in 0 .. 1" + errormsg: "conversion from int literal(3) to range 0..1(int) is invalid" line: 9 """ diff --git a/tests/js/tindexdefect.nim b/tests/js/tindexdefect.nim index 37994ec2e749..eea376883836 100644 --- a/tests/js/tindexdefect.nim +++ b/tests/js/tindexdefect.nim @@ -5,5 +5,6 @@ discard """ """ var s = ['a'] -let z = s[10000] == 'a' -echo z \ No newline at end of file +let i = 10000 +let z = s[i] == 'a' +echo z