ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:123180] [Ruby Bug#21563] Misleading error message when `to_proc` does not return a Proc in an Object used as a &block argument
@ 2025-09-05 23:56 soulcutter (Bradley Schaefer) via ruby-core
  2025-09-06  0:10 ` [ruby-core:123181] " jeremyevans0 (Jeremy Evans) via ruby-core
  2025-09-06  6:58 ` [ruby-core:123183] " jeremyevans0 (Jeremy Evans) via ruby-core
  0 siblings, 2 replies; 3+ messages in thread
From: soulcutter (Bradley Schaefer) via ruby-core @ 2025-09-05 23:56 UTC (permalink / raw)
  To: ruby-core; +Cc: soulcutter (Bradley Schaefer)

Issue #21563 has been reported by soulcutter (Bradley Schaefer).

----------------------------------------
Bug #21563: Misleading error message when `to_proc` does not return a Proc in an Object used as a &block argument
https://bugs.ruby-lang.org/issues/21563

* Author: soulcutter (Bradley Schaefer)
* Status: Open
* ruby -v: ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [arm64-darwin24]
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
When a class implements `#to_proc` it should always return a Proc. Nonetheless bugs are possible, and when an implementation returns something else, and is used as a block argument to a method, you get an error that claims that the argument type is the implementing class rather than the type that was returned from `to_proc`.


Reproduction
```
 class SayHi
   def call = "hi"
   def to_proc = "obviously not a proc"
 end

 def callablock(&block) = block.call
 
 callablock &SayHi.new # wrong argument type SayHi (expected Proc) (TypeError)
```

In real-world code it may not be _this_ obvious that `to_proc` is returning the wrong type, and so I would expect the error would report the type of object returned by `to_proc` so that you have a more-direct path to understanding the misbehavior. In this case I would expect:

    wrong argument type String (expected Proc)

Or maybe something more-descriptive as an error message, but I don't have a concrete suggestion for that.



-- 
https://bugs.ruby-lang.org/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:123181] [Ruby Bug#21563] Misleading error message when `to_proc` does not return a Proc in an Object used as a &block argument
  2025-09-05 23:56 [ruby-core:123180] [Ruby Bug#21563] Misleading error message when `to_proc` does not return a Proc in an Object used as a &block argument soulcutter (Bradley Schaefer) via ruby-core
@ 2025-09-06  0:10 ` jeremyevans0 (Jeremy Evans) via ruby-core
  2025-09-06  6:58 ` [ruby-core:123183] " jeremyevans0 (Jeremy Evans) via ruby-core
  1 sibling, 0 replies; 3+ messages in thread
From: jeremyevans0 (Jeremy Evans) via ruby-core @ 2025-09-06  0:10 UTC (permalink / raw)
  To: ruby-core; +Cc: jeremyevans0 (Jeremy Evans)

Issue #21563 has been updated by jeremyevans0 (Jeremy Evans).


If we want to change the behavior in this case, we should report both the actual argument and the return value of `to_proc`.  That's what we do for `*a` where `a.to_a` returns non-Array and for `**h` where `h.to_hash` returns non-Hash):

```ruby
A = Class.new
a = A.new
def a.to_a = 1
def a.to_hash = 1
p(*a)  # can't convert A to Array (A#to_a gives Integer) (TypeError)
p(**a) # can't convert A to Hash (A#to_hash gives Integer) (TypeError)

def a.to_proc = 1
p(&a)
# currently: wrong argument type A (expected Proc) (TypeError)
# preferable: can't convert A to Proc (A#to_proc gives Integer) (TypeError)
```

----------------------------------------
Bug #21563: Misleading error message when `to_proc` does not return a Proc in an Object used as a &block argument
https://bugs.ruby-lang.org/issues/21563#change-114512

* Author: soulcutter (Bradley Schaefer)
* Status: Open
* ruby -v: ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [arm64-darwin24]
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
When a class implements `#to_proc` it should always return a Proc. Nonetheless bugs are possible, and when an implementation returns something else, and is used as a block argument to a method, you get an error that claims that the argument type is the implementing class rather than the type that was returned from `to_proc`.


Reproduction
```
 class SayHi
   def call = "hi"
   def to_proc = "obviously not a proc"
 end

 def callablock(&block) = block.call
 
 callablock &SayHi.new # wrong argument type SayHi (expected Proc) (TypeError)
```

In real-world code it may not be _this_ obvious that `to_proc` is returning the wrong type, and so I would expect the error would report the type of object returned by `to_proc` so that you have a more-direct path to understanding the misbehavior. In this case I would expect:

    wrong argument type String (expected Proc)

Or maybe something more-descriptive as an error message, but I don't have a concrete suggestion for that.



-- 
https://bugs.ruby-lang.org/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:123183] [Ruby Bug#21563] Misleading error message when `to_proc` does not return a Proc in an Object used as a &block argument
  2025-09-05 23:56 [ruby-core:123180] [Ruby Bug#21563] Misleading error message when `to_proc` does not return a Proc in an Object used as a &block argument soulcutter (Bradley Schaefer) via ruby-core
  2025-09-06  0:10 ` [ruby-core:123181] " jeremyevans0 (Jeremy Evans) via ruby-core
@ 2025-09-06  6:58 ` jeremyevans0 (Jeremy Evans) via ruby-core
  1 sibling, 0 replies; 3+ messages in thread
From: jeremyevans0 (Jeremy Evans) via ruby-core @ 2025-09-06  6:58 UTC (permalink / raw)
  To: ruby-core; +Cc: jeremyevans0 (Jeremy Evans)

Issue #21563 has been updated by jeremyevans0 (Jeremy Evans).


I submitted a PR to make the error messages for & consistent with */**: https://github.com/ruby/ruby/pull/14463

----------------------------------------
Bug #21563: Misleading error message when `to_proc` does not return a Proc in an Object used as a &block argument
https://bugs.ruby-lang.org/issues/21563#change-114514

* Author: soulcutter (Bradley Schaefer)
* Status: Open
* ruby -v: ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [arm64-darwin24]
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
When a class implements `#to_proc` it should always return a Proc. Nonetheless bugs are possible, and when an implementation returns something else, and is used as a block argument to a method, you get an error that claims that the argument type is the implementing class rather than the type that was returned from `to_proc`.


Reproduction
```
 class SayHi
   def call = "hi"
   def to_proc = "obviously not a proc"
 end

 def callablock(&block) = block.call
 
 callablock &SayHi.new # wrong argument type SayHi (expected Proc) (TypeError)
```

In real-world code it may not be _this_ obvious that `to_proc` is returning the wrong type, and so I would expect the error would report the type of object returned by `to_proc` so that you have a more-direct path to understanding the misbehavior. In this case I would expect:

    wrong argument type String (expected Proc)

Or maybe something more-descriptive as an error message, but I don't have a concrete suggestion for that.



-- 
https://bugs.ruby-lang.org/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-09-06  6:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-05 23:56 [ruby-core:123180] [Ruby Bug#21563] Misleading error message when `to_proc` does not return a Proc in an Object used as a &block argument soulcutter (Bradley Schaefer) via ruby-core
2025-09-06  0:10 ` [ruby-core:123181] " jeremyevans0 (Jeremy Evans) via ruby-core
2025-09-06  6:58 ` [ruby-core:123183] " jeremyevans0 (Jeremy Evans) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).