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-2549] [CRTP] Non-generic class inheriting from a generic class with Self as generic parameter locks execution #45154

Closed
DevAndArtist mannequin opened this issue Sep 2, 2016 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@DevAndArtist
Copy link
Mannequin

DevAndArtist mannequin commented Sep 2, 2016

Previous ID SR-2549
Radar None
Original Reporter @DevAndArtist
Type Bug
Status Closed
Resolution Done
Environment

Xcode 7.3.1 or Xcode 8 beta 6, Swift 2.2 or latest Swift 3.0 dev snapshot

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 870d56ea45c052912195b3e9cdf64c43

duplicates:

  • SR-263 Implement a general solution to prevent deadlocks when generic types rely on their own metadata recursively

is duplicated by:

  • SR-4553 Recursive generics causes runtime crash
  • SR-6469 instantiating a generic in such a way that it references itself breaks compiler

Issue Description:

I recently spotted this pattern in UIKit

// Generic superclass
open class NSLayoutAnchor<AnchorType : AnyObject> : NSObject {}

// Non-generic subclasses with the pattern: `Sub : Super<Sub>`
open class NSLayoutXAxisAnchor : NSLayoutAnchor<NSLayoutXAxisAnchor> {}
open class NSLayoutYAxisAnchor : NSLayoutAnchor<NSLayoutYAxisAnchor> {}
open class NSLayoutDimension : NSLayoutAnchor<NSLayoutDimension> {}

If you try this patter, the compiler won't warn or raise an error:

print(1)

class Test<T> {}

class Foo : Test<Foo> {
    
    override init() { print(2) }
    func foo() { print(3) }
}

Foo().foo()

print(4)

The example above will only print "1\n" and lock on `Foo()` without any error message or crash.

@swift-ci
Copy link
Collaborator

swift-ci commented Oct 1, 2018

Comment by Ammo Goettsch (JIRA)

This was closed as duplicate, but the issue that it was folded into (SR-263) does not resolve this bug. It should not be marked as duplicate, because crashes still happen with the currently released code (Apple Swift 4.2 release.)

@slavapestov
Copy link
Member

derammo (JIRA User) Please try a 5.0 development snapshot. We resolve issues in the public tracker when the fix is checked in, not when Apple ships a formal release with the change.

@swift-ci
Copy link
Collaborator

Comment by Ammo Goettsch (JIRA)

I should have been more explicit. The Apple release I mentioned contains the fix for SR 263. However, that fix is for a slightly different issue, and does not resolve this bug. I don't have tooling to try a 5.0 dev snapshot and I am not using Swift at the moment, so this bug will die unless someone else picks it up.

@slavapestov
Copy link
Member

deramm (JIRA User) I think I was too eager to dupe the two issues together, but both are fixed in 5.0. In 4.2 we implemented recursive value type metadata. There was additional work on class metadata initialization in 5.0 and I believe all the cases are handled correctly now. I tested the test case in the bug description and it works, and I can confirm similar ones also appear in our test suite.

@swift-ci
Copy link
Collaborator

Comment by Ammo Goettsch (JIRA)

That's exactly what I was trying to say. I had traced the bugs (in the 4.2 time frame?) and noticed the bug I had filed (SR 6469) got resolved by a fix that was actually for an adjacent issue. Since you have separately fixed it since then, I think everything is correct now. Thanks for taking the time to comment so this will be properly documented for the next person who comes here.

@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
Projects
None yet
Development

No branches or pull requests

2 participants