Skip to content

Commit 3b1960e

Browse files
authored
Merge pull request #85016 from slavapestov/prepared-fuzz
Sema: Fix a fuzzer crash
2 parents 0849b2e + 69c8d15 commit 3b1960e

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,34 +2859,50 @@ static bool diagnoseContextualFunctionCallGenericAmbiguity(
28592859
return false;
28602860

28612861
auto contextualFix = contextualFixes.front();
2862-
if (!std::all_of(contextualFixes.begin() + 1, contextualFixes.end(),
2863-
[&contextualFix](FixInContext fix) {
2864-
return fix.second->getLocator() ==
2865-
contextualFix.second->getLocator();
2866-
}))
2867-
return false;
2868-
28692862
auto fixLocator = contextualFix.second->getLocator();
2863+
28702864
auto contextualAnchor = fixLocator->getAnchor();
28712865
auto *AE = getAsExpr<ApplyExpr>(contextualAnchor);
28722866
// All contextual failures anchored on the same function call.
28732867
if (!AE)
28742868
return false;
28752869

28762870
auto fnLocator = cs.getConstraintLocator(AE->getSemanticFn());
2877-
auto overload = contextualFix.first->getOverloadChoiceIfAvailable(fnLocator);
2878-
if (!overload)
2879-
return false;
28802871

2881-
auto applyFnType = overload->adjustedOpenedType->castTo<FunctionType>();
2882-
auto resultTypeVar = applyFnType->getResult()->getAs<TypeVariableType>();
2872+
auto getResultTypeVar = [&](FixInContext contextualFix) -> TypeVariableType * {
2873+
auto overload = contextualFix.first->getOverloadChoiceIfAvailable(fnLocator);
2874+
if (!overload)
2875+
return nullptr;
2876+
2877+
auto applyFnType = overload->adjustedOpenedType->castTo<FunctionType>();
2878+
return applyFnType->getResult()->getAs<TypeVariableType>();
2879+
};
2880+
2881+
auto resultTypeVar = getResultTypeVar(contextualFix);
28832882
if (!resultTypeVar)
28842883
return false;
28852884

28862885
auto *GP = resultTypeVar->getImpl().getGenericParameter();
28872886
if (!GP)
28882887
return false;
28892888

2889+
if (!std::all_of(contextualFixes.begin() + 1, contextualFixes.end(),
2890+
[&](FixInContext fix) {
2891+
if (fix.second->getLocator() != fixLocator)
2892+
return false;
2893+
2894+
auto resultTypeVar = getResultTypeVar(fix);
2895+
2896+
if (!resultTypeVar)
2897+
return false;
2898+
2899+
if (resultTypeVar->getImpl().getGenericParameter() != GP)
2900+
return false;
2901+
2902+
return true;
2903+
}))
2904+
return false;
2905+
28902906
auto applyLoc =
28912907
cs.getConstraintLocator(AE, {LocatorPathElt::ApplyArgument()});
28922908
auto argMatching =
@@ -2903,6 +2919,11 @@ static bool diagnoseContextualFunctionCallGenericAmbiguity(
29032919
continue;
29042920

29052921
auto argParamMatch = argMatching->second.parameterBindings[i];
2922+
2923+
// FIXME: We're just looking at the first solution's overload here,
2924+
// is that correct?
2925+
auto overload = contextualFix.first->getOverloadChoiceIfAvailable(fnLocator);
2926+
auto applyFnType = overload->adjustedOpenedType->castTo<FunctionType>();
29062927
auto param = applyFnType->getParams()[argParamMatch.front()];
29072928
auto paramFnType = param.getPlainType()->getAs<FunctionType>();
29082929
if (!paramFnType)
@@ -2922,8 +2943,15 @@ static bool diagnoseContextualFunctionCallGenericAmbiguity(
29222943
// from all the closure contextual fix/solutions and if there are more than
29232944
// one fixed type diagnose it.
29242945
swift::SmallSetVector<Type, 4> genericParamInferredTypes;
2925-
for (auto &fix : contextualFixes)
2946+
for (auto &fix : contextualFixes) {
2947+
auto resultTypeVar = getResultTypeVar(fix);
2948+
genericParamInferredTypes.insert(fix.first->getFixedType(resultTypeVar));
2949+
}
2950+
2951+
for (auto &fix : allFixes) {
2952+
auto resultTypeVar = getResultTypeVar(fix);
29262953
genericParamInferredTypes.insert(fix.first->getFixedType(resultTypeVar));
2954+
}
29272955

29282956
if (llvm::all_of(allFixes, [&](FixInContext fix) {
29292957
auto fixLocator = fix.second->getLocator();

validation-test/compiler_crashers/Solution-getFixedType-98b9c4.swift renamed to validation-test/compiler_crashers_fixed/Solution-getFixedType-98b9c4.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// {"kind":"typecheck","signature":"swift::constraints::Solution::getFixedType(swift::TypeVariableType*) const","signatureAssert":"Assertion failed: (knownBinding != typeBindings.end()), function getFixedType"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
class a {
44
static
55
b {

0 commit comments

Comments
 (0)