From 23b0b10886e50c089200450fffcbcdb61bf66296 Mon Sep 17 00:00:00 2001 From: flywind <43030857+xflywind@users.noreply.github.com> Date: Thu, 12 Nov 2020 16:20:10 +0800 Subject: [PATCH] fix #14157 (#15877) * fix #14157 * Update compiler/jsgen.nim * add changelog * Update compiler/jsgen.nim * Update tests/js/tmodify_cstring.nim Co-authored-by: Andreas Rumpf (cherry picked from commit 1f9bf43100f7236d8ccbcaa14c43bc18f7e6e5d8) --- changelog.md | 2 ++ compiler/jsgen.nim | 4 ++++ tests/js/tmodify_cstring.nim | 6 ++++++ 3 files changed, 12 insertions(+) create mode 100644 tests/js/tmodify_cstring.nim diff --git a/changelog.md b/changelog.md index 4d01737a6d..a575b9a4c6 100644 --- a/changelog.md +++ b/changelog.md @@ -18,6 +18,8 @@ ## Language changes +- The `cstring` doesn't support `[]=` operator in JS backend. + ## Compiler changes diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 708c1cad7e..050b50f768 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -1049,6 +1049,10 @@ proc genAsgnAux(p: PProc, x, y: PNode, noCopyNeeded: bool) = var a, b: TCompRes var xtyp = mapType(p, x.typ) + # disable `[]=` for cstring + if x.kind == nkBracketExpr and x.len >= 2 and x[0].typ.skipTypes(abstractInst).kind == tyCString: + localError(p.config, x.info, "cstring doesn't support `[]=` operator") + gen(p, x, a) genLineDir(p, y) gen(p, y, b) diff --git a/tests/js/tmodify_cstring.nim b/tests/js/tmodify_cstring.nim new file mode 100644 index 0000000000..82f8ccb232 --- /dev/null +++ b/tests/js/tmodify_cstring.nim @@ -0,0 +1,6 @@ +discard """ + errormsg: "cstring doesn't support `[]=` operator" +""" + +var x = cstring"abcd" +x[0] = 'x'