Skip to content

The suggested fix for the existential types with implicit unwrapping is incorrect when enable upcoming featureExistentialAny #65026

@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 implicitly unwrapped optionals.

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 an error).
//
//  main.swift
//  ExistentialAnySample
//
//  Created by treastrain on 2023/04/08.
//

protocol SampleProtocol {}

let _: SampleProtocol! // ❌ Use of protocol 'SampleProtocol' as a type must be written 'any SampleProtocol'
  1. Click "Fix" to accept the suggestion.

  2. The error continues and the build fails.

Expected behavior
After clicking "Fix", any SampleProtocol! should be replaced with (any SampleProtocol)!.

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-itsimplicitly unwrapped optionalsFeature → types: Implicitly unwrapped optional types, aka IUOswift 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