Bug #22632
openparse.y magic encoding comments accept NUL bytes
Updated by nobu (Nobuyoshi Nakada) 2 days ago
This isn't limited to “magic comments”; NUL bytes within comments are generally ignored.
In this case, it's simply a delimiter.
I once tried to treat NUL bytes within comments as EOF, just as they are outside comments, but this attempt was abandoned because this behavior was being abused in RubyGems stub lines.
Updated by nobu (Nobuyoshi Nakada) 1 day ago
Now I think a SyntaxError would be better for such a line, while both of parse.y and prism accept it.
$ for x in parse.y prism; do echo $'# encoding: binary\0ignored' | ./ruby -vc --parser=$x -; done
ruby 4.1.0dev (2026-11-25T06:56:36Z master f63048e942) [arm64-darwin27]
Syntax OK
ruby 4.1.0dev (2026-11-25T06:56:36Z master f63048e942) +PRISM [arm64-darwin27]
Syntax OK
Shouldn't we make it so that the both raise a SyntaxError together?
Updated by nobu (Nobuyoshi Nakada) about 23 hours ago
Or do not consider such a line as a magic comment at all?
Updated by kddnewton (Kevin Newton) about 18 hours ago
My concern is just about the value of the magic comment. Basically if # encoding: binary\0ignored should result in Encoding.find("binary") or Encoding.find("binary\0ignored") with the second one being an ArgumentError. To me it seems like we should make it an ArgumentError because it can't find the encoding.