Diagnostic and cause
LinkDuplicate key in map literal
- Family
- topaz
- Phase
- check
- Code
TPZ5602
Cause and conditions
Two map-literal keys with identical literal spelling are rejected with TPZ5602 at the repeated key. Different string keys are accepted.
Related rule: Repeated literal map keyResponse
Remove the repeated entry or use a different key. Combine values before constructing the map when both entries describe the same key.
Source evidence
- compiler/crates/topaz_diag/src/explain.rs at
151141d7bb44· 893-905 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
151141d7bb44· 787-787 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
17711dcdbc9b· 777-795
Compiler phase: check.
Compiler explanation: A map literal contains the same constant key more than once.
Message templates. The marker ⟨value⟩ stands for text computed for the reported program.
- duplicate key in
map { … }literal Source
Observed for the candidate in Topaz 5.21.3. Execution
- error[TPZ5602]: duplicate key in
map { … }literal
Two map-literal keys with identical literal spelling are rejected with TPZ5602 at the repeated key. Different string keys are accepted.
Repeated literal map key
Two map-literal keys with identical literal spelling are rejected with TPZ5602 at the repeated key. Different string keys are accepted.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
17711dcdbc9b· 777-795
Response
LinkRemove the repeated entry or use a different key. Combine values before constructing the map when both entries describe the same key.
Reproduction and verification
LinkRejected program
Linklet values = map { "a": 1, "a": 2 }
No stdout.
Expected result
Expected result
Rejection during check. Nonzero exit.
TPZ5602repeated-identical-string-key-in-map-literal atexample.tpz:1:28
The SELF check compares the literal kind and source spelling. This example does not claim static detection of equal values written with different spellings.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
17711dcdbc9b· 777-795
Repaired program
Linklet values = map { "a": 1, "b": 2 }
Expected result
Expected result
Checking accepts the program. Exit code 0.
The SELF check compares the literal kind and source spelling. This example does not claim static detection of equal values written with different spellings.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
17711dcdbc9b· 777-795