Skip to content

Commit 2f75594

Browse files
committed
Fixes crash when inferring constrained variadic tuple types when source has insufficient fixed elements
1 parent 7f6a846 commit 2f75594

File tree

4 files changed

+539
-61
lines changed

4 files changed

+539
-61
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27352,8 +27352,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2735227352
const constraint = param && getBaseConstraintOfType(param);
2735327353
if (constraint && isTupleType(constraint) && !(constraint.target.combinedFlags & ElementFlags.Variable)) {
2735427354
const impliedArity = constraint.target.fixedLength;
27355-
inferFromTypes(sliceTupleType(source, startLength, sourceArity - (startLength + impliedArity)), elementTypes[startLength]);
27356-
inferFromTypes(getElementTypeOfSliceOfTupleType(source, startLength + impliedArity, endLength)!, elementTypes[startLength + 1]);
27355+
if (startLength + impliedArity <= source.target.fixedLength) {
27356+
inferFromTypes(sliceTupleType(source, startLength, sourceArity - (startLength + impliedArity)), elementTypes[startLength]);
27357+
inferFromTypes(getElementTypeOfSliceOfTupleType(source, startLength + impliedArity, endLength)!, elementTypes[startLength + 1]);
27358+
}
2735727359
}
2735827360
}
2735927361
else if (elementFlags[startLength] & ElementFlags.Rest && elementFlags[startLength + 1] & ElementFlags.Variadic) {
@@ -27363,12 +27365,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2736327365
const constraint = param && getBaseConstraintOfType(param);
2736427366
if (constraint && isTupleType(constraint) && !(constraint.target.combinedFlags & ElementFlags.Variable)) {
2736527367
const impliedArity = constraint.target.fixedLength;
27366-
const endIndex = sourceArity - getEndElementCount(target.target, ElementFlags.Fixed);
27367-
const startIndex = endIndex - impliedArity;
27368-
const trailingSlice = createTupleType(getTypeArguments(source).slice(startIndex, endIndex), source.target.elementFlags.slice(startIndex, endIndex), /*readonly*/ false, source.target.labeledElementDeclarations && source.target.labeledElementDeclarations.slice(startIndex, endIndex));
27368+
if (endLength + impliedArity <= getEndElementCount(source.target, ElementFlags.Fixed)) {
27369+
const endIndex = sourceArity - getEndElementCount(target.target, ElementFlags.Fixed);
27370+
const startIndex = endIndex - impliedArity;
27371+
const trailingSlice = createTupleType(getTypeArguments(source).slice(startIndex, endIndex), source.target.elementFlags.slice(startIndex, endIndex), /*readonly*/ false, source.target.labeledElementDeclarations && source.target.labeledElementDeclarations.slice(startIndex, endIndex));
2736927372

27370-
inferFromTypes(getElementTypeOfSliceOfTupleType(source, startLength, endLength + impliedArity)!, elementTypes[startLength]);
27371-
inferFromTypes(trailingSlice, elementTypes[startLength + 1]);
27373+
inferFromTypes(getElementTypeOfSliceOfTupleType(source, startLength, endLength + impliedArity)!, elementTypes[startLength]);
27374+
inferFromTypes(trailingSlice, elementTypes[startLength + 1]);
27375+
}
2737227376
}
2737327377
}
2737427378
}

0 commit comments

Comments
 (0)