ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: mame@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:103492] [Ruby master Bug#16983] RubyVM::AbstractSyntaxTree.of(method) returns meaningless node if the method is defined in eval
Date: Sat, 17 Apr 2021 07:21:17 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-91588.20210417072116.11727@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16983.20200624125756.11727@ruby-lang.org

Issue #16983 has been updated by mame (Yusuke Endoh).

Assignee set to ko1 (Koichi Sasada)
Status changed from Open to Assigned

This ticket was discussed on dev-meeting. A method (or proc) created in an `eval` context should be marked and `AST.of` should raise an exception against a marked method. @ko1 said that he will do.

----------------------------------------
Bug #16983: RubyVM::AbstractSyntaxTree.of(method) returns meaningless node if the method is defined in eval
https://bugs.ruby-lang.org/issues/16983#change-91588

* Author: pocke (Masataka Kuwabara)
* Status: Assigned
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: ruby 2.8.0dev (2020-06-23T13:58:26Z master dc351ff984) [x86_64-linux]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------

# Problem

`RubyVM::AST.of(method)` returns a meaningless node if the method is defined in eval.
For example:


```ruby
p 'blah'

eval <<~RUBY, binding, __FILE__, __LINE__ + 1
  def foo
  end
RUBY

method = method(:foo)
pp RubyVM::AbstractSyntaxTree.of(method)
# => (STR@3:5-3:12 "def foo\n" + "end\n")
```


I expect the node of `foo` method, or `nil`. But it returns a `STR` node.





It becomes a big problem when `AST.of` receives arbitrary methods.
Because we can't distinguish a method is defined in `eval` or not.
It means we can't believe the returned value of `AST.of` if the method may receive a method defined in `eval`.

For example:

```ruby
def do_something_for_each_method_ast(klass)
  klass.instance_methods(false).each do |m|
    ast = RubyVM::AbstractSyntaxTree.of(klass.instance_method(m))
    next unless ast

    do_something ast
  end
end

class A
  eval <<~RUBY, binding, __FILE__, __LINE__ + 1
    def foo
    end
  RUBY
end

do_something_for_each_method_ast A
```


In the example, I expect the `do_something` method receives only node for a method definition,
but it may pass a wrong node if any method is defined in `eval`.



# Cause (I guess)


I guess the cause is misleading node number.
In and out of an `eval` block uses different sequences of node number.
So if I specify `__FILE__` to `eval`, the actual file and code in `eval` may have the same node number.

For example


```ruby
p 'blah' # Node number for 'blah' is 1, file name is "test.rb"

eval <<~RUBY, binding, __FILE__, __LINE__ + 1
  def foo # Node number for `def` is also 1, file name is also "test.rb"
  end
RUBY

method = method(:foo)
# It finds a node from node number 1 by reading "test.rb", so it get the str node.
pp RubyVM::AbstractSyntaxTree.of(method)
# => (STR@3:5-3:12 "def foo\n" + "end\n")
```





-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2021-04-17  7:21 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-24 12:57 [ruby-core:98931] [Ruby master Bug#16983] RubyVM::AbstractSyntaxTree.of(method) returns meaningless node if the method is defined in eval kuwabara
2020-08-28 21:35 ` [ruby-core:99759] " merch-redmine
2021-04-17  7:21 ` mame [this message]
2021-05-21 19:11 ` [ruby-core:103957] " merch-redmine

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-91588.20210417072116.11727@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).