`ReadonlySet<T>` and `ReadonlyMap<K, V>` are not mapped to `set` / `map`

Aperta Adatta ai principianti
#308 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
typescript

Direzione di ricerca

Inizia in tools/src/types.ts, da tyFromTypeNode (intorno alla riga 199), poi verifica se esiste un percorso di analisi dei tipi basato su stringhe, come suggerisce l’issue. Confronta come viene mappato ReadonlyArray con i casi esistenti Set, Map e Record. Il lavoro è completo quando i parametri di ReadonlySet e ReadonlyMap vengono mappati ai tipi set e map di Dafny e gli esempi nell’issue vengono generati e verificati correttamente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Version: lsc 0.6.10 (npm), Dafny 4.11. Still present on main at 93283ec, tools/src/types.ts, tyFromTypeNode (around line 199).

ReadonlyArray<T> and readonly T[] become seq<T>. Set<T> becomes set<T>, and Map<K, V> and Record<K, V> become map<K, V>. ReadonlySet and ReadonlyMap fall through to a user type, so the usual read-only operations fail:

//@ backend dafny
export function known(names: ReadonlySet<string>, n: string): boolean {
  //@ verify
  //@ ensures \result === names.has(n)
  return names.has(n)
}
ERROR: Dafny emission failed for 'known' in i7.ts: Unsupported Dafny method call: .has() on user
export function copy(s: ReadonlySet<string>): Set<string> {
  //@ verify
  return new Set(s)
}
ERROR: new Set(...) expects an array argument (got user)
export function lookup(m: ReadonlyMap<string, number>, k: string): number {
  //@ verify
  return m.get(k) ?? 0
}
ERROR: nullish reached transform — narrow should have rewritten it: {"kind":"nullish","left":{"kind":"call","fn":{"kind":"field","obj":{"kind":"var","name":"m","ty":{"kind":"user","name":"ReadonlyMap<string, number>"}}, ...

Reading only .size gets past emission. lsc then expands the lib interface into a datatype that does not parse:

type forEach(==)
type has(==)
...
datatype ReadonlySet = ReadonlySet(forEach: forEach, has: has, size: int, entries: entries, keys: keys, values: values, i__@iterator@9: i__)

function size(s: ReadonlySet<string>): int
i7d.dfy(15,123): Error: closeparen expected
i7d.dfy(15,134): Error: invalid TopDecl

ReadonlySet<T> is Set<T> without the mutators, and ReadonlyMap<K, V> is Map<K, V> without them. Verified code is pure, so it already can't call add / set / delete. These are the natural parameter types for it.

Suggested fix: in tyFromTypeNode, and in the string-based type path if there is one, treat ReadonlySet like Set and ReadonlyMap like Map, as ReadonlyArray is already treated like Array:

if ((name === "Set" || name === "ReadonlySet") && args.length === 1) return { kind: "set", ... };
if ((name === "Map" || name === "ReadonlyMap" || name === "Record") && args.length === 2) ...

Workaround: declare the parameter as Set<T> / Map<K, V>. With Set<string>, the known function above verifies, but callers that hold a ReadonlySet then need a cast.

Lingua principale
TypeScript
Stelle
113
Fork
13
Merge medio
16h 22m
PR unite (30g)
40

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di midspiral/LemmaScript

Tutte le issue di midspiral/LemmaScript

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.