An early return on an optional chain or a field path does not narrow the type in resolve, so nested optional fields stay wrapped
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 62/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- typescript
- Ambito
- compilers
Direzione di ricerca
Start with collectEarlyReturnNarrowings and negatedVarPresence in tools/src/resolve.ts, then compare them with ruleEarlyReturnOptChainCompare in tools/src/narrow.ts and noneDetector in tools/src/condition-facts.ts. Run the two reproduction functions through the toolchain and check the emitted Dafny. Done when the nested inner reads come out as int in both functions and the early-return narrowing for d.b is recorded in resolve, with the PR #345 condition applied.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Problem
After an early return on an optional chain or a field path, such as if (b?.tag !== 1) return 0; or if (d.b === undefined) return 0;, narrow unwraps the optional for the rest of the block, but resolve keeps its Option type. A later presence check on a nested optional field then does not narrow that field, and its value is returned still wrapped in Option. Dafny rejects the generated code.
Reproduced on upstream main at 2ccd0a5 (0.6.11) with Dafny 4.11.0.
Reproduction
export interface Box {
tag: number;
inner?: number;
}
export interface Deep {
b?: Box;
}
export function optChainGuardThenNestedOpt(b: Box | undefined): number {
if (b?.tag !== 1) return 0;
if (b.inner !== undefined) {
return b.inner;
}
return 0;
}
export function fieldGuardThenBindNestedOpt(d: Deep): number {
if (d.b === undefined) return 0;
const x = d.b;
if (x.inner !== undefined) {
return x.inner;
}
return 0;
}
emits
function optChainGuardThenNestedOpt(b: Option<Box>): int
{
match b {
case Some(i_b_val) =>
if (i_b_val.tag != 1) then
0
else
if (i_b_val.inner != None) then
i_b_val.inner
else
0
case None =>
0
}
}
function fieldGuardThenBindNestedOpt(d: Deep): int
{
match d.b {
case Some(i_d_b_val) =>
var x := i_d_b_val;
if (x.inner != None) then
x.inner
else
0
case None =>
0
}
}
i_b_val.inner and x.inner are returned as Option<int>, and Dafny reports Error: the two branches of an if-then-else expression must have the same type (got int and Option<int>) and Error: Function body type mismatch (expected int, got Option<int>).
Expected
After if (b?.tag !== 1) return 0;, resolve should treat b as Box for the rest of the block, as narrow already does. After if (d.b === undefined) return 0;, it should record d.b as narrowed to Box, as it already does inside if (d.b !== undefined) { ... }.
Likely cause
In tools/src/resolve.ts, collectEarlyReturnNarrowings uses negatedVarPresence, which only accepts a bare variable, so it misses both forms. resolveBlock threads narrowed paths across early returns for discriminant checks but not for field-path presence checks. Narrow handles both forms (ruleEarlyReturnOptChainCompare in tools/src/narrow.ts and noneDetector in tools/src/condition-facts.ts), so the two passes disagree about the types.
Related
- #268: narrowing within an
&&chain. - #339: presence checks in a
whilecondition. - #340: assigning a defined value to an optional.
- PR #345 restricts the
opt?.chain !== rhsearly-return rule in narrow to arhsthat cannot be undefined. Resolve should apply the same condition.
- Lingua principale
- TypeScript
- Stelle
- 113
- Fork
- 13
- Merge medio
- 15h 35m
- PR unite (30g)
- 56
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 72/100
midspiral/LemmaScript#355 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
In a pure function body, a `const` declared inside an `if` branch stays in scope after the `if`, so a false `ensures` can verifyForse già presa @namin l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
midspiral/LemmaScript#362 ·
I maintainer di solito rispondono entro 1 giorno
-
A `const` or `let` inside a bare `{ }` block stays in scope after the block, so a false `ensures` can verifyForse già presa @namin l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 15/100
midspiral/LemmaScript#361 ·
I maintainer di solito rispondono entro 1 giorno
-
A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verifyForse già presa @namin l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 18/100
midspiral/LemmaScript#360 ·
I maintainer di solito rispondono entro 1 giorno
-
An assignment used as an expression, such as `(x = 7);` or `const y = (x = 7)`, is extracted as an equality test, so a false `ensures` can verifyForse già presa @namin l’ha presa oggi. Aperta
Difficoltà 3/5 Mezza giornata Idoneità per principianti 30/100
midspiral/LemmaScript#356 · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di midspiral/LemmaScript
Issue simili
-
bug done
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
neavo/LinguaGacha#1124 ·
I maintainer di solito rispondono entro 1 giorno
-
[fullsend] E2E: backstage-microsoft-auth — catalogQuery missing error handler for network errorsForse già presa @fullsend-ai-coder l’ha presa oggi. Apertae2e-failure ready-to-code
Difficoltà 2/5 Meno di un'ora Idoneità per principianti 78/100
redhat-developer/rhdh-plugin-export-overlays#4821 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
brylie/mission-island#225 ·
I maintainer di solito rispondono entro 1 giorno
-
status/need-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
google-gemini/gemini-cli#30412 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno