Skip to content

The suggested fix for the protocol composition type is incorrect when enable upcoming featureExistentialAny #65027

@treastrain

Description

@treastrain

Description
In Swift 5.8, SE-0335 "Introduce existential any" can be enabled by SE-0362 "Piecemeal adoption of upcoming language improvements". In that case, if the existential type is not given the any keyword, it causes a build error.

That build error contains a suggested fix, but it suggests a fix that seems incorrect when using protocol composition types.

Steps to reproduce

  1. Create a Swift Package and open it Xcode 14.3.
% mkdir ExistentialAnySample

% cd ExistentialAnySample

% swift package init --type executable
Creating executable package: ExistentialAnySample
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift

% xed .
  1. Enable upcoming feature ExistentialAny.
// swift-tools-version: 5.8

import PackageDescription

let package = Package(
    name: "ExistentialAnySample",
    targets: [
        .executableTarget(
            name: "ExistentialAnySample",
            path: "Sources",
            swiftSettings: [
                .enableUpcomingFeature("ExistentialAny"),
            ]),
    ]
)
  1. Write the following code in main.swift and build it (we will get some errors).
//
//  main.swift
//  ExistentialAnySample
//
//  Created by treastrain on 2023/04/08.
//

class C {}
protocol P {}
protocol Q {}

func f1(_ v: C & P) {} // ❌ Use of protocol 'P' as a type must be written 'any P'

func f2(_ v: P & Q) {} // ❌ Use of protocol 'P' as a type must be written 'any P'
                       // ❌ Use of protocol 'Q' as a type must be written 'any Q'

  1. In the Xcode menu bar, "Editor" > "Fix all issues (Cmd + Ctrl + Option + F)".

  2. The error continues and the build fails.

Expected behavior
After "Fix all issues (Cmd + Ctrl + Option + F)", C & P should be replaced with any C & P, and P & Q should be replaced with any P & Q in one shot.

Environment

  • Swift compiler version info swift-driver version: 1.75.2 Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100) Target: arm64-apple-macosx13.0
  • Xcode version info Xcode 14.3 Build version 14E222b
  • Deployment target: iOS 16.4, macOS 13.3, tvOS 16.4, watchOS 9.4

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of ImplementationexistentialsFeature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased valuesfix-itsFeature: diagnostic fix-itsprotocol compositionsFeature → types: protocol composition typesswift 6.0type checkerArea → compiler: Semantic analysistypesFeature: typesunexpected behaviorBug: Unexpected behavior or incorrect output

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions