Dafny: a `string | readonly string[]` value passed where that same union is expected is wrapped again in `NonArrayBranch`; `.includes()` on the narrowed array fails

Aperta
#306 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à
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
56/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
typescript

Direzione di ricerca

Start in tools/src/resolve.ts at coerceToTargetTy, as the issue identifies that function as the source of the repeated union wrapping. Trace how narrowed array types are represented and how method calls are resolved in both function bodies and spec expressions. Done means passing an already-union-typed value preserves its type and narrowed-array calls such as .includes() work; the payload names no test file, so locate or add coverage for both failures.

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/resolve.ts, coerceToTargetTy (around line 200).

A JWT aud claim is one string or a list. lsc synthesizes ArrayOf_string_Or_string = ArrayBranch(arr: seq<string>) | NonArrayBranch(val: string) for it, and a function that takes the union and narrows it with Array.isArray verifies. Passing a value that already has the union type does not.

//@ backend dafny
export interface Claims { readonly aud: string | readonly string[] }

export function audiences(aud: string | readonly string[]): string[] {
  //@ verify
  if (Array.isArray(aud)) return [...aud]
  return [`${aud}`]
}

export function audienceCount(c: Claims): number {
  //@ verify
  return audiences(c.aud).length
}
datatype ArrayOf_string_Or_string = ArrayBranch(arr: seq<string>) | NonArrayBranch(val: string)
datatype Claims = Claims(aud: ArrayOf_string_Or_string)

function audienceCount(c: Claims): int
{
  |audiences(NonArrayBranch(c.aud))|
}
i5.dfy(19,30): Error: incorrect argument type for datatype constructor parameter 'val' (expected string, found ArrayOf_string_Or_string)

coerceToTargetTy wraps any value whose type is not array in NonArrayBranch when the target is a synth array-union. It doesn't check whether the value already has the target type. The same happens with const a = c.aud (var a := NonArrayBranch(c.aud)). It also happens inside if (Array.isArray(a)) for the narrowed array, which is emitted as audiences(NonArrayBranch(i_a_arr)).

Second gap: .includes() on the narrowed array. Element access and .length work after Array.isArray, but a method call is resolved against the union:

//@ backend dafny
export function has(aud: string | readonly string[], want: string): boolean {
  //@ verify
  //@ ensures Array.isArray(aud) ==> \result === aud.includes(want)
  if (Array.isArray(aud)) { const xs: readonly string[] = aud; return xs.includes(want) }
  return aud === want
}
ERROR: Dafny emission failed for 'has' in i5b.ts: Unsupported Dafny method call: .includes() on user

The body above is fine, because it copies into a typed local first. The ensures fails, and if (Array.isArray(aud)) return aud.includes(want) fails the same way in a body. typeof aud === "string" is not an alternative (see #266).

In TypeScript, all of this typechecks and narrows as expected.

Suggested fix:

  1. In coerceToTargetTy, return value unchanged when value.ty is already the synth union (value.ty.kind === "user" && value.ty.name === targetTy.name). Use the narrowed binder's type (seq<T> / U) when the value is a narrowed path.
  2. Give the narrowed binder its array type when lowering method calls, so .includes, .some and the others resolve, in spec expressions too.

Workaround: keep the union out of verified signatures. Normalise aud to readonly string[] in unverified code, and verify functions over the array. The local-copy trick above covers .includes() in bodies, but not in contracts.

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.