Skip to content

[SR-13303] Erroneous warning shown when casting protocol type to dictionary type #55743

@swift-ci

Description

@swift-ci
Previous ID SR-13303
Radar None
Original Reporter ketzusaka (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

macOS X 10.15.6.

Bug is present in Xcode 11.6, Xcode 12 Beta 2. It is not present in Xcode 11.3.1

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: e1b5118e1d41c3d0c99d8fe0ff8ba714

Issue Description:

I have a protocol that a dictionary conforms to. Types can be cast from this protocol to the concrete dictionary type. This has worked for us before, but is now showing a warning. It works correctly (not showing the warning) on Xcode 11.3.1. The warning shows on Xcode 11.6; we have not tested the in between Xcodes. This issue is still present in Xcode 12 beta 2.

Sample code to reproduce:

import Foundation


protocol Widget {}
extension String: Widget {}
extension Dictionary: Widget where Key == String, Value == Widget {}


var subject: Widget = ["Foo": "Bar"]


// Shows warning: Cast from 'Widget' to unrelated type '[String : String]' always fails
guard let castedSubject = subject as? [String: String] else { fatalError("Cast failed") }
print(castedSubject) // Prints successfully as ["Foo": "Bar"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselftype checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions