Forum: Ruby-core [ruby-trunk - Bug #7539][Open] Misleading error message "can't convert nil into string"

Posted by connec (Chris Connelly) (Guest)
on 2012-12-10 13:29
(Received via mailing list)
Issue #7539 has been reported by connec (Chris Connelly).

----------------------------------------
Bug #7539: Misleading error message "can't convert nil into string"
https://bugs.ruby-lang.org/issues/7539

Author: connec (Chris Connelly)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 1.9.3
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]


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
    ''
Posted by kosaki (Motohiro KOSAKI) (Guest)
on 2012-12-10 13:52
(Received via mailing list)
Issue #7539 has been updated by kosaki (Motohiro KOSAKI).


I support "can't append nil to string" error.
----------------------------------------
Bug #7539: Misleading error message "can't convert nil into string"
https://bugs.ruby-lang.org/issues/7539#change-34586

Author: connec (Chris Connelly)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 1.9.3
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]


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
    ''
Posted by marcandre (Marc-Andre Lafortune) (Guest)
on 2012-12-10 16:45
(Received via mailing list)
Issue #7539 has been updated by marcandre (Marc-Andre Lafortune).


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"

----------------------------------------
Bug #7539: Misleading error message "can't convert nil into string"
https://bugs.ruby-lang.org/issues/7539#change-34590

Author: connec (Chris Connelly)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 1.9.3
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]


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
    ''
Posted by rosenfeld (Rodrigo Rosenfeld Rosas) (Guest)
on 2012-12-10 17:44
(Received via mailing list)
Issue #7539 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).


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
----------------------------------------
Bug #7539: Misleading error message "can't convert nil into string"
https://bugs.ruby-lang.org/issues/7539#change-34591

Author: connec (Chris Connelly)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 1.9.3
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]


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
    ''
Posted by Matthew Kerwin (mattyk)
on 2012-12-10 22:09
(Received via mailing list)
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]
Posted by "duerst (Martin Dürst)" <duerst@it.aoyama.ac.jp> (Guest)
on 2012-12-11 05:50
(Received via mailing list)
Issue #7539 has been updated by duerst (Martin Dürst).

File patch#7539.txt 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.

----------------------------------------
Bug #7539: Misleading error message "can't convert nil into string"
https://bugs.ruby-lang.org/issues/7539#change-34598

Author: connec (Chris Connelly)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: next minor
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]


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
    ''
Posted by drbrain (Eric Hodel) (Guest)
on 2012-12-11 06:17
(Received via mailing list)
Issue #7539 has been updated by drbrain (Eric Hodel).


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.
----------------------------------------
Bug #7539: Misleading error message "can't convert nil into string"
https://bugs.ruby-lang.org/issues/7539#change-34599

Author: connec (Chris Connelly)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: next minor
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]


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
    ''
Posted by marcandre (Marc-Andre Lafortune) (Guest)
on 2012-12-11 15:53
(Received via mailing list)
Issue #7539 has been updated by marcandre (Marc-Andre Lafortune).

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 <object> into <Class>" unless 
there's a better wording proposed
----------------------------------------
Bug #7539: Misleading error message "can't convert nil into string"
https://bugs.ruby-lang.org/issues/7539#change-34614

Author: connec (Chris Connelly)
Status: Open
Priority: Normal
Assignee: marcandre (Marc-Andre Lafortune)
Category: core
Target version: next minor
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]


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
    ''
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.