[ruby-core:112717] [Ruby master Feature#19452] `Thread::Backtrace::Location` should have column information if possible.
From:
"mame (Yusuke Endoh) via ruby-core" <ruby-core@...>
Date:
2023-03-07 11:22:13 UTC
List:
ruby-core #112717
Issue #19452 has been updated by mame (Yusuke Endoh).
@ioquatix Please, please write a use case in every proposal.
First of all, I think the POC itself is very naive for daily use. Consider the following method call. For Thread::Bactrace::Location of the call to foo, `#first_column` will point before the receiver, not before the method name. And `#last_column` will point after all arguments.
```
very_very_long_receiver.foo(many_many_arguments)
^ ^
| |
+--- #first_column #last_columns ---+
```
Without a use case, it would be impossible to discuss whether and how it is useful. This is a heavier feature than you might think to introduce for the sake of "better than nothing".
---
`ErrorHighlight.spot` is a bit more intelligent: it cuts out the method name part.
```
very_very_long_receiver.foo(many_many_arguments)
^^^^
|
+--- ErrorHighlight.spot
```
To achieve this, ErrorHighlight uses `RubyVM::AST.of`: it reparses the source code, identify the node in the method call, and draw an underline after its receiver and before its arguments.
Because of using `RubyVM::AST.of`, ErrorHighlight works only with CRuby. That is unfortunate. Currently @kddeisz is working on a new Ruby parser project called [yarp](https://github.com/Shopify/yarp). As far as I know, its goal is to be a common parser for all major Ruby implementations including CRuby and TruffleRuby. If the goal is accomplished, I will use yarp for ErrorHighlight, which will (hopefully) allow ErrorHighlight to work on Ruby interpreters rather than CRuby.
Even in that case, it is still necessary to map `Thread::Backtrace::Location` to the AST node. We may use `Thread::Backtrace::Location#first_lineno`, etc. to identify the AST node corresponding to the `Thread::Backtrace::Location`. But this should be considered after yarp actually becomes a CRuby parser. (Implementation note: currently, `Thread::Backtrace::Location` has the ID of the AST node, which is a number assigned internally to the nodes in the AST in (approximate) pre-order. `RubyVM::AST.of` re-parses the source code to recover the full AST, and then identifies the AST node by using the node ID. This is the method suggested by @ko1. The four values, first_lineno / first_column / last_lineno / last_column, could be another way to identify nodes. But this is slightly less accurate: if there is another node in the exact same code range, it is not uniquely identifiable. I don't think this is a problem for ErrorHighlight, though.)
@eregon @kddeisz Any opinions?
----------------------------------------
Feature #19452: `Thread::Backtrace::Location` should have column information if possible.
https://bugs.ruby-lang.org/issues/19452#change-102173
* Author: ioquatix (Samuel Williams)
* Status: Open
* Priority: Normal
----------------------------------------
I discussed this with @mame and it would be pretty useful if we could also get the column information from exception backtrace location, even if it was slow.
A POC:
```ruby
class Thread::Backtrace::Location
if defined?(RubyVM::AbstractSyntaxTree)
def first_column
RubyVM::AbstractSyntaxTree.of(self, keep_script_lines: true).first_column
end
else
def first_column
raise NotImplementedError
end
end
end
```
It would be good to have a standard interface, so we follow the same interface as https://bugs.ruby-lang.org/issues/19451 and vice versa where it makes sense. I'll investigate it.
--
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/postorius/lists/ruby-core.ml.ruby-lang.org/