From: "Eregon (Benoit Daloze)" Date: 2022-01-06T11:43:01+00:00 Subject: [ruby-core:106983] [Ruby master Feature#18461] closures are capturing unused variables Issue #18461 has been updated by Eregon (Benoit Daloze). I think Ruby JIT implementers are already aware of this, but these are the current semantics of Ruby. I think JRuby does some optimization like this (only keeps the outside scope if there is `binding/eval` inside the block, but JRuby does not account for aliases) That optimization is however unsound and incompatible, although maybe in practice not that big a deal. ``` $ juby -ve 'module Kernel; alias b binding; end; def foo; a = 1; -> { b }; end; p foo.call.local_variable_get :a' jruby 9.3.2.0 (2.6.8) 2021-12-01 0b8223f905 OpenJDK 64-Bit Server VM 11.0.10+9 on 11.0.10+9 +jit [linux-x86_64] -e:1: warning: assigned but unused variable - a -e:2: warning: Kernel#binding accesses caller method's state and should not be aliased NameError: local variable `a' not defined for # ``` As long as Proc#binding exists and it's possible to alias `binding`, I think it's problematic to change semantics for compatibility and soundness. Proc#binding's purpose AFAIK is to access variables in the outer scope (it cannot variables defined inside the block). But, maybe we could deprecate Proc#binding and remove it, I think that would be a good start. What do committers and people think about that? ---------------------------------------- Feature #18461: closures are capturing unused variables https://bugs.ruby-lang.org/issues/18461#change-95813 * Author: bughit (bug hit) * Status: Open * Priority: Normal ---------------------------------------- ```rb def foo a = 1 ->{} end p foo.binding.local_variables # [:a] ``` Shouldn't `a` be optimized away? Like v8 does (https://bugs.chromium.org/p/v8/issues/detail?id=3491) -- https://bugs.ruby-lang.org/ Unsubscribe: