swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.59k stars 10.37k forks source link

[SR-11125] new, recent Swift 5.1 compiler ambiguity error & crash when compiling project #53521

Closed weissi closed 4 years ago

weissi commented 5 years ago
Previous ID SR-11125
Radar rdar://problem/53052121
Original Reporter @weissi
Type Bug
Status Resolved
Resolution Cannot Reproduce

Attachment: Download

Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, CompilerCrash | |Assignee | None | |Priority | Medium | md5: c3526df0957007fcac1ce3f6d9b8dbcb

relates to:

Issue Description:

The attached sample project (folder name qwer) compiled just fine with Swift 5.1 from Xcode 11, beta 3 (Apple Swift version 5.1 (swiftlang-1100.0.212.5 clang-1100.0.28.2)) but throws ambiguity errors and crashes the compiler with the 12th July 2019 snapshot ({{Apple Swift version 5.1-dev (LLVM 688b71e2f9, Swift a062b3ca64) }}).

The project can be described by this Package.swift:

// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "qwer",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/vapor/console-kit.git", .exact("4.0.0-alpha.1")),
        .package(url: "https://github.com/vapor/nio-websocket-client.git", .exact("1.0.0-alpha.1")),
        .package(url: "https://github.com/apple/swift-nio-ssl.git", .exact("2.1.1")),
        .package(url: "https://github.com/apple/swift-log.git", .exact("1.1.0")),
        .package(url: "https://github.com/vapor/vapor.git", .exact("4.0.0-alpha.1.5")),
        .package(url: "https://github.com/vapor/swift-nio-http-client.git", .exact("0.0.0")),
        .package(url: "https://github.com/kylebrowning/APNSwift", .exact("1.0.0")),
        .package(url: "https://github.com/vapor/crypto-kit.git", .exact("4.0.0-alpha.1")),
        .package(url: "https://github.com/apple/swift-nio-http2.git", .exact("1.3.0")),
        .package(url: "https://github.com/apple/swift-nio-extras.git", .exact("1.1.0")),
        .package(url: "https://github.com/vapor/routing-kit.git", .exact("4.0.0-alpha.1")),
        .package(url: "https://github.com/vapor/async-kit.git", .exact("1.0.0-alpha.1")),
        .package(url: "https://github.com/apple/swift-nio.git", .exact("2.4.0")),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "qwer",
            dependencies: [
"APNSwift",
"Vapor",
"NIOWebSocketClient",
"NIOHTTPClient",
"NIOExtras",
"NIOHTTPCompression",
"ConsoleKit",
"RoutingKit",
"NIO",
"_NIO1APIShims",
"NIOTLS",
"NIOHTTP1",
"NIOConcurrencyHelpers",
"NIOFoundationCompat",
"NIOWebSocket",
"NIOTestUtils",
"Logging",
"AsyncKit",
"NIOHTTP2",
"NIOSSL",
"CryptoKit",
            ]),
        .testTarget(
            name: "qwerTests",
            dependencies: ["qwer"]),
    ]
)

and this main.swift

import APNSwift
import Vapor
import NIOWebSocketClient
import NIOHTTPClient
import NIOExtras
import NIOHTTPCompression
import ConsoleKit
import RoutingKit
import NIO
import _NIO1APIShims
import NIOTLS
import NIOHTTP1
import NIOConcurrencyHelpers
import NIOFoundationCompat
import NIOWebSocket
import NIOTestUtils
import Logging
import AsyncKit
import NIOHTTP2
import NIOSSL
import CryptoKit
print("Hello, world!")

repro:

tar xf repro.tar.gz && cd qwer && swift build

for APNSwift, you'll need to have openssl 1.1 installed (just run brew install brew install openssl@1.1 before the repro instructions)

errors are:

<unknown>:0: error: reference to 'ECDSA_SIG' is ambiguous
/private/tmp/qwer/.build/checkouts/swift-nio-ssl/Sources/CNIOBoringSSL/include/CNIOBoringSSL/base.h:388:29: note: candidate found by name lookup is 'ECDSA_SIG'
typedef struct ecdsa_sig_st ECDSA_SIG;
                            ^
/usr/local/Cellar/openssl/1.0.2r/include/openssl/ecdsa.h:81:3: note: candidate found by name lookup is 'ECDSA_SIG'
} ECDSA_SIG;
  ^
Failed to reconstruct type for $sSpySo9ECDSA_SIGaGSgD
Original type:
(bound_generic_enum_type decl=Swift.(file).Optional
  (bound_generic_struct_type decl=Swift.(file).UnsafeMutablePointer
    (type_alias_type decl=CAPNSOpenSSL.(file).ECDSA_SIG)))
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-5.1-DEVELOPMENT-SNAPSHOT-2019-07-12-a.xctoolchain/usr/bin/swift -frontend -c /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSSwiftJWT/APNSwiftJWT.swift /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSSwiftJWT/APNSwiftJWTError.swift /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSSwiftJWT/Data+Base64URL.swift /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSwiftConfiguration.swift /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSwiftConnection.swift /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSwiftErrors.swift /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSwiftPointerTricks.swift /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSwiftRequest.swift /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSwiftRequestContext.swift /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSwiftRequestEncoder.swift /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSwiftResponse.swift -primary-file /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSwiftResponseDecoder.swift -primary-file /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSwiftSigner.swift -primary-file /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSwiftStreamHandler.swift -emit-module-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftResponseDecoder~partial.swiftmodule -emit-module-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftSigner~partial.swiftmodule -emit-module-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftStreamHandler~partial.swiftmodule -emit-module-doc-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftResponseDecoder~partial.swiftdoc -emit-module-doc-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftSigner~partial.swiftdoc -emit-module-doc-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftStreamHandler~partial.swiftdoc -emit-dependencies-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftResponseDecoder.d -emit-dependencies-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftSigner.d -emit-dependencies-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftStreamHandler.d -emit-reference-dependencies-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftResponseDecoder.swiftdeps -emit-reference-dependencies-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftSigner.swiftdeps -emit-reference-dependencies-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftStreamHandler.swiftdeps -target x86_64-apple-macosx10.10 -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -I /private/tmp/qwer/.build/x86_64-apple-macosx/debug -I /private/tmp/qwer/.build/checkouts/swift-nio/Sources/CNIOHTTPParser/include -I /private/tmp/qwer/.build/checkouts/swift-nio-ssl/Sources/CNIOBoringSSLShims/include -I /private/tmp/qwer/.build/checkouts/swift-nio-ssl/Sources/CNIOBoringSSL/include -I /private/tmp/qwer/.build/checkouts/swift-nio/Sources/CNIOSHA1/include -I /private/tmp/qwer/.build/checkouts/swift-nio/Sources/CNIOAtomics/include -I /private/tmp/qwer/.build/checkouts/swift-nio/Sources/CNIODarwin/include -I /private/tmp/qwer/.build/checkouts/swift-nio/Sources/CNIOLinux/include -I /usr/local/Cellar/openssl/1.0.2r/include -I /usr/local/Cellar/openssl/1.0.2r/include -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -enable-testing -g -module-cache-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -color-diagnostics -enable-anonymous-context-mangled-names -Xcc -fmodule-map-file=/private/tmp/qwer/.build/x86_64-apple-macosx/debug/CNIOHTTPParser.build/module.modulemap -Xcc -fmodule-map-file=/private/tmp/qwer/.build/x86_64-apple-macosx/debug/CNIOBoringSSLShims.build/module.modulemap -Xcc -fmodule-map-file=/private/tmp/qwer/.build/checkouts/swift-nio-ssl/Sources/CNIOBoringSSL/include/module.modulemap -Xcc -fmodule-map-file=/private/tmp/qwer/.build/x86_64-apple-macosx/debug/CNIOSHA1.build/module.modulemap -Xcc -fmodule-map-file=/private/tmp/qwer/.build/x86_64-apple-macosx/debug/CNIOAtomics.build/module.modulemap -Xcc -fmodule-map-file=/private/tmp/qwer/.build/x86_64-apple-macosx/debug/CNIODarwin.build/module.modulemap -Xcc -fmodule-map-file=/private/tmp/qwer/.build/x86_64-apple-macosx/debug/CNIOLinux.build/module.modulemap -Xcc -fmodule-map-file=/private/tmp/qwer/.build/checkouts/APNSwift/Sources/CAPNSOpenSSL/module.modulemap -parse-as-library -module-name APNSwift -o /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftResponseDecoder.swift.o -o /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftSigner.swift.o -o /private/tmp/qwer/.build/x86_64-apple-macosx/debug/APNSwift.build/APNSwiftStreamHandler.swift.o -index-store-path /private/tmp/qwer/.build/x86_64-apple-macosx/debug/index/store -index-system-modules 
1.  While emitting IR SIL function "@$s8APNSwift0A6SignerV4sign6digest3NIO10ByteBufferVAH_tKF".
 for 'sign(digest:)' (at /private/tmp/qwer/.build/checkouts/APNSwift/Sources/APNSwift/APNSwiftSigner.swift:35:14)
