Skip to content

[Sema][Diagnostics][SR-14644] Improving diagnostics for key path with invalid components #37521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -5833,7 +5833,10 @@ class KeyPathExpr : public Expr {
/// Indicates if the key path expression is composed by a single invalid
/// component. e.g. missing component `\Root`
bool hasSingleInvalidComponent() const {
return Components.size() == 1 && !Components.front().isValid();
if (ParsedRoot && ParsedRoot->getKind() == ExprKind::Type) {
return Components.size() == 1 && !Components.front().isValid();
}
return false;
}

/// Retrieve the string literal expression, which will be \c NULL prior to
Expand Down
9 changes: 9 additions & 0 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,15 @@ TypeVariableBinding::fixForHole(ConstraintSystem &cs) const {
if (cs.hasFixFor(kpLocator, FixKind::AllowKeyPathWithoutComponents))
return None;

// If key path has any invalid component, let's just skip fix because the
// invalid component would be already diagnosed.
auto keyPath = castToExpr<KeyPathExpr>(srcLocator->getAnchor());
if (llvm::any_of(keyPath->getComponents(),
[](KeyPathExpr::Component component) {
return !component.isValid();
}))
return None;

ConstraintFix *fix = SpecifyKeyPathRootType::create(cs, dstLocator);
return std::make_pair(fix, defaultImpact);
}
Expand Down
10 changes: 10 additions & 0 deletions test/expr/unary/keypath/keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,16 @@ func testSyntaxErrors() {
_ = \A.a!;
}

// SR-14644
func sr14644() {
_ = \Int.byteSwapped.signum() // expected-error {{invalid component of Swift key path}}
_ = \Int.byteSwapped.init() // expected-error {{invalid component of Swift key path}}
_ = \Int // expected-error {{key path must have at least one component}}
_ = \Int? // expected-error {{key path must have at least one component}}
_ = \Int. // expected-error {{invalid component of Swift key path}}
// expected-error@-1 {{expected member name following '.'}}
}

// SR-13364 - keypath missing optional crashes compiler: "Inactive constraints left over?"
func sr13364() {
let _: KeyPath<String?, Int?> = \.utf8.count // expected-error {{no exact matches in reference to property 'count'}}
Expand Down