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-14660] Swift Compiler Crashes When Compiling Extensions to Optional #57012

Closed
swift-ci opened this issue May 26, 2021 · 3 comments · Fixed by #59171
Closed

[SR-14660] Swift Compiler Crashes When Compiling Extensions to Optional #57012

swift-ci opened this issue May 26, 2021 · 3 comments · Fixed by #59171
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-14660
Radar rdar://problem/78514055
Original Reporter VaslD (JIRA User)
Type Bug

Attachment: Download

Environment

macOS 11.4 (20F71)

Xcode 12.5 (12E262)

Swift 5.4 (swiftlang-1205.0.26.9 clang-1205.0.19.55)

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

md5: 511faf1769264cce28ba907b89fbd44b

Issue Description:

I'm trying to extend Swift's Optional type to add constructors that take Optionals associated with other types so I don't have to guard multiple times when converting between primitive types. I simply expect the whole statement to cascade to nil if it encounters nil anywhere or the constructor fails.

Anyway, I wrote the following code (included in the reproducible):

import Foundation

public extension Optional where Wrapped: BinaryInteger {
    init?<T>(_ value: Optional<T>) where T: BinaryFloatingPoint {
        guard let value = value else { return nil }
        let intValue = Wrapped(value)
        self.init(intValue)
    }
}

public extension Optional where Wrapped: BinaryFloatingPoint {
    init?<T>(_ value: Optional<T>) where T: BinaryInteger {
        guard let value = value else { return nil }
        let floatValue = Wrapped(value)
        self.init(floatValue)
    }
}

Whatever project I put this file into, it crashes Swift compiler with Segmentation fault: 11; and Xcode stops helping me work due to internal error.

Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c /Users/yi.ding.o/Developer/CrashCompiler/CrashCompiler/ViewController.swift /Users/yi.ding.o/Developer/CrashCompiler/CrashCompiler/AppDelegate.swift -primary-file /Users/yi.ding.o/Developer/CrashCompiler/CrashCompiler/Swift.Optional.swift -emit-module-path /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/Objects-normal/x86_64/Swift.Optional~partial.swiftmodule -emit-module-doc-path /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/Objects-normal/x86_64/Swift.Optional~partial.swiftdoc -emit-module-source-info-path /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/Objects-normal/x86_64/Swift.Optional~partial.swiftsourceinfo -serialize-diagnostics-path /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/Objects-normal/x86_64/Swift.Optional.dia -emit-dependencies-path /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/Objects-normal/x86_64/Swift.Optional.d -emit-reference-dependencies-path /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/Objects-normal/x86_64/Swift.Optional.swiftdeps -target x86_64-apple-ios11.0-simulator -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.5.sdk -I /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Products/Debug-iphonesimulator -F /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Products/Debug-iphonesimulator -enable-testing -g -module-cache-path /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D DEBUG -serialize-debugging-options -Xcc -working-directory -Xcc /Users/yi.ding.o/Developer/CrashCompiler -enable-anonymous-context-mangled-names -Xcc -I/Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/CrashCompiler-generated-files.hmap -Xcc -I/Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/CrashCompiler-own-target-headers.hmap -Xcc -I/Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/CrashCompiler-all-target-headers.hmap -Xcc -iquote -Xcc /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/CrashCompiler-project-headers.hmap -Xcc -I/Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Products/Debug-iphonesimulator/include -Xcc -I/Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/DerivedSources-normal/x86_64 -Xcc -I/Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/DerivedSources/x86_64 -Xcc -I/Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/DerivedSources -Xcc -DDEBUG=1 -target-sdk-version 14.5 -module-name CrashCompiler -o /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Build/Intermediates.noindex/CrashCompiler.build/Debug-iphonesimulator/CrashCompiler.build/Objects-normal/x86_64/Swift.Optional.o -index-store-path /Users/yi.ding.o/Library/Developer/Xcode/DerivedData/CrashCompiler-brfrfsiajyrrhrdfgtdbdsmlngdv/Index/DataStore -index-system-modules 
1.  Apple Swift version 5.4 (swiftlang-1205.0.26.9 clang-1205.0.19.55)
2.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/yi.ding.o/Developer/CrashCompiler/CrashCompiler/Swift.Optional.swift")
3.  While silgen emitConstructor SIL function "@$sSq13CrashCompilerSzRzlEyxSgSgqd__SgcSBRd__lufC".
 for 'init(_:)' (at /Users/yi.ding.o/Developer/CrashCompiler/CrashCompiler/Swift.Optional.swift:4:5)
