Skip to content

cstring implicit conversion to pointer broken on JS #14097

@metagn

Description

@metagn

For some reason cstring can implicitly convert to pointer but not ptr char, and that's true for all backends. Converting to pointer in JS though is broken because of the following:

Example

let x: pointer = cstring"abcd"
let y = x
discard y

Current Output

Generated codegen:

var x_622003 = "abcd";
var y_622004 = [x_622003[0], x_622003[1]];

Nim treats the cstring like it's a pointer already and tries to copy instead of converting. This will break, ["a", "b"] is not a valid pointer.

Expected Output

meaning expected codegen:

var x_622003 = "abcd";
var y_622004 = [x_622003, 0];

Additional Information

  • ["abcd", 0] being a valid pointer is another question, since JS cstrings are immutable, but this already happens when used like this:
var x = cstring"abcd"
let y = addr x[0]
discard y

y in this case is ["abcd", 0]. A good solution to this would be to make cstrings in JS unsafe addresses like let variables, but the same would have to apply to the implicit pointer conversion.

$ nim -v
Nim Compiler Version 1.2.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions