Repository navigation
An inferred return type keeps undefined and null - #429
Merged
Merged
Conversation
…d T | null unions, and add tests for bare returns
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.
An inferred return type is the union of what the function returns, as in TypeScript, so
undefinedandnullare no longer dropped:if (c) return "a"; return undefined;string(=== undefinedfalse)string | undefinedif (c) return undefined; return "a";string | undefinedif (c) return "a"; return;stringstring | undefinedif (c) return 1; return undefined;s32s32 | undefinedif (c) return { v: "x" }; return undefined;{v: string}{v: string} | undefinedif (c) return "a"; return null;string | nullChange
processReturnTypefiltered out a laterreturn undefined/return null, andmergeTypeabsorbsundefinedinto the other type (canCastFromTo(undefined -> T)), per field of a tuple too. The newMLIRTypeHelper::mergeReturnTypemakes one side being undefined or null a union, and merges tuples field by field. It's used only byprocessReturnType, somergeType's other callers (array literals, conditionals, generic arguments) don't change.return;beside value returns addsundefinedto the inferred type (PassResult::hasBareReturn; never over an explicit return type), and in a function that returns a value it returnsundefined. A barereturnin a void function or a constructor is unchanged.next()returnsIterator<T>'s{value: T, done}, and a finished generator's value isT's zero by design (00generator_done_value_zero.ts). Making itT | undefinedbroke everyIterator<T>match (56 tests).next()is markedInternalFlags::GeneratorNextand goes throughPassResult::iteratorResult.Visible in DefaultLib
reduce/reduceRight(inferred return type,return undefinedon an empty array with no initial value) now returnV | undefined. Their call sites still work: assigning tonumber, printing, arithmetic, and an empty array really givesundefined. But arithmetic on the result warnscasting from optional (undefined) value. TypeScript declaresreduceas returningV; declaring: Vthere in DefaultLib would keep the old signature.Not changed
function f(c) { if (c) return "a"; }) still infersstring. It needs a reachability check, and getting it wrong would add| undefinedto functions that always return.value(see above).Tests
00return_inference_undefined.ts(compile + jit): both orders, barereturn, integer, object,null, a void function with a bare return, a declaredT | undefined, a constructor.-mm=own.🤖 Generated with Claude Code