0  swift-frontend           0x000000010a20ae85 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift-frontend           0x000000010a209e78 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x000000010a20b446 SignalHandler(int) + 262
3  libsystem_platform.dylib 0x00007fff2051cd7d _sigtramp + 29
4  swift-frontend           0x00000001068edb89 swift::Type::transformRec(llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>) const + 57
5  swift-frontend           0x000000010600a793 swift::Lowering::AbstractionPattern::requiresClass() const + 339
6  swift-frontend           0x00000001060a1119 swift::CanTypeVisitor<(anonymous namespace)::LowerType, swift::Lowering::TypeLowering*, swift::Lowering::AbstractionPattern, swift::Lowering::IsTypeExpansionSensitive_t>::visit(swift::CanType, swift::Lowering::AbstractionPattern, swift::Lowering::IsTypeExpansionSensitive_t) + 281
7  swift-frontend           0x00000001060a245f swift::Lowering::TypeConverter::getTypeLoweringForLoweredType(swift::Lowering::AbstractionPattern, swift::CanType, swift::TypeExpansionContext, swift::Lowering::IsTypeExpansionSensitive_t) + 527
8  swift-frontend           0x000000010594154c swift::Lowering::SILGenFunction::emitManagedBufferWithCleanup(swift::SILValue) + 188
9  swift-frontend           0x00000001058ad506 swift::Lowering::CleanupCloner::clone(swift::SILValue) const + 310
10 swift-frontend           0x0000000105906f6d swift::Lowering::SILGenBuilder::createUncheckedTakeEnumDataAddr(swift::SILLocation, swift::Lowering::ManagedValue, swift::EnumElementDecl*, swift::SILType) + 477
11 swift-frontend           0x0000000105924be7 swift::Lowering::SILGenFunction::emitUncheckedGetOptionalValueFrom(swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::TypeLowering const&, swift::Lowering::SGFContext) + 295
12 swift-frontend           0x0000000105962674 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 63444
13 swift-frontend           0x0000000105945957 swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 759
14 swift-frontend           0x00000001059d39fe swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 270
15 swift-frontend           0x000000010591a99b swift::Lowering::SILGenFunction::emitValueConstructor(swift::ConstructorDecl*) + 4123
16 swift-frontend           0x00000001058bffb6 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 5990
17 swift-frontend           0x00000001058c2b7e emitOrDelayFunction(swift::Lowering::SILGenModule&, swift::SILDeclRef, bool) + 206
18 swift-frontend           0x00000001058c2ffb swift::Lowering::SILGenModule::emitConstructor(swift::ConstructorDecl*) + 667
19 swift-frontend           0x00000001059e8ad3 swift::ASTVisitor<SILGenExtension, void, void, void, void, void, void>::visit(swift::Decl*) + 147
20 swift-frontend           0x00000001059e49db SILGenExtension::emitExtension(swift::ExtensionDecl*) + 59
21 swift-frontend           0x00000001058c8ddc swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 1164
22 swift-frontend           0x00000001058c5aae swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 4526
23 swift-frontend           0x00000001059d350d swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> > (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 205
24 swift-frontend           0x00000001058cb4ac llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 780
25 swift-frontend           0x000000010542ac75 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6197
26 swift-frontend           0x00000001053ace82 main + 1266
27 libdyld.dylib            0x00007fff204f2f5d start + 1
error: Segmentation fault: 11 (in target 'CrashCompiler' from project 'CrashCompiler')

I put it in a blank iOS project and uploaded a reproducible.

P.S.: I'm not 100% sure the code above should compile since I've got no way to know now.

@typesanitizer
Copy link

Triggers an assertion on main.

Assertion failed: (resultTy.getASTType().getOptionalObjectType() && "input was not a nested optional value"), function flattenOptional, file SILGenExpr.cpp, line 3928. 
extension Optional where Wrapped: BinaryInteger {
    init?<T>(_ value: Optional<T>) where T: BinaryFloatingPoint {
    return nil
    }
}

extension Optional where Wrapped: BinaryFloatingPoint {
    init?<T>(_ value: Optional<T>) where T: BinaryInteger {
        self.init(Wrapped(value!))
    }
}

@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis
Copy link
Collaborator

Reduced:

extension Optional {
  init?(x: Bool) {
    self.init(x: x)
  }
}

@AnthonyLatsis AnthonyLatsis self-assigned this May 14, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
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 crash Bug: A crash, i.e., an abnormal termination of software
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants