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-10941] Stack overflow from swift_getAssociatedConformanceWitness infinite recursion #53332

Closed
swift-ci opened this issue Jun 16, 2019 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10941
Radar rdar://problem/77358477
Original Reporter TadeasKriz (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Xcode 10.2.1

Swift 5.0.1

macOS 10.14.5

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

md5: ef7e675cf10cb3ea988af2eef3d02ec8

Issue Description:

While working on some heavy generics/associated type constraints, I got to a state where I have a code that can be compiled successfully, but crashes in runtime.

From what I can gather, it seems to be an infinite recursion leading to a stack overflow.

This is the stack trace in question:

I also compiled a Swift file that reproduces the problem:

public protocol SupportedPropertyType { }


public protocol TypedSupportedType: SupportedPropertyType where FactoryType.BuildType == Self {
    associatedtype FactoryType: TypedSupportedTypeFactory


    var typedFactory: FactoryType { get }
}


public protocol SupportedTypeFactory { }


public protocol TypedSupportedTypeFactory: SupportedTypeFactory {
    associatedtype BuildType: SupportedPropertyType
}


public enum PropertyValue<T: TypedSupportedTypeFactory> {
    case value(T.BuildType)


    var value: SupportedPropertyType {
        switch self {
        case .value(let value):
            return value
        }
    }
}


public final class EnumTypeFactory<Enum: TypedSupportedType>: TypedSupportedTypeFactory {
    public typealias BuildType = Enum
}


public protocol EnumPropertyType: RawRepresentable, TypedSupportedType, CaseIterable where FactoryType == EnumTypeFactory<Self>, RawValue == String { }


public extension EnumPropertyType {
    static var typeFactory: FactoryType {
        return EnumTypeFactory<Self>()
    }


    var typedFactory: FactoryType {
        return Self.typeFactory
    }
}


public enum ContentMode: String, EnumPropertyType {
    case bottom
}


let value = PropertyValue<EnumTypeFactory<ContentMode>>.value(ContentMode.bottom)


print("ok", value)
print("not ok", value.value)

After reducing the reproducer to the form you see above, I found that when I change the protocol TypedSupportedTypeFactory from:

public protocol TypedSupportedTypeFactory: SupportedTypeFactory {
    associatedtype BuildType: SupportedPropertyType
}

to:

public protocol TypedSupportedTypeFactory: SupportedTypeFactory {
    associatedtype BuildType: TypedSupportedType
}

then it still compiles successfully and doesn't crash in runtime anymore.

Also, before the change, Swift compiler was producing a warning that the TypedSupportedType's conformance to SupportedPropertyType is redundant.

@belkadan
Copy link
Contributor

cc @mikeash, @rjmccall

@rjmccall
Copy link
Member

cc @DougGregor, @slavapestov

Looks like for some reason the conformance access path for `Enum: SupportedPropertyType` in the context of `EnumTypeFactory` is not the expected super-protocol constraint of the conformance of `Enum: TypeSupportedType`, and instead is this trivial recursion.

@ffried
Copy link
Contributor

ffried commented Jan 29, 2021

We just ran into the same problem.
The superfluous warning mentioned by TadeasKriz (JIRA User) is interesting, because it was the reason behind opening SR-13898. We simply removed the conformance constraint to fix the warning. But apparently there's more to it than just a warning. Or at least it seems that way.

@slavapestov
Copy link
Member

I think this is a GSB bug.

@slavapestov
Copy link
Member

@swift-ci create

@slavapestov
Copy link
Member

#37466

@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
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. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

6 participants