Skip to content

Commit 78072de

Browse files
committed
[CS] Assert that we don't end up with unsolved constraints
Make sure we don't end up in a situation where we have unsolved constraints left over and consider the system fully solved. This requires tweaking the type matching code for dependent members such that a concrete base is considered a failure rather than being left unsolved. This should only happen when not in diagnostic mode, as otherwise we use a hole.
1 parent d69a42d commit 78072de

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4296,6 +4296,15 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
42964296
llvm_unreachable("type variables should have already been handled by now");
42974297

42984298
case TypeKind::DependentMember: {
4299+
// If one of the dependent member types has no type variables,
4300+
// this comparison is effectively illformed, because dependent
4301+
// member couldn't be simplified down to the actual type, and
4302+
// we wouldn't be able to solve this constraint, so let's just fail.
4303+
// This should only happen outside of diagnostic mode, as otherwise the
4304+
// member is replaced by a hole in simplifyType.
4305+
if (!desugar1->hasTypeVariable() || !desugar2->hasTypeVariable())
4306+
return getTypeMatchFailure(locator);
4307+
42994308
// Nothing we can solve yet, since we need to wait until
43004309
// type variables will get resolved.
43014310
return formUnsolvedResult();

lib/Sema/CSStep.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,25 @@ StepResult ComponentStep::take(bool prevFailed) {
354354
return finalize(/*isSuccess=*/false);
355355
}
356356

357+
// If we don't have any disjunction or type variable choices left, we're done
358+
// solving. Make sure we don't have any unsolved constraints left over, using
359+
// report_fatal_error to make sure we trap in release builds instead of
360+
// potentially miscompiling.
361+
if (!CS.ActiveConstraints.empty()) {
362+
CS.print(llvm::errs());
363+
llvm::report_fatal_error("Active constraints left over?");
364+
}
365+
if (!CS.solverState->allowsFreeTypeVariables()) {
366+
if (!CS.InactiveConstraints.empty()) {
367+
CS.print(llvm::errs());
368+
llvm::report_fatal_error("Inactive constraints left over?");
369+
}
370+
if (CS.hasFreeTypeVariables()) {
371+
CS.print(llvm::errs());
372+
llvm::report_fatal_error("Free type variables left over?");
373+
}
374+
}
375+
357376
// If this solution is worse than the best solution we've seen so far,
358377
// skip it.
359378
if (CS.worseThanBestSolution())

0 commit comments

Comments
 (0)