-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Avoid dependent parameters narrowings if any declared symbol of the parameter is assigned to #56313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
982f99a
72b5004
6e7aa4d
0ccedd6
a02be27
fc368df
bbda1ed
f82fbb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27252,7 +27252,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
case SyntaxKind.Identifier: | ||
if (!isThisInTypeQuery(node)) { | ||
const symbol = getResolvedSymbol(node as Identifier); | ||
return isConstantVariable(symbol) || isParameterOrCatchClauseVariable(symbol) && !isSymbolAssigned(symbol); | ||
return isConstantVariable(symbol) || isParameterOrCatchClauseVariable(symbol) && !isSomeSymbolAssigned(getRootDeclaration(symbol.valueDeclaration!)); | ||
} | ||
break; | ||
case SyntaxKind.PropertyAccessExpression: | ||
|
@@ -28527,37 +28527,33 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
node.kind === SyntaxKind.PropertyDeclaration)!; | ||
} | ||
|
||
// Check if a parameter or catch variable is assigned anywhere | ||
function isSymbolAssigned(symbol: Symbol) { | ||
if (!symbol.valueDeclaration) { | ||
return false; | ||
} | ||
const parent = getRootDeclaration(symbol.valueDeclaration).parent; | ||
function isSomeSymbolAssigned(rootDeclaration: Node) { | ||
const parent = rootDeclaration.parent; | ||
const links = getNodeLinks(parent); | ||
if (!(links.flags & NodeCheckFlags.AssignmentsMarked)) { | ||
links.flags |= NodeCheckFlags.AssignmentsMarked; | ||
if (!hasParentWithAssignmentsMarked(parent)) { | ||
markNodeAssignments(parent); | ||
} | ||
} | ||
return symbol.isAssigned || false; | ||
return getNodeLinks(rootDeclaration).someSymbolAssigned; | ||
} | ||
|
||
function hasParentWithAssignmentsMarked(node: Node) { | ||
return !!findAncestor(node.parent, node => (isFunctionLike(node) || isCatchClause(node)) && !!(getNodeLinks(node).flags & NodeCheckFlags.AssignmentsMarked)); | ||
} | ||
|
||
function markNodeAssignments(node: Node) { | ||
function markNodeAssignments(node: Node): true | undefined { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is used by That being said, I probably need to mark both individual symbols as assigned or not together with the aggregate information on the root declaration to fix the regression found here. A minimal repro case for this regression: declare function useRouter(): {
push: (href: string) => void;
}
type SidebarFooterButtonProps = {
href?: string;
onClick?: () => void;
};
export const SidebarFooterButton = ({
href,
onClick,
}: SidebarFooterButtonProps): JSX.Element => {
const router = useRouter();
if (href !== undefined) {
onClick = () => {
void router.push(href);
};
}
return (
<button type="button" onClick={onClick} />
);
}; We still want to narrow "const" parameters and only turn off the dependent parameter narrowing if some of the relevant symbols are reassigned - so we need to keep track of both (or well, derive the aggregate information on demand from the root declaration's symbols) So If I do any of those, this confusing |
||
if (node.kind === SyntaxKind.Identifier) { | ||
if (isAssignmentTarget(node)) { | ||
const symbol = getResolvedSymbol(node as Identifier); | ||
if (isParameterOrCatchClauseVariable(symbol)) { | ||
symbol.isAssigned = true; | ||
return getNodeLinks(getRootDeclaration(symbol.valueDeclaration!)).someSymbolAssigned = true; | ||
} | ||
} | ||
} | ||
else { | ||
forEachChild(node, markNodeAssignments); | ||
return forEachChild(node, markNodeAssignments); | ||
} | ||
} | ||
|
||
|
@@ -28726,7 +28722,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
const parentType = getTypeForBindingElementParent(parent, CheckMode.Normal); | ||
const parentTypeConstraint = parentType && mapType(parentType, getBaseConstraintOrType); | ||
links.flags &= ~NodeCheckFlags.InCheckIdentifier; | ||
if (parentTypeConstraint && parentTypeConstraint.flags & TypeFlags.Union && !(rootDeclaration.kind === SyntaxKind.Parameter && isSymbolAssigned(symbol))) { | ||
if (parentTypeConstraint && parentTypeConstraint.flags & TypeFlags.Union && !(rootDeclaration.kind === SyntaxKind.Parameter && isSomeSymbolAssigned(rootDeclaration))) { | ||
const pattern = declaration.parent; | ||
const narrowedType = getFlowTypeOfReference(pattern, parentTypeConstraint, parentTypeConstraint, /*flowContainer*/ undefined, location.flowNode); | ||
if (narrowedType.flags & TypeFlags.Never) { | ||
|
@@ -28766,7 +28762,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
const contextualSignature = getContextualSignature(func); | ||
if (contextualSignature && contextualSignature.parameters.length === 1 && signatureHasRestParameter(contextualSignature)) { | ||
const restType = getReducedApparentType(instantiateType(getTypeOfSymbol(contextualSignature.parameters[0]), getInferenceContext(func)?.nonFixingMapper)); | ||
if (restType.flags & TypeFlags.Union && everyType(restType, isTupleType) && !isSymbolAssigned(symbol)) { | ||
if (restType.flags & TypeFlags.Union && everyType(restType, isTupleType) && !isSomeSymbolAssigned(declaration)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this case here is not fixed by aggregating const test2: (...args: [1, 2] | [3, 4]) => void = (x, y) => {
if (Math.random()) {
y = 2;
}
if (y === 2) {
x
// ^? (parameter) x: 1
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A great catch! The "dependent root declaration" might come from the context... I'll have to think about this and recheck how this kind of narrowing is handled today. |
||
const narrowedType = getFlowTypeOfReference(func, restType, restType, /*flowContainer*/ undefined, location.flowNode); | ||
const index = func.parameters.indexOf(declaration) - (getThisParameter(func) ? 1 : 0); | ||
return getIndexedAccessType(narrowedType, getNumberLiteralType(index)); | ||
|
@@ -28907,7 +28903,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
// The declaration container is the innermost function that encloses the declaration of the variable | ||
// or parameter. The flow container is the innermost function starting with which we analyze the control | ||
// flow graph to determine the control flow based type. | ||
const isParameter = getRootDeclaration(declaration).kind === SyntaxKind.Parameter; | ||
const rootDeclaration = getRootDeclaration(declaration); | ||
const isParameter = rootDeclaration.kind === SyntaxKind.Parameter; | ||
const declarationContainer = getControlFlowContainer(declaration); | ||
let flowContainer = getControlFlowContainer(node); | ||
const isOuterVariable = flowContainer !== declarationContainer; | ||
|
@@ -28921,7 +28918,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
while ( | ||
flowContainer !== declarationContainer && (flowContainer.kind === SyntaxKind.FunctionExpression || | ||
flowContainer.kind === SyntaxKind.ArrowFunction || isObjectLiteralOrClassExpressionMethodOrAccessor(flowContainer)) && | ||
(isConstantVariable(localOrExportSymbol) && type !== autoArrayType || isParameter && !isSymbolAssigned(localOrExportSymbol)) | ||
(isConstantVariable(localOrExportSymbol) && type !== autoArrayType || isParameter && !isSomeSymbolAssigned(rootDeclaration)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the change to Maybe we want to keep marking symbols with Here's a more minimal repro of the break linked above: function ff({ a, b }: { a: string | undefined, b: () => void }) {
if (a !== undefined) {
b = () => {
const x: string = a;
}
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heh, I really need to start reading through all of the existing comments at once instead of reading through them one by one. It would save me a few minutes here 😅 |
||
) { | ||
flowContainer = getControlFlowContainer(flowContainer); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.