From 482fe75dda86e1c3b5c0bbde90d42c2b48ddeadf Mon Sep 17 00:00:00 2001 From: metagn Date: Sat, 9 Nov 2024 18:28:16 +0300 Subject: [PATCH 1/2] don't skip conversions in array indices in transf fixes #17958 --- compiler/transf.nim | 4 +--- tests/array/tindexconv.nim | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 tests/array/tindexconv.nim diff --git a/compiler/transf.nim b/compiler/transf.nim index 615bff31ee6b..94fdfcfb3d66 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -865,9 +865,7 @@ 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.. Date: Sat, 9 Nov 2024 20:19:01 +0300 Subject: [PATCH 2/2] weird alternative --- compiler/transf.nim | 11 +++++++++++ tests/array/tinvalidarrayaccess.nim | 2 +- tests/array/tinvalidarrayaccess2.nim | 2 +- tests/js/tindexdefect.nim | 5 +++-- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/compiler/transf.nim b/compiler/transf.nim index 94fdfcfb3d66..59e4453fd7fb 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -866,6 +866,17 @@ proc transformArrayAccess(c: PTransf, n: PNode): PNode = result = n else: result = transformSons(c, n) + if n.len >= 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/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