swiftlang/swift

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

Open

#47,526 创建于 2017年5月19日

在 GitHub 查看
 (5 评论) (0 反应) (0 负责人)Swift (69,989 star) (10,719 fork)batch import
bugcompilerdiagnostics qualitygood first issue

描述

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")
}

贡献者指南