summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorflywind <xzsflywind@gmail.com>2021-09-15 01:40:42 +0800
committerGitHub <noreply@github.com>2021-09-14 19:40:42 +0200
commitcebf7cdc1ede7385e8d774b83c52d17e7021899b (patch)
tree9152f34f5dbb9637dd1a6b2ada2d961e51b1fed2
parentbf1700bab17da3c8776914ac5bdfff016eb70442 (diff)
downloadNim-cebf7cdc1ede7385e8d774b83c52d17e7021899b.tar.gz
fix #10128 (#18852)
-rw-r--r--compiler/ccgexprs.nim6
-rw-r--r--tests/ccgbugs/t10128.nim18
2 files changed, 21 insertions, 3 deletions
diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim
index 374876367..7a3d769aa 100644
--- a/compiler/ccgexprs.nim
+++ b/compiler/ccgexprs.nim
@@ -1006,14 +1006,14 @@ proc genOpenArrayElem(p: BProc, n, x, y: PNode, d: var TLoc) =
     # emit range check:
     if optBoundsCheck in p.options:
       linefmt(p, cpsStmts, "if ((NU)($1) >= (NU)($2Len_0)){ #raiseIndexError2($1,$2Len_0-1); $3}$n",
-              [rdLoc(b), rdLoc(a), raiseInstr(p)]) # BUGFIX: ``>=`` and not ``>``!
+              [rdCharLoc(b), rdLoc(a), raiseInstr(p)]) # BUGFIX: ``>=`` and not ``>``!
     inheritLocation(d, a)
     putIntoDest(p, d, n,
                 ropecg(p.module, "$1[$2]", [rdLoc(a), rdCharLoc(b)]), a.storage)
   else:
     if optBoundsCheck in p.options:
       linefmt(p, cpsStmts, "if ((NU)($1) >= (NU)($2.Field1)){ #raiseIndexError2($1,$2.Field1-1); $3}$n",
-              [rdLoc(b), rdLoc(a), raiseInstr(p)]) # BUGFIX: ``>=`` and not ``>``!
+              [rdCharLoc(b), rdLoc(a), raiseInstr(p)]) # BUGFIX: ``>=`` and not ``>``!
     inheritLocation(d, a)
     putIntoDest(p, d, n,
                 ropecg(p.module, "$1.Field0[$2]", [rdLoc(a), rdCharLoc(b)]), a.storage)
@@ -1028,7 +1028,7 @@ proc genSeqElem(p: BProc, n, x, y: PNode, d: var TLoc) =
   if optBoundsCheck in p.options:
     linefmt(p, cpsStmts,
             "if ((NU)($1) >= (NU)$2){ #raiseIndexError2($1,$2-1); $3}$n",
-            [rdLoc(b), lenExpr(p, a), raiseInstr(p)])
+            [rdCharLoc(b), lenExpr(p, a), raiseInstr(p)])
   if d.k == locNone: d.storage = OnHeap
   if skipTypes(a.t, abstractVar).kind in {tyRef, tyPtr}:
     a.r = ropecg(p.module, "(*$1)", [a.r])
diff --git a/tests/ccgbugs/t10128.nim b/tests/ccgbugs/t10128.nim
new file mode 100644
index 000000000..48970916f
--- /dev/null
+++ b/tests/ccgbugs/t10128.nim
@@ -0,0 +1,18 @@
+# bug #10128
+let data = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
+var seq2 = newSeq[char](data.len)
+for i in 0..<data.len:
+  seq2[i] = data[i]
+
+let c = '\128'
+
+# case 1
+doAssert data[c.int] == 'y'
+doAssert seq2[c.int] == 'y'
+
+proc play(x: openArray[char]) =
+  doAssert x[c.int] == 'y'
+
+# case2
+play(data)
+play(seq2)
\ No newline at end of file