Repository navigation
Falling off the end of a function returns undefined - #430
Merged
Merged
Conversation
…a";` is `string | undefined` A function whose end is reachable beside value returns inferred T, and falling off returned the return slot as it was left. bodyFallsOffEnd asks whether the end of the body can be reached - exact for return, throw, blocks, if/else, try/catch/finally, a switch with a default, and `while (true)` / `for (;;)` / `do .. while (true)` without a break out (BreakOutVisitor); anything else is taken to complete. Such a function infers T | undefined, and one returning T | undefined (inferred or declared), any or unknown returns undefined there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #429. A function that can fall off its end beside value returns still inferred
T, and falling off returned the return slot as it was left:Change
bodyFallsOffEndchecks whether the end of the body is reachable, by AST analysis (canCompleteNormally). It's exact forreturn,throw, blocks,if/else,try/catch/finally, aswitchwith adefault(the end of its last clause, or abreak), andwhile (true)/for (;;)/do .. while (true)without abreakthat leaves them (BreakOutVisitor: an unlabeled break outside nested loops/switches, or any labeled break). Any other statement is taken to complete. MLIRGen's IR can't answer this:ts.ReturnValsits insidets.Ifregions and isn't a terminator.undefinedto an inferred type when the end is reachable, as An inferred return type keepsundefinedandnull#429 does for a barereturn;. An explicit return type is never changed.T | undefined(inferred or declared),anyorunknownnow generatesreturn undefinedat a reachable end. ExplicitT | undefinedfunctions that fell off used to return garbage there.if (c) return "a";string | undefinedif (c) return "a"; else return "b";stringswitch (n) { case 1: return "one"; default: return "many"; }stringswitch (n) { case 1: return "one"; case 2: return "two"; }string | undefinedwhile (true) { if (n > 0) return n; n++; }numbertry { ...; throw 1; } catch { return 2; }s32if (c) return "x"; throw "e";stringfor (const k of a) { if (k == n) return k; }s32 | undefinedIf the analysis is unsure it says "reachable", which adds
| undefined, so a call into a function that never returns (where TypeScript would knownever) still counts as completing.Checked
src/lib.ts) have nooptionalresult after this change, so their API doesn't change. Generic ones are instantiated in user code;reduce/reduceRightbecameV | undefinedwith An inferred return type keepsundefinedandnull#429 already, and DefaultLib can I use it in a release environment yet #18 declares them: V.Tests
00return_falls_off_end.ts(compile + jit): each row above, plus a declarednumber | undefined.-mm=own.🤖 Generated with Claude Code