TypeError in string interpolation (when #to_s does not return a String) #34

Closed
brixen opened this Issue Oct 7, 2009 · 4 comments

Comments

Projects
None yet
3 participants
@brixen
Member

brixen commented Oct 7, 2009

LH 765 by shugo.

Rubinius handles the following script differently from MRI. Is it intensional?

$ cat t.rb
x = Object.new
def x.to_s
  return 1
end
p "#{x}"
$ ruby-1_8_6 -v t.rb
ruby 1.8.6 (2008-08-08 patchlevel 286) [i686-linux]
"#<Object:0xb7dedbd8>"
$ rbx -v t.rb     
rubinius 0.11.0-dev (ruby 1.8.6) (eabf5ec7d 12/31/2009) [i686-pc-linux-gnu]
An exception has occurred:
      Tried to use non-reference value 0x3 as type String (48) (TypeError)

Backtrace:
            Compiler::Utils.single_load at kernel/compiler/compile.rb:245
    Compiler::Utils.load_from_extension at kernel/compiler/compile.rb:323
                      Object#__script__ at kernel/loader.rb:240
@burke

This comment has been minimized.

Show comment
Hide comment
@burke

burke Feb 4, 2010

http://github.com/evanphx/rubinius/blob/65944505d2f2a34d29944d27457a0479cbf0ffb0/lib/compiler/ast/literals.rb#L289

  # We also do not handle any case where #to_s does not actually return a
  # String instance.

So yes, it seems that it's intentional.

burke commented Feb 4, 2010

http://github.com/evanphx/rubinius/blob/65944505d2f2a34d29944d27457a0479cbf0ffb0/lib/compiler/ast/literals.rb#L289

  # We also do not handle any case where #to_s does not actually return a
  # String instance.

So yes, it seems that it's intentional.

@burke

This comment has been minimized.

Show comment
Hide comment
@burke

burke Feb 4, 2010

Umm, just realized you wrote the comment I quoted. So uhh.. yeah.

burke commented Feb 4, 2010

Umm, just realized you wrote the comment I quoted. So uhh.. yeah.

@evanphx

This comment has been minimized.

Show comment
Hide comment
@evanphx

evanphx Mar 31, 2010

Member

This is particularly hairy because we call #to_s in the bytecode, so by the time the string construction starts, we've lost the object we called #to_s on to use the fallback.

We'd have to call out to a helper that calls #to_s and checks the return value. Thats expensive since this almost never happens anyway.

I wonder if I shouldn't just take the value, and if it's not a string, return it as one. So in your example case, the value would be "1", since we'd render the 1.

Perhaps I'll just do that.

Member

evanphx commented Mar 31, 2010

This is particularly hairy because we call #to_s in the bytecode, so by the time the string construction starts, we've lost the object we called #to_s on to use the fallback.

We'd have to call out to a helper that calls #to_s and checks the return value. Thats expensive since this almost never happens anyway.

I wonder if I shouldn't just take the value, and if it's not a string, return it as one. So in your example case, the value would be "1", since we'd render the 1.

Perhaps I'll just do that.

@evanphx

This comment has been minimized.

Show comment
Hide comment
@evanphx

evanphx Mar 31, 2010

Member

Use a fallback conversion in string_build. Closed by 33daa62.

Member

evanphx commented Mar 31, 2010

Use a fallback conversion in string_build. Closed by 33daa62.

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment