Debian Codegraph @

docs/local_variable_depth.md

raw symbols SCIP
1 # Local variable depth
2
3 One feature of Prism is that it resolves local variables as it parses. It's necessary to do this because of ambiguities in the grammar. For example, consider the following code:
4
5 ```ruby
6 foo / bar#/
7 ```
8
9 If `foo` is a local variable, this is a call to `/` with `bar` as an argument, followed by a comment. If it's not a local variable, this is a method call to `foo` with a regular expression argument.
10
11 "Depth" refers to the number of visible scopes that Prism has to go up to find the declaration of a local variable.
12 Note that this follows the same scoping rules as Ruby, so a local variable is only visible in the scope it is declared in and in blocks nested in that scope.
13 The rules for calculating the depth are very important to understand because they may differ from individual Ruby implementations since they are not specified by the language.
14
15 Prism uses the minimum number of scopes, i.e., it only creates scopes when necessary semantically, in other words when there must be distinct scopes (which can be observed through `binding.local_variables`).
16 That are no "transparent/invisible" scopes in Prism.
17 Some Ruby implementations use those for some language constructs and need to adjust by maintaining a depth offset.
18
19 Below are the places where a local variable can be written/targeted, along with how the depth is calculated at that point.
20
21 ## General
22
23 In the course of general Ruby code when reading a local variable, the depth is equal to the number of scopes to go up to find the declaration of that variable. For example:
24
25 ```ruby
26 foo = 1
27 bar = 2
28 baz = 3
29
30 foo # depth 0
31 tap { bar } # depth 1
32 tap { tap { baz } } # depth 2
33 ```
34
35 This also includes writing to a local variable, which could be writing to a local variable that is already declared. For example:
36
37 ```ruby
38 foo = 1
39 bar = 2
40
41 foo = 3 # depth 0
42 tap { bar = 4 } # depth 1
43 ```
44
45 This includes multiple assignment, where the same principle applies. For example:
46
47 ```ruby
48 foo = 1
49 bar = 2
50
51 foo, bar = 3, 4 # depth 0
52 tap { foo, bar = 5, 6 } # depth 1
53 ```
54
55 ## `for` loops
56
57 `for` loops in Ruby break down to calls to `.each` with a block.
58 However in that case local variable reads and writes within the block will be in the same scope as the scope surrounding the `for` and not in a deeper/separate scope (surprising, but this is Ruby semantics).
59 For example:
60
61 ```ruby
62 foo = 1
63
64 for e in baz
65 foo # depth 0
66 bar = 2 # depth 0
67 end
68
69 p bar # depth 0, prints 2
70 ```
71
72 The local variable(s) used for the index of the `for` are also at the same depth (as variables inside and outside the `for`):
73
74 ```ruby
75 for e in [1, 2] # depth 0
76 e # depth 0
77 end
78
79 p e # depth 0, prints 2
80 ```
81
82 ## Pattern matching captures
83
84 You can target a local variable in a pattern matching expression using capture syntax. Using this syntax, you can target local variables in the current scope or in visible parent scopes. For example:
85
86 ```ruby
87 42 => bar # depth 0
88 ```
89
90 The example above writes to a local variable in the current scope. If the variable is already declared in a higher visible scope, it will be written to that scope instead. For example:
91
92 ```ruby
93 foo = 1
94 tap { 42 => foo } # depth 1
95 ```
96
97 ## Named capture groups
98
99 You can target local variables through named capture groups in regular expressions if they are used on the left-hand side of a `=~` operator. For example:
100
101 ```ruby
102 /(?<foo>\d+)/ =~ "42" # depth 0
103 ```
104
105 This will write to a `foo` local variable. If the variable is already declared in a higher visible scope, it will be written to that scope instead. For example:
106
107 ```ruby
108 foo = 1
109 tap { /(?<foo>\d+)/ =~ "42" } # depth 1
110 ```
111
112 ## "interpolated once" regular expressions
113
114 Regular expressions that interpolate local variables (unrelated to capture group local variables) and have the `o` flag will only interpolate the local variables once for the runtime of the program.
115 In CRuby, this is implemented by compiling the regular expression within a nested instruction sequence, which means CRuby thinks the depth is one more than prism does. For example:
116
117 ```
118 $ ruby --dump=insns -e 'foo = 1; /#{foo}/o'
119 == disasm: #<ISeq:<main>@-e:1 (1,0)-(1,18)> (catch: false)
120 local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
121 [ 1] foo@0
122 0000 putobject_INT2FIX_1_ ( 1)[Li]
123 0001 setlocal_WC_0 foo@0
124 0003 once block in <main>, <is:0>
125 0006 leave
126
127 == disasm: #<ISeq:block in <main>@-e:1 (1,9)-(1,18)> (catch: false)
128 0000 putobject "" ( 1)
129 0002 getlocal_WC_1 foo@0
130 0004 dup
131 0005 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
132 0007 anytostring
133 0008 toregexp 0, 2
134 0011 leave
135 ```
136
137 In this case CRuby fetches the local variable with `getlocal_WC_1` as the second instruction to the "once" instruction sequence. When compiling CRuby, prism therefore will adjust the depth to account for this difference.
138
139 ## `rescue` clauses
140
141 In CRuby, `rescue` clauses are implemented as their own instruction sequence, and therefore CRuby thinks the depth is one more than prism does. For example:
142
143 ```
144 $ ruby --dump=insns -e 'begin; foo = 1; rescue; foo; end'
145 == disasm: #<ISeq:<main>@-e:1 (1,0)-(1,32)> (catch: true)
146 == catch table
147 | catch type: rescue st: 0000 ed: 0004 sp: 0000 cont: 0005
148 | == disasm: #<ISeq:rescue in <main>@-e:1 (1,16)-(1,28)> (catch: true)
149 | local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
150 | [ 1] $!@0
151 | 0000 getlocal_WC_0 $!@0 ( 1)
152 | 0002 putobject StandardError
153 | 0004 checkmatch 3
154 | 0006 branchunless 11
155 | 0008 getlocal_WC_1 foo@0[Li]
156 | 0010 leave
157 | 0011 getlocal_WC_0 $!@0
158 | 0013 throw 0
159 | catch type: retry st: 0004 ed: 0005 sp: 0000 cont: 0000
160 |------------------------------------------------------------------------
161 local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
162 [ 1] foo@0
163 0000 putobject_INT2FIX_1_ ( 1)[Li]
164 0001 dup
165 0002 setlocal_WC_0 foo@0
166 0004 nop
167 0005 leave
168 ```
169
170 In the catch table, CRuby is reading the `foo` local variable using `getlocal_WC_1` as the fifth instruction to the "rescue" instruction sequence. When compiling CRuby, prism therefore will adjust the depth to account for this difference.
171
172 Note that this includes the error reference, which can target local variables, as in:
173
174 ```
175 $ ruby --dump=insns -e 'foo = 1; begin; rescue => foo; end'
176 == disasm: #<ISeq:<main>@-e:1 (1,0)-(1,34)> (catch: true)
177 == catch table
178 | catch type: rescue st: 0003 ed: 0004 sp: 0000 cont: 0005
179 | == disasm: #<ISeq:rescue in <main>@-e:1 (1,16)-(1,30)> (catch: true)
180 | local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
181 | [ 1] $!@0
182 | 0000 getlocal_WC_0 $!@0 ( 1)
183 | 0002 putobject StandardError
184 | 0004 checkmatch 3
185 | 0006 branchunless 14
186 | 0008 getlocal_WC_0 $!@0
187 | 0010 setlocal_WC_1 foo@0
188 | 0012 putnil
189 | 0013 leave
190 | 0014 getlocal_WC_0 $!@0
191 | 0016 throw 0
192 | catch type: retry st: 0004 ed: 0005 sp: 0000 cont: 0003
193 |------------------------------------------------------------------------
194 local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
195 [ 1] foo@0
196 0000 putobject_INT2FIX_1_ ( 1)[Li]
197 0001 setlocal_WC_0 foo@0
198 0003 putnil
199 0004 nop
200 0005 leave
201 ```
202
203 Note that CRuby is writing to the `foo` local variable using the `setlocal_WC_1` instruction as the sixth instruction to the "rescue" instruction sequence. When compiling CRuby, prism therefore will adjust the depth to account for this difference.
204
205 ## Post execution blocks
206
207 The `END {}` syntax allows executing code when the program exits. In CRuby, this is implemented as two nested instruction sequences. CRuby therefore thinks the depth is two more than prism does. For example:
208
209 ```
210 $ ruby --dump=insns -e 'foo = 1; END { foo }'
211 == disasm: #<ISeq:<main>@-e:1 (1,0)-(1,20)> (catch: false)
212 local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
213 [ 1] foo@0
214 0000 putobject_INT2FIX_1_ ( 1)[Li]
215 0001 setlocal_WC_0 foo@0
216 0003 once block in <main>, <is:0>
217 0006 leave
218
219 == disasm: #<ISeq:block in <main>@-e:0 (0,0)-(-1,-1)> (catch: false)
220 0000 putspecialobject 1 ( 1)
221 0002 send <calldata!mid:core#set_postexe, argc:0, FCALL>, block in <main>
222 0005 leave
223
224 == disasm: #<ISeq:block in <main>@-e:1 (1,9)-(1,20)> (catch: false)
225 0000 getlocal foo@0, 2 ( 1)[LiBc]
226 0003 leave [Br]
227 ```
228
229 In the instruction sequence corresponding to the code that gets executed inside the `END` block, CRuby is reading the `foo` local variable using `getlocal` as the second instruction to the `"block in <main>"` instruction sequence. When compiling CRuby, prism therefore will adjust the depth to account for this difference.