Skip to content

Commit c539857

Browse files
authored
Merge pull request #2432 from eregon/thread-safe-location
Make location methods thread-safe
2 parents 20b0602 + ff762dc commit c539857

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

lib/prism/parse_result.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,9 @@ def deconstruct_keys(keys)
477477

478478
# A Location object representing the location of this token in the source.
479479
def location
480-
return @location if @location.is_a?(Location)
481-
@location = Location.new(source, @location >> 32, @location & 0xFFFFFFFF)
480+
location = @location
481+
return location if location.is_a?(Location)
482+
@location = Location.new(source, location >> 32, location & 0xFFFFFFFF)
482483
end
483484

484485
# Implement the pretty print interface for Token.

templates/lib/prism/node.rb.erb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ module Prism
99
# A Location instance that represents the location of this node in the
1010
# source.
1111
def location
12-
return @location if @location.is_a?(Location)
13-
@location = Location.new(source, @location >> 32, @location & 0xFFFFFFFF)
12+
location = @location
13+
return location if location.is_a?(Location)
14+
@location = Location.new(source, location >> 32, location & 0xFFFFFFFF)
1415
end
1516

1617
def newline? # :nodoc:
@@ -196,18 +197,20 @@ module Prism
196197
<%- case field -%>
197198
<%- when Prism::LocationField -%>
198199
def <%= field.name %>
199-
return @<%= field.name %> if @<%= field.name %>.is_a?(Location)
200-
@<%= field.name %> = Location.new(source, @<%= field.name %> >> 32, @<%= field.name %> & 0xFFFFFFFF)
200+
location = @<%= field.name %>
201+
return location if location.is_a?(Location)
202+
@<%= field.name %> = Location.new(source, location >> 32, location & 0xFFFFFFFF)
201203
end
202204
<%- when Prism::OptionalLocationField -%>
203205
def <%= field.name %>
204-
case @<%= field.name %>
206+
location = @<%= field.name %>
207+
case location
205208
when nil
206209
nil
207210
when Location
208-
@<%= field.name %>
211+
location
209212
else
210-
@<%= field.name %> = Location.new(source, @<%= field.name %> >> 32, @<%= field.name %> & 0xFFFFFFFF)
213+
@<%= field.name %> = Location.new(source, location >> 32, location & 0xFFFFFFFF)
211214
end
212215
end
213216
<%- else -%>

0 commit comments

Comments
 (0)