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-5546] Assertion failed: (isa<X>(Val) && "cast<Ty>() argument of incompatible type!") #48118

Closed
weissi opened this issue Jul 24, 2017 · 8 comments
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

@weissi
Copy link
Member

weissi commented Jul 24, 2017

Previous ID SR-5546
Radar rdar://problem/33509989
Original Reporter @weissi
Type Bug
Status Resolved
Resolution Done

Attachment: Download

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

md5: 50c08792daff0f3df2d6e7f7ed336709

relates to:

  • SR-6516 error: reference to invalid associated type 'Context' of type 'S'
  • SR-8813 Using implicit associatedtype in a stored property defined by typealias segfaults the compiler

Issue Description:

The following code

public struct Foo<A, B, C> {}

public protocol P {
    associatedtype PA
    associatedtype PB = Never
    associatedtype PC = Never

    typealias Context = Foo<PA, PB, PC>

    func f1(_ x: Context, _ y: PA)
    func f2(_ x: Context, _ y: PB)
    func f3(_ x: Context, _ y: PC)
    func f4(_ x: Context)
}

public extension P {
    public func f1(_ x: Context, _ y: PA) {
    }
    public func f2(_ x: Context, _ y: PB) {
    }
    public func f3(_ x: Context, _ y: PC) {
    }
    public func f4(_ x: Context) {
    }
}

public struct S: P {
    public typealias PA = String
    public typealias PB = Int

    public func f1(_ x: Context, _ y: PA) {
    }
    public func f2(_ x: Context, _ y: PB) {
    }
}

makes Apple Swift version 4.0-dev (LLVM 2c7b572db2, Clang 54c8e38855, Swift d84e4b4de1) crash with

$ /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2017-07-10-a.xctoolchain/usr/bin/swiftc  test2.swift Assertion failed: (isa<X>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file /Users/buildnode/jenkins/workspace/oss-swift-package-osx/llvm/include/llvm/Support/Casting.h, line 236.
0  swift                    0x000000010f7cfd08 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010f7cec66 llvm::sys::RunSignalHandlers() + 86
2  swift                    0x000000010f7d02ce SignalHandler(int) + 366
3  libsystem_platform.dylib 0x00007fff6e8d8f5a _sigtramp + 26
4  libsystem_platform.dylib 0x00007ffee34c69d0 _sigtramp + 1958664848
5  libsystem_c.dylib        0x00007fff6e703cea abort + 127
6  libsystem_c.dylib        0x00007fff6e6cbd40 basename_r + 0
7  swift                    0x000000010d6b2872 (anonymous namespace)::Verifier::verifyChecked(swift::AbstractFunctionDecl*) + 818
8  swift                    0x000000010d6a959a (anonymous namespace)::Verifier::walkToDeclPost(swift::Decl*) + 5482
9  swift                    0x000000010d6b3164 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 388
10 swift                    0x000000010d6b9dcb (anonymous namespace)::Traversal::visitNominalTypeDecl(swift::NominalTypeDecl*) + 443
11 swift                    0x000000010d6b3126 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 326
12 swift                    0x000000010d6b2fcb swift::Decl::walk(swift::ASTWalker&) + 27
13 swift                    0x000000010d74090a swift::SourceFile::walk(swift::ASTWalker&) + 170
14 swift                    0x000000010d6a231b swift::verify(swift::SourceFile&) + 59
15 swift                    0x000000010d622fbd swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int) + 2125
16 swift                    0x000000010d17720f swift::CompilerInstance::performSema() + 3535
17 swift                    0x000000010c7792ea performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 1450
18 swift                    0x000000010c777e56 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3494
19 swift                    0x000000010c739a30 main + 3312
20 libdyld.dylib            0x00007fff6e6589c1 start + 1
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2017-07-10-a.xctoolchain/usr/bin/swift -frontend -c -primary-file test2.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -color-diagnostics -module-name test2 -o /var/folders/k8/7d0p_dxx2ll8vnw5mt5__ddc0000gn/T/test2-bb04db.o 
1.  While walking into decl 'S' at test2.swift:27:8
2.  While verifying FuncDecl 'f2(_:_:)' at test2.swift:33:12
3.  While verifying AbstractFunctionDecl 'f2(_:_:)' at test2.swift:33:12
<unknown>:0: error: unable to execute command: Abort trap: 6
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)

or this if it helps

$ ~/devel/build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/bin/swiftc test2.swift 
Assertion failed: (isa<X>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file /Users/johannes/devel/llvm/include/llvm/Support/Casting.h, line 236.
0  swift                    0x000000010a586768 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010a5856c6 llvm::sys::RunSignalHandlers() + 86
2  swift                    0x000000010a586d2e SignalHandler(int) + 366
3  libsystem_platform.dylib 0x00007fff6e8d8f5a _sigtramp + 26
4  libsystem_platform.dylib 0x00007ffee87279a0 _sigtramp + 2045045344
5  libsystem_c.dylib        0x00007fff6e703cea abort + 127
6  libsystem_c.dylib        0x00007fff6e6cbd40 basename_r + 0
7  swift                    0x0000000108465222 (anonymous namespace)::Verifier::verifyChecked(swift::AbstractFunctionDecl*) + 818
8  swift                    0x000000010845b9da (anonymous namespace)::Verifier::walkToDeclPost(swift::Decl*) + 5482
9  swift                    0x0000000108465b14 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 388
10 swift                    0x000000010846c89b (anonymous namespace)::Traversal::visitNominalTypeDecl(swift::NominalTypeDecl*) + 443
11 swift                    0x0000000108465ad6 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 326
12 swift                    0x000000010846597b swift::Decl::walk(swift::ASTWalker&) + 27
13 swift                    0x00000001084f54ea swift::SourceFile::walk(swift::ASTWalker&) + 170
14 swift                    0x000000010845463b swift::verify(swift::SourceFile&) + 59
15 swift                    0x000000010825423d swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int) + 2141
16 swift                    0x0000000107f2829f swift::CompilerInstance::performSema() + 3599
17 swift                    0x0000000107518c1a performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 1450
18 swift                    0x0000000107517776 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3494
19 swift                    0x00000001074d9380 main + 3312
20 libdyld.dylib            0x00007fff6e6589c1 start + 1
21 libdyld.dylib            0x000000000000000d start + 2442819149
Stack dump:
0.  Program arguments: /Users/johannes/devel/build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/bin/swift -frontend -c -primary-file test2.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -color-diagnostics -module-name test2 -o /var/folders/k8/7d0p_dxx2ll8vnw5mt5__ddc0000gn/T/test2-c2684a.o 
1.  While walking into decl 'S' at test2.swift:27:8
2.  While verifying FuncDecl 'f2(_:_:)' at test2.swift:33:12
3.  While verifying AbstractFunctionDecl 'f2(_:_:)' at test2.swift:33:12
<unknown>:0: error: unable to execute command: Abort trap: 6
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)

from a very recent swiftc on master (the one from #11040

@weissi
Copy link
Member Author

weissi commented Jul 24, 2017

slightly smaller repro

public struct Foo<A, B> {}

public protocol P {
    associatedtype PA
    associatedtype PB = Never

    typealias Context = Foo<PA, PB>

    func f1(_ x: Context, _ y: PA)
    func f2(_ x: Context, _ y: PB)
}

public struct S: P {
    public typealias PA = Never

    public func f1(_ x: Context, _ y: PA) {
    }
    public func f2(_ x: Context, _ y: PB) {
    }
}

@weissi
Copy link
Member Author

weissi commented Jul 25, 2017

@swift-ci create

@slavapestov
Copy link
Member

Even smaller repro:

public protocol P {
    associatedtype PC = Never

    func f2(_ x: PC)
}

public struct S: P {
    public func f1(_ x: PC) {
    }

    public func f2(_ x: PC) {
    }

}

It works if you switch the order of f1 and f2 inside struct S, but for the wrong reason.

@slavapestov
Copy link
Member

Hi @weissi, my suggested workaround would be to explicitly define the associated types as typealiases inside the concrete type, instead of relying on associated type inference. It is unlikely we'll be able to fix this for 4.0, at least not in a general way.

@slavapestov
Copy link
Member

Also tested in 3.1, it also crashes as I suspected, just a little bit later because the AST verifier is not compiled in.

@slavapestov
Copy link
Member

In getWitnessTypeForMatching(), the hasValidSignature() check should be an assertion, instead of silently bailing out:

  if (witness->isInvalid() || !witness->hasValidSignature())
    return Type();

@weissi
Copy link
Member Author

weissi commented Nov 29, 2017

@slavapestov with the Swift 4.1 dev snapshot from today both your and my reduced repro doesn't work anymore. The attached test2.swift still reproes though.

@gregomni
Copy link
Collaborator

Fix for linked SR-8813 turns out to fix this as well.

@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. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software
Projects
None yet
Development

No branches or pull requests

4 participants