bug: a bare list or dict annotation loses the element type, so dict keys stop deduplicating
- Lingua principale
- Rust
- Stelle
- 22
- Fork
- 6
- Merge medio
- 1g 3h
- PR unite (30g)
- 6
Descrizione
## Description
A bare `list` or `dict` annotation carries no element type, so a function annotated `-> list` loses it. Downstream, string values from that list compare as untyped words rather than by content, and a dict keyed on them silently fails to deduplicate. Compilation reports success and the count is wrong.
The parameterised forms (`List[str]`, `Dict[str, int]`) work correctly, so this is about what happens when the bare form is used, which is ordinary Python and increasingly the common style since PEP 585.
## Steps to Reproduce
```python
def tokens_bare() -> list:
out = []
for w in "the cat the".split():
out.append(w)
return out
def unique_bare() -> int:
counts = {}
for w in tokens_bare():
counts[w] = 1
return len(counts)
```
`unique_bare()` answers `3`. CPython answers `2`, because `"the"` appears twice.
Changing only the annotation to `-> List[str]` makes the same program answer `2`.
## Environment
- waspy 0.15.0, commit d7dcbc4
- Verified under both `wasmi` (the test harness) and Node 22
## Additional Context
The bare builtin annotations map to collection types whose element type is `Unknown`: https://github.com/anistark/waspy/blob/d7dcbc4/src/ir/converter.rs#L965-L975
Dict keys compare by content only when the key type is known to be a string; with `Unknown` on both sides the comparison falls back to the raw slot word, which for two equal strings built separately is two different offsets.
Two ways out, and they are not exclusive:
1. Infer an element type through a bare annotation where the body makes it clear (the return expression's element type, or what `append` puts in), the way collection fields already get theirs.
2. Say plainly in `README.md` that the parameterised form is required for element types to be known, and reject or warn on a bare annotation whose element type later matters.
The second is the smaller change and would at least stop the wrong answer being silent. `examples/text_report.py` uses the parameterised form for exactly this reason and documents why: https://github.com/anistark/waspy/blob/d7dcbc4/examples/text_report.py
Guida per i contributori
Apri la guida per i contributori
Valutazione
Questa issue non è ancora stata valutata.