0  swift                    0x000000010fcebf15 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x000000010fceb205 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x000000010fcec4f8 SignalHandler(int) + 264
3  libsystem_platform.dylib 0x00007fff67ae2b1d _sigtramp + 29
4  libsystem_platform.dylib 0x000000012599b9cc _sigtramp + 3186331340
5  libsystem_c.dylib        0x00007fff679b8a5c abort + 120
6  swift                    0x000000010fde4f1c (anonymous namespace)::IRGenDebugInfoImpl::getOrCreateType(swift::irgen::DebugTypeInfo) (.cold.2) + 92
7  swift                    0x000000010c6002b3 (anonymous namespace)::IRGenDebugInfoImpl::getOrCreateType(swift::irgen::DebugTypeInfo) + 3667
8  swift                    0x000000010c5fa825 (anonymous namespace)::IRGenDebugInfoImpl::emitVariableDeclaration(swift::irgen::IRBuilder&, llvm::ArrayRef<llvm::Value*>, swift::irgen::DebugTypeInfo, swift::SILDebugScope const*, swift::ValueDecl*, llvm::StringRef, unsigned int, swift::irgen::IndirectionKind, swift::irgen::ArtificialKind) + 373
9  swift                    0x000000010c629640 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 55248
10 swift                    0x000000010c618d43 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8051
11 swift                    0x000000010c5352ce swift::irgen::IRGenerator::emitGlobalTopLevel() + 734
12 swift                    0x000000010c5f3a32 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**) + 1282
13 swift                    0x000000010c5f3f22 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, llvm::GlobalVariable**) + 82
14 swift                    0x000000010c4a22d7 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 13943
15 swift                    0x000000010c49dd3a swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3002
16 swift                    0x000000010c446908 main + 696
17 libdyld.dylib            0x00007fff678e1c49 start + 1
weissi commented 5 years ago

this was first reported as https://github.com/apple/swift-nio-ssl/issues/116

weissi commented 5 years ago

CC @Lukasa

weissi commented 5 years ago

@swift-ci create

belkadan commented 5 years ago

The compiler shouldn't crash, but that looks like a real ambiguity to me?

belkadan commented 5 years ago

Oh, I see. The ambiguity is in trying to figure out what the mangled name "typedef ECDSA_SIG from C" means, which is a true ambiguity…but shouldn't crash the compiler. I don't think anything changed here recently; it's just a comparison between a with-asserts and a no-asserts compiler. It's also unclear what should be done about this, since the typedefs really are incompatible and that means that you potentially won't be able to debug this program.

weissi commented 5 years ago

@belkadan So yes, the typedef ECDSA_SIG will be present multiple times in the program but it should never be included into the same compilation unit. All the actual symbols at runtime will not be clashing because they're prefixed in CNIOBoringSSL (something like CNIOBoringSSL_SSL_write instead of SSL_write). Also the header paths are also CNIOBoringSSL/bio.h (instead of the usual ssl/bio.h.

But I can see that it would be troublesome for a debugger to find the right decl but tbh if somebody needs to debug BoringSSL, they'll have a bad day anyway 😛

belkadan commented 5 years ago

The compiler can still see them both because it has to resolve imports. Maybe implementation-only imports will be able to handle this at some point, though.

belkadan commented 5 years ago

(To be clear, I definitely think this is a problem. I just don't know what to do about it.)

weissi commented 5 years ago

Thanks @belkadan, do we understand why it’s working on Swift 5.0.x then? Because it seems to be no problem there...

belkadan commented 5 years ago

I suspect we just didn't have the assertion there, and it was just as broken. But you're right that I haven't confirmed that.

weissi commented 5 years ago

Added a much smaller repro (in clash-crash-repro.tar.gz):

Pacakge.swift

// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "adsf",
    dependencies: [
        .package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .systemLibrary(
            name: "CAPNSOpenSSL",
            pkgConfig: "openssl",
            providers: [
                .apt(["openssl libssl-dev"]),
                .brew(["openssl"]),
            ]
        ),
        .target(
            name: "adsf",
            dependencies: ["NIOSSL", "CAPNSOpenSSL"]),
        .testTarget(
            name: "adsfTests",
            dependencies: ["adsf"]),
    ]
)

and main.swift

import NIOSSL
import CAPNSOpenSSL

let x: UnsafePointer<SHA256_CTX>? = nil
print(x ?? "all good")
weissi commented 5 years ago

@belkadan hmm, but before the crash (the assertion) we hit a compiler error, right? So even if the assertion were taken out, we'd still hit the compiler error which wasn't the case in 5.0.x.

belkadan commented 5 years ago

I am 95% sure the compiler error is coming from work being done for the assertion, but we'll have to test that.

weissi commented 5 years ago

That makes sense! Thanks @belkadan

typesanitizer commented 4 years ago

Looks like this has been fixed with recent compilers (the clash-crash-repro no longer crashes the compiler).