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-177] Swift does not report a type error at compile-time -- program crashes at runtime instead #42799

Closed
swift-ci opened this issue Dec 10, 2015 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-177
Radar None
Original Reporter davidrichards (JIRA User)
Type Bug
Status Closed
Resolution Invalid
Environment

The swift binary ("swift build") from Xcode Swift 2.2 Snapshot, December 1, 2015.
OS X 10.11.2.

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

md5: 66f3b25f0cbbb2f3a680f50012948c63

Issue Description:

Problem:
This code (all in main.swift) crashes at runtime at the line indicated:

protocol DoubleProtocol {}
extension Double : DoubleProtocol {}
func -(d1: DoubleProtocol, d2: DoubleProtocol) -> Double {
    return d1 - d2 //Crash here
}

extension ClosedInterval where Bound : DoubleProtocol {
    func isCloseTo(interval: ClosedInterval) -> Bool {
        if abs(self.start - interval.start) < 1e-3 && abs(self.end - interval.end) < 1e-3 {
            return true
        } else {
            return false
        }
    }
}

let close = ClosedInterval<Double>(0, 1).isCloseTo(ClosedInterval<Double>(0, 1))
print("they're close: \(close)")

Solution:
As expected, if I turn the crashing line into the following line, things work properly again:

return (d1 as! Double) - (d2 as! Double) //No crash anymore.

Expected behavior:
The compiler should have shown a compile-time error as "d1 - d2" is meaningless because there is no "-" operator implemented for the DoubleProtocol.

@swift-ci
Copy link
Collaborator Author

Comment by Tadeas Kriz (JIRA)

But there is a "-" operator implemented for DoubleProtocol. What you see is probably a stack overflow, because you are calling the same operator function over and over again.

Casting it back to double is unsafe and might fail if in future someone would do `extension Float: DoubleProtocol`, or something similar.

EDIT: It would be cool if the compiler told you that it is a recursive call and possibly that is an infinite recursive call as well.

@swift-ci
Copy link
Collaborator Author

Comment by David Richards (JIRA)

TadeasKriz (JIRA User) My bad, of course "-" for DoubleProtocol was my own creation.You're exactly right. The run-time crash I was having was indeed an infinite recursion. I agree. It would be cool if the compiler detected the infinite recursion.

@swift-ci
Copy link
Collaborator Author

Comment by Tadeas Kriz (JIRA)

I think that closing this issue would be the right way to continue now.

@ahoppen
Copy link
Contributor

ahoppen commented Mar 25, 2016

As mentioned in the comments, this is not a bug

@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. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants