[go: up one dir, main page]

ruby-core

Mailing list archive

[#116534] [Ruby master Bug#20231] Don't wait in io_binwrite_string if not necessary. — "ioquatix (Samuel Williams) via ruby-core" <ruby-core@...>

Issue #20231 has been reported by ioquatix (Samuel Williams).

8 messages 2024/02/01

[#116565] [Ruby master Feature#20235] Deprecate CHAR syntax — "Dan0042 (Daniel DeLorme) via ruby-core" <ruby-core@...>

Issue #20235 has been reported by Dan0042 (Daniel DeLorme).

8 messages 2024/02/03

[#116581] [Ruby master Bug#20237] Unable to unshare(CLONE_NEWUSER) in Linux because of timer thread — "hanazuki (Kasumi Hanazuki) via ruby-core" <ruby-core@...>

Issue #20237 has been reported by hanazuki (Kasumi Hanazuki).

10 messages 2024/02/05

[#116589] [Ruby master Misc#20238] Use prism for mk_builtin_loader.rb — "kddnewton (Kevin Newton) via ruby-core" <ruby-core@...>

Issue #20238 has been reported by kddnewton (Kevin Newton).

22 messages 2024/02/05

[#116640] [Ruby master Feature#20249] Print only backtraces in rb_bug(), by default — "osyoyu (Daisuke Aritomo) via ruby-core" <ruby-core@...>

Issue #20249 has been reported by osyoyu (Daisuke Aritomo).

11 messages 2024/02/09

[#116664] [Ruby master Misc#20254] FYI: Add Launchable into Ruby CI — "ono-max (Naoto Ono) via ruby-core" <ruby-core@...>

Issue #20254 has been reported by ono-max (Naoto Ono).

18 messages 2024/02/10

[#116666] [Ruby master Bug#20255] Embedded arrays aren't moved correctly across ractors — "luke-gru (Luke Gruber) via ruby-core" <ruby-core@...>

Issue #20255 has been reported by luke-gru (Luke Gruber).

18 messages 2024/02/10

[#116681] [Ruby master Misc#20260] ISEQ flag for prism compiler — "kddnewton (Kevin Newton) via ruby-core" <ruby-core@...>

Issue #20260 has been reported by kddnewton (Kevin Newton).

15 messages 2024/02/12

[#116696] [Ruby master Bug#20264] Segfault installing RMagick on M1 Mac — "andy@... (Andy Jeffries) via ruby-core" <ruby-core@...>

Issue #20264 has been reported by andy@andyjeffries.co.uk (Andy Jeffries).

7 messages 2024/02/13

[#116760] [Ruby master Feature#20265] Deprecate and remove rb_newobj and rb_newobj_of — "peterzhu2118 (Peter Zhu) via ruby-core" <ruby-core@...>

SXNzdWUgIzIwMjY1IGhhcyBiZWVuIHJlcG9ydGVkIGJ5IHBldGVyemh1MjExOCAoUGV0ZXIgWmh1

8 messages 2024/02/14

[#116769] [Ruby master Feature#20266] New syntax to escape embed strings in Regexp literal — "usa (Usaku NAKAMURA) via ruby-core" <ruby-core@...>

Issue #20266 has been reported by usa (Usaku NAKAMURA).

8 messages 2024/02/15

[#116819] [Ruby master Feature#20275] Avoid extra backtrace entries for rescue and ensure — "Eregon (Benoit Daloze) via ruby-core" <ruby-core@...>

Issue #20275 has been reported by Eregon (Benoit Daloze).

8 messages 2024/02/17

[#116827] [Ruby master Feature#20276] Introduce Fiber interfaces for Ractors — "forthoney (Seong-Heon Jung) via ruby-core" <ruby-core@...>

Issue #20276 has been reported by forthoney (Seong-Heon Jung).

8 messages 2024/02/17

[#116846] [Ruby master Misc#20281] DevMeeting-2024-03-14 — "mame (Yusuke Endoh) via ruby-core" <ruby-core@...>

Issue #20281 has been reported by mame (Yusuke Endoh).

16 messages 2024/02/19

[#116853] [Ruby master Feature#20282] Enhancing Ruby's Coverage with Per-Test Coverage Reports — "ioquatix (Samuel Williams) via ruby-core" <ruby-core@...>

Issue #20282 has been reported by ioquatix (Samuel Williams).

7 messages 2024/02/19

[#116902] [Ruby master Feature#20290] Add API for C extensions to free memory — "peterzhu2118 (Peter Zhu) via ruby-core" <ruby-core@...>

Issue #20290 has been reported by peterzhu2118 (Peter Zhu).

9 messages 2024/02/21

[#116940] [Ruby master Feature#20300] Hash: set value and get pre-existing value in one call — "AMomchilov (Alexander Momchilov) via ruby-core" <ruby-core@...>

Issue #20300 has been reported by AMomchilov (Alexander Momchilov).

19 messages 2024/02/26

[#116941] [Ruby master Bug#20301] `Set#add?` does two hash look-ups — "AMomchilov (Alexander Momchilov) via ruby-core" <ruby-core@...>

Issue #20301 has been reported by AMomchilov (Alexander Momchilov).

10 messages 2024/02/26

[#116965] [Ruby master Bug#20307] `Hash#update` from compare_by_identity hash can have unfrozen string keys — "nobu (Nobuyoshi Nakada) via ruby-core" <ruby-core@...>

Issue #20307 has been reported by nobu (Nobuyoshi Nakada).

7 messages 2024/02/27

[#116983] [Ruby master Feature#20309] Bundled gems for Ruby 3.5 — "hsbt (Hiroshi SHIBATA) via ruby-core" <ruby-core@...>

Issue #20309 has been reported by hsbt (Hiroshi SHIBATA).

28 messages 2024/02/27

[ruby-core:117005] [Ruby master Feature#15554] warn/error passing a block to a method which never use a block

From: "ko1 (Koichi Sasada) via ruby-core" <ruby-core@...>
Date: 2024-02-29 08:05:53 UTC
List: ruby-core #117005
Issue #15554 has been updated by ko1 (Koichi Sasada).


Retry it on current master 2024.

Summary:

* false positive on several cases:
  * intentional cases for duck typing
  * making mock cases they want to ignore arguments
* found a few issues by warnings

----

https://gist.github.com/ko1/804a0034fb0ec0bdcb67f492932dc976
is a patch to make `unused block` warning.

----

on `make test-all`

I need to add some patches to remove warnings:
https://gist.github.com/ko1/ee47ff7ba1eb8d2beaee74f4124a348d
Most of cases they are mock/wrapper methods for tests.


----

on `make test-spec`

I need to add some patches to remove warnings:
https://gist.github.com/ko1/ba24938ee810793d52e462961056190c
Most of cases they are mock/wrapper methods for tests.

We got several issues on cgi. Passing blocks for CGI related methods can be bugs (not checked details).
https://gist.github.com/ko1/3aafaf775e77074108bb0058ab47cd10

I found a test that the passed block will be ignored, but I'm not sure it is needed.

```ruby
  it "ignores the supplied block" do
    -> { ObjectSpace.garbage_collect {} }.should_not raise_error
  end
```

The warning is the following:

```
/home/ko1/ruby/src/trunk/spec/ruby/core/objectspace/garbage_collect_spec.rb:14: warning: a block is given for a block unused method 'ObjectSpace.garbage_collect'
```



----------------------------------------
Feature #15554: warn/error passing a block to a method which never use a block
https://bugs.ruby-lang.org/issues/15554#change-107064

* Author: ko1 (Koichi Sasada)
* Status: Open
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
# Abstract

Warn or raise an ArgumentError if block is passed to a method which does not use a block.
In other words, detect "block user methods" implicitly and only "block user methods" can accept a block.

# Background

Sometimes, we pass a block to a method which ignores the passed block accidentally.

```
def my_open(name)
  open(name)
end

# user hopes it works as Kernel#open which invokes a block with opened file.
my_open(name){|f| important_work_with f }
# but simply ignored...
```

To solve this issue, this feature request propose showing warnings or raising an exception on such case.

Last developer's meeting, matz proposed `&nil` which declares this method never receive a block. It is explicit, but it is tough to add this `&nil` parameter declaration to all of methods (do you want to add it to `def []=(i, e, &nil)`?).
(I agree `&nil` is valuable on some situations)

# Spec

## Define "use a block" methods

We need to define which method accepts a block and which method does not.

* (1) method has a block parameter (`&b`)
* (2) method body has `yield'
* (3) method body has `super` (ZSUPER in internal terminology) or `super(...)`
* (4) method body has singleton method (optional)

(1) and (2) is very clear. I need to explain about (3) and (4).

(3). `super` (ZSUPER) passes all parameters as arguments. So there is no surprise that which can accept `block`.
However `super(...)` also passes a block if no explicit block passing (like `super(){}` or `super(&b)`) are written.
I'm not sure we need to continue this strange specification, but to keep compatibility depending this spec, I add this rule.

(4). surprisingly, the following code invoke a block:

```
def foo
  class << Object.new
    yield
  end
end

foo{ p :ok } #=> :ok

```

I'm also not sure we need to keep this spec, but to allow this spec, I added (4) rule.
Strictly speaking, it is not required, but we don't keep the link from singleton class ISeq to lexical parent iseq now, so I added it.

## Exceptional cases

A method called by `super` doesn`t warn warning even if this method doesn't use a block.
The rule (3) can pass blocks easily and there are many methods don`t use a block.

So my patch ignores callings by `super`.

## corner cases

There are several cases to use block without (1)-(4) rules.

### `Proc.new/proc/lambda` without a block

Now it was deprecated in r66772 (commit:9f1fb0a17febc59356d58cef5e98db61a3c03550).
Related discussion: [Bug #15539]

### `block_given?`

`block_given?` expects block, but I believe we use it with `yield` or a block parameter.
If you know the usecase without them, please tell us.

### `yield` in `eval`

We can't know `yield` (or (3), (4) rule) in an `eval` evaluating string at calling time.

```
def foo
  eval('yield`)
end

foo{} # at calling time,
      # we can't know the method foo can accept a block or not.
```

So I added a warning to use `yield` in `eval` like that: `test.rb:4: warning: use yield in eval will not be supported in Ruby 3.`

Workaround is use a block parameter explicitly.

```
def foo &b
  eval('b.call')
end

foo{ p :ok }
```

# Implementation

Strategy is:

* [compile time] introduce `iseq::has_yield` field and check it if the iseq (or child iseq) contains `yield` (or something)
* [calling time] if block is given, check `iseq::has_yield` flag and show warning (or raise an exception)

https://gist.github.com/ko1/c9148ad0224bf5befa3cc76ed2220c0b

On this patch, now it raises an error to make it easy to detect.
It is easy to switch to show the warning.

# Evaluation and discussion

I tried to avoid ruby's tests.

https://gist.github.com/ko1/37483e7940cdc4390bf8eb0001883786

Here is a patch.

There are several patterns to avoid warnings.

## tests for `block_given?`, `Proc.new` (and similar) without block

Add a dummy block parameter.
It is test-specific issue.

## empty `each`

Some tests add `each` methods do not `yield`, like: `def each; end`.
Maybe test-specific issue, and adding a dummy block parameter.

## Subtyping / duck typing

https://github.com/ruby/ruby/blob/c01a5ee85e2d6a7128cccafb143bfa694284ca87/lib/optparse.rb#L698

This `parse` method doesn't use `yield`, but other sub-type's `parse` methods use.

## `super` with `new` method

https://gist.github.com/ko1/37483e7940cdc4390bf8eb0001883786#file-tests-patch-L61

This method override `Class#new` method and introduce a hook with block (yield a block in this hook code).

https://github.com/ruby/ruby/blob/trunk/lib/rubygems/package/tar_writer.rb#L81

In this method, call `super` and it also passing a block. However, called `initialize` doesn't use a block.

## Change robustness

This change reduce robustness for API change.

`Delegator` requires to support `__getobj__` for client classes.
Now `__getobj__` should accept block but most of `__getobj__` clients do not call given block.

https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L80

This is because of delegator.rb's API change.

https://gist.github.com/ko1/37483e7940cdc4390bf8eb0001883786#file-tests-patch-L86

Nobu says calling block is not required (ignoring a block is no problem) so it is not a bug for delegator client classes.

## Found issues.

```
[ 2945/20449] Rinda::TestRingServer#test_do_reply = 0.00 s
  1) Error:
Rinda::TestRingServer#test_do_reply:
ArgumentError: passing block to the method "with_timeout" (defined at /home/ko1/src/ruby/trunk/test/rinda/test_rinda.rb:787) is never used.
    /home/ko1/src/ruby/trunk/test/rinda/test_rinda.rb:635:in `test_do_reply'

[ 2946/20449] Rinda::TestRingServer#test_do_reply_local = 0.00 s
  2) Error:
Rinda::TestRingServer#test_do_reply_local:
ArgumentError: passing block to the method "with_timeout" (defined at /home/ko1/src/ruby/trunk/test/rinda/test_rinda.rb:787) is never used.
    /home/ko1/src/ruby/trunk/test/rinda/test_rinda.rb:657:in `test_do_reply_local'

[10024/20449] TestGemRequestSetGemDependencyAPI#test_platform_mswin = 0.01 s
  3) Error:
TestGemRequestSetGemDependencyAPI#test_platform_mswin:
ArgumentError: passing block to the method "util_set_arch" (defined at /home/ko1/src/ruby/trunk/lib/rubygems/test_case.rb:1053) is never used.
    /home/ko1/src/ruby/trunk/test/rubygems/test_gem_request_set_gem_dependency_api.rb:655:in `test_platform_mswin'

[10025/20449] TestGemRequestSetGemDependencyAPI#test_platforms = 0.01 s
  4) Error:
TestGemRequestSetGemDependencyAPI#test_platforms:
ArgumentError: passing block to the method "util_set_arch" (defined at /home/ko1/src/ruby/trunk/lib/rubygems/test_case.rb:1053) is never used.
    /home/ko1/src/ruby/trunk/test/rubygems/test_gem_request_set_gem_dependency_api.rb:711:in `test_platforms'
```

These 4 detection show the problem. `with_timeout` method (used in Rinda test) and `util_set_arch` method (used in Rubygems test) simply ignore the given block.
So these tests are simply ignored.

I reported them. (https://github.com/rubygems/rubygems/issues/2601)

## raise an error or show a warning?

At least, Ruby 2.7 should show warning for this kind of violation with `-w`.
How about for Ruby3?




-- 
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/postorius/lists/ruby-core.ml.ruby-lang.org/

In This Thread

Prev Next