<commit_before>// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test

// REQUIRES: objc_interop

import CoreFoundation
import Foundation

class SwiftClass { }

func genericCast<T>(_ x: AnyObject, _: T.Type) -> T? {
  return x as? T
}

func genericCastUnconditional<T>(_ x: AnyObject, _: T.Type) -> T {
  return x as! T
}

// Check _cfTypeID() on a Swift class
let nsObject = NSObject()
let swiftObject = SwiftClass()
assert(CFGetTypeID(nsObject) == CFGetTypeID(swiftObject))

// Check CFString <-> AnyObject
func testCFStringAnyObject() {
  // Create a CFString
  let cfStr: CFString
    = CFStringCreateWithCString(nil, "Swift", CFStringBuiltInEncodings.ASCII.rawValue)

  // CHECK: Swift
  print(cfStr)

  // Convert it to AnyObject
  let anyObject: AnyObject = cfStr

  // CHECK: Swift
  print(anyObject)

  // Convert it back to a CFString
  let cfStr2 = anyObject as! CFString

  // CHECK: Swift
  print(cfStr2)

  // Conditional cast through a generic to a CFString
  if let cfStr3 = genericCast(anyObject, CFString.self) {
    // CHECK: Swift
    print(cfStr3)
  } else {
    print("Conditional cast failed")
  }

  // Forced cast through a generic to a CFString
  let cfStr4 = genericCastUnconditional(anyObject, CFString.self)

  // CHECK: Swift
  print(cfStr4)

  // CHECK: done
  print("done")
}
testCFStringAnyObject()


// Check CFString.Type <-> AnyObject.Type
func testCFStringAnyObjectType() {
  let cfStr: CFString
    = CFStringCreateWithCString(nil, "Swift", CFStringBuiltInEncodings.ASCII.rawValue)

  let cfStrType = type(of: cfStr)
  // CHECK: [[STRING_CLASS:(NS|CF).*String]]
  print(cfStrType)

  // Convert to AnyObject.Type
  let anyObjectType: AnyObject.Type = cfStrType
  // CHECK: [[STRING_CLASS]]
  print(anyObjectType)

  // Convert back to CFString.Type
  let cfStrType2 = anyObjectType as! CFString.Type
  // CHECK: [[STRING_CLASS]]
  print(cfStrType2)

  // CHECK: done
  print("done")
}
testCFStringAnyObjectType()<commit_msg>Add regression test to close #43022<commit_after>// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test

// REQUIRES: objc_interop

import CoreFoundation
import Foundation

class SwiftClass { }

func genericCast<T>(_ x: AnyObject, _: T.Type) -> T? {
  return x as? T
}

func genericCastUnconditional<T>(_ x: AnyObject, _: T.Type) -> T {
  return x as! T
}

// Check _cfTypeID() on a Swift class
let nsObject = NSObject()
let swiftObject = SwiftClass()
assert(CFGetTypeID(nsObject) == CFGetTypeID(swiftObject))

// Check CFString <-> AnyObject
func testCFStringAnyObject() {
  // Create a CFString
  let cfStr: CFString
    = CFStringCreateWithCString(nil, "Swift", CFStringBuiltInEncodings.ASCII.rawValue)

  // CHECK: Swift
  print(cfStr)

  // Convert it to AnyObject
  let anyObject: AnyObject = cfStr

  // CHECK: Swift
  print(anyObject)

  // Convert it back to a CFString
  let cfStr2 = anyObject as! CFString

  // CHECK: Swift
  print(cfStr2)

  // Conditional cast through a generic to a CFString
  if let cfStr3 = genericCast(anyObject, CFString.self) {
    // CHECK: Swift
    print(cfStr3)
  } else {
    print("Conditional cast failed")
  }

  // Forced cast through a generic to a CFString
  let cfStr4 = genericCastUnconditional(anyObject, CFString.self)

  // CHECK: Swift
  print(cfStr4)

  // CHECK: done
  print("done")
}
testCFStringAnyObject()


// Check CFString.Type <-> AnyObject.Type
func testCFStringAnyObjectType() {
  let cfStr: CFString
    = CFStringCreateWithCString(nil, "Swift", CFStringBuiltInEncodings.ASCII.rawValue)

  let cfStrType = type(of: cfStr)
  // CHECK: [[STRING_CLASS:(NS|CF).*String]]
  print(cfStrType)

  // Convert to AnyObject.Type
  let anyObjectType: AnyObject.Type = cfStrType
  // CHECK: [[STRING_CLASS]]
  print(anyObjectType)

  // Convert back to CFString.Type
  let cfStrType2 = anyObjectType as! CFString.Type
  // CHECK: [[STRING_CLASS]]
  print(cfStrType2)

  // CHECK: done
  print("done")
}
testCFStringAnyObjectType()

// https://github.com/apple/swift/issues/43022
func testOptionalCFStringToStringAndNSString() {
  let optCFStr: CFString? = "Swift" as CFString

  // CHECK: {{^}}Optional(Swift){{$}}
  print(optCFStr)

  let swiftStr1 = optCFStr as? String
  let swiftStr2 = optCFStr as String?

  let nsStr1 = optCFStr as? NSString
  let nsStr2 = optCFStr as NSString?

  if let unwrapped = swiftStr1 {
    // CHECK-NEXT: {{^}}Swift{{$}}
    print(unwrapped)
  }
  if let unwrapped = swiftStr2 {
    // CHECK-NEXT: {{^}}Swift{{$}}
    print(unwrapped)
  }
  if let unwrapped = nsStr1 {
    // CHECK-NEXT: {{^}}Swift{{$}}
    print(unwrapped)
  }
  if let unwrapped = nsStr2 {
    // CHECK-NEXT: {{^}}Swift{{$}}
    print(unwrapped)
  }

  // CHECK-NEXT: {{^}}done{{$}}
  print("done")
}
testOptionalCFStringToStringAndNSString()
{'fraction_non_alphanumeric': 0.12505201831044527, 'fraction_numerical': 0.0070744902205576365, 'mean_word_length': 3.703522504892368, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1209996', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1628, 'n_words': 416}