Skip to content

Avoid unsafe extraction in BindingReducer #3315

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct BindingForm {
}

var body: some Reducer<State, Action> {
BindingReducer()
BindingReducer(action: \.binding)
Reduce { state, action in
switch action {
case .binding(\.stepCount):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct FocusDemo {
}

var body: some Reducer<State, Action> {
BindingReducer()
BindingReducer(action: \.binding)
Reduce { state, action in
switch action {
case .binding:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private struct BasicsFeature {
case binding(BindingAction<State>)
}
var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)
}
}

Expand Down Expand Up @@ -161,7 +161,7 @@ private struct PersonalInfoFeature {
}
@Dependency(\.dismiss) var dismiss
var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)
}
}

Expand Down Expand Up @@ -217,7 +217,7 @@ private struct TopicsFeature {
}
@Dependency(\.dismiss) var dismiss
var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)
Reduce { state, action in
switch action {
case .alert:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private struct Child {
case onDisappear
}
var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private enum PresentationTestCase {
}
@Dependency(\.dismiss) var dismiss
var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)
Reduce<State, Action> { state, action in
switch action {
case .binding:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private struct Child {
case onDisappear
}
var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ private struct Feature {
}
@Dependency(\.defaultAppStorage) var defaults
var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)
Reduce<State, Action> { state, action in
switch action {
case .binding(_):
case .binding:
return .none
case .deleteFileButtonTapped:
return .run { _ in
Expand Down
2 changes: 1 addition & 1 deletion Examples/SyncUps/SyncUps/SyncUpForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct SyncUpForm {
@Dependency(\.uuid) var uuid

var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)
Reduce { state, action in
switch action {
case .addAttendeeButtonTapped:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct Login: Sendable {
public init() {}

public var body: some Reducer<State, Action> {
BindingReducer(action: \.view)
BindingReducer(action: \.view.binding)
Reduce { state, action in
switch action {
case .alert:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct NewGame {
public init() {}

public var body: some Reducer<State, Action> {
BindingReducer()
BindingReducer(action: \.binding)
Reduce { state, action in
switch action {
case .binding:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct TwoFactor: Sendable {
public init() {}

public var body: some ReducerOf<Self> {
BindingReducer(action: \.view)
BindingReducer(action: \.view.binding)
Reduce { state, action in
switch action {
case .alert:
Expand Down
2 changes: 1 addition & 1 deletion Examples/Todos/Todos/Todo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Todo {
}

var body: some Reducer<State, Action> {
BindingReducer()
BindingReducer(action: \.binding)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Examples/Todos/Todos/Todos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct Todos {
private enum CancelID { case todoCompletion }

var body: some Reducer<State, Action> {
BindingReducer()
BindingReducer(action: \.binding)
Reduce { state, action in
switch action {
case .addTodoButtonTapped:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ struct Settings {
enum Action: BindableAction { /* ... */ }

var body: some Reducer<State, Action> {
BindingReducer()
BindingReducer(action: \.binding)
}
}
```
Expand Down Expand Up @@ -252,7 +252,7 @@ action for a given key path in the reducer:

```swift
var body: some Reducer<State, Action> {
BindingReducer()
BindingReducer(action: \.binding)

Reduce { state, action in
switch action
Expand All @@ -273,7 +273,7 @@ particular fields:

```swift
var body: some Reducer<State, Action> {
BindingReducer()
BindingReducer(action: \.binding)
.onChange(of: \.displayName) { oldValue, newValue in
// Validate display name
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct SyncUpForm {
}

var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)

Reduce { state, action in
switch action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct SyncUpForm {
}

var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)

Reduce { state, action in
switch action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct SyncUpForm {
}

var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)

Reduce { state, action in
switch action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct SyncUpForm {
}

var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)

Reduce { state, action in
switch action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct SyncUpForm {
}

var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)

Reduce { state, action in
switch action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct SyncUpForm {
}

var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)

Reduce { state, action in
switch action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct SyncUpForm {
}

var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)

Reduce { state, action in
switch action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct SyncUpForm {
}

var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)

Reduce { state, action in
switch action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct SyncUpForm {
@Dependency(\.uuid) var uuid

var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)

Reduce { state, action in
switch action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct SyncUpForm {
@Dependency(\.uuid) var uuid

var body: some ReducerOf<Self> {
BindingReducer()
BindingReducer(action: \.binding)

Reduce { state, action in
switch action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import SwiftUI
/// }
///
/// var body: some ReducerOf<Self> {
/// BindingReducer()
/// BindingReducer(action: \.binding)
/// Reduce { state, action in
/// // Your feature's logic...
/// }
Expand All @@ -35,47 +35,81 @@ import SwiftUI
/// Reduce { state, action in
/// // Your feature's logic...
/// }
/// BindingReducer()
/// BindingReducer(action: \.binding)
/// }
/// ```
///
/// If you forget to compose the ``BindingReducer`` into your feature's reducer, then when a binding
/// is written to it will cause a runtime purple Xcode warning letting you know what needs to be
/// fixed.
public struct BindingReducer<State, Action, ViewAction: BindableAction>: Reducer
where State == ViewAction.State {
public struct BindingReducer<State, Action>: Reducer {
@usableFromInline
let toViewAction: (Action) -> ViewAction?
let toBindingAction: (Action) -> BindingAction<State>?

@usableFromInline
init(internal toBindingAction: @escaping (Action) -> BindingAction<State>?) {
self.toBindingAction = toBindingAction
}

/// Initializes a reducer that updates bindable state when it receives binding actions.
///
/// - Parameter toBindingAction: A case key path to the binding action case.
@inlinable
public init() where Action == ViewAction {
self.init(internal: { $0 })
public init(action toBindingAction: CaseKeyPath<Action, BindingAction<State>>) {
self.init(internal: AnyCasePath(toBindingAction).extract(from:))
}

@inlinable
public init(action toViewAction: CaseKeyPath<Action, ViewAction>) where Action: CasePathable {
self.init(internal: { $0[case: toViewAction] })
public func reduce(into state: inout State, action: Action) -> Effect<Action> {
guard let bindingAction = toBindingAction(action)
else { return .none }

bindingAction.set(&state)
return .none
}
}

@available(
iOS, deprecated: 9999,
message: "Pass an explicit case key path, for example: 'BindingReducer(action: \\.binding)'"
)
@available(
macOS, deprecated: 9999,
message: "Pass an explicit case key path, for example: 'BindingReducer(action: \\.binding)'"
)
@available(
tvOS, deprecated: 9999,
message: "Pass an explicit case key path, for example: 'BindingReducer(action: \\.binding)'"
)
@available(
watchOS, deprecated: 9999,
message: "Pass an explicit case key path, for example: 'BindingReducer(action: \\.binding)'"
)
extension BindingReducer {
@inlinable
public init(action toViewAction: @escaping (_ action: Action) -> ViewAction?) {
self.init(internal: toViewAction)
public init() where Action: BindableAction<State> {
self.init(internal: AnyCasePath(unsafe: { .binding($0) }).extract(from:))
}

@usableFromInline
init(internal toViewAction: @escaping (_ action: Action) -> ViewAction?) {
self.toViewAction = toViewAction
@_disfavoredOverload
@inlinable
public init<ViewAction: BindableAction<State>>(
action toViewAction: CaseKeyPath<Action, ViewAction>
) {
self.init(
internal: AnyCasePath(toViewAction)
.appending(path: AnyCasePath(unsafe: { .binding($0) }))
.extract(from:)
)
}

@_disfavoredOverload
@inlinable
public func reduce(into state: inout State, action: Action) -> Effect<Action> {
// NB: Using a closure and not a `\.binding` key path literal to avoid a bug with archives:
// https://github.com/pointfreeco/swift-composable-architecture/pull/2641
guard let bindingAction = self.toViewAction(action).flatMap({ $0.binding })
else { return .none }

bindingAction.set(&state)
return .none
public init<ViewAction: BindableAction<State>>(
action toViewAction: @escaping (_ action: Action) -> ViewAction?
) {
let toBindingAction = AnyCasePath<ViewAction, BindingAction<State>>(unsafe: { .binding($0) })
.extract(from:)
self.init(internal: { toViewAction($0).flatMap(toBindingAction) })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension Reducer {
/// }
///
/// var body: some Reducer<State, Action> {
/// BindingReducer()
/// BindingReducer(action: \.binding)
/// .onChange(
/// of: { ($0.userSettings.isHapticFeedbackEnabled, $0.userSettings.isPushEnabled) },
/// removeDuplicates: ==
Expand Down Expand Up @@ -83,7 +83,7 @@ extension Reducer {
/// }
///
/// var body: some Reducer<State, Action> {
/// BindingReducer()
/// BindingReducer(action: \.binding)
/// .onChange(of: \.userSettings.isHapticFeedbackEnabled) { oldValue, newValue in
/// Reduce { state, action in
/// .run { send in
Expand Down
Loading