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-12486] Value.self is Any.Protocol broke in 5.1, got bad warning in 5.2 #54927

Closed
lilyball mannequin opened this issue Apr 1, 2020 · 10 comments
Closed

[SR-12486] Value.self is Any.Protocol broke in 5.1, got bad warning in 5.2 #54927

lilyball mannequin opened this issue Apr 1, 2020 · 10 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@lilyball
Copy link
Mannequin

lilyball mannequin commented Apr 1, 2020

Previous ID SR-12486
Radar rdar://problem/62201613
Original Reporter @lilyball
Type Bug
Status Resolved
Resolution Done
Environment

Swift 5.1.5, Swift 5.2

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee @tbkka
Priority Medium

md5: 868c763c40543f8ea25aec14dbda84d3

Issue Description:

I have some generic code that, in an attempt to check if the generic type is literally Any, evaluates Value.self is Any.Protocol. This used to work correctly up through Swift 5.0.3. Testing in Swift 5.1.5, this code now always evaluates to true, which is a surprise. Testing in Swift 5.2, this code not only evaluates to true, but triggers a compiler warning that erroneously claims it will always evaluate to false.

func isAny<Value>(_ type: Value.Type) -> Bool {
  return Value.self is Any.Protocol
}

print(isAny(Int.self))
print(isAny(Any.self))
print(isAny(Any?.self))
false
true
false
true
true
true
unnamed.swift:2:21: warning: cast from 'Value.Type' to unrelated type 'Any.Protocol' always fails
  return Value.self is Any.Protocol
         ~~~~~~~~~~ ^  ~~~~~~~~~~~~
true
true
true
@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Apr 1, 2020

Bizarrely, in my source project where I ran into this, my tests all pass with Swift 5.1.5 and as best as I can tell, it's because the Value.self is Any.Protocol expression is returning false where Value is Any? even though my sample code shows that as returning true.

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Apr 1, 2020

Oh dear. Behavior changes under optimization with both Swift 5.1.5 and Swift 5.2. When I turn on -O both start returning the correct values (the ones that 5.0.3 returned always).

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Apr 1, 2020

Swift 5.1.5 also changes behavior based on deployment target. With Swift 5.1.5, -target x86_64-apple-macos10.9 always produces the correct answer (regardless of optimization) but -target x86_64-apple-macos10.15 (or no target) produces the wrong answer without optimization. Swift 5.2 produces the wrong answer without optimization regardless of target.

@slavapestov
Copy link
Member

There are two bugs here. The false positive warning is actually a different code path than the runtime cast implementation...

@beccadax
Copy link
Contributor

@swift-ci create

@tbkka
Copy link
Contributor

tbkka commented May 2, 2020

Lily,

Thanks for the report. I'm trying to dig through and figure out what changed and why.

As a workaround can you change the original code to `return Value.self == Any.self`? If that works for you, it should actually be a lot faster.

@tbkka
Copy link
Contributor

tbkka commented May 8, 2020

I believe this is fixed by #31662

@LucianoPAlmeida
Copy link
Collaborator

@tbkka@lilyball The false positive warning, in this case, is fixed on master by #32234 🙂

@tbkka
Copy link
Contributor

tbkka commented Jul 6, 2020

Nominated for inclusion in Swift 5.3: #32725

@tbkka
Copy link
Contributor

tbkka commented Jan 5, 2021

I believe this is fixed in Swift 5.3. Please let me know if you see any further problems in this area.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

4 participants