-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting subscript index of cstring is allowed and generates bad code #14157
Comments
Just FYI - this is not specific to the JS backend :) |
bad code gen at both C and JS banckend. I think we should disable it in code gen phase |
For c, see another issue (search sigbus), it leads to bad code only in specific cases so disabling it for c in all cases is not good. It's a complicated issue, but theres a way to solve it. |
Could you give me an example?
At least it fails in the simple example above. Edited: |
Anyway, we should document this situation. Whether this is already documented? |
Let's suppose that c part won't be fixed like #8463 said or we could document c part. Because c part is only specific to This works in c backend not in JS backend. var x = "123456"
var s: cstring = x
s[0] = 'u' I will label this issue as |
for js it should be statically disallowed, that's simple. for c, there are 2 solutions:
|
There should be no |
I try to undefine proc `[]=`*[I: Ordinal;T: not cstring,S](a: T; i: I;
x: sink S) {.noSideEffect, magic: "ArrPut".} Overload import system except `[]=`
proc `[]=`(x: cstring, index: int, c: string) =
echo "Hui"
var wind = cstring"abcd"
wind[0] = "1234" |
that'd be a large breaking change: proc main()=
var a = "foo"
var b: cstring = a # or getting `b` from C API that returns a `char*` (not a `const char*`)
b[0] = 'F'
echo a
main()
to mitigate (and keep this 0 cost) but still. noteeven if we add this proc `[]=`(a: cstring, b: index) =
when compileOption("cstringChecks):
doAssert b < a.len
#rest of code ditto with Whereas this wouldn't be possible with So |
* fix nim-lang#14157 * Update compiler/jsgen.nim * add changelog * Update compiler/jsgen.nim * Update tests/js/tmodify_cstring.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
…ification is not allowed (nim-lang#15878) * follow nim-lang#8463 nim-lang#14157 and document cstring literals * Update doc/manual.rst Co-authored-by: Juan Carlos <juancarlospaco@gmail.com> Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* fix nim-lang#14157 * Update compiler/jsgen.nim * add changelog * Update compiler/jsgen.nim * Update tests/js/tmodify_cstring.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
…ification is not allowed (nim-lang#15878) * follow nim-lang#8463 nim-lang#14157 and document cstring literals * Update doc/manual.rst Co-authored-by: Juan Carlos <juancarlospaco@gmail.com> Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
@xflywind IMO #15877 is proc main()=
var a = "abc".cstring
template fn() =
a[0] = 'x'
# fn() # ok: correctly gives: Error: cstring doesn't support `[]=` operator
doAssert not compiles(fn()) # bug: assert fails
main()
IMO the right fix should happen "earlier", we need to look deeper into #15877 (comment) see also: #15911 |
It seems to compile static:
var a = "abc".cstring
template fn() =
a[0] = 'x'
fn() |
I don't know if this is solvable or not TBH. proc main1() =
var a = "abc"
var b = a.cstring
b[0] = 'x'
template fn() = b[0] = 'x'
doAssert compiles(fn())
proc main2()=
var a = "abc"
var b = a.cstring
# b[0] = 'x' # correctly gives: Error: cstring doesn't support `[]=` operator
doAssert not compiles(fn()) # should succeed, but right now fails
when compiles(fn()):
echo "foo" # should this print or not?
static: main1()
main2() |
My PR just fix the JS code gen. |
I'm thinking of the following general approach to solve this kind of problem (refs timotheecour#564): when defined(js):
proc `[]=`*(a: cstring, b: int, c: char) {.nimvm.} = ...
else:
proc `[]=`*(a: cstring, b: int, c: char) = ... in this example, you'd have this behavior:
there are other use cases of |
* fix nim-lang#14157 * Update compiler/jsgen.nim * add changelog * Update compiler/jsgen.nim * Update tests/js/tmodify_cstring.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
…ification is not allowed (nim-lang#15878) * follow nim-lang#8463 nim-lang#14157 and document cstring literals * Update doc/manual.rst Co-authored-by: Juan Carlos <juancarlospaco@gmail.com> Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* fix nim-lang#14157 * Update compiler/jsgen.nim * add changelog * Update compiler/jsgen.nim * Update tests/js/tmodify_cstring.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
…ification is not allowed (nim-lang#15878) * follow nim-lang#8463 nim-lang#14157 and document cstring literals * Update doc/manual.rst Co-authored-by: Juan Carlos <juancarlospaco@gmail.com> Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Low priority, disallowing this makes more sense than making it work.
Example
Current Output
Expected Output
compile error
Possible Solution
Nim/lib/system.nim
Lines 437 to 438 in dc3919b
The text was updated successfully, but these errors were encountered: