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-10300] Trying to implement default parameters via an extension for a class protocol crashes with stack overflow #52700

Closed
swift-ci opened this issue Apr 3, 2019 · 6 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

swift-ci commented Apr 3, 2019

Previous ID SR-10300
Radar rdar://problem/49609418
Original Reporter crushforth_rnp (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee crushforth_rnp (JIRA)
Priority Medium

md5: fc0fd85f901e012aa471ef2930c4ece7

Issue Description:

If you want to have a protocol with default parameters via an extension it works fine for straight protocols but you get a stackoverflow/recursion if it’s a class protocol.

This works fine and does what you’d expect

protocol Foo {     
    func bar(parameter: String) 
}

extension Foo {
     func bar(parameter: String = "default") {         
        bar(parameter: parameter)     
   }
}

can simply call `bar()`

This will crash your app with a stack overflow

protocol Foo : class { <----- !! Note `class` in the inheritance list (AnyObject also crashes)      
    func bar(parameter: String) 
}

extension Foo {
     func bar(parameter: String = "default")

     {         
         // Stack overflow here         
        bar(parameter: parameter)      
     }

}

calling `bar()` will crash

We had two examples like this in the code. One worked and one didn’t the only difference was the class inheritance. Unfortunately you need class/AnyObject inheritance for weak delegates. I tried to reproduce this in a playground but I crashed the playground and I didn't save the file.

@belkadan
Copy link
Contributor

belkadan commented Apr 4, 2019

That is a weird (unintentional) difference for sure. In this case you could work around it by just defining the extension method as foo(), but if you had multiple defaults that wouldn't work.

@theblixguy
Copy link
Collaborator

I can't reproduce this on 5.1 (near master).

protocol Foo: class {     
    func bar(parameter: String) 
}

extension Foo {
     func bar(parameter: String = "default") {         
        bar(parameter: parameter)     
   }
}

class A: Foo {
  func bar(parameter: String) {
    print(parameter)
  }
}

let a = A()
a.bar() // prints 'default'

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 4, 2019

Comment by Craig (JIRA)

We're currently using 4.2 but we're planning on updating updating to 5 later this week. I could give it a try again when we update. Does it still work fine on 4.2? I'm 95% positive there was an issue in our app and I tried it in a playground and and crashed the playground. Let me try it again quickly now....

@theblixguy
Copy link
Collaborator

Yep, works for me on 5.0 (Xcode 10.2) and 4.2.1 (tried on https://repl.it/languages/swift)

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 4, 2019

Comment by Craig (JIRA)

You're right. You can probably close this. Seems to be working for me now. I'm going to revert my fix in our code and get it back crashing again and see if I can narrow it down to something concrete and easy to reproduce. Sorry for wasting anyones time.

@xedin
Copy link
Member

xedin commented Mar 31, 2021

Please close this issue as per discussion thread.

@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

4 participants