|
| 1 | +package vm |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +// Ruby 4.0 takes the numbered parameters out of Binding's local-variable API |
| 6 | +// (language/numbered_parameters_spec.rb:112-135, opened by `ruby_version_is |
| 7 | +// "4.0"`; the block at :93-110 asserts the opposite below it and closes). |
| 8 | +// |
| 9 | +// ⛔ THE REFUSAL IS ABOUT THE NAME, NOT THE BINDING. MRI tests |
| 10 | +// rb_numparam_id_p(lid) at the TOP of each of the three methods, before any |
| 11 | +// lookup happens (proc.c v4.0.9:549, 602, 649), so `_1` raises in a binding |
| 12 | +// that has no numbered parameter at all. `_1` is not a local variable that is |
| 13 | +// being hidden; it is not a local variable name. |
| 14 | +func TestNumberedParametersAreNotLocalVariables(t *testing.T) { |
| 15 | + const msg = "numbered parameter '_1' is not a local variable" |
| 16 | + for _, tc := range []struct{ name, src, want string }{ |
| 17 | + { |
| 18 | + "local_variable_get raises", |
| 19 | + `begin; -> { _1; binding.local_variable_get(:_1) }.call("a"); rescue NameError => e; e.message; end`, |
| 20 | + msg, |
| 21 | + }, |
| 22 | + { |
| 23 | + "local_variable_set raises", |
| 24 | + `begin; -> { _1; binding.local_variable_set(:_1, "b") }.call("a"); rescue NameError => e; e.message; end`, |
| 25 | + msg, |
| 26 | + }, |
| 27 | + { |
| 28 | + // RAISES rather than answering false, which is the easy one to get |
| 29 | + // wrong: "not defined" is the natural reading and the wrong one. |
| 30 | + "local_variable_defined? raises rather than answering false", |
| 31 | + `begin; -> { _1; binding.local_variable_defined?(:_1) }.call("a"); rescue NameError => e; e.message; end`, |
| 32 | + msg, |
| 33 | + }, |
| 34 | + |
| 35 | + // ⛔ Before the lookup: no numbered parameter is in scope anywhere here, |
| 36 | + // and it still raises. A guard placed after the lookup would answer |
| 37 | + // "local variable '_1' is not defined for ..." instead. |
| 38 | + { |
| 39 | + "it raises even where no numbered parameter exists", |
| 40 | + `begin; binding.local_variable_get(:_1); rescue NameError => e; e.message; end`, |
| 41 | + msg, |
| 42 | + }, |
| 43 | + { |
| 44 | + "and for defined?, where false would otherwise be the answer", |
| 45 | + `begin; binding.local_variable_defined?(:_9); rescue NameError => e; e.message; end`, |
| 46 | + "numbered parameter '_9' is not a local variable", |
| 47 | + }, |
| 48 | + |
| 49 | + // ⛔ CONTROL: `it` is a LEGAL local variable name, so the refusal must |
| 50 | + // NOT widen to rb_implicit_param_p. MRI uses the narrower |
| 51 | + // rb_numparam_id_p in all three methods for exactly this reason. |
| 52 | + { |
| 53 | + "it is an ordinary local variable name and is not refused", |
| 54 | + `it = 5; binding.local_variable_get(:it).inspect`, |
| 55 | + "5", |
| 56 | + }, |
| 57 | + { |
| 58 | + "and can be set through a binding", |
| 59 | + `it = 5; binding.local_variable_set(:it, 6); it.inspect`, |
| 60 | + "6", |
| 61 | + }, |
| 62 | + { |
| 63 | + "and answers defined?", |
| 64 | + `it = 5; binding.local_variable_defined?(:it).inspect`, |
| 65 | + "true", |
| 66 | + }, |
| 67 | + |
| 68 | + // ⛔ CONTROL: the boundary of the name shape. `_0` and `_10` are |
| 69 | + // ordinary locals in MRI, so a refusal that tested "starts with _" or |
| 70 | + // "_ then digits" would reject names the program may legitimately use. |
| 71 | + { |
| 72 | + "_0 is an ordinary local", |
| 73 | + `_0 = 1; binding.local_variable_defined?(:_0).inspect`, |
| 74 | + "true", |
| 75 | + }, |
| 76 | + { |
| 77 | + "_10 is an ordinary local", |
| 78 | + `_10 = 1; binding.local_variable_defined?(:_10).inspect`, |
| 79 | + "true", |
| 80 | + }, |
| 81 | + { |
| 82 | + "a plain underscore is an ordinary local", |
| 83 | + `_ = 1; binding.local_variable_defined?(:_).inspect`, |
| 84 | + "true", |
| 85 | + }, |
| 86 | + |
| 87 | + // ⛔ CONTROL, and the trade this change must NOT make: #parameters is |
| 88 | + // ungated (numbered_parameters_spec.rb:85) and still names `_1`. Only |
| 89 | + // the LOCAL TABLE loses the name, never the parameter list -- which is |
| 90 | + // why the compiler blanks b.locals and leaves b.params alone. |
| 91 | + { |
| 92 | + "a proc still names its numbered parameters", |
| 93 | + `proc { _1 }.parameters.inspect`, |
| 94 | + "[[:opt, :_1]]", |
| 95 | + }, |
| 96 | + { |
| 97 | + "and a lambda does too", |
| 98 | + `-> { _2 }.parameters.inspect`, |
| 99 | + "[[:req, :_1], [:req, :_2]]", |
| 100 | + }, |
| 101 | + |
| 102 | + // ⛔ CONTROL: they did not vanish, they moved. 4.0 gives them an API of |
| 103 | + // their own, and taking them out of one must not take them out of both. |
| 104 | + { |
| 105 | + "implicit_parameters still sees them", |
| 106 | + `-> { _1; binding.implicit_parameters }.call("a").inspect`, |
| 107 | + "[:_1]", |
| 108 | + }, |
| 109 | + { |
| 110 | + "and implicit_parameter_get still reads them", |
| 111 | + `-> { _1; binding.implicit_parameter_get(:_1) }.call("a").inspect`, |
| 112 | + `"a"`, |
| 113 | + }, |
| 114 | + } { |
| 115 | + t.Run(tc.name, func(t *testing.T) { |
| 116 | + if got := evalString(t, tc.src); got != tc.want { |
| 117 | + t.Errorf("%s\n got %q\nwant %q", tc.src, got, tc.want) |
| 118 | + } |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// #local_variables is tested through runSrc rather than evalString because |
| 124 | +// evalString wraps the source in `__v = begin … end`, and that assignment makes |
| 125 | +// `__v` a local of the ENCLOSING scope — which a binding taken inside a block |
| 126 | +// lists as well. The first run of the cases above read `[:__v]` where it wanted |
| 127 | +// `[]`: the numbered parameter was already gone and the harness was supplying |
| 128 | +// the remaining entry. An assertion written against that output would have |
| 129 | +// pinned my wrapper's name as Ruby behaviour. |
| 130 | +func TestNumberedParametersAreNotInLocalVariables(t *testing.T) { |
| 131 | + for _, tc := range []struct{ name, src, want string }{ |
| 132 | + // The two examples numbered_parameters_spec.rb:113-116 opens at 4.0. |
| 133 | + { |
| 134 | + "one numbered parameter", |
| 135 | + `p(-> { _1; binding.local_variables }.call("a"))`, |
| 136 | + "[]", |
| 137 | + }, |
| 138 | + { |
| 139 | + "several", |
| 140 | + `p(-> { _2; binding.local_variables }.call("a", "b"))`, |
| 141 | + "[]", |
| 142 | + }, |
| 143 | + // ⛔ CONTROL: the list is not simply empty for every block. A real local |
| 144 | + // still appears, so the two cases above measure the hiding and not a |
| 145 | + // #local_variables that stopped working. |
| 146 | + { |
| 147 | + "a real local in the same shape still appears", |
| 148 | + `p(-> { x = 1; binding.local_variables }.call)`, |
| 149 | + "[:x]", |
| 150 | + }, |
| 151 | + // ⛔ CONTROL: `it` is blanked from the local table the same way, and |
| 152 | + // this is the 4.0 behaviour rbgo already had before the version moved. |
| 153 | + { |
| 154 | + "it is not listed either", |
| 155 | + `p(-> { it; binding.local_variables }.call("a"))`, |
| 156 | + "[]", |
| 157 | + }, |
| 158 | + } { |
| 159 | + t.Run(tc.name, func(t *testing.T) { |
| 160 | + if got := runSrc(t, tc.src); got != tc.want { |
| 161 | + t.Errorf("%s\n got %q\nwant %q", tc.src, got, tc.want) |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
0 commit comments