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-2041] subtyping of tuple expressions is not implemented properly, causing compiler crashes #44650

Open
swift-ci opened this issue Jul 10, 2016 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-2041
Radar None
Original Reporter gold_hat (JIRA User)
Type Bug
Environment

Swift 2.2

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

md5: a471581c51906b9e1b69b04097bac50d

Issue Description:

Here is one example of code that will crash the compiler:

let _ = "a".isEmpty ? (2, 3) as (Int, Int) : (2, 3) as (Int?, Int?)

I believe this is caused by an inconsistent application of the principle of subtyping to the components of tuples. This inconsistency is illustrated by these examples:

class C {}
let _ = (C(), 3 as Int) as (Any, Int?)  // WORKS FINE
let _ = (C(), 3) as (C, Int) as (Any, Int?)  // TYPE ERROR
let _ = ((C(), 3), true).0 as (Any, Int?)  // TYPE ERROR

This inconsistency is likely also responsible for the rather comical output when you compile and run this code:

if (2 as Int, 3 as Int) is (Int?, Int?) {
  print("test is true")
} else {
  print("test is false")
}

Here is the output I get:

test.swift:43:25: warning: 'is' test is always true
if (2 as Int, 3 as Int) is (Int?, Int?) {
                        ^
test is false
@swift-ci
Copy link
Collaborator Author

Comment by Aaron Bohannon (JIRA)

It may not be obvious from the examples above, but this issue is also highly relevant in code of a more practical sort. In fact, I'm quite surprised that nobody has reported this issue before, considering that it affects code like this:

func returnPair() -> (C, Int) { return (C(), 3) }
var x: Int? = nil

x = returnPair().1  // WORKS FINE

(_, x) = returnPair()  // TYPE ERROR

@CodaFi
Copy link
Member

CodaFi commented Jul 11, 2016

It's not a subtyping inconsistency so much as a poor application of optional coercion in operators that is at play here (one that I wish we would remove).

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
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. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants