Skip to content
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

%Q String literal with \0 delimiter is non-interpolatable #2646

Closed
andrykonchin opened this issue Mar 29, 2024 · 1 comment · Fixed by #2647
Closed

%Q String literal with \0 delimiter is non-interpolatable #2646

andrykonchin opened this issue Mar 29, 2024 · 1 comment · Fixed by #2647
Labels
bug Something isn't working

Comments

@andrykonchin
Copy link
Member

This trick is used in the haml gem here. The issue was reported in sinatra/sinatra#2007.

How to reproduce:

  1. Generate a source file with code snippet:
source_code = "%Q\0" + 'Hello #{a.strip}!' + "\0"
File.write("test.rb", source_code)
  1. Run CRuby:
$ ruby test.rb
sinatra-test.rb:1:in `<main>': undefined local variable or method `a' for main:Object (NameError)

%QHello #{a.strip}!
           ^

So the string literal is interpolatable

  1. Parse with Prism:
$ bin/parse sinatra-test.rb
@ ProgramNode (location: (1,0)-(1,21))
├── locals: []
└── statements:
    @ StatementsNode (location: (1,0)-(1,21))
    └── body: (length: 1)
        └── @ StringNode (location: (1,0)-(1,21))
            ├── flags: ∅
            ├── opening_loc: (1,0)-(1,3) = "%Q\u0000"
            ├── content_loc: (1,3)-(1,20) = "Hello \#{a.strip}!"
            ├── closing_loc: (1,20)-(1,21) = "\u0000"
            └── unescaped: "Hello \#{a.strip}!"

The string literal is not interpolatable

@kddnewton
Copy link
Collaborator

Weird! I'll get it fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants