From: "Eregon (Benoit Daloze) via ruby-core" Date: 2025-12-16T09:31:36+00:00 Subject: [ruby-core:124227] [Ruby Bug#21783] {Method,UnboundMethod,Proc}#source_location returns columns in bytes and not in characters Issue #21783 has been updated by Eregon (Benoit Daloze). I made a PR to re-add `{Method,UnboundMethod,Proc}#source_location` and fix all known issues: https://github.com/ruby/ruby/pull/15580 @matz Would it be OK to merge it? ���� For context I opened this issue because I was surprised at the semantics and it was documented in character columns. If the documentation stated the columns are in bytes I would have thought "somewhat unexpected for me, but I can deal with it, moving on". So for the next person, if they look at the docs it should be clear now with this PR. I would have never expected this issue to revert the feature, this is certainly *not* what I want. I opened this issue to show the doc & implementation inconsistency, explain my expectations, and discuss what fix makes sense. I think @kddnewton makes a good point about grapheme clusters and east asian width, where even character width is not enough. And it's probably not reasonable to ask parsers to handle those cases either. So I now think byte columns is a good choice, as long as it's properly documented. I think this is a case of we shouldn't let perfection get in the way of usefulness, from the saying [Perfect is the enemy of good](https://en.wikipedia.org/wiki/Perfect_is_the_enemy_of_good). --- BTW using a variant of my C example from above with different compilers shows some variety: ```c int main() { char* s = "������������"; oops } ``` ``` $ gcc test.c test.c: In function ���main���: test.c:2:25: error: ���oops��� undeclared (first use in this function) 2 | char* s = "������������"; oops | ^~~~ $ clang test.c test.c:2:31: error: use of undeclared identifier 'oops' 2 | char* s = "������������"; oops | ^ ``` `gcc` shows column 25 which corresponds to nothing, `' char* s = "������������"; '` is 21 characters and 30 bytes. `clang` shows the column in bytes, so there is clearly some variety there, and at least `clang` chose to show byte columns. ---------------------------------------- Bug #21783: {Method,UnboundMethod,Proc}#source_location returns columns in bytes and not in characters https://bugs.ruby-lang.org/issues/21783#change-115713 * 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/