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-4888] Crash: Tuple KeyPaths #47465

Closed
stephencelis opened this issue May 15, 2017 · 9 comments
Closed

[SR-4888] Crash: Tuple KeyPaths #47465

stephencelis opened this issue May 15, 2017 · 9 comments

Comments

@stephencelis
Copy link
Contributor

stephencelis commented May 15, 2017

Previous ID SR-4888
Radar rdar://problem/34186458
Original Reporter @stephencelis
Type Bug
Status Reopened
Resolution
Environment

Swift Development Snapshot 2017-05-12 (a)
Xcode 8.3.2
macOS 10.12.5 Beta (16F71b)

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

md5: e43e68b9511248f280cf77762db2a6d0

Issue Description:

Right now, tuple KeyPaths crash the compiler.

Assertion failed: (isDecl() && "Not a declaration"), function getDecl, file /Users/buildnode/jenkins/workspace/oss-swift-package-osx/swift/lib/Sema/OverloadChoice.h, line 251.
0  swift                    0x000000010efc8b18 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010efc7ae6 llvm::sys::RunSignalHandlers() + 86
2  swift                    0x000000010efc90d9 SignalHandler(int) + 361
3  libsystem_platform.dylib 0x00007fffb72cbb3a _sigtramp + 26
4  libsystem_platform.dylib 0x00007fff53e6fbb8 _sigtramp + 2629451928
5  libsystem_c.dylib        0x00007fffb7150420 abort + 129
6  libsystem_c.dylib        0x00007fffb7117893 basename_r + 0
7  swift                    0x000000010cba0f20 swift::constraints::ConstraintSystem::simplifyKeyPathConstraint(swift::Type, swift::Type, swift::Type, swift::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) + 2064

Basic case:

struct User {
  let names: (first: String, last: String)

  func test() {
    _ = \User.names.0
    _ = \User.names.first
  }
}

Ideally the KeyPath works in a read-only fashion! Less ideally we get a compiler error.

@jckarter
Copy link
Member

jckarter commented May 15, 2017

@swift-ci create

@jckarter
Copy link
Member

jckarter commented May 15, 2017

PR for master: #9618
and 4.0: #9620

@jckarter
Copy link
Member

jckarter commented May 16, 2017

Merged. Support for tuple elements will have to wait for another time, but the compiler shouldn't crash anymore. Thanks!

@ffried
Copy link
Contributor

ffried commented Aug 31, 2017

I'm seeing this again in the current Xcode 9 beta 6.

The only difference is that I'm using a typealias.

struct Container {
typealias Element = (name: String, count: Int)

let elem: Element? = (name: "test", count: 2)
}

let keyPath: KeyPath<Container, String> = \Container.elem?.0
let keyPath: KeyPath<Container, String> = \Container.elem?.name

When compiling, Swift fails with a Segfault 11:

0  swift                    0x000000011133942a PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x0000000111338866 SignalHandler(int) + 662
2  libsystem_platform.dylib 0x00007fff983c6b3a _sigtramp + 26
3  swift                    0x000000010ef16164 swift::Type::transformRec(llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>) const + 148
4  swift                    0x000000010ec34869 (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 25
5  swift                    0x000000010ed50f44 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) + 1540
6  swift                    0x000000010ec5d52b (anonymous namespace)::FailureDiagnosis::typeCheckChildIndependently(swift::Expr*, swift::Type, swift::ContextualTypePurpose, swift::OptionSet<TCCFlags, unsigned int>, swift::ExprTypeCheckListener*, bool) + 1963
7  swift                    0x000000010ec56076 (anonymous namespace)::FailureDiagnosis::diagnoseContextualConversionError(swift::Expr*, swift::Type) + 86
8  swift                    0x000000010ec4da05 swift::constraints::ConstraintSystem::diagnoseFailureForExpr(swift::Expr*) + 101
9  swift                    0x000000010ec837f7 swift::constraints::ConstraintSystem::salvage(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::Expr*) + 6199
10 swift                    0x000000010ed507a1 swift::TypeChecker::solveForExpression(swift::Expr*&, swift::DeclContext*, swift::Type, swift::FreeTypeVariableBinding, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem&, llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 8353
11 swift                    0x000000010ed50c1d swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) + 733
12 swift                    0x000000010ed53e1e swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*, bool) + 366
13 swift                    0x000000010ed543dc swift::TypeChecker::typeCheckPatternBinding(swift::PatternBindingDecl*, unsigned int, bool) + 188
14 swift                    0x000000010ed671d5 (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 1349
15 swift                    0x000000010edd64ad swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 13805
16 swift                    0x000000010edd8fdc swift::TypeChecker::typeCheckTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 220
17 swift                    0x000000010edf56a5 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int) + 1861
18 swift                    0x000000010e926144 swift::CompilerInstance::performSema() + 4628
19 swift                    0x000000010dcaaa62 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 1378
20 swift                    0x000000010dca8c94 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7716
21 swift                    0x000000010dc5dbb8 main + 12248
22 libdyld.dylib            0x00007fff981b7235 start + 1
23 libdyld.dylib            0x000000000000000a start + 1743031766
Stack dump:
0.  Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret s4_keypath_tuple.swift -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -color-diagnostics -module-name s4_keypath_tuple 
1.  While type-checking declaration 0x7ff386859398 at s4_keypath_tuple.swift:7:1
2.  While type-checking expression at [s4_keypath_tuple.swift:7:43 - line:7:60] RangeText="\Container.elem?.0"
3.  While type-checking expression at [s4_keypath_tuple.swift:7:43 - line:7:60] RangeText="\Container.elem?.0"
$ swift --version
Apple Swift version 4.0 (swiftlang-900.0.63.10 clang-900.0.36)
Target: x86_64-apple-macosx10.9

@jckarter
Copy link
Member

jckarter commented Aug 31, 2017

@swift-ci create

@xAlien95
Copy link
Contributor

xAlien95 commented Sep 16, 2020

Both the code snippets of @stephencelis and @ffried do not crash the compiler using Swift version 5.3 (swiftlang-1200.0.22.4 clang-1200.0.25.1).

A quick note on @ffried's example:

let keyPath: KeyPath<Container, String> = \Container.elem?.0
let keyPath: KeyPath<Container, String> = \Container.elem?.name

In order to return String, you need to force unwrap the elem key:

let keyPath: KeyPath<Container, String> = \Container.elem!.0
let keyPath: KeyPath<Container, String> = \Container.elem!.name

@ffried
Copy link
Contributor

ffried commented Sep 17, 2020

@xAlien95 Thanks. Yea, the KeyPath Value should be String? in my example of course. Or force-unwrapping them - true.

@typesanitizer
Copy link

typesanitizer commented Sep 22, 2020

Should this be marked as resolved/closed now?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@stephencelis
Copy link
Contributor Author

stephencelis commented Apr 26, 2022

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants