Skip to content

Commit 6ff8f55

Browse files
committed
Sema: Fix handling of Solution::typeBindings in replaySolution()
1 parent de731b4 commit 6ff8f55

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

lib/Sema/CSRanking.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,9 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
13501350
const auto &bindings2 = solutions[idx2].typeBindings;
13511351

13521352
for (const auto &binding1 : bindings1) {
1353+
if (!binding1.second)
1354+
continue;
1355+
13531356
auto *typeVar = binding1.first;
13541357
auto *loc = typeVar->getImpl().getLocator();
13551358

@@ -1375,6 +1378,9 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
13751378
if (binding2 == bindings2.end())
13761379
continue;
13771380

1381+
if (!binding2->second)
1382+
continue;
1383+
13781384
TypeBindingsToCompare typesToCompare(binding1.second, binding2->second);
13791385

13801386
// For short-form and self-delegating init calls, we want to prefer

lib/Sema/CSSolver.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ Solution ConstraintSystem::finalize() {
103103
// This type variable has no binding. Allowed only
104104
// when `FreeTypeVariableBinding::Allow` is set,
105105
// which is checked above.
106-
if (!getFixedType(tv))
106+
if (!getFixedType(tv)) {
107+
solution.typeBindings[tv] = Type();
107108
continue;
109+
}
108110

109111
solution.typeBindings[tv] = simplifyType(tv)->reconstituteSugar(false);
110112
}
@@ -274,10 +276,15 @@ void ConstraintSystem::replaySolution(const Solution &solution,
274276
if (shouldIncreaseScore)
275277
replayScore(solution.getFixedScore());
276278

277-
// Assign fixed types to the type variables solved by this solution.
278279
for (auto binding : solution.typeBindings) {
279280
// If we haven't seen this type variable before, record it now.
280281
addTypeVariable(binding.first);
282+
}
283+
284+
// Assign fixed types to the type variables solved by this solution.
285+
for (auto binding : solution.typeBindings) {
286+
if (!binding.second)
287+
continue;
281288

282289
// If we don't already have a fixed type for this type variable,
283290
// assign the fixed type from the solution.

test/Constraints/ranking.swift

+10
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,13 @@ extension SignalProtocol where Element: SignalProtocol, Error == Never {
440440
func no_ambiguity_error_vs_never<Element, Error>(_ signals: [Signal<Element, Error>]) -> Signal<Element, Error> {
441441
return Signal(sequence: signals).flatten() // Ok
442442
}
443+
444+
// Regression test for a crash I reduced from the stdlib that wasn't covered
445+
// by tests
446+
struct HasIntInit {
447+
init(_: Int) {}
448+
}
449+
450+
func compare_solutions_with_bindings(x: UInt8, y: UInt8) -> HasIntInit {
451+
return .init(Int(x / numericCast(y)))
452+
}

0 commit comments

Comments
 (0)