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-11665] Cannot use key path subscript with default argument #54074

Closed
hamishknight opened this issue Oct 24, 2019 · 6 comments
Closed

[SR-11665] Cannot use key path subscript with default argument #54074

hamishknight opened this issue Oct 24, 2019 · 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 key paths Feature: key paths (both native and Objective-C)

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-11665
Radar None
Original Reporter @hamishknight
Type Bug
Status Closed
Resolution Done
Environment

Swift version 5.1.1-dev (Swift 9721fd7)
Target: x86_64-apple-darwin18.7.0

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

md5: f9a220d52a1805e08c3fef90e7d87cad

Issue Description:

On master we currently support subscripts with default arguments (SR-6118), however they don't appear to currently work with key paths. The following hits an assertion:

struct S {
  subscript(x: Int = 0) -> Int { x }
}

_ = \S.[]
Assertion failed: (hashables.size() == SubscriptSize), function setSubscriptIndexHashableConformances, file /Users/hamishknight/Desktop/swift-dev/swift/lib/AST/Expr.cpp, line 2179.
Stack dump:
0.  Program arguments: ./swift -frontend -emit-silgen /Users/hamishknight/Desktop/newnew/newnew/main.swift -debug-diagnostic-names -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/
1.  Swift version 5.1.1-dev (Swift 025f97d746)
2.  While type-checking statement at [/Users/hamishknight/Desktop/newnew/newnew/main.swift:460:1 - line:460:9] RangeText="_ = \S.["
3.  While type-checking expression at [/Users/hamishknight/Desktop/newnew/newnew/main.swift:460:1 - line:460:9] RangeText="_ = \S.["
0  swift                    0x000000010ad92cb5 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x000000010ad91cb8 llvm::sys::RunSignalHandlers() + 248
2  swift                    0x000000010ad932a8 SignalHandler(int) + 264
3  libsystem_platform.dylib 0x00007fff62fa1b5d _sigtramp + 29
4  libsystem_platform.dylib 0x000000011f218938 _sigtramp + 3156700664
5  libsystem_c.dylib        0x00007fff62e5b6a6 abort + 127
6  libsystem_c.dylib        0x00007fff62e2420d basename_r + 0
7  swift                    0x000000010b18d9d3 swift::KeyPathExpr::Component::setSubscriptIndexHashableConformances(llvm::ArrayRef<swift::ProtocolConformanceRef>) (.cold.1) + 35
8  swift                    0x0000000107cb0bd1 swift::KeyPathExpr::Component::setSubscriptIndexHashableConformances(llvm::ArrayRef<swift::ProtocolConformanceRef>) + 593
9  swift                    0x000000010773cf7d (anonymous namespace)::ExprRewriter::buildKeyPathSubscriptComponent(swift::constraints::SelectedOverload&, swift::SourceLoc, swift::Expr*, llvm::ArrayRef<swift::Identifier>, swift::constraints::ConstraintLocator*) + 1789
10 swift                    0x000000010772d89c swift::ASTVisitor<(anonymous namespace)::ExprRewriter, swift::Expr*, void, void, void, void, void>::visit(swift::Expr*) + 24428
11 swift                    0x00000001077271a6 (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 22
12 swift                    0x0000000107bf37f2 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Expr*) + 2226
13 swift                    0x0000000107bf29b1 swift::Expr::walk(swift::ASTWalker&) + 81
14 swift                    0x00000001077229fd swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::Expr*, swift::Type, bool, bool) + 525
15 swift                    0x000000010787cd27 swift::TypeChecker::typeCheckExpressionImpl(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener&, swift::constraints::ConstraintSystem*) + 1159
16 swift                    0x000000010787c88f swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) + 63
17 swift                    0x000000010791d00b swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 603
18 swift                    0x000000010791b948 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 136
19 swift                    0x000000010791ba45 swift::TypeChecker::typeCheckTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 181
20 swift                    0x0000000107948662 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int) + 1682
21 swift                    0x00000001070954e9 swift::CompilerInstance::parseAndTypeCheckMainFileUpTo(swift::SourceFile::ASTStage_t, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>) + 489
22 swift                    0x0000000107094127 swift::CompilerInstance::parseAndCheckTypesUpTo(swift::CompilerInstance::ImplicitImports const&, swift::SourceFile::ASTStage_t) + 519
23 swift                    0x000000010709392d swift::CompilerInstance::performSemaUpTo(swift::SourceFile::ASTStage_t) + 621
24 swift                    0x0000000106e7a3a1 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 1681
25 swift                    0x0000000106e78da6 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3046
26 swift                    0x0000000106e1e349 main + 729
27 libdyld.dylib            0x00007fff62db63d5 start + 1
28 libdyld.dylib            0x0000000000000007 start + 2636422195
fish: './swift -frontend -emit-silgen…' terminated by signal SIGABRT (Abort)
@hamishknight
Copy link
Collaborator Author

cc @slavapestov

@zoecarver
Copy link
Collaborator

I'd like to try to fix this.

@slavapestov
Copy link
Member

Sounds good! @jckarter is the best person to ask questions about the keypath implementation.

@zoecarver
Copy link
Collaborator

PR #27951 fixes this issue.

There were primarily two issues here. The first is that this code seemed to expect that the number of `Hashable` parameters would equal the number of labels. This both triggered the assertion above and caused this function to return the wrong size (it should have returned the number of parameters, not labels). The second issue is that lowering a keypath with default parameters requires that code to be able to lower default parameters, which it could not.

Thanks to @xedin for the help with this patch. I couldn't have done it without your guidance.


A few more things for posterity:

  • `lowerKeyPathSubscriptIndexTypes` expects `Hashable` parameters (not arguments).

  • `ASTVisitor::visitDefaultArgumentExpr` shouldn't be implemented (correct me if I'm wrong).

  • `getParameterAt` can be used to get default parameters.

@hamishknight
Copy link
Collaborator Author

Fixed by #27951 thanks Zoe!

@hamishknight
Copy link
Collaborator Author

Filed a new bug for variadic params: https://bugs.swift.org/browse/SR-11812

@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 key paths Feature: key paths (both native and Objective-C)
Projects
None yet
Development

No branches or pull requests

4 participants