[ruby-core:111822] [Ruby master Bug#19345] Class variable access from top-level inappropriate error

Issue #19345 has been reported by luke-gru (Luke Gruber). ---------------------------------------- Bug #19345: Class variable access from top-level inappropriate error https://bugs.ruby-lang.org/issues/19345 * Author: luke-gru (Luke Gruber) * Status: Open * Priority: Normal * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I was looking at doing something like this: ```ruby set_cvar = EnvUtil.labeled_class("SetCVar") do def self.set(val) @@a = val end end set_cvar.set(1) # fill write cache set_cvar.freeze set_cvar.set(2) # hit write cache, but should check frozen status ``` while working on a ruby issue (https://github.com/ruby/ruby/pull/7124) and I ran into an issue. This code above gives the error "class variable access from toplevel (RuntimeError)" Is this a bug or how it's supposed to work? The error is being raised in function `vm_get_cvar_base`. -- https://bugs.ruby-lang.org/

Issue #19345 has been updated by luke-gru (Luke Gruber). I just looked into this some more and if we want to allow the above code to work then we need to change the way the class variable cache works right now. Right now because the lookup of the class where we set the @@variables is static, it allows the class variable cache to cache the class. If we wanted it to be dynamic we would lose this cache. ---------------------------------------- Bug #19345: Class variable access from top-level inappropriate error https://bugs.ruby-lang.org/issues/19345#change-101231 * Author: luke-gru (Luke Gruber) * Status: Open * Priority: Normal * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I was looking at doing something like this: ```ruby set_cvar = EnvUtil.labeled_class("SetCVar") do def self.set(val) @@a = val end end set_cvar.set(1) # fill write cache set_cvar.freeze set_cvar.set(2) # hit write cache, but should check frozen status ``` while working on a ruby issue (https://github.com/ruby/ruby/pull/7124) and I ran into an issue. This code above gives the error "class variable access from toplevel (RuntimeError)" Is this a bug or how it's supposed to work? The error is being raised in function `vm_get_cvar_base`. -- https://bugs.ruby-lang.org/

Issue #19345 has been updated by Eregon (Benoit Daloze). Status changed from Open to Closed Class variables work similar to constants in terms of lookup, they only use the scopes opened by `class Name/module Name/class << expr`. `module_{eval,exec}/class_{eval,exec}` do not change that scope. So this is expected, `EnvUtil.labeled_class` doesn't change the scope for constants & class variables. ---------------------------------------- Bug #19345: Class variable access from top-level inappropriate error https://bugs.ruby-lang.org/issues/19345#change-101241 * Author: luke-gru (Luke Gruber) * Status: Closed * Priority: Normal * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I was looking at doing something like this: ```ruby set_cvar = EnvUtil.labeled_class("SetCVar") do def self.set(val) @@a = val end end set_cvar.set(1) # fill write cache set_cvar.freeze set_cvar.set(2) # hit write cache, but should check frozen status ``` while working on a ruby issue (https://github.com/ruby/ruby/pull/7124) and I ran into an issue. This code above gives the error "class variable access from toplevel (RuntimeError)" Is this a bug or how it's supposed to work? The error is being raised in function `vm_get_cvar_base`. -- https://bugs.ruby-lang.org/

Issue #19345 has been updated by luke-gru (Luke Gruber). Yes, thanks for explanation. class_eval with string also works to change scope. ---------------------------------------- Bug #19345: Class variable access from top-level inappropriate error https://bugs.ruby-lang.org/issues/19345#change-101249 * Author: luke-gru (Luke Gruber) * Status: Closed * Priority: Normal * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I was looking at doing something like this: ```ruby set_cvar = EnvUtil.labeled_class("SetCVar") do def self.set(val) @@a = val end end set_cvar.set(1) # fill write cache set_cvar.freeze set_cvar.set(2) # hit write cache, but should check frozen status ``` while working on a ruby issue (https://github.com/ruby/ruby/pull/7124) and I ran into an issue. This code above gives the error "class variable access from toplevel (RuntimeError)" Is this a bug or how it's supposed to work? The error is being raised in function `vm_get_cvar_base`. -- https://bugs.ruby-lang.org/
participants (2)
-
Eregon (Benoit Daloze)
-
luke-gru (Luke Gruber)