Project

General

Profile

Bug #12884

Using a HashWithIndifferentAccess with a default value in a function with a keyword parameter converts it to a Hash.

Added by mderelle (Mathieu Derelle) over 2 years ago. Updated about 2 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:77813]

Description

A strange bug that broke our production when migrating from 2.1.9 to 2.2.5 (still present in 2.3.1)
It involves HashWithIndifferentAccess from active support. The version used is 3.2.22.4 (against all ruby versions)

Here's a bit of code to reproduce it :

def test(params = HashWithIndifferentAccess.new, disrupter: nil)
    params.class
end

x = HashWithIndifferentAccess.new
x[:foo] = 'bar'
test x
# 2.3.1 => Hash
# 2.2.5 => Hash
# 2.1.9 => HashWithIndifferentAccess

But this only happens if there is a keyword parameter AND a default value for your HashWithIndifferentAccess parameter :

Not using a keyword parameter doesn't change the type

def test(params = HashWithIndifferentAccess.new, disrupter = nil)
    params.class
end

x = HashWithIndifferentAccess.new
x[:foo] = 'bar'
test x
# 2.3.1 => HashWithIndifferentAccess
# 2.2.5 => HashWithIndifferentAccess
# 2.1.9 => HashWithIndifferentAccess

Not using a default value don't change the type

def test(params, disrupter: nil)
    params.class
end

x = HashWithIndifferentAccess.new
x[:foo] = 'bar'
test x
# 2.3.1 => HashWithIndifferentAccess
# 2.2.5 => HashWithIndifferentAccess
# 2.1.9 => HashWithIndifferentAccess

Related issues

Is duplicate of Ruby trunk - Bug #12821: Object converted to Hash unexpectedly under certain method callClosedActions

Associated revisions

Revision 13dffd8a
Added by nobu (Nobuyoshi Nakada) over 2 years ago

class.c: non-keyword hash class

  • class.c (rb_extract_keywords): keep the class of non-keyword elements hash as the original. [ruby-core:77813] [Bug #12884]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57360 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 57360
Added by nobu (Nobuyoshi Nakada) over 2 years ago

class.c: non-keyword hash class

  • class.c (rb_extract_keywords): keep the class of non-keyword elements hash as the original. [ruby-core:77813] [Bug #12884]

Revision 57360
Added by nobu (Nobuyoshi Nakada) over 2 years ago

class.c: non-keyword hash class

  • class.c (rb_extract_keywords): keep the class of non-keyword elements hash as the original. [ruby-core:77813] [Bug #12884]

Revision 57360
Added by nobu (Nobuyoshi Nakada) over 2 years ago

class.c: non-keyword hash class

  • class.c (rb_extract_keywords): keep the class of non-keyword elements hash as the original. [ruby-core:77813] [Bug #12884]

Revision 490691c6
Added by naruse (Yui NARUSE) over 2 years ago

merge revision(s) 57360: [Backport #12884]

    class.c: non-keyword hash class

    * class.c (rb_extract_keywords): keep the class of non-keyword
      elements hash as the original.  [ruby-core:77813] [Bug #12884]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@57861 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 57861
Added by naruse (Yui NARUSE) over 2 years ago

merge revision(s) 57360: [Backport #12884]

class.c: non-keyword hash class

* class.c (rb_extract_keywords): keep the class of non-keyword
  elements hash as the original.  [ruby-core:77813] [Bug #12884]

Revision d8b81e52
Added by usa (Usaku NAKAMURA) about 2 years ago

merge revision(s) 57360: [Backport #12884]

    class.c: non-keyword hash class

    * class.c (rb_extract_keywords): keep the class of non-keyword
      elements hash as the original.  [ruby-core:77813] [Bug #12884]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@58092 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 58092
Added by usa (Usaku NAKAMURA) about 2 years ago

merge revision(s) 57360: [Backport #12884]

class.c: non-keyword hash class

* class.c (rb_extract_keywords): keep the class of non-keyword
  elements hash as the original.  [ruby-core:77813] [Bug #12884]

Revision ba81af97
Added by nagachika (Tomoyuki Chikanaga) about 2 years ago

merge revision(s) 57360: [Backport #12884]

    class.c: non-keyword hash class

    * class.c (rb_extract_keywords): keep the class of non-keyword
      elements hash as the original.  [ruby-core:77813] [Bug #12884]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_3@58167 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 58167
Added by nagachika (Tomoyuki Chikanaga) about 2 years ago

merge revision(s) 57360: [Backport #12884]

class.c: non-keyword hash class

* class.c (rb_extract_keywords): keep the class of non-keyword
  elements hash as the original.  [ruby-core:77813] [Bug #12884]

History

Updated by shyouhei (Shyouhei Urabe) over 2 years ago

(Just to be clear) It has nothing to do with HWIA. I can reproduce the situation without it.

class X < Hash; end

def test(params = X.new, disrupter: nil)
  return params
end

x = X.new
x['foo'] = 'bar'
p test(x).class
#2

Updated by shyouhei (Shyouhei Urabe) over 2 years ago

  • Is duplicate of Bug #12821: Object converted to Hash unexpectedly under certain method call added
#3

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

  • Status changed from Open to Closed

Applied in changeset r57360.


class.c: non-keyword hash class

  • class.c (rb_extract_keywords): keep the class of non-keyword elements hash as the original. [ruby-core:77813] [Bug #12884]

Updated by naruse (Yui NARUSE) over 2 years ago

  • Backport changed from 2.1: DONTNEED, 2.2: REQUIRED, 2.3: REQUIRED to 2.1: DONTNEED, 2.2: REQUIRED, 2.3: REQUIRED, 2.4: DONE

ruby_2_4 r57861 merged revision(s) 57360.

Updated by usa (Usaku NAKAMURA) about 2 years ago

  • Backport changed from 2.1: DONTNEED, 2.2: REQUIRED, 2.3: REQUIRED, 2.4: DONE to 2.1: DONTNEED, 2.2: DONE, 2.3: REQUIRED, 2.4: DONE

ruby_2_2 r58092 merged revision(s) 57360.

Updated by nagachika (Tomoyuki Chikanaga) about 2 years ago

  • Backport changed from 2.1: DONTNEED, 2.2: DONE, 2.3: REQUIRED, 2.4: DONE to 2.1: DONTNEED, 2.2: DONE, 2.3: DONE, 2.4: DONE

ruby_2_3 r58167 merged revision(s) 57360.

Also available in: Atom PDF