[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-core
Subject:    [ruby-core:92867] [Ruby trunk Bug#15877] Incorrect constant lookup result in method on cloned class
From:       stephen.gregory () gmail ! com
Date:       2019-05-27 21:01:01
Message-ID: redmine.journal-78245.20190527210101.1d86f33bc4c12290 () ruby-lang ! org
[Download RAW message or body]

Issue #15877 has been updated by sag (Stephen Gregory).


I tested this on 2.5, 2.4 and the behavior is the same there.  Also this applies to \
subclasses as well. The first class READ determines the values for all of them.

``` ruby
class Unrelated
    TEST='UNRELATED'
    def test
      TEST
    end
end

class Foo
  def test
     TEST
   end
end
  
Bar2 = Foo.clone
Bar1 = Foo.clone
  

class Bar1
  TEST = 'bar-1'
end
class Bar2
  TEST = 'bar-2'
end
class Bar3 < Foo
  TEST = 'bar-3'
end

puts "Bar2 #{Bar2.new.test}"  # Bar2 bar-2
puts "Bar1 #{Bar1.new.test}"   # Bar1 bar-2
puts "Bar3 #{Bar3.new.test}"  # Bar3 bar-2
puts "Foo #{Foo.new.test}"     # Foo bar-2
puts "Unrelated #{Unrelated.new.test}"  #Unrelated UNRELATED
```

Interestingly if Bar3 is read first then you get:
```
sample/array_second.rb:12:in `test': uninitialized constant Foo::TEST (NameError)
	from sample/array_second.rb:30:in `<main>'
```




----------------------------------------
Bug #15877: Incorrect constant lookup result in method on cloned class
https://bugs.ruby-lang.org/issues/15877#change-78245

* Author: danielwaterworth (Daniel Waterworth)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 2.6.3
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
This behavior seems wrong to me:

``` ruby
class Foo
  def test
    TEST
  end
end

Bar1 = Foo.clone
Bar2 = Foo.clone

class Bar1
  TEST = 'bar-1'
end

class Bar2
  TEST = 'bar-2'
end

# If these two lines are reordered, 'bar-2' is produced each time
p [:bar1_method, Bar1.new.test] # outputs 'bar-1' (correct)
p [:bar2_method, Bar2.new.test] # outputs 'bar-1' (incorrect)

p [:bar1_const, Bar1::TEST] # outputs 'bar-1' (correct)
p [:bar2_const, Bar2::TEST] # outputs 'bar-2' (correct)
```

Possibly related to #9603, #7107



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic