Bug #7539

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

Added by Chris Connelly over 1 year ago. Updated about 1 year ago.

[ruby-core:50733]
Status:Closed
Priority:Normal
Assignee:Marc-Andre Lafortune
Category:core
Target version:next minor
ruby -v:ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux] Backport:

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
''

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

Associated revisions

Revision 38979
Added by Marc-Andre Lafortune about 1 year ago

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

  • error.c: Adapt rdoc

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

Revision 38980
Added by Marc-Andre Lafortune about 1 year ago

  • re.c (reg_operand): Simplify and reuse error handling [Bug #7539]

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

History

#1 Updated by Motohiro KOSAKI over 1 year ago

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

#2 Updated by Marc-Andre Lafortune over 1 year 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"

#3 Updated by Rodrigo Rosenfeld Rosas over 1 year 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.moduleeval{alias tostr to_s}; '' + nil

#4 Updated by Matthew Kerwin over 1 year 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 #tos
and #to
str earlier.

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

#5 Updated by Martin Dürst over 1 year ago

  • File patch#7539.txtMagnifier added
  • Target version changed from 1.9.3 to next minor

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.

#6 Updated by Eric Hodel over 1 year ago

I am worried about implying that the user should add tostr, toary, 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.

#7 Updated by Marc-Andre Lafortune over 1 year ago

  • Assignee set to 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 tostr, toary, 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

#8 Updated by Marc-Andre Lafortune about 1 year 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

Also available in: Atom PDF