ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "henryanvyl (Henry Tseng)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:109818] [Ruby master Bug#18990] Pattern matching unexpectedly raises "duplicated key name" error
Date: Thu, 01 Sep 2022 20:00:44 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-99057.20220901200044.52300@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-18990.20220901150605.52300@ruby-lang.org

Issue #18990 has been updated by henryanvyl (Henry Tseng).


Verifying that this occurs in ruby 3.1.0 but does not exist in 3.2.x

----------------------------------------
Bug #18990: Pattern matching unexpectedly raises "duplicated key name" error
https://bugs.ruby-lang.org/issues/18990#change-99057

* Author: zeke (Zeke Gabrielse)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin19]
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
I've found that sometimes, when matching nested patterns, I get an unexpected "duplicate key name" error but there's no duplicate key in any individual pattern. The "duplicated" key is actually in another pattern that's in scope, which I found was odd. (And the fix is even more odd.)

This seems like unexpected behavior to me. There's an example of this behavior below. The patterns in #all? cause the error, even though they're separate lines.

```ruby
class User
  attr_reader :first_name,
              :last_name

  def initialize(first_name:, last_name:)
    @first_name, @last_name = first_name, last_name
  end

  def deconstruct_keys(keys)
    { first_name:, last_name: }
  end
end

users = [
  User.new(first_name: 'Jane', last_name: 'Doe'),
  User.new(first_name: 'Jane', last_name: 'Smith'),
]

# Fails
case users
in [{ first_name: 'John', ** }, *]
  users.all? { _1 in first_name: 'John' }
in [{ first_name: 'Jane', ** }, *]
  users.all? { _1 in first_name: 'Jane' } # => bug.rb:17: duplicated key name
end
```

Counterintuitively, wrapping the pattern in {} resolves the error.

```ruby
# Works
case users
in [{ first_name: 'John', ** }, *]
  users.all? { _1 in { first_name: 'John' } }
in [{ first_name: 'Jane', ** }, *]
  users.all? { _1 in { first_name: 'Jane' } }
end
```

Seems like a bug?



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

  reply	other threads:[~2022-09-01 20:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-01 15:06 [ruby-core:109813] [Ruby master Bug#18990] Pattern matching unexpectedly raises "duplicated key name" error zeke (Zeke Gabrielse)
2022-09-01 20:00 ` henryanvyl (Henry Tseng) [this message]
2022-09-09  5:16 ` [ruby-core:109860] " ktsj (Kazuki Tsujimoto)
2022-09-10  7:12 ` [ruby-core:109868] " nagachika (Tomoyuki Chikanaga)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-99057.20220901200044.52300@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).