From: "mame (Yusuke Endoh) via ruby-core" Date: 2025-12-16T14:01:48+00:00 Subject: [ruby-core:124246] [Ruby Bug#21783] {Method,UnboundMethod,Proc}#source_location returns columns in bytes and not in characters Issue #21783 has been updated by mame (Yusuke Endoh). > And also to be able to use `Prism.node_for` one of course needs `Proc#source_location`. Huh? `Proc` already has its `node_id` (via its ISeq), so `Prism.node_for` is implementable without `source_location`. In fact, by doing the following, you can obtain the Prism node for Proc. ```ruby f = -> {} require "prism" node_id = RubyVM::InstructionSequence.of(-> {}).to_a[4][:node_id] pp Prism.parse_file(__FILE__).value.breadth_first_search { |node| node.node_id == node_id } #=> @ LambdaNode (location: (4,41)-(4,46)) # ��������� flags: ��� # ��������� locals: [] # ��������� operator_loc: (4,41)-(4,43) = "->" # ��������� opening_loc: (4,44)-(4,45) = "{" # ��������� closing_loc: (4,45)-(4,46) = "}" # ��������� parameters: ��� # ��������� body: ��� ``` Note that I don't say users should write `RubyVM::InstructionSequence.of(-> {}).to_a[4][:node_id]` manually. It should be hidden within `Prism.node_for`. This is what `RubyVM::AbstractSyntaxTree.of` does actually. I want to say that `source_location` is unnecessary to implement `Prism.node_for`. Regarding to power_assert, I talked with @ktsj, and he said that `Proc#source_location` is not more useful than `Kernel#caller_locations`. Since power_assert pinpoints expressions within a block at a finer granularity, rough block's location information isn't particulary useful. ---------------------------------------- Bug #21783: {Method,UnboundMethod,Proc}#source_location returns columns in bytes and not in characters https://bugs.ruby-lang.org/issues/21783#change-115734 * Author: Eregon (Benoit Daloze) * Status: Open * ruby -v: ruby 4.0.0dev (2025-12-14T07:11:02Z master 711d14992e) +PRISM [x86_64-linux] * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- The documentation says: ``` = Proc.source_location (from ruby core) ------------------------------------------------------------------------ prc.source_location -> [String, Integer, Integer, Integer, Integer] ------------------------------------------------------------------------ Returns the location where the Proc was defined. The returned Array contains: (1) the Ruby source filename (2) the line number where the definition starts (3) the column number where the definition starts (4) the line number where the definition ends (5) the column number where the definitions ends This method will return nil if the Proc was not defined in Ruby (i.e. native). ``` So it talks about column numbers, so it should be a number of characters and not of bytes. But currently it's a number of bytes: ``` $ ruby --parser=prism -ve 'def ��t��; end; p method(:��t��).source_location' ruby 4.0.0dev (2025-12-14T07:11:02Z master 711d14992e) +PRISM [x86_64-linux] ["-e", 1, 0, 1, 14] $ ruby --parser=parse.y -ve 'def ��t��; end; p method(:��t��).source_location' ruby 4.0.0dev (2025-12-14T07:11:02Z master 711d14992e) [x86_64-linux] ["-e", 1, 0, 1, 14] ``` The last number should be 12 because `"def ��t��; end".size` is 12 characters. This is a Ruby-level API so I would never expect "byte columns" here, I think it's clear it should be a number of "editor columns" i.e. a number of characters. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/