Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-8731] Distinct results for nullity checking #51240

Open
swift-ci opened this issue Sep 12, 2018 · 2 comments
Open

[SR-8731] Distinct results for nullity checking #51240

swift-ci opened this issue Sep 12, 2018 · 2 comments
Labels
access control Feature → modifiers: Access control and access levels bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.2 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-8731
Radar None
Original Reporter luiswdy (JIRA User)
Type Bug

Attachment: Download

Environment

iOS 11, Xcode 9.4, Swift 4.1

iOS 12, Xcode 10 beta 6, Swift 4.2

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

md5: 426f1ff173e8149553c083039341e8b4

Issue Description:

When running a piece of code in Xcode's playground (please refer the attachment), Xcode 9.4 and Xcode 10 got distinct results

In Xcode 9.4 playground I got false, yet Xcode 10 beta 6 I got true. Perhaps swift compiler changed its way to resolve generics and then led the issue. I haven't reasoned which of results sounds more logical; therefore I am not sure whether this is a bug.

@belkadan
Copy link
Contributor

typealias S = String
typealias T = Any

func pickSomething(_ items: [S]) -> S? {
 return items.first
}

func doSomething<T>() -> T? {
 return pickSomething([]) as? T
}

func doSomething() -> S? {
 return pickSomething([])
}

let result = doSomething() as Any? != nil
print("result: \(result)")

@rudkx, you've been looking at these lately, right?

@hamishknight
Copy link
Collaborator

This is similar to https://bugs.swift.org/browse/SR-8725 – and is due to a change where the compiler is now more conservative with unwrapping an optional value that's being cast to a generic placeholder type.

Now, the results you get are consistent with those that you would get in a non-generic context, for example:

typealias S = String

func pickSomething(_ items: [S]) -> S? {
  return items.first
}

func doSomething<T>() -> T? {
  return pickSomething([]) as? T
}

let result1 = doSomething() as Any?
print(result1 as Any) // Optional(nil)

let result2 = pickSomething([]) as? Any?
print(result2 as Any) // Optional(nil)

However this change wasn't properly gated by Swift version – I'm working on restoring the old behaviour under Swift 4 mode (#19217

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added regression swift 4.2 access control Feature → modifiers: Access control and access levels and removed 4.2 regression labels Nov 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
access control Feature → modifiers: Access control and access levels bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.2 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants