From c34e9f23aa2b6909e92623710e91cd99eaff2dc3 Mon Sep 17 00:00:00 2001 From: marcandre Date: Tue, 29 Jan 2013 22:00:58 +0000 Subject: [PATCH] * object.c: Improve error for failed implicit conversions [Bug #7539] * error.c: Adapt rdoc * test/ruby/test_object.rb: Test for above git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ error.c | 2 +- object.c | 5 ++++- test/ruby/test_object.rb | 8 ++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index d44d5c6c23..7e067a64ee 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Wed Jan 30 07:00:16 2013 Marc-Andre Lafortune + + * object.c: Improve error for failed implicit conversions [Bug #7539] + + * error.c: Adapt rdoc + + * test/ruby/test_object.rb: Test for above + Tue Jan 29 21:40:12 2013 Tanaka Akira * lib/net/http/generic_request.rb (encode_multipart_form_data): remove diff --git a/error.c b/error.c index da24505eb0..4b678d8957 100644 --- a/error.c +++ b/error.c @@ -1390,7 +1390,7 @@ syserr_eqq(VALUE self, VALUE exc) * * raises the exception: * - * TypeError: can't convert String into Integer + * TypeError: no implicit conversion of String into Integer * */ diff --git a/object.c b/object.c index 09b8040820..ba4f997b3a 100644 --- a/object.c +++ b/object.c @@ -2277,6 +2277,7 @@ static struct conv_method_tbl { {"to_s", 0}, {NULL, 0} }; +#define IMPLICIT_CONVERSIONS 7 static VALUE convert_type(VALUE val, const char *tname, const char *method, int raise) @@ -2296,7 +2297,9 @@ convert_type(VALUE val, const char *tname, const char *method, int raise) r = rb_check_funcall(val, m, 0, 0); if (r == Qundef) { if (raise) { - rb_raise(rb_eTypeError, "can't convert %s into %s", + rb_raise(rb_eTypeError, i < IMPLICIT_CONVERSIONS + ? "no implicit conversion of %s into %s" + : "can't convert %s into %s", NIL_P(val) ? "nil" : val == Qtrue ? "true" : val == Qfalse ? "false" : diff --git a/test/ruby/test_object.rb b/test/ruby/test_object.rb index 9f0e9bc5b2..9787525bce 100644 --- a/test/ruby/test_object.rb +++ b/test/ruby/test_object.rb @@ -885,4 +885,12 @@ class TestObject < Test::Unit::TestCase assert_not_initialize_copy {st.new} assert_not_initialize_copy {Time.now} end + + def test_type_error_message + issue = "Bug #7539" + err = assert_raise(TypeError){ Integer([42]) } + assert_equal "can't convert Array into Integer", err.message, issue + err = assert_raise(TypeError){ [].first([42]) } + assert_equal 'no implicit conversion of Array into Integer', err.message, issue + end end