Project

General

Profile

Actions

Bug #14083

closed

Refinement in block calling incorrect method

Added by bjfish (Brandon Fish) almost 7 years ago. Updated over 1 year ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:83681]
Tags:

Description

Tested on ruby versions 2.3.4 and 2.4.1

When a refinement is used inside a block, the scope of the refinement is not ending when the block has ended. The following example illustrates the issue:

Example:

class Example
  def test
    puts "Example#test"
  end
end

module M1
  refine Example do
    def test
      puts "Example#test in M1"
    end
  end
end

module M2
  refine Example do
    def test
      puts "Example#test in M2"
    end
  end
end

e = Example.new
[M1, M2].each { |r| 
  e.test
  using r
  e.test 
}

Actual Output

Example#test
Example#test in M1
Example#test in M1
Example#test in M2

Expected output

Example#test
Example#test in M1
Example#test
Example#test in M2

Related issues 2 (0 open2 closed)

Related to Ruby master - Bug #11704: Refinements only get "used" once in loopRejectedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Bug #18572: Performance regression when invoking refined methodsClosedko1 (Koichi Sasada)Actions
Actions #1

Updated by jeremyevans0 (Jeremy Evans) about 3 years ago

  • Related to Bug #11704: Refinements only get "used" once in loop added

Updated by alanwu (Alan Wu) almost 2 years ago

  • Status changed from Open to Rejected

This is working as intended. Scoping rules for refinements is similar
to the scoping rules for constant resolution. The scope only
changes when one uses the class/module keyword. In the posted
example the two using calls act on the same scope; the refinement
scope does not end when the block scope ends.

See also: https://bugs.ruby-lang.org/issues/11779#note-31

Updated by Eregon (Benoit Daloze) over 1 year ago

My reading of https://bugs.ruby-lang.org/projects/ruby-master/wiki/RefinementsSpec#Scope-of-refinements is that the expected output here is:

A refinement is activated in a certain scope.The scope of a refinement is lexical in the sense that, when control is transferred outside the scope (e.g., by an invocation of a method defined outside the scope, by load/require, etc...), the refinement is deactivated.In the body of a method defined in a scope where a refinement is activated, the refinement is activated even if the method is invoked outside the scope.

Given the scope is limited by class/module keywords like constant scopes (https://bugs.ruby-lang.org/issues/11779#note-31), it should be:

Example#test
Example#test in M1
Example#test
Example#test in M1

which is what TruffleRuby does currently.
Seeing both M1 and M2 from the same call site wouldn't be lexical.

The reason we don't see M2 there is changing the refinements for a scope after there have been calls is AFAIK an incorrect usages of refinements (ideally using would raise for such a case, but it might be difficult to detect).
In other words, refinements at a given call site must always be the same and so it's enough to consider refinements during the initial lookup for the cache and not after.
This is the key point after the very long discussion on the mailing list about the original design of refinements (at least that's what I recall from it), they must not have dynamic rebinding so they don't have extra cost (e.g. on non-refined method calls when in a scope with some refinements activated, and also obviously nobody wants to check if there are active refinements at every call site), the refinements for a given call site should be fixed and never change. If they change, it's an incorrect usage and it should be fair to just ignore the change (what TruffleRuby does, or even better to raise an error in that case).

In practice, I believe real usages of using are only early at the top-level, much like require, and maybe sometimes at the beginning of a module/class body.
Both of these are fine and can't run into this problem (well, except if they meant to refine require but then it's only natural to call using before require).

@shugo (Shugo Maeda) What do you think about this?
I think we should try to raise an error for using in such invalid cases.
That would make it much easier to fix #18572 on CRuby and allow simplifying the implementation of refinements on CRuby (e.g., TruffleRuby doesn't track if a method has refinements).

Actions #4

Updated by Eregon (Benoit Daloze) over 1 year ago

  • Related to Bug #18572: Performance regression when invoking refined methods added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0