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-6897] Compiler crashes when giving enum a raw value type of ExpressibleByStringLiteral struct #49446

Closed
itaiferber opened this issue Feb 1, 2018 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@itaiferber
Copy link
Contributor

Previous ID SR-6897
Radar rdar://problem/37133177
Original Reporter @itaiferber
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: e4ee1ad5598af9e29fb79ab35d7c4887

Issue Description:

The following code compiles:

struct S : ExpressibleByStringLiteral {
    init(stringLiteral: String) {}
}

The following does not:

struct S : ExpressibleByStringLiteral {
    init(stringLiteral: String) {}
}

enum E : S {}

producing

Untitled.swift:7:10: error: an enum with no cases cannot declare a raw type
enum E : S {}
         ^
Untitled.swift:7:10: error: 'E' declares raw type 'S', but does not conform to RawRepresentable and conformance could not be synthesized
enum E : S {}
         ^
Untitled.swift:7:10: error: RawRepresentable conformance cannot be synthesized because raw type 'S' is not Equatable
enum E : S {}
         ^
Swift.RawRepresentable:94:20: note: protocol requires nested type 'RawValue'; do you want to add it?
    associatedtype RawValue
                   ^

Adding RawValue as recommended cuts down on the errors:

struct S : ExpressibleByStringLiteral {
    init(stringLiteral: String) {}
}

enum E : S {
    typealias RawValue = S
}

produces

Untitled.swift:7:10: error: an enum with no cases cannot declare a raw type
enum E : S {
         ^

Adding a case crashes the compiler:

import Foundation

struct S : ExpressibleByStringLiteral {
    init(stringLiteral: String) {}
}

enum E : S {
    typealias RawValue = S
    case a = "a"
}

produces

0  swift                    0x00000001084517da PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x0000000108450c96 SignalHandler(int) + 598
2  libsystem_platform.dylib 0x00007fff63ec2dfa _sigtramp + 26
3  libsystem_platform.dylib 0x00007f817a2b38b0 _sigtramp + 373230288
4  swift                    0x0000000105577a69 swift::SILWitnessVisitor<(anonymous namespace)::SILGenConformance>::visitProtocolDecl(swift::ProtocolDecl*) + 761
5  swift                    0x0000000105576788 swift::Lowering::SILGenModule::getWitnessTable(swift::ProtocolConformance*) + 488
6  swift                    0x000000010557b194 (anonymous namespace)::SILGenType::emitType() + 2308
7  swift                    0x000000010549b67a swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 74
8  swift                    0x000000010549a93b swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 1115
9  swift                    0x000000010549c2c9 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool) + 841
10 swift                    0x0000000104bfae14 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 12980
11 swift                    0x0000000104bf62d9 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 8697
12 swift                    0x0000000104ba739e main + 13918
13 libdyld.dylib            0x00007fff63cd1f29 start + 1
14 libdyld.dylib            0x000000000000000e start + 2620580070
Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file Untitled.swift -target x86_64-apple-darwin18.0.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -module-name Untitled -o /var/folders/0_/2gdgjnt94rj7hqjcrnkh4m3c0000gn/T/Untitled-ea6dbb.o 
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: compile command failed due to signal 11 (use -v to see invocation)
@itaiferber
Copy link
Contributor Author

@swift-ci Create

@itaiferber
Copy link
Contributor Author

@itaiferber
Copy link
Contributor Author

Interestingly, looks like this is fixed by adding Equatable conformance:

struct S : ExpressibleByStringLiteral, Equatable {
    let value: String
    init(stringLiteral: String) {
        value = stringLiteral
    }
    
    static func ==(_ lhs: S, _ rhs: S) -> Bool {
        return lhs.value == rhs.value
    }
}

enum E : S {
    typealias RawValue = S
    case a = "a"
}

print(E.a) // => a

@swift-ci
Copy link
Collaborator

swift-ci commented Feb 2, 2018

Comment by Tommaso Piazza (JIRA)

@itaiferber Equatable is also what makes https://bugs.swift.org/browse/SR-6899 compile in the first place with `Foo` backed by RawValue String

@theblixguy
Copy link
Collaborator

I was investigating this - another fix is to add a rawValue computed property and rawValue initialiser.

@theblixguy
Copy link
Collaborator

Here's a fix: #27050

@slavapestov
Copy link
Member

#29173

@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

4 participants