Skip to content

Commit 06201a6

Browse files
[Sema] Diagnose wrong type coercion involving metatypes
1 parent 18f52dc commit 06201a6

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,12 @@ bool ContextualFailure::diagnoseAsError() {
19051905
diagnostic = diag::cannot_convert_condition_value;
19061906
break;
19071907
}
1908+
1909+
case ConstraintLocator::InstanceType: {
1910+
if (diagnoseCoercionToUnrelatedType())
1911+
return true;
1912+
break;
1913+
}
19081914

19091915
case ConstraintLocator::TernaryBranch: {
19101916
auto *ifExpr = cast<IfExpr>(getRawAnchor());
@@ -2235,11 +2241,12 @@ bool ContextualFailure::diagnoseCoercionToUnrelatedType() const {
22352241
auto *anchor = getAnchor();
22362242

22372243
if (auto *coerceExpr = dyn_cast<CoerceExpr>(anchor)) {
2238-
auto fromType = getFromType();
2244+
auto fromType = getType(coerceExpr->getSubExpr());
22392245
auto toType = getType(coerceExpr->getCastTypeLoc());
2246+
22402247
auto diagnostic =
22412248
getDiagnosticFor(CTP_CoerceOperand,
2242-
/*forProtocol=*/toType->isAnyExistentialType());
2249+
/*forProtocol=*/toType->isExistentialType());
22432250

22442251
auto diag =
22452252
emitDiagnostic(anchor->getLoc(), *diagnostic, fromType, toType);
@@ -4772,6 +4779,13 @@ bool MissingContextualConformanceFailure::diagnoseAsError() {
47724779
break;
47734780
}
47744781

4782+
case ConstraintLocator::InstanceType: {
4783+
// If the missing conformance involves a coercion of metatypes
4784+
if (diagnoseCoercionToUnrelatedType())
4785+
return true;
4786+
break;
4787+
}
4788+
47754789
default:
47764790
break;
47774791
}

lib/Sema/CSSimplify.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,15 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
22032203
if (!(anchor && isa<AssignExpr>(anchor)))
22042204
return getTypeMatchFailure(locator);
22052205
}
2206+
2207+
auto *anchor = locator.getAnchor();
2208+
if (isa<CoerceExpr>(anchor)) {
2209+
auto *fix = ContextualMismatch::create(
2210+
*this, type1, type2, getConstraintLocator(locator));
2211+
if (recordFix(fix))
2212+
return getTypeMatchFailure(locator);
2213+
break;
2214+
}
22062215

22072216
auto *fix = MissingConformance::forContextual(
22082217
*this, type1, proto, getConstraintLocator(locator));
@@ -4059,9 +4068,20 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
40594068
subKind = ConstraintKind::Bind;
40604069
}
40614070

4062-
return matchTypes(
4063-
instanceType1, instanceType2, subKind, subflags,
4064-
locator.withPathElement(ConstraintLocator::InstanceType));
4071+
auto result =
4072+
matchTypes(instanceType1, instanceType2, subKind, subflags,
4073+
locator.withPathElement(ConstraintLocator::InstanceType));
4074+
if (shouldAttemptFixes() && result.isFailure()) {
4075+
auto *anchor = locator.getAnchor();
4076+
if (anchor && isa<CoerceExpr>(anchor)) {
4077+
auto *fix =
4078+
ContextualMismatch::create(*this, instanceType1, instanceType2,
4079+
getConstraintLocator(locator));
4080+
conversionsOrFixes.push_back(fix);
4081+
break;
4082+
}
4083+
}
4084+
return result;
40654085
}
40664086

40674087
case TypeKind::Function: {

0 commit comments

Comments
 (0)