Skip to content

Macro.to_string and Code.string_to_quoted adding more \ escape characters #10864

@polvalente

Description

@polvalente

Environment

  • Elixir & Erlang/OTP versions (elixir --version): Elixir 1.11.2 (compiled with Erlang/OTP 23), Erlang 23.2.5
  • Operating system: Ubuntu 20.04

Current behavior

It seems that Macro.to_string is applying some escapes where it shouldn't. Maybe this is intended behavior, but I wasn't expecting it.

iex> "~r/\\a/" |> Code.string_to_quoted!() |> Macro.to_string             
"~r/\\\\a/"
iex> "~r/\\a/" |> Code.string_to_quoted!() |> Macro.to_string == "~r/\\a/"
false

From what I gathered, it seems to be something specific to explicit sigils:

iex> "~s/\\a/" |> Code.string_to_quoted!() |> Macro.to_string
"~s/\\\\a/" # This has two \\ added and isn't equivalent to the original input
iex> ~s/"\\a"/ |> Code.string_to_quoted!() |> Macro.to_string   
"\"\\a\"" # This yields an equivalent string to the original input

Expected behavior

iex> "~s/\\a/" |> Code.string_to_quoted!() |> Macro.to_string
"~s/\\a/"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions