Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/kernel#warn uplevel #2442

Merged
3 commits merged into from
Oct 25, 2018
Merged

Bug/kernel#warn uplevel #2442

3 commits merged into from
Oct 25, 2018

Conversation

nobu
Copy link
Contributor

@nobu nobu commented Oct 22, 2018

Description:

With this script w.rb:

require "Win32API"

The expected line is shown when rubygems is disabled.

$ ruby -w --disable=gems -I./lib -I./ext/win32/lib w.rb 
w.rb:1: warning: Win32API is deprecated after Ruby 1.9.1; use fiddle directly instead

But kernel_require.rb is shown when rubygems is enabled.

$ ruby -w --enable=gems -I./lib -I./ext/win32/lib w.rb 
/opt/local/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54: warning: Win32API is deprecated after Ruby 1.9.1; use fiddle directly instead

Tasks:

  • Describe the problem / feature
  • Write tests
  • Write code to solve the problem
  • Get code review from coworkers / friends

I will abide by the code of conduct.

.travis.yml Outdated
@@ -8,6 +8,7 @@ branches:
- trying
- /^[\d.]+$/
- /.+-stable$/
- /^bug\//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding this shouldn't be necessary, since you're making a PR into a branch that's already tested

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fork. Fork. Fork. Nothing is nicer than working on a PR in one's fork and discovering that you can't run a test on it because the branch name isn't allowed.

Also, I think it's also used in another popular repo, one that Nobu and some others here occasionally contribute to...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the last commit is unnecessary, and I'll remove it.
It is just to run CIs before creating this PR.

appveyor.yml Outdated
@@ -4,6 +4,7 @@ branches:
- master
- auto
- /[\d.]+/
- /^bug/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Copy link
Member

@duckinator duckinator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for the PR!

If you could revert 7506506 (which made unnecessary CI changes) and address the comments I made, that'd be fantastic. 🙂

Let me know if you need clarifications on any of my comments, or if you need any help!

lib/rubygems/core_ext/kernel_warn.rb Show resolved Hide resolved
lib/rubygems/core_ext/kernel_warn.rb Show resolved Hide resolved

module_function define_method(:warn) {|*messages, uplevel: nil|
if uplevel
uplevel, = [uplevel].pack("l!").unpack("l!")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this even doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting uplevel to an Integer which fits long, with calling to_int if needed.
There is no such method right now.

start = 0
begin
loc, = caller_locations(start, 1)
break start += uplevel unless loc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit more readable:

unless loc
  start += uplevel
  break start
end

loc, = caller_locations(start, 1)
break start += uplevel unless loc
start += 1
end while (loc.path.start_with?(path) or (uplevel -= 1) >= 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things with how this loop is done:

  1. loop do/end and a conditional break is generally preferable over begin/end while ....
  2. Please avoid assignments in conditions, especially complicated conditions like this one.

lib/rubygems/core_ext/kernel_warn.rb Show resolved Hide resolved
lib/rubygems/core_ext/kernel_warn.rb Show resolved Hide resolved
* lib/rubygems/core_ext/kernel_warn.rb (Kernel#warn): skip
  kernel_require.rb's frames when `uplevel` option is given.
* added comments
* prefer `while` over `begin`/`end while`
* prefer guard clauase over `if`..`else`
* avoid assignments in conditions
@duckinator
Copy link
Member

Thanks for the PR, @nobu!

@bundlerbot r+

ghost pushed a commit that referenced this pull request Oct 25, 2018
2442: Bug/kernel#warn uplevel r=duckinator a=nobu

# Description:

With this script `w.rb`:
```ruby
require "Win32API"
```

The expected line is shown when rubygems is disabled.
```
$ ruby -w --disable=gems -I./lib -I./ext/win32/lib w.rb 
w.rb:1: warning: Win32API is deprecated after Ruby 1.9.1; use fiddle directly instead
```

But `kernel_require.rb` is shown when rubygems is enabled.
```
$ ruby -w --enable=gems -I./lib -I./ext/win32/lib w.rb 
/opt/local/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54: warning: Win32API is deprecated after Ruby 1.9.1; use fiddle directly instead
```

______________

# Tasks:

- [x] Describe the problem / feature
- [x] Write tests
- [x] Write code to solve the problem
- [ ] Get code review from coworkers / friends

I will abide by the [code of conduct](https://github.com/rubygems/rubygems/blob/master/CODE_OF_CONDUCT.md).

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
@duckinator
Copy link
Member

@nobu by the way, thank you for taking the time to explain the parts of this PR I asked about. The .pack('l!').unpack('l!') and your approach to suppressing that warning are things I hadn't encountered previously, and I wanted to at least understand what they did before merging it.

@ghost
Copy link

ghost commented Oct 25, 2018

Build succeeded

@ghost ghost merged commit 98b351d into rubygems:master Oct 25, 2018
@nobu nobu deleted the bug/Kernel#warn-uplevel branch October 26, 2018 05:06
@MSP-Greg
Copy link
Contributor

@y3i12

I believe this PR/commit includes the file rubygems/core_ext/kernel_warn.rb, so the only way you could have a load error is if you're using a new version of rubygems.rb but an old version of the files in rubygems/core_ext?

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants