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-6730] SILgen segmentation fault #49279

Closed
NachoSoto opened this issue Jan 9, 2018 · 11 comments
Closed

[SR-6730] SILgen segmentation fault #49279

NachoSoto opened this issue Jan 9, 2018 · 11 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@NachoSoto
Copy link
Contributor

Previous ID SR-6730
Radar rdar://problem/36387123
Original Reporter @NachoSoto
Type Bug
Status Resolved
Resolution Cannot Reproduce
Environment

Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee @xedin
Priority Medium

md5: 14e75aff971213fcb4f341e731528969

Issue Description:

Code:

import Foundation

public protocol OptionalProtocol {
  associatedtype Wrapped

  var optional: Wrapped? { get }
}

extension Optional: OptionalProtocol {
  public var optional: Wrapped? {
    return self
  }
}

public extension Sequence where Element: OptionalProtocol {
  func skipNil() -> [Element.Wrapped] {
    return self
    .flatMap { $0.optional }
  }
}

class A {}
class A1: A {}
class A2: A {}
class A3: A {}

final class V {
  init() {
    ([
      self.a1, self.a2, self.a3
      ] as [A])
    .skipNil()
    .forEach { self.f($0) }
  }

  func f(_ a: A) {}

  private let a1 = A1()
  private let a2 = A2()
  private let a3 = A3()
}

Crash:

0  swift                    0x0000000108e5b36a PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x0000000108e5a7a6 SignalHandler(int) + 662
2  libsystem_platform.dylib 0x00007fff7bc98f5a _sigtramp + 26
3  libsystem_platform.dylib 0x0000000100000001 _sigtramp + 2218160321
4  swift                    0x000000010608786a swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 9610
5  swift                    0x000000010603cec0 (anonymous namespace)::SILGenApply::visitExpr(swift::Expr*) + 48
6  swift                    0x000000010603ee04 (anonymous namespace)::SILGenApply::visitApplyExpr(swift::ApplyExpr*) + 2644
7  swift                    0x000000010602c9fb prepareApplyExpr(swift::Lowering::SILGenFunction&, swift::Expr*) + 219
8  swift                    0x000000010608533b swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 91
9  swift                    0x000000010608786a swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 9610
10 swift                    0x000000010603cec0 (anonymous namespace)::SILGenApply::visitExpr(swift::Expr*) + 48
11 swift                    0x000000010603ee04 (anonymous namespace)::SILGenApply::visitApplyExpr(swift::ApplyExpr*) + 2644
12 swift                    0x000000010602c9fb prepareApplyExpr(swift::Lowering::SILGenFunction&, swift::Expr*) + 219
13 swift                    0x000000010608533b swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 91
14 swift                    0x000000010609a144 swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 500
15 swift                    0x00000001060f8d25 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 1733
16 swift                    0x0000000106068142 swift::Lowering::SILGenFunction::emitClassConstructorInitializer(swift::ConstructorDecl*) + 3666
17 swift                    0x0000000106021374 swift::Lowering::SILGenModule::emitConstructor(swift::ConstructorDecl*) + 3236
18 swift                    0x0000000106107020 (anonymous namespace)::SILGenType::emitType() + 896
19 swift                    0x000000010602665a swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 74
20 swift                    0x000000010602591b swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 1115
21 swift                    0x00000001060272f2 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool) + 914
22 swift                    0x00000001057b7d54 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 13044
23 swift                    0x00000001057b31f4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7716
24 swift                    0x0000000105767e78 main + 12248
25 libdyld.dylib            0x00007fff7ba18145 start + 1
26 libdyld.dylib            0x000000000000000a start + 2220785350
Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret test.swift -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -color-diagnostics -module-name test
1.  While silgen constructor initializer SIL function "@_T04test1VCACycfc".
 for 'init()' at test.swift:36:5
Segmentation fault: 11
@gottesmm
Copy link
Member

gottesmm commented Jan 9, 2018

@swift-ci create

@belkadan
Copy link
Contributor

Does not crash on master:

<stdin>:18:6: warning: 'flatMap' is deprecated: renamed to 'compactMap(_:)'
    .flatMap { $0.optional }
     ^
<stdin>:18:6: note: use 'compactMap(_:)' instead
    .flatMap { $0.optional }
     ^~~~~~~
     compactMap
<stdin>:33:23: error: cannot convert value of type 'A.Wrapped' to expected argument type 'A'
    .forEach { self.f($0) }
                      ^~

That last part looks like the real issue, and I think @xedin fixed that a while back. Pavel?

@NachoSoto
Copy link
Contributor Author

@belkadan how about when you replace it to compactMap?
The last issue seems like a bug too, right? That diagnostic makes no sense :/

@belkadan
Copy link
Contributor

Sorry, you're right. The crash may be fixed, but either the whole thing should type-check or it should refuse to let you call skipNil in the first place.

@NachoSoto
Copy link
Contributor Author

> or it should refuse to let you call skipNil in the first place.

LOL, interestingly, in my project some of these properties were optional, which is why I was calling skipNil(). When I reduced the crasher, I forgot about that detail and didn't check since I got the same crash without optionals anyway 😛

Does this type check in master if some of the properties are optional?

@NachoSoto
Copy link
Contributor Author

Oh but I see what was wrong in my code in the first place, I needed as [A?]. This is what happens when I need to do mental gymnastics to figure out what stupid thing I did rather than a robust diagnostic system helping me :/

@xedin
Copy link
Member

xedin commented Jan 10, 2018

Diagnostic is pretty bad here which is a consequence of us trying to diagnose bottom-up in the AST...

@xedin
Copy link
Member

xedin commented Jan 10, 2018

Better error here would be "type 'A' does not conform to protocol 'OptionalProtocol'" which points to "skipNil()".

@NachoSoto
Copy link
Contributor Author

Agreed, but note that this crash happened with this code too (which is what my project had):

import Foundation

public protocol OptionalProtocol {
  associatedtype Wrapped

  var optional: Wrapped? { get }
}

extension Optional: OptionalProtocol {
  public var optional: Wrapped? {
    return self
  }
}

public extension Sequence where Element: OptionalProtocol {
  func skipNil() -> [Element.Wrapped] {
    return self
    .flatMap { $0.optional }
  }
}

class A {}
class A1: A {}
class A2: A {}
class A3: A {}

final class V {
  init() {
    ([
      self.a1, self.a2, self.a3
      ] as [A])
    .skipNil()
    .forEach { self.f($0) }
  }

  func f(_ a: A) {}

  private let a1 = A1()
  private let a2: A2? = A2() // Note this changed from the original code in the Jira
  private let a3 = A3()
}

In this case the diagnostic (instead of the crash, I wonder if this also crashes in master still) should be that not all elements of that array literal are non-optional (however that should be worded).

@xedin
Copy link
Member

xedin commented Jan 10, 2018

Code from your comment doesn't crash on master. As for the diagnostic I think it should be "cannot convert value of type 'A2?' to expected element type 'A'" which points to the "self.a2" element of the array.

@NachoSoto
Copy link
Contributor Author

👍

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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 diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants