swiftlang / swift

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

[SR-2603] Crash with default function parameter body #45208

Open BigZaphod opened 8 years ago

BigZaphod commented 8 years ago
Previous ID SR-2603
Radar rdar://problem/28787005
Original Reporter @BigZaphod
Type Bug

Attachment: Download

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

Issue Description:

I get the following crash in the Xcode 8 GM (which did not occur in prior Xcode betas):

CompileSwift normal x86_64 /Users/sean/Development/TEMP/SwiftCrash/SwiftCrash/Pathfinding.swift
    cd /Users/sean/Development/TEMP/SwiftCrash
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file /Users/sean/Development/TEMP/SwiftCrash/SwiftCrash/Pathfinding.swift /Users/sean/Development/TEMP/SwiftCrash/SwiftCrash/BinaryHeap.swift /Users/sean/Development/TEMP/SwiftCrash/SwiftCrash/PriorityQueue.swift /Users/sean/Development/TEMP/SwiftCrash/SwiftCrash/ViewController.swift /Users/sean/Development/TEMP/SwiftCrash/SwiftCrash/AppDelegate.swift -target x86_64-apple-ios10.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator10.0.sdk -I /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Products/Debug-iphonesimulator -F /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Products/Debug-iphonesimulator -enable-testing -g -module-cache-path /Users/sean/Library/Developer/Xcode/DerivedData/ModuleCache -D DEBUG -serialize-debugging-options -Xcc -I/Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/SwiftCrash-generated-files.hmap -Xcc -I/Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/SwiftCrash-own-target-headers.hmap -Xcc -I/Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/SwiftCrash-all-target-headers.hmap -Xcc -iquote -Xcc /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/SwiftCrash-project-headers.hmap -Xcc -I/Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Products/Debug-iphonesimulator/include -Xcc -I/Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/DerivedSources/x86_64 -Xcc -I/Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -working-directory/Users/sean/Development/TEMP/SwiftCrash -emit-module-doc-path /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/Objects-normal/x86_64/Pathfinding~partial.swiftdoc -Onone -module-name SwiftCrash -emit-module-path /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/Objects-normal/x86_64/Pathfinding~partial.swiftmodule -serialize-diagnostics-path /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/Objects-normal/x86_64/Pathfinding.dia -emit-dependencies-path /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/Objects-normal/x86_64/Pathfinding.d -emit-reference-dependencies-path /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/Objects-normal/x86_64/Pathfinding.swiftdeps -o /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/Objects-normal/x86_64/Pathfinding.o

0  swift                    0x000000011049bb6d PrintStackTraceSignalHandler(void*) + 45
1  swift                    0x000000011049b5b6 SignalHandler(int) + 470
2  libsystem_platform.dylib 0x00007fffb1c86bba _sigtramp + 26
3  libsystem_platform.dylib 0x0000000000000002 _sigtramp + 1312265314
4  swift                    0x000000010dafc494 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::SGFContext) + 8884
5  swift                    0x000000010db0411c (anonymous namespace)::TranslateArguments::translateSingle(swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::ManagedValue, swift::SILParameterInfo) + 652
6  swift                    0x000000010dafe59b (anonymous namespace)::TranslateArguments::translate(swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType) + 1755
7  swift                    0x000000010dafe378 (anonymous namespace)::TranslateArguments::translate(swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType) + 1208
8  swift                    0x000000010dafd258 createThunk(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::TypeLowering const&) + 1784
9  swift                    0x000000010dafa949 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::SGFContext) + 1897
10 swift                    0x000000010dac0409 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 59017
11 swift                    0x000000010db121ab swift::Lowering::SILGenFunction::emitReturnExpr(swift::SILLocation, swift::Expr*) + 491
12 swift                    0x000000010dad2f4f swift::Lowering::SILGenFunction::emitGeneratorFunction(swift::SILDeclRef, swift::Expr*) + 431
13 swift                    0x000000010da4bc8a swift::Lowering::SILGenModule::emitAbstractFuncDecl(swift::AbstractFunctionDecl*) + 1738
14 swift                    0x000000010da4b1af swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 31
15 swift                    0x000000010db17907 swift::Lowering::SILGenModule::visitExtensionDecl(swift::ExtensionDecl*) + 535
16 swift                    0x000000010da5819b swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 1835
17 swift                    0x000000010da59e2d swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool, bool) + 1629
18 swift                    0x000000010d8b116f performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 19503
19 swift                    0x000000010d8aa265 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 17029
20 swift                    0x000000010d86782d main + 8685
21 libdyld.dylib            0x00007fffb1a7a255 start + 1
22 libdyld.dylib            0x0000000000000040 start + 1314414060
Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file /Users/sean/Development/TEMP/SwiftCrash/SwiftCrash/Pathfinding.swift /Users/sean/Development/TEMP/SwiftCrash/SwiftCrash/BinaryHeap.swift /Users/sean/Development/TEMP/SwiftCrash/SwiftCrash/PriorityQueue.swift /Users/sean/Development/TEMP/SwiftCrash/SwiftCrash/ViewController.swift /Users/sean/Development/TEMP/SwiftCrash/SwiftCrash/AppDelegate.swift -target x86_64-apple-ios10.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator10.0.sdk -I /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Products/Debug-iphonesimulator -F /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Products/Debug-iphonesimulator -enable-testing -g -module-cache-path /Users/sean/Library/Developer/Xcode/DerivedData/ModuleCache -D DEBUG -serialize-debugging-options -Xcc -I/Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/SwiftCrash-generated-files.hmap -Xcc -I/Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/SwiftCrash-own-target-headers.hmap -Xcc -I/Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/SwiftCrash-all-target-headers.hmap -Xcc -iquote -Xcc /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/SwiftCrash-project-headers.hmap -Xcc -I/Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Products/Debug-iphonesimulator/include -Xcc -I/Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/DerivedSources/x86_64 -Xcc -I/Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -working-directory/Users/sean/Development/TEMP/SwiftCrash -emit-module-doc-path /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/Objects-normal/x86_64/Pathfinding~partial.swiftdoc -Onone -module-name SwiftCrash -emit-module-path /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/Objects-normal/x86_64/Pathfinding~partial.swiftmodule -serialize-diagnostics-path /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/Objects-normal/x86_64/Pathfinding.dia -emit-dependencies-path /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/Objects-normal/x86_64/Pathfinding.d -emit-reference-dependencies-path /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/Objects-normal/x86_64/Pathfinding.swiftdeps -o /Users/sean/Library/Developer/Xcode/DerivedData/SwiftCrash-gtkymxppldrkvievofpbiaqxdoab/Build/Intermediates/SwiftCrash.build/Debug-iphonesimulator/SwiftCrash.build/Objects-normal/x86_64/Pathfinding.o 
1.  While silgen emitDefaultArgGenerator  SIL function @_TIFE10SwiftCrashPS_11Pathfinding4pathFT4fromwx4Node2towxS1_6isGoalFTwxS1_wxS1__Sb_GSqGVS_4PathwxS1___A1_ for expression at [/Users/sean/Development/TEMP/SwiftCrash/SwiftCrash/Pathfinding.swift:78:81 - line:78:92] RangeText="{ $0 == $1 }"
2.  While emitting reabstraction thunk in SIL function @_TTRGRx10SwiftCrash11PathfindingrXFo_iVs11AnyHashableiS1__dSb_XFo_iwx4NodeiwxS2__dSb_
belkadan commented 8 years ago

Reduced:

func test<T: Hashable>(check: (T, T) -> Bool = { $0 == $1 }) {}
belkadan commented 8 years ago

@slavapestov, you've been working with generics a lot lately. Any ideas?

swift-ci commented 8 years ago

Comment by Myke Olson (JIRA)

@swift-ci create

slavapestov commented 8 years ago

This is weird, why do we have AnyHashable showing up here?

(function_conversion_expr implicit type='(T, T) -> Bool' location=def.swift:1:48 range=[def.swift:1:48 - line:1:59]
  (closure_expr type='(AnyHashable, AnyHashable) -> Bool' location=def.swift:1:48 range=[def.swift:1:48 - line:1:59] discriminator=0 single-expression
    (parameter_list
      (parameter "$0" type='AnyHashable')
      (parameter "$1" type='AnyHashable'))
    (binary_expr type='Bool' location=def.swift:1:53 range=[def.swift:1:50 - line:1:56] nothrow
      (dot_syntax_call_expr implicit type='(AnyHashable, AnyHashable) -> Bool' location=def.swift:1:53 range=[def.swift:1:53 - line:1:53] nothrow
        (declref_expr type='(AnyHashable.Type) -> (AnyHashable, AnyHashable) -> Bool' location=def.swift:1:53 range=[def.swift:1:53 - line:1:53] decl=Swift.(file).AnyHashable.== function_ref=unapplied specialized=no)
        (type_expr implicit type='AnyHashable.Type' location=def.swift:1:53 range=[def.swift:1:53 - line:1:53] typerepr='AnyHashable'))
      (tuple_expr implicit type='(AnyHashable, AnyHashable)' location=def.swift:1:50 range=[def.swift:1:50 - line:1:56]
        (declref_expr type='AnyHashable' location=def.swift:1:50 range=[def.swift:1:50 - line:1:50] decl=def.(file).func decl.default argument initializer.explicit closure discriminator=0.$0@def.swift:1:48 function_ref=unapplied specialized=no)
        (declref_expr type='AnyHashable' location=def.swift:1:56 range=[def.swift:1:56 - line:1:56] decl=def.(file).func decl.default argument initializer.explicit closure discriminator=0.$1@def.swift:1:48 function_ref=unapplied specialized=no)))))
slavapestov commented 8 years ago

If I change 'T : Hashable' to 'T : Equatable', there's no problem, so there's some kind of 'erase type variable to AnyHashable' thing that's kicking in erroneously.

slavapestov commented 8 years ago

Here's the culprit in CSSimplify.cpp:

    // T -> AnyHashable.
    if (isAnyHashableType(desugar2)) {
      // Don't allow this in operator contexts or we'll end up allowing
      // 'T() == U()' for unrelated T and U that just happen to be Hashable.
      // We can remove this special case when we implement operator hiding.
      if (kind != ConstraintKind::OperatorArgumentConversion) {
        conversionsOrFixes.push_back(
                              ConversionRestrictionKind::HashableToAnyHashable);
      }
    }
slavapestov commented 8 years ago

For some reason, we pick the AnyHashable:== overload, instead of Equatable:==, even though the former requires a conversion. We don't even consider Equatable:== at all.

slavapestov commented 8 years ago

@DougGregor or @rudkx any ideas?

slavapestov commented 8 years ago

This has nothing to do with default arguments either:

func test<T : Hashable>(t: T) {
  let check: (T, T) -> Bool = { $0 == $1 }
}
slavapestov commented 8 years ago

This works, but if I omit the type annotations on x and y, it fails:

func test<T : Hashable>(t: T) -> Bool {
  let check: (T, T) -> Bool = { (x: T, y: T) in x == y }
}

So when we're inferring argument types for the closure, we prefer non-generic ones for some reason?

slavapestov commented 8 years ago

So I realized there really is a bug here, SILGen needs to be able to emit a archetype to AnyHashable erasure, because you can request one with a function cast. However, the other issue in overload resolution is still worth investigating.