From 7166f193b5586f9ba74b49bded345141a52a2bb4 Mon Sep 17 00:00:00 2001 From: Pavel Yaskevich Date: Fri, 9 Dec 2022 15:47:54 -0500 Subject: [PATCH] [ConstraintSystem] Reject property wrapper transform on func parameters in pattern context The transform is not currently supported on anything but regular calls. --- lib/Sema/ConstraintSystem.cpp | 9 +++++ ...rloaded_elements_in_optional_context.swift | 36 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 validation-test/Sema/SwiftUI/case_with_overloaded_elements_in_optional_context.swift diff --git a/lib/Sema/ConstraintSystem.cpp b/lib/Sema/ConstraintSystem.cpp index 4cb383b1d6907..788980158ebd3 100644 --- a/lib/Sema/ConstraintSystem.cpp +++ b/lib/Sema/ConstraintSystem.cpp @@ -1403,6 +1403,15 @@ unwrapPropertyWrapperParameterTypes(ConstraintSystem &cs, AbstractFunctionDecl * return functionType; } + // This transform is not applicable to pattern matching context. + // + // Note: If the transform is ever enabled for patterns - new branch + // would have to be added to `nameLoc` selection. + if (auto last = locator.last()) { + if (last->is()) + return functionType; + } + auto *paramList = funcDecl->getParameters(); auto paramTypes = functionType->getParams(); SmallVector adjustedParamTypes; diff --git a/validation-test/Sema/SwiftUI/case_with_overloaded_elements_in_optional_context.swift b/validation-test/Sema/SwiftUI/case_with_overloaded_elements_in_optional_context.swift new file mode 100644 index 0000000000000..cbb9d8cc7f8d7 --- /dev/null +++ b/validation-test/Sema/SwiftUI/case_with_overloaded_elements_in_optional_context.swift @@ -0,0 +1,36 @@ +// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macosx10.15 -swift-version 5 -enable-experimental-feature ResultBuilderASTTransform - + +// REQUIRES: objc_interop +// REQUIRES: OS=macosx + +import SwiftUI + +struct Person { + var fullName: String +} + +enum State { + case success(Input, Output) + case failure(Input, Failure) +} + +// Type-checker finds this overload based on +// `extension Optional : View where Wrapped : View { ... }` +extension View { + func failure(_: String) -> some View { EmptyView() } +} + +struct MyTest : View { + var state: State? + + // `switch` has to be anchored on `AccessorDecl` to reproduce + // a crash. + var body: some View { + switch state { + case nil: + Text("nil") + case .success(let person, _), .failure(let person, _): + Text(person.fullName) + } + } +}