Introduction

Pattern matching, a powerful feature in many programming languages, allows developers to deconstruct complex data structures with elegance and precision. However, when it comes to dictionaries, this elegance can mask a critical issue: non-strict shape matching. Unlike sequence patterns, which demand an exact match, dictionary pattern matching in certain languages silently ignores unspecified keys. This behavior, while seemingly flexible, can lead to unexpected bugs and security vulnerabilities if developers assume strict shape enforcement.

To illustrate, consider a dictionary pattern match in a language like Python or Rust. If you write a pattern to match a dictionary with keys {'a', 'b'}, and the actual dictionary contains {'a', 'b', 'c'}, the match will succeed, and the key 'c' will be ignored. This might seem harmless, but it violates the developer’s expectation of a strict shape match, akin to what sequence patterns provide. The causal chain here is straightforward: impact (developer assumes strict matching) → internal process (language ignores unspecified keys) → observable effect (unexpected behavior or bugs).

The root of this issue lies in the design choice of prioritizing flexibility over strictness. Languages often default to this behavior to accommodate varying data shapes, but this comes at the cost of clarity and predictability. Compounding the problem is the lack of clear documentation or understanding of this behavior, leading developers to make incorrect assumptions based on their experience with sequence patterns.