Skip to content

Commit 161e126

Browse files
committed
[CSDiagnostics] Diagnose a key path literal without components early
Detect that diagnose key path literals that do not have any components.
1 parent 19e9cb0 commit 161e126

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

lib/Sema/CSGen.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3866,6 +3866,12 @@ namespace {
38663866
base = optTy;
38673867
}
38683868

3869+
// If we have a malformed KeyPathExpr e.g. let _: KeyPath<A, C> = \A
3870+
// let's record a AllowKeyPathMissingComponent fix.
3871+
if (E->hasSingleInvalidComponent()) {
3872+
(void)CS.recordFix(AllowKeyPathWithoutComponents::create(CS, locator));
3873+
}
3874+
38693875
auto valueLocator =
38703876
CS.getConstraintLocator(E, ConstraintLocator::KeyPathValue);
38713877
auto *value = CS.createTypeVariable(valueLocator, TVO_CanBindToNoEscape |

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12299,13 +12299,9 @@ ConstraintSystem::simplifyKeyPathConstraint(
1229912299
if (keyPathTy->isPlaceholder())
1230012300
return SolutionKind::Solved;
1230112301

12302-
// If we have a malformed KeyPathExpr e.g. let _: KeyPath<A, C> = \A
12303-
// let's record a AllowKeyPathMissingComponent fix.
12304-
if (keyPath->hasSingleInvalidComponent()) {
12305-
auto *fix = AllowKeyPathWithoutComponents::create(
12306-
*this, getConstraintLocator(locator));
12307-
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
12308-
}
12302+
if (hasFixFor(getConstraintLocator(keyPath),
12303+
FixKind::AllowKeyPathWithoutComponents))
12304+
return SolutionKind::Solved;
1230912305

1231012306
// If the root type has been bound to a hole, we cannot infer it.
1231112307
if (getFixedTypeRecursive(rootTy, /*wantRValue*/ true)->isPlaceholder())

0 commit comments

Comments
 (0)