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-1725] Assertion failed: ((HadError || !M.is<SourceFile*>() || M.get<SourceFile*>()->ASTStage < SourceFile::TypeChecked) && "UnresolvedDot" "in wrong phase") #44334

Closed
ddunbar opened this issue Jun 10, 2016 · 6 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

@ddunbar
Copy link
Member

ddunbar commented Jun 10, 2016

Previous ID SR-1725
Radar None
Original Reporter @ddunbar
Type Bug
Status Resolved
Resolution Done

Attachment: Download

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

md5: 87e835799ceb9cf29e7c3d4815d3ba1c

relates to:

  • SR-4272 Assertion failed: ((HadError || !M.is<SourceFile*>() || M.get<SourceFile*>()->ASTStage < SourceFile::TypeChecked) && "UnresolvedDot" "in wrong phase"
  • SR-4314 Assertion failed: ((HadError || !M.is<SourceFile*>() || M.get<SourceFile*>()->ASTStage < SourceFile::TypeChecked) && "UnresolvedDot" "in wrong phase")
  • SR-3053 UnresolvedDotExpr not resolved by type checking (leads to crash)
  • SR-4272 Assertion failed: ((HadError || !M.is<SourceFile*>() || M.get<SourceFile*>()->ASTStage < SourceFile::TypeChecked) && "UnresolvedDot" "in wrong phase"

Issue Description:

Swift TOT crashes on this code:

$ cat z.swift
import Darwin.C
func f0() {
    var s = dirent()
    withUnsafeMutablePointer(&s.d_name) { ptr in
        ptr[0] = 0
    }
}

$ swift-dev z.swift
Assertion failed: ((HadError || !M.is<SourceFile*>() || M.get<SourceFile*>()->ASTStage < SourceFile::TypeChecked) && "UnresolvedDot" "in wrong phase"), function walkToExprPre, file /Volumes/Data/ddunbar/public/swift-project/swift/include/swift/AST/ExprNodes.def, line 80.
0  swift                    0x000000010ff1b67b llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 43
1  swift                    0x000000010ff1a8a6 llvm::sys::RunSignalHandlers() + 70
2  swift                    0x000000010ff1bf51 SignalHandler(int) + 641
3  libsystem_platform.dylib 0x00007fff85d9552a _sigtramp + 26
4  swift                    0x0000000110b0a561 cmark_strbuf__initbuf + 74416
5  swift                    0x000000010ff1bb9e abort + 14
6  swift                    0x000000010ff1bb81 __assert_rtn + 81
7  swift                    0x000000010d9e85b2 (anonymous namespace)::Verifier::walkToExprPre(swift::Expr*) + 162
8  swift                    0x000000010d9fac88 (anonymous namespace)::Traversal::visit(swift::Expr*) + 1784
9  swift                    0x000000010d9fba02 (anonymous namespace)::Traversal::visit(swift::Expr*) + 5234
10 swift                    0x000000010d9fc7c4 (anonymous namespace)::Traversal::visitApplyExpr(swift::ApplyExpr*) + 132
11 swift                    0x000000010d9fa657 (anonymous namespace)::Traversal::visit(swift::Expr*) + 199
12 swift                    0x000000010d9fca59 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Stmt*) + 185
13 swift                    0x000000010d9fee65 (anonymous namespace)::Traversal::visitAbstractFunctionDecl(swift::AbstractFunctionDecl*) + 1253
14 swift                    0x000000010d9f9ebd (anonymous namespace)::Traversal::doIt(swift::Decl*) + 269
15 swift                    0x000000010d9f9d9b swift::Decl::walk(swift::ASTWalker&) + 27
16 swift                    0x000000010daa423a swift::SourceFile::walk(swift::ASTWalker&) + 170
17 swift                    0x000000010d9e7f4b swift::verify(swift::SourceFile&) + 59
18 swift                    0x000000010d92e0ae swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int) + 1790
19 swift                    0x000000010d5ec440 swift::CompilerInstance::performSema() + 3664
20 swift                    0x000000010d102a7a performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 442
21 swift                    0x000000010d10162f swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2895
22 swift                    0x000000010d0c4100 main + 2448
23 libdyld.dylib            0x00007fff84f445ad start + 1
24 libdyld.dylib            0x000000000000000b start + 2064366175
Stack dump:
0.  Program arguments: /Volumes/Data/ddunbar/public/swift-project/build/Ninja-ReleaseAssert/swift-dev.xctoolchain/usr/bin/swift -frontend -interpret z.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -sdk /Volumes/Data/Xcode/Whitney8A140a/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -module-name z 
1.  While walking into decl 'f0' at z.swift:2:1
2.  While walking into body of 'f0' at z.swift:2:1
Illegal instruction: 4
@swift-ci
Copy link
Collaborator

