swiftlang/swift

[SR-4949] No error for using 'as' patterns with CF types

Open

#47,526 opened on 2017年5月19日

GitHub で見る
 (5 comments) (0 reactions) (0 assignees)Swift (10,719 forks)batch import
bugcompilerdiagnostics qualitygood first issue

Repository metrics

Stars
 (69,989 stars)
PR merge metrics
 (平均マージ 8d 17h) (30d で 510 merged PRs)

説明

Previous ID SR-4949
Radar rdar://problem/32255478
Original Reporter @belkadan
Type Bug
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, StarterBug
Assignee None
Priority Medium

md5: 62d9e9e2da2d4b28fa3d10ebd350dba6

Issue Description:

import Foundation

let x = ([1: 2] as NSDictionary) as Any

switch x {
case let b as CFBoolean:
    print("it's a cfbool: \(b)")
    
default:
    print("n/a")
}

We should warn here that we can't actually do checked downcasts for CF types, like we do with as?.

import Foundation
let x = ([1: 2] as NSDictionary) as Any
let y = x as? CFDictionary // error: conditional downcast to CoreFoundation type 'CFDictionary' will always succeed

We can't make it an error because that would break source compatibility, but at least a warning will work. The best workaround today (and a possible fix-it):

switch x {
case let rawB:
    let b = rawB as! CFBoolean
    print("it's a cfbool: \(b)")
    
default:
    print("n/a")
}

コントリビューターガイド