Project

General

Profile

Actions

Bug #7539

closed

Misleading error message "can't convert nil into string"

Added by connec (Chris Connelly) about 12 years ago. Updated almost 12 years ago.

Status:
Closed
Target version:
ruby -v:
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
Backport:
[ruby-core:50733]

Description

When trying to call String#+ with nil as an argument, you get the error "can't convert nil into String", which does not make sense (in fact seeming blatantly false) as nil.to_s, String(nil) etc. all return '' without errors.

Ideally, this method should use to_s to convert the argument, or else report an error along the lines of "can't append nil to string".

Minimal test case:

Actual:
> '' + nil
TypeError: can't convert nil into String

Expected:
> '' + nil
''


Files

patch#7539.txt (583 Bytes) patch#7539.txt duerst (Martin Dürst), 12/11/2012 01:49 PM

Updated by kosaki (Motohiro KOSAKI) about 12 years ago

I support "can't append nil to string" error.

Updated by marcandre (Marc-Andre Lafortune) about 12 years ago

You'll get this error message for every failed conversion; the use the corresponding implicit conversion methods (to_str, to_int, ...)

"" + 42
[].concat(1..2)

I'm surprised this leads to confusion, but we could change the message to something like:

"no implicit conversion of nil into String"

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) about 12 years ago

kosaki (Motohiro KOSAKI) wrote:

I support "can't append nil to string" error.

I'd prefer a more descriptive message like:

"can't append nil to string (object doesn't respond to to_str)"

This way someone would know that this would work:

nil.class.module_eval{alias to_str to_s}; '' + nil

Updated by phluid61 (Matthew Kerwin) about 12 years ago

I support adding the 'implicit' word to the error message.

"no implicit conversion of nil into String"

I remember when I first encountered it, knowing to search for "implicit
conversion" would have helped me understand the difference between #to_s
and #to_str earlier.

[note: it's more than just implementing #to_str , there are other
implications for implicit casting]

Updated by duerst (Martin Dürst) about 12 years ago

rosenfeld (Rodrigo Rosenfeld Rosas) wrote:

kosaki (Motohiro KOSAKI) wrote:

I support "can't append nil to string" error.

I'd prefer a more descriptive message like:

"can't append nil to string (object doesn't respond to to_str)"

Looking at the implementation, a "can't append" would need some special code. A message such as ""can't convert nil into String (using to_str)" would be relatively easy to generate. I have attached a patch (not compiled or tested, sorry). This change would show up in many other places that use the same conversion logic, so let's make sure we really want it.

Updated by drbrain (Eric Hodel) about 12 years ago

I am worried about implying that the user should add to_str, to_ary, etc. to fix this problem. Rarely is implementing these methods appropriate since they are used for objects that are intended to behave exactly as a String, Array, etc.

An error message that omitted mention of these methods can help prevent users from accidentally shooting themselves in the foot.

Updated by marcandre (Marc-Andre Lafortune) about 12 years ago

  • Assignee set to marcandre (Marc-Andre Lafortune)

Looking at the implementation, a "can't append" would need some special code

Indeed. It would be redundant anyways, since the error message starts with "in `+':", or whichever method was called last.

I am worried about implying that the user should add to_str, to_ary, etc. to fix this problem

Agreed, which is why I proposed making explicit the fact that implicit conversion failed.

So, I'll commit "no implicit conversion of into " unless there's a better wording proposed

Actions #8

Updated by marcandre (Marc-Andre Lafortune) almost 12 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r38979.
Chris, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • object.c: Improve error for failed implicit conversions [Bug #7539]

  • error.c: Adapt rdoc

  • test/ruby/test_object.rb: Test for above

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0