Dafny: a `string | readonly string[]` value passed where that same union is expected is wrapped again in `NonArrayBranch`; `.includes()` on the narrowed array fails
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
- Ambito
- backend-api-design, compilers
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:
- In
coerceToTargetTy, returnvalueunchanged whenvalue.tyis 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. - Give the narrowed binder its array type when lowering method calls, so
.includes,.someand 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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di midspiral/LemmaScript
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
midspiral/LemmaScript#308 ·
I maintainer di solito rispondono entro 1 giorno
-
Dafny: `toUpperCase`/`toLowerCase` are modeled as ASCII-only, so false contracts about non-ASCII text verifyForse già presa @quangng2000 l’ha presa 1 giorno fa. Aperta
midspiral/LemmaScript#312 · 1 commento · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 1-2 giorni Idoneità per principianti 56/100
midspiral/LemmaScript#311 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 48/100
midspiral/LemmaScript#310 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 67/100
midspiral/LemmaScript#309 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di midspiral/LemmaScript
Issue simili
-
area: batch
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
inabagumi/shinju-date#7294 ·
I maintainer di solito rispondono entro 1 giorno
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
streaming-video-technology-alliance/common-media-library#700 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
vercel-labs/ui#809 ·
I maintainer di solito rispondono entro 1 giorno
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 67/100
KrisCris/village-in-the-shade-guide#75 ·
I maintainer di solito rispondono entro 1 giorno
-
bot:ai-assisted component:platform-js status:untriaged
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 74/100
midnightntwrk/platform-js#38 ·