[ruby-core:124426] [Ruby Feature#6012] Proc#source_location also return the column
Issue #6012 has been updated by Eregon (Benoit Daloze). mame (Yusuke Endoh) wrote in #note-43:
However, we should consider the fact that code like `source_location.last` actually exists in the wild, as RSpec fixed in advance. It seems this idiom is used in quite a few gems.
This change has been in all 3.5 and 4.0 preview builds and seems to have caused very little incompatibility. It will have another full year before the 4.1 release, so I think this is no problem. BTW, if we remove tests & specs from that list (since they wouldn't break any code) we have: ``` $ cat matches.txt | grep -Fv '_spec.rb' | grep -Fv '_test.rb' | grep -Fv '/test_' 1980-01-01 /srv/gems/minitest-junit-2.1.0/lib/minitest/junit.rb: testcase['line'] = result.source_location.last 1980-01-02 /srv/gems/ci-queue-0.81.0/lib/minitest/queue/junit_reporter.rb: lineno = tests.first.source_location.last 1980-01-02 /srv/gems/command_deck-0.3.3/TAGS: def lineno() @binding.source_location.last endlineno11,0 1980-01-02 /srv/gems/grape_rails_logger-1.2.0/lib/grape_rails_logger/subscriber.rb: line = endpoint.source.source_location.last 1980-01-02 /srv/gems/itsi-0.2.20/gems/server/lib/itsi/server/typed_handlers/source_parser.rb: proc_line = source_location.last - 1 1980-01-02 /srv/gems/itsi-server-0.2.20/lib/itsi/server/typed_handlers/source_parser.rb: proc_line = source_location.last - 1 1980-01-02 /srv/gems/mutant-0.14.1/lib/mutant/matcher/method.rb: source_location.last 1980-01-02 /srv/gems/openhab-scripting-5.44.0/lib/openhab/dsl/rules/name_inference.rb: "#{file}:#{block.source_location.last}".tr(".", "_") 1980-01-02 /srv/gems/pry-0.16.0/lib/pry/wrapped_module/candidate.rb: last_method_source_location.last 1980-01-02 /srv/gems/rspec-expectations-3.13.5/lib/rspec/expectations/block_snippet_extractor.rb: source_location.last 1980-01-02 /srv/gems/scnr-introspector-0.3.2/lib/scnr/introspector/data_flow/sink.rb: lineno = @method_source_location.last 2011-09-03 /srv/gems/minecraft-0.3.3/lib/minecraft/extensions.rb: src_b, src_e = get_comment_range(meth.source_location.last) 2012-10-25 /srv/gems/rubymirrors-0.0.3/lib/ruby/reflection/method_mirror.rb: source_location.last - 1 2015-02-02 /srv/gems/billygoat-0.0.7.2/lib/billygoat/documentation.rb: @line_number ||= self.method.source_location.last 2015-04-01 /srv/gems/turnip-dry_run-0.1.0/lib/turnip/dry_run.rb: step_method.source_location.last 2015-04-18 /srv/gems/fare-0.1.8/lib/fare/configuration_dsl.rb: raise "No run list specified for stack on line #{@source_location.last} of #{@source_location.first}" 2015-04-18 /srv/gems/fare-0.1.8/lib/fare/configuration_dsl.rb: raise "Stack without topics for stack on line #{@source_location.last} of #{@source_location.first}" 2015-11-12 /srv/gems/git_compound-0.2.2/lib/git_compound/logger/debug/task.rb: "in line `#{@block.source_location.last}`, " \ 2015-12-08 /srv/gems/minirails-0.2.0/lib/minirails.rb: c.instance_method(m).source_location.last 2016-05-06 /srv/gems/matest-1.7.4/lib/matest/example_block.rb: lineno = block.source_location.last 2016-05-22 /srv/gems/transform_tree-0.3.1/lib/transform_tree/node.rb: "#{File.basename closure.source_location.first}:#{closure.source_location.last}" 2019-02-11 /srv/gems/mutest-0.0.10/lib/mutest/matcher/method.rb: source_location.last 2019-03-11 /srv/gems/pomodorokun-0.1.0/bundle/ruby/2.4.0/gems/rspec-expectations-3.8.2/lib/rspec/expectations/block_snippet_extractor.rb: source_location.last 2020-01-03 /srv/gems/puppet-function-updater-0.0.5/lib/pfu/parser.rb: return [funcname, opts, block.source_location.last] 2020-11-28 /srv/gems/spud-0.2.10/lib/spud/block_param_info.rb: line = File.read(@filename).split("\n")[@block.source_location.last - 1] 2021-03-04 /srv/gems/dialekt-0.1.0/lib/dialekt/util/call_adapter.rb: "#{File.basename(@callable.source_location.first)}:#{@callable.source_location.last}" 2021-09-17 /srv/gems/loupe-0.1.5/lib/loupe/executor.rb: line_numbers.include?(klass.instance_method(method_name).source_location.last.to_s) 2021-11-23 /srv/gems/glimmer-dsl-specification-0.0.5/lib/glimmer/specification/element/fact.rb: source_code = PutsDebuggerer::SourceFile.new(@block.source_location.first).source(1, @block.source_location.last) 2022-05-02 /srv/gems/boothby-0.1.1/lib/boothby/seeds.rb: methods.sort_by { |method| seed_class.method(method).source_location.last } 2022-05-02 /srv/gems/boothby-0.1.1/lib/boothby/seeds.rb: methods.sort_by { |method| seed_class.method(method).source_location.last } 2022-11-24 /srv/gems/opal-rspec-1.0.0/rspec-expectations/upstream/lib/rspec/expectations/block_snippet_extractor.rb: source_location.last 2022-12-28 /srv/gems/pork-2.1.0/lib/pork/extra/show_source.rb: lowers = test.source_location.last 2023-01-06 /srv/gems/takuya-lvm-snapshot-0.1.0/bundle/ruby/2.7.0/gems/rspec-expectations-3.12.1/lib/rspec/expectations/block_snippet_extractor.rb: source_location.last 2023-03-04 /srv/gems/blinka-reporter-0.8.0/lib/blinka_reporter/minitest_adapter.rb: @line ||= source_location.last 2023-09-05 /srv/gems/web-console-4.2.1/lib/web_console/source_location.rb: def lineno() @binding.source_location.last end 2023-12-18 /srv/gems/ali-cli-0.0.2/lib/ali/template.rb: Rouge.highlight "\n#{"".ljust(indent, " ")}# #{task_path}:#{source_location.last}\n#{source}", lexer, formatter 2023-12-18 /srv/gems/builder_apm-0.5.14/lib/builder_apm/controllers/instrumenter.rb: line_number = method_info.source_location.last 2023-12-18 /srv/gems/constrain-0.10.0/lib/constrain.rb: when Proc; "Proc@#{expr.source_location.first}:#{expr.source_location.last}" 2024-02-11 /srv/gems/prawn-manual_builder-0.4.0/lib/prawn/manual_builder/chapter.rb: block_source_line = example.source_location.last 2024-07-10 /srv/gems/curlybars-1.12.0/lib/curlybars/rendering_support.rb: line_number = source_location ? helper.source_location.last : "n/a" 2024-09-16 /srv/gems/minitest-distributed-0.2.11/lib/minitest/distributed/reporters/junitxml_reporter.rb: lineno = test.source_location.last 2025-01-07 /srv/gems/lazy_graph-0.2.0/lib/lazy_graph/node/derived_rules.rb: derived.source_location.last + offset 2025-01-07 /srv/gems/lazy_graph-0.2.0/lib/lazy_graph/node/derived_rules.rb: proc_line = source_location.last - 1 2025-08-08 /srv/gems/openstudio-common-measures-0.12.3/lib/measures/envelope_and_internal_load_breakdown/measure.rb: method_hash[section.to_s] = OsLib_ReportingHeatGainLoss.method(section).source_location.last 2025-08-08 /srv/gems/openstudio-common-measures-0.12.3/lib/measures/example_report/measure.rb: method_hash[section.to_s] = OsLib_Reporting_example.method(section).source_location.last ``` In any case it's a tiny fraction of e.g. the change in 3.4 for backtraces from `foo' to 'Module#foo'. In https://bugs.ruby-lang.org/issues/21777#note-7 you write:
Is this incompatibility really ok? I feel it is disproportionately incompatible for unclear use cases.
Why don't you consider the gems that need this functionality `method_source` (700 million downloads) and `proc_to_ast` (71 million downloads) as valid use cases? Clearly people use them. Currently `method_source` which defines `Method#source` works around by parsing from the start line and adding an extra line one by one until valid: https://github.com/banister/method_source/blob/06f21c66380c64ff05c8031c0208e... This is obviously hacky (but there was no better way before this), and wouldn't work with multiple `def` on the same line. `proc_to_ast` currently uses the `parser` gem to find the block at the given line but fails if there are multiple blocks at that line. This functionality will fix that: https://github.com/joker1007/proc_to_ast/issues/5 This is already plenty of use cases I think, but additionally we also discussed: * It would enabling fixing the issue with `power_assert` with 2 `assert` on the same line: https://bugs.ruby-lang.org/issues/21783#note-21. It might also help `power_assert` to support multi-line blocks, which is a pretty big limitation. * It enables to implement `Prism.node_for` (https://bugs.ruby-lang.org/issues/21005#note-5) and similar functionality in a portable-across-Ruby-implementations way, so it's both useful directly and indirectly. #21795 would also solve that, but not necessarily the other use cases above, which don't all need the AST (e.g. `Method#source`, which e.g. can be useful to just show the source of a method for documentation, debugging, etc). ---------------------------------------- Feature #6012: Proc#source_location also return the column https://bugs.ruby-lang.org/issues/6012#change-115975 * Author: rogerdpack (Roger Pack) * Status: Closed * Assignee: nobu (Nobuyoshi Nakada) ---------------------------------------- As originally suggested in http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/42418 Suggestion/feature request: have #source_location also return the beginning column where it was defined. ["test.rb", 8, 33] Thanks! -roger- -- https://bugs.ruby-lang.org/
participants (1)
-
Eregon (Benoit Daloze)