Skip to content

Commit af2ec35

Browse files
authored
Fix data race via RequiresAddingImplicitUndefined (#1152)
1 parent d145105 commit af2ec35

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

internal/checker/emitresolver.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,28 +472,35 @@ func (r *emitResolver) IsImportRequiredByAugmentation(decl *ast.ImportDeclaratio
472472
}
473473

474474
func (r *emitResolver) RequiresAddingImplicitUndefined(declaration *ast.Node, symbol *ast.Symbol, enclosingDeclaration *ast.Node) bool {
475+
if !ast.IsParseTreeNode(declaration) {
476+
return false
477+
}
478+
r.checkerMu.Lock()
479+
defer r.checkerMu.Unlock()
480+
return r.requiresAddingImplicitUndefined(declaration, symbol, enclosingDeclaration)
481+
}
482+
483+
func (r *emitResolver) requiresAddingImplicitUndefined(declaration *ast.Node, symbol *ast.Symbol, enclosingDeclaration *ast.Node) bool {
475484
// node = r.emitContext.ParseNode(node)
476485
if !ast.IsParseTreeNode(declaration) {
477486
return false
478487
}
479488
switch declaration.Kind {
480489
case ast.KindPropertyDeclaration, ast.KindPropertySignature, ast.KindJSDocPropertyTag:
481-
r.checkerMu.Lock()
482-
defer r.checkerMu.Unlock()
483490
if symbol == nil {
484491
symbol = r.checker.getSymbolOfDeclaration(declaration)
485492
}
486493
t := r.checker.getTypeOfSymbol(symbol)
487494
r.checker.mappedSymbolLinks.Has(symbol)
488495
return !!((symbol.Flags&ast.SymbolFlagsProperty != 0) && (symbol.Flags&ast.SymbolFlagsOptional != 0) && isOptionalDeclaration(declaration) && r.checker.ReverseMappedSymbolLinks.Has(symbol) && r.checker.ReverseMappedSymbolLinks.Get(symbol).mappedType != nil && containsNonMissingUndefinedType(r.checker, t))
489496
case ast.KindParameter, ast.KindJSDocParameterTag:
490-
return r.requiresAddingImplicitUndefined(declaration, enclosingDeclaration)
497+
return r.requiresAddingImplicitUndefinedWorker(declaration, enclosingDeclaration)
491498
default:
492499
panic("Node cannot possibly require adding undefined")
493500
}
494501
}
495502

496-
func (r *emitResolver) requiresAddingImplicitUndefined(parameter *ast.Node, enclosingDeclaration *ast.Node) bool {
503+
func (r *emitResolver) requiresAddingImplicitUndefinedWorker(parameter *ast.Node, enclosingDeclaration *ast.Node) bool {
497504
return (r.isRequiredInitializedParameter(parameter, enclosingDeclaration) || r.isOptionalUninitializedParameterProperty(parameter)) && !r.declaredParameterTypeContainsUndefined(parameter)
498505
}
499506

internal/checker/nodebuilderimpl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,7 @@ func (b *nodeBuilderImpl) serializeTypeForDeclaration(declaration *ast.Declarati
18981898
}
18991899
}
19001900
// !!! TODO: JSDoc, getEmitResolver call is unfortunate layering for the helper - hoist it into checker
1901-
addUndefinedForParameter := declaration != nil && (ast.IsParameter(declaration) /*|| ast.IsJSDocParameterTag(declaration)*/) && b.ch.GetEmitResolver(nil, true).RequiresAddingImplicitUndefined(declaration, symbol, b.ctx.enclosingDeclaration)
1901+
addUndefinedForParameter := declaration != nil && (ast.IsParameter(declaration) /*|| ast.IsJSDocParameterTag(declaration)*/) && b.ch.GetEmitResolver(nil, true).requiresAddingImplicitUndefined(declaration, symbol, b.ctx.enclosingDeclaration)
19021902
if addUndefinedForParameter {
19031903
t = b.ch.getOptionalType(t, false)
19041904
}

0 commit comments

Comments
 (0)