Comment by Quildreen Motta (JIRA)

Unsure if the cause is the same, but this causes the same crash:

struct Task<S, E> { 
  typealias Fork = ((S) -> Void, (E) -> Void) -> Void 
  let fork: Fork 
     
  init(fork: Fork) { self.fork = fork } 
     
  func flatMap<S2>(transform: (S) -> Task<S2, E>) -> Task<S2, E> { 
    let task = self 
    return Task { resolve, reject in 
      task.fork({ 
        v in transform(v).fork({ v2 in resolve(v2) }, { e in reject(e) })
      }, 
      {
        e in reject(e)
      }) 
    } 
  } 
}

This happens both on the preview and on the latest snapshot:
preview-trace.txt development-snapshot-trace.txt

Swift version 3.0 (swift-3.0-PREVIEW-1)
Target: x86_64-unknown-linux-gnu

Swift version 3.0-dev (LLVM cb08d1dbbd, Clang 383859a9c4, Swift 9e8266a)
Target: x86_64-unknown-linux-gnu

@slavapestov
Copy link
Member

Ok, the CSDiag.cpp stuff is creating an expression with ErrorType, but failing to emit any diagnostics. I guess there's a missing case here.

If I remove most of the closure contents, the actual bug is this:

cr.swift:9:12: error: cannot convert return expression of type 'Task<S, E>' to return type 'Task<S2, E>'
return Task { resolve, reject in
^~~~~~~~~~~~~~~~~~~~~~~~~~

Arguably, we should be smarter about resolving the generic parameters of 'Task' here. Referencing a generic without arguments is handled via two cases:

  • If the type names one of the types containing the current declaration, we inherit the generic parameters from the context (here, <S, E>)

  • If the type is something else, we open it to infer the generic arguments (which in this case would yield <S2, E>, if only we got a chance to try)

I think in addition to fixing the diagnostic logic here, we should be doing #2 if #1 fails, but this is a bit tricky.

In any case, to make your code work, change 'return Task {' to 'return Task<S2, E> {'.

@slavapestov
Copy link
Member

Over to Chris for properly emitting the diagnostic.

@gwynne
Copy link
Contributor

gwynne commented Aug 30, 2016

This crash showed up for me in the latest development toolchain (2016-08-29) using the following snippet:

struct Tester { var value: Int }
let c: (Tester) -> Void = { t in { [v = t.value] in }() }

As far as I can tell, the key seems to be the inner closure's capture list specifying an ivar of the outer closure's parameter.

This further-reduced case also crashes, but with a different trace:

struct Tester {}
let c: (Tester) -> Void = { t in { [v = t] in }() }

The trace in this case is:

non-canonical or unchecked type
UNREACHABLE executed at /Users/buildnode/jenkins/workspace/oss-swift-package-osx/swift/include/swift/AST/CanTypeVisitor.h:41!
...
Thread 7 Crashed:: lldb.debugger.io-handler
0   libsystem_kernel.dylib          0x00007fff91392f06 __pthread_kill + 10
1   libsystem_pthread.dylib         0x00007fff8ac3e4ec pthread_kill + 90
2   com.apple.LLDB.framework        0x000000010cf48a1e abort + 14 (Signals.inc:439)
3   com.apple.LLDB.framework        0x000000010cef2237 llvm::llvm_unreachable_internal(char const*, char const*, unsigned int) + 471
4   com.apple.LLDB.framework        0x000000010d99461d swift::Lowering::ManagedValue swift::CanTypeVisitor<(anonymous namespace)::EmitBBArguments, swift::Lowering::ManagedValue>::visit<>(swift::CanType) + 1773 (Type.h:245)
5   com.apple.LLDB.framework        0x000000010d993244 swift::Lowering::SILGenFunction::emitProlog(llvm::ArrayRef<swift::ParameterList*>, swift::Type, swift::DeclContext*) + 404 (SILGenProlog.cpp:236)
6   com.apple.LLDB.framework        0x000000010d99265e swift::Lowering::SILGenFunction::emitProlog(swift::AnyFunctionRef, llvm::ArrayRef<swift::ParameterList*>, swift::Type) + 94 (SILGenProlog.cpp:427)
7   com.apple.LLDB.framework        0x000000010d956dbc swift::Lowering::SILGenFunction::emitClosure(swift::AbstractClosureExpr*) + 108 (SILGenFunction.cpp:486)
8   com.apple.LLDB.framework        0x000000010d8f00ea swift::Lowering::SILGenModule::emitClosure(swift::AbstractClosureExpr*) + 266 (SILGen.cpp:809)
9   com.apple.LLDB.framework        0x000000010d94df69 (anonymous namespace)::RValueEmitter::visitAbstractClosureExpr(swift::AbstractClosureExpr*, swift::Lowering::SGFContext) + 41 (SILGenExpr.cpp:1933)
10  com.apple.LLDB.framework        0x000000010d943fa3 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 131 (ASTVisitor.h:69)
11  com.apple.LLDB.framework        0x000000010d93f1c0 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*) + 304 (SILGenExpr.cpp:104)
12  com.apple.LLDB.framework        0x000000010d92f2b5 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 213 (SILGenDecl.cpp:992)
13  com.apple.LLDB.framework        0x000000010d92f3ad swift::Lowering::SILGenFunction::visitPatternBindingDecl(swift::PatternBindingDecl*) + 45 (SILGenDecl.cpp:1002)
14  com.apple.LLDB.framework        0x000000010d8f308c swift::Lowering::SILGenModule::visitTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 220 (SILGen.cpp:1203)
15  com.apple.LLDB.framework        0x000000010d8f386b swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 715 (SILGen.cpp:1407)
16  com.apple.LLDB.framework        0x000000010d8f4765 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool, bool) + 949 (SILGen.cpp:1441)
17  com.apple.LLDB.framework        0x000000010d8f4c55 swift::performSILGeneration(swift::FileUnit&, swift::SILOptions&, llvm::Optional<unsigned int>, bool) + 117 (SILGen.cpp:1505)
18  com.apple.LLDB.framework        0x000000010de818da lldb_private::SwiftExpressionParser::Parse(lldb_private::DiagnosticManager&, unsigned int, unsigned int, unsigned int) + 14348 (SwiftExpressionParser.cpp:1729)
19  com.apple.LLDB.framework        0x000000010dec3583 lldb_private::SwiftUserExpression::Parse(lldb_private::DiagnosticManager&, lldb_private::ExecutionContext&, lldb_private::ExecutionPolicy, bool, bool, unsigned int) + 825 (SwiftUserExpression.cpp:511)
20  com.apple.LLDB.framework        0x000000010dc8cf20 lldb_private::UserExpression::Evaluate(lldb_private::ExecutionContext&, lldb_private::EvaluateExpressionOptions const&, char const*, char const*, lldb_private::SharingPtr<lldb_private::ValueObject>&, lldb_private::Error&, unsigned int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, std::__1::shared_ptr<lldb_private::Module>*) + 1064 (UserExpression.cpp:270)
21  com.apple.LLDB.framework        0x000000010db7c48c lldb_private::REPL::IOHandlerInputComplete(lldb_private::IOHandler&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) + 1612 (REPL.cpp:359)
22  com.apple.LLDB.framework        0x000000010dc006f3 lldb_private::IOHandlerEditline::Run() + 383 (IOHandler.cpp:698)
23  com.apple.LLDB.framework        0x000000010daee6a9 lldb_private::Debugger::ExecuteIOHandlers() + 63 (Debugger.cpp:1020)
24  com.apple.LLDB.framework        0x000000010daf06ee lldb_private::Debugger::IOHandlerThread(void*) + 14 (Debugger.cpp:1844)
25  libsystem_pthread.dylib         0x00007fff8ac3b99d _pthread_body + 131
26  libsystem_pthread.dylib         0x00007fff8ac3b91a _pthread_start + 168
27  libsystem_pthread.dylib         0x00007fff8ac39351 thread_start + 13

@slavapestov
Copy link
Member

@gwynne the bug you're describing is different I think, it's https://bugs.swift.org/browse/SR-5870.

@slavapestov
Copy link
Member

@ddunbar your original example no longer crashes. Instead it fails to type check with

z.swift:4:45: error: cannot convert value of type '(UnsafeMutablePointer<_>) -> ()' to expected argument type '(UnsafeMutablePointer<_>) -> _'
    withUnsafeMutablePointer(to: &s.d_name) { ptr in
                                            ^~~~~~~~

This is a lousy diagnostic but we have a number of bugs concerning closure diagnostics already so I'll just close this one.

@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

5 participants