From: "ktsj (Kazuki Tsujimoto)" Date: 2022-09-09T05:16:07+00:00 Subject: [ruby-core:109860] [Ruby master Bug#18990] Pattern matching unexpectedly raises "duplicated key name" error Issue #18990 has been updated by ktsj (Kazuki Tsujimoto). Backport changed from 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN to 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: REQUIRED @zeke Thanks for reporting. ---------------------------------------- Bug #18990: Pattern matching unexpectedly raises "duplicated key name" error https://bugs.ruby-lang.org/issues/18990#change-99099 * Author: zeke (Zeke Gabrielse) * Status: Closed * 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: REQUIRED ---------------------------------------- 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/ Unsubscribe: