ruby-changes:26284
From: nobu <ko1@a...>
Date: Wed, 12 Dec 2012 14:49:49 +0900 (JST)
Subject: [ruby-changes:26284] nobu:r38337 (trunk): vm_insnhelper.c: no warnings for non-object
nobu 2012-12-12 13:40:55 +0900 (Wed, 12 Dec 2012) New Revision: 38337 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38337 Log: vm_insnhelper.c: no warnings for non-object * vm_insnhelper.c (vm_getivar): no uninitialized instance variables warnings for non-object if attr method. Modified files: trunk/ChangeLog trunk/test/ruby/test_module.rb trunk/vm_insnhelper.c Index: ChangeLog =================================================================== --- ChangeLog (revision 38336) +++ ChangeLog (revision 38337) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#1 +Wed Dec 12 13:40:52 2012 Nobuyoshi Nakada <nobu@r...> + + * vm_insnhelper.c (vm_getivar): no uninitialized instance variables + warnings for non-object if attr method. + Wed Dec 12 06:43:37 2012 Benoit Daloze <eregontp@g...> * iseq.c (rb_iseq_parameters): fix limit for optional arguments. Index: vm_insnhelper.c =================================================================== --- vm_insnhelper.c (revision 38336) +++ vm_insnhelper.c (revision 38337) @@ -538,6 +538,8 @@ vm_getivar(VALUE obj, ID id, IC ic, rb_c https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#538 return val; } #endif /* USE_IC_FOR_IVAR */ + if (is_attr) + return rb_attr_get(obj, id); return rb_ivar_get(obj, id); } Index: test/ruby/test_module.rb =================================================================== --- test/ruby/test_module.rb (revision 38336) +++ test/ruby/test_module.rb (revision 38337) @@ -1513,4 +1513,67 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#1513 end end end + + class AttrTest + class << self + attr_accessor :cattr + end + attr_accessor :iattr + def ivar + @ivar + end + end + + def test_uninitialized_instance_variable + a = AttrTest.new + stderr = EnvUtil.verbose_warning do + assert_nil(a.ivar) + end + assert_match(/instance variable @ivar not initialized/, stderr) + a.instance_variable_set(:@ivar, 42) + stderr = EnvUtil.verbose_warning do + assert_equal(42, a.ivar) + end + assert_equal("", stderr) + end + + def test_uninitialized_attr + a = AttrTest.new + stderr = EnvUtil.verbose_warning do + assert_nil(a.iattr) + end + assert_equal("", stderr) + a.iattr = 42 + stderr = EnvUtil.verbose_warning do + assert_equal(42, a.iattr) + end + assert_equal("", stderr) + end + + def test_uninitialized_attr_class + stderr = EnvUtil.verbose_warning do + assert_nil(AttrTest.cattr) + end + assert_equal("", stderr) + AttrTest.cattr = 42 + stderr = EnvUtil.verbose_warning do + assert_equal(42, AttrTest.cattr) + end + assert_equal("", stderr) + end + + def test_uninitialized_attr_non_object + a = Class.new(Array) do + attr_accessor :iattr + end.new + stderr = EnvUtil.verbose_warning do + assert_nil(a.iattr) + end + assert_equal("", stderr) + a.iattr = 42 + stderr = EnvUtil.verbose_warning do + assert_equal(42, a.iattr) + end + assert_equal("", stderr) + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/