Project

General

Profile

Actions

Bug #21026

open

`def __FILE__.a; end` should be a syntax error

Added by Earlopain (Earlopain _) 5 days ago. Updated 4 days ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
[ruby-core:120611]

Description

Constants like __FILE__, __LINE__ and __ENCODING__ are literals and as such you shouldn't be able to defined singleton methods on them.

It already doesn't seem to actually do anything:

def __FILE__.a
end
__FILE__.a #=>  undefined method 'a' for an instance of String (NoMethodError)

Wrapping it in brackets correctly reports a syntax error:

code.rb:1: syntax error found (SyntaxError)
> 1 | def (__FILE__).a
    |      ^~~~~~~~ cannot define singleton method for literals
  2 | end

The behavior is consistent between prism and parse.y

__ENCODING__ is frozen and so will result in a runtime error. Same for __LINE__, and also __FILE__ with frozen string literals.

Actions

Also available in: Atom PDF

Like1
Like0Like0Like0