[Sema] Tighten up function call check in resolveKeyPathExpr

Check for `CallExpr` instead of `ApplyExpr`, we don't support
arbitrary postfix operators in key paths.
This commit is contained in:
Hamish Knight
2025-06-17 20:58:25 +01:00
parent 2e18973569
commit f95eb6bf48
4 changed files with 19 additions and 4 deletions

View File

@@ -2531,11 +2531,11 @@ void PreCheckTarget::resolveKeyPathExpr(KeyPathExpr *KPE) {
(void)outermostExpr;
assert(OEE == outermostExpr);
expr = OEE->getSubExpr();
} else if (auto AE = dyn_cast<ApplyExpr>(expr)) {
} else if (auto CE = dyn_cast<CallExpr>(expr)) {
// foo(), foo(val value: Int) or unapplied foo
components.push_back(KeyPathExpr::Component::forUnresolvedApply(
getASTContext(), AE->getArgs()));
expr = AE->getFn();
getASTContext(), CE->getArgs()));
expr = CE->getFn();
} else {
if (emitErrors) {
// \(<expr>) may be an attempt to write a string interpolation outside

View File

@@ -407,7 +407,6 @@ _ = /\()/
// expected-error@-1 {{'/' is not a prefix unary operator}}
// expected-error@-2 {{'/' is not a postfix unary operator}}
// expected-error@-3 {{invalid component of Swift key path}}
// expected-error@-4 {{type of expression is ambiguous without a type annotation}}
do {
let _: Regex = (/whatever\)/

View File

@@ -46,3 +46,16 @@ func f_56996() {
_ = \Int.byteSwapped.signum() // expected-error {{key path cannot refer to instance method 'signum()'}}
_ = \Int.byteSwapped.init() // expected-error {{key path cannot refer to initializer 'init()'}}
}
postfix operator ^
postfix func ^ <T>(_ x: T) -> T { x }
func unsupportedOperator() {
struct S {
var x: Int
}
_ = \.^ // expected-error {{invalid component of Swift key path}}
_ = \S^ // expected-error {{invalid component of Swift key path}}
_ = \S.x^ // expected-error {{invalid component of Swift key path}}
_ = \.x^ // expected-error {{invalid component of Swift key path}}
}

View File

@@ -0,0 +1,3 @@
// {"signature":"(anonymous namespace)::ConstraintWalker::walkToExprPost(swift::Expr*)"}
// RUN: not %target-swift-frontend -typecheck %s
\.+=