11#include "rbs_extension.h"
2+ #include "rbs/util/rbs_constant_pool.h"
23
34#define RBS_LOC_REQUIRED_P (loc , i ) ((loc)->children->required_p & (1 << (i)))
45#define RBS_LOC_OPTIONAL_P (loc , i ) (!RBS_LOC_REQUIRED_P((loc), (i)))
@@ -168,14 +169,31 @@ static VALUE location_end_pos(VALUE self) {
168169 return INT2FIX (loc -> rg .end );
169170}
170171
172+ static rbs_constant_id_t rbs_constant_id_from_ruby_symbol (VALUE symbol ) {
173+ const char * name = rb_id2name (SYM2ID (symbol ));
174+
175+ if (name == NULL ) {
176+ // This happens if `symbol` was a Dynamic symbol (as opposed to a "static symbol" which has
177+ // been interned to get a an ID).
178+ return RBS_CONSTANT_ID_UNSET ;
179+ }
180+
181+ return rbs_constant_pool_find (RBS_GLOBAL_CONSTANT_POOL , (const uint8_t * ) name , strlen (name ));
182+ }
183+
184+ static VALUE rbs_constant_to_ruby_symbol (rbs_constant_t * constant ) {
185+ // Casts back the symbol that was looked up by `pm_constant_pool_id_to_constant()`.
186+ return (VALUE ) constant ;
187+ }
188+
171189static VALUE location_add_required_child (VALUE self , VALUE name , VALUE start , VALUE end ) {
172190 rbs_loc * loc = rbs_check_location (self );
173191
174192 range rg ;
175193 rg .start = rbs_loc_position (FIX2INT (start ));
176194 rg .end = rbs_loc_position (FIX2INT (end ));
177195
178- rbs_loc_add_required_child (loc , SYM2ID (name ), rg );
196+ rbs_loc_add_required_child (loc , rbs_constant_id_from_ruby_symbol (name ), rg );
179197
180198 return Qnil ;
181199}
@@ -187,15 +205,15 @@ static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VA
187205 rg .start = rbs_loc_position (FIX2INT (start ));
188206 rg .end = rbs_loc_position (FIX2INT (end ));
189207
190- rbs_loc_add_optional_child (loc , SYM2ID (name ), rg );
208+ rbs_loc_add_optional_child (loc , rbs_constant_id_from_ruby_symbol (name ), rg );
191209
192210 return Qnil ;
193211}
194212
195213static VALUE location_add_optional_no_child (VALUE self , VALUE name ) {
196214 rbs_loc * loc = rbs_check_location (self );
197215
198- rbs_loc_add_optional_child (loc , SYM2ID (name ), NULL_RANGE );
216+ rbs_loc_add_optional_child (loc , rbs_constant_id_from_ruby_symbol (name ), NULL_RANGE );
199217
200218 return Qnil ;
201219}
@@ -221,9 +239,9 @@ static VALUE rbs_new_location_from_loc_range(VALUE buffer, rbs_loc_range rg) {
221239static VALUE location_aref (VALUE self , VALUE name ) {
222240 rbs_loc * loc = rbs_check_location (self );
223241
224- ID id = SYM2ID (name );
242+ rbs_constant_id_t id = rbs_constant_id_from_ruby_symbol (name );
225243
226- if (loc -> children != NULL ) {
244+ if (loc -> children != NULL && id != RBS_CONSTANT_ID_UNSET ) {
227245 for (unsigned short i = 0 ; i < loc -> children -> len ; i ++ ) {
228246 if (loc -> children -> entries [i ].name == id ) {
229247 rbs_loc_range result = loc -> children -> entries [i ].rg ;
@@ -252,8 +270,8 @@ static VALUE location_optional_keys(VALUE self) {
252270
253271 for (unsigned short i = 0 ; i < children -> len ; i ++ ) {
254272 if (RBS_LOC_OPTIONAL_P (loc , i )) {
255- rb_ary_push ( keys , ID2SYM ( children -> entries [i ].name ) );
256-
273+ rbs_constant_t * key = rbs_constant_pool_id_to_constant ( RBS_GLOBAL_CONSTANT_POOL , children -> entries [i ].name );
274+ rb_ary_push ( keys , rbs_constant_to_ruby_symbol ( key ));
257275 }
258276 }
259277
@@ -271,7 +289,8 @@ static VALUE location_required_keys(VALUE self) {
271289
272290 for (unsigned short i = 0 ; i < children -> len ; i ++ ) {
273291 if (RBS_LOC_REQUIRED_P (loc , i )) {
274- rb_ary_push (keys , ID2SYM (children -> entries [i ].name ));
292+ rbs_constant_t * key = rbs_constant_pool_id_to_constant (RBS_GLOBAL_CONSTANT_POOL , children -> entries [i ].name );
293+ rb_ary_push (keys , rbs_constant_to_ruby_symbol (key ));
275294 }
276295 }
277296
0 commit comments