Bug #2419

super call may use wrong receiver object

Added by tmat (Tomas Matousek) over 2 years ago. Updated about 1 year ago.

[ruby-core:26984]
Status:Closed Start date:12/02/2009
Priority:Normal Due date:
Assignee:nobu (Nobuyoshi Nakada) % Done:

100%

Category:-
Target version:-
ruby -v:ruby 1.8.6 (2009-03-31 patchlevel 368) [i386-mingw32]

Description

The code below prints:
C
main
Object::m

which means that the super calls the instance method B#m on a receiver that is not an instance of B. It's not what makes the super call to pick the main object as a receiver in this case. I would expect it to use the "self" object that the block captures, i.e. the class object C, and thus an exception "no superclass method `m' (NoMethodError)" should be raised.

class B
  def m
    p self
    puts self.class.to_s + '::m'
  end
end

class C < B
  q = Proc.new do
    p self
    super()
  end

  mq = define_method :m, &q

  mq.call
end


BTW: The behavior is even worse in Ruby 1.9:
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mswin32]
C
a.rb:11:in `==': wrong number of arguments(0 for 1) (ArgumentError)
        from a.rb:11:in `block in <class:C>'
        from a.rb:16:in `call'
        from a.rb:16:in `<class:C>'
        from a.rb:8:in `<main>'

Associated revisions

Revision 25975
Added by nobu over 2 years ago

* eval.c (proc_invoke): unbound block created by define_method cannot call super. [ruby-core:26984]

Revision 26536
Added by nobu over 2 years ago

* eval.c (rb_mod_define_method): return original block but not bound block. [ruby-core:26984]

History

Updated by ujihisa (ujihisa .) over 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to nobu (Nobuyoshi Nakada)
FYI: In my environment, the latter result was different.

ruby 1.9.2dev (2009-11-30 trunk 25961) [i386-darwin9.8.0]
C
/var/folders/Dz/Dz5WpFSZGUaFLA8jp8kT5E+++TM/-Tmp-/v408625/85:12:in `block in <class:C>': super: no superclass method `remove_const' for C:Class (NoMethodError)
	from /var/folders/Dz/Dz5WpFSZGUaFLA8jp8kT5E+++TM/-Tmp-/v408625/85:17:in `call'
	from /var/folders/Dz/Dz5WpFSZGUaFLA8jp8kT5E+++TM/-Tmp-/v408625/85:17:in `<class:C>'
	from /var/folders/Dz/Dz5WpFSZGUaFLA8jp8kT5E+++TM/-Tmp-/v408625/85:9:in `<main>'

(Please ignore the difference of line numbers)

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100
This issue was solved with changeset r25975.
Tomas, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

  • Status changed from Closed to Open

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

  • Status changed from Open to Closed
This issue was solved with changeset r26536.
Tomas, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

Also available in: Atom PDF