From: "Dan0042 (Daniel DeLorme)" <noreply@...>
Date: 2022-05-13T13:17:29+00:00
Subject: [ruby-core:108542] [Ruby master Feature#18742] Introduce a way to tell if a method invokes the `super` keyword

Issue #18742 has been updated by Dan0042 (Daniel DeLorme).


Thank you for the suggestion, and I apologize for the late reply.

This works remarquably well.
```ruby
class UnboundMethod
  def calls_super?
    iseqs = [RubyVM::InstructionSequence.of(self)]
    iseqs.any? do |iseq|
      iseq.each_child{ |c| iseqs << c }
      iseq.to_a.last.any?{ |v,| v == :invokesuper }
    end
  end
end
```

Interestingly I found that `super if false` is optimized away so example c didn't work; I had to use `0.times{super}`

But I must say it feels a bit weird to use something so heavy just to get a bit of metadata about the method.

----------------------------------------
Feature #18742: Introduce a way to tell if a method invokes the `super` keyword
https://bugs.ruby-lang.org/issues/18742#change-97586

* Author: Dan0042 (Daniel DeLorme)
* Status: Open
* Priority: Normal
----------------------------------------
In order to implement a "no clobber" checker as in #18618, I would like to have a way to check if a method calls `super` or not.

So I'm thinking that something along the line of `Method#calls_super?` could return true/false if the method simply contains the `super` keyword. I'm not really interested in handling weird/artificial edge cases with eval and binding and whatnot.

```ruby
class X
  def a
  end; p instance_method(:a).calls_super? #=> false

  def b
    super
  end; p instance_method(:b).calls_super? #=> true

  def c
    super if false
  end; p instance_method(:c).calls_super? #=> true

  def d
    eval 'super'
  end; p instance_method(:d).calls_super? #=> false (I doubt there's a reasonable way for this to return true)
end
```

With the above it would be possible to warn against a method that has a `super_method` but doesn't use the `super` keyword.



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

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>