File tree Expand file tree Collapse file tree 3 files changed +23
-1
lines changed Expand file tree Collapse file tree 3 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -5842,7 +5842,10 @@ class KeyPathExpr : public Expr {
5842
5842
// / Indicates if the key path expression is composed by a single invalid
5843
5843
// / component. e.g. missing component `\Root`
5844
5844
bool hasSingleInvalidComponent () const {
5845
- return Components.size () == 1 && !Components.front ().isValid ();
5845
+ if (ParsedRoot && ParsedRoot->getKind () == ExprKind::Type) {
5846
+ return Components.size () == 1 && !Components.front ().isValid ();
5847
+ }
5848
+ return false ;
5846
5849
}
5847
5850
5848
5851
// / Retrieve the string literal expression, which will be \c NULL prior to
Original file line number Diff line number Diff line change @@ -1851,6 +1851,15 @@ TypeVariableBinding::fixForHole(ConstraintSystem &cs) const {
1851
1851
if (cs.hasFixFor (kpLocator, FixKind::AllowKeyPathWithoutComponents))
1852
1852
return None;
1853
1853
1854
+ // If key path has any invalid component, let's just skip fix because the
1855
+ // invalid component would be already diagnosed.
1856
+ auto keyPath = castToExpr<KeyPathExpr>(srcLocator->getAnchor ());
1857
+ if (llvm::any_of (keyPath->getComponents (),
1858
+ [](KeyPathExpr::Component component) {
1859
+ return !component.isValid ();
1860
+ }))
1861
+ return None;
1862
+
1854
1863
ConstraintFix *fix = SpecifyKeyPathRootType::create (cs, dstLocator);
1855
1864
return std::make_pair (fix, defaultImpact);
1856
1865
}
Original file line number Diff line number Diff line change @@ -1054,6 +1054,16 @@ func testSyntaxErrors() {
1054
1054
_ = \A . a!;
1055
1055
}
1056
1056
1057
+ // SR-14644
1058
+ func sr14644 ( ) {
1059
+ _ = \Int . byteSwapped. signum ( ) // expected-error {{invalid component of Swift key path}}
1060
+ _ = \Int . byteSwapped. init ( ) // expected-error {{invalid component of Swift key path}}
1061
+ _ = \Int // expected-error {{key path must have at least one component}}
1062
+ _ = \Int ? // expected-error {{key path must have at least one component}}
1063
+ _ = \Int . // expected-error {{invalid component of Swift key path}}
1064
+ // expected-error@-1 {{expected member name following '.'}}
1065
+ }
1066
+
1057
1067
// SR-13364 - keypath missing optional crashes compiler: "Inactive constraints left over?"
1058
1068
func sr13364( ) {
1059
1069
let _: KeyPath < String ? , Int ? > = \. utf8. count // expected-error {{no exact matches in reference to property 'count'}}
You can’t perform that action at this time.
0 commit comments