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-10987] Compiler crashes on invalid reference to 'subscript' member #53377

Closed
hamishknight opened this issue Jun 20, 2019 · 4 comments
Closed
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 type checker Area → compiler: Semantic analysis

Comments

@hamishknight
Copy link
Collaborator

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

Swift version 5.1-dev (LLVM af1f73e9e9, Swift 93d98f8eec)
Target: x86_64-apple-darwin18.5.0

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

md5: c5c3521c3d8d6e3c1a30d4d8f6d85919

Issue Description:

The following trips an assertion on diagnosis:

struct S {
  subscript(_ x: Int) -> String { "" }
}

let s = S()
s.subscript
Assertion failed: (parentExpr && "Couldn't find a parent expression for a member call?!"), function diagnoseAsError, file /Users/hamishknight/Desktop/swift-dev/swift/lib/Sema/CSDiagnostics.cpp, line 1916.
Stack dump:
0.  Program arguments: ./swift -frontend -typecheck /Users/hamishknight/Desktop/Stochastic Projects/newnew/newnew/main.swift -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
1.  Swift version 5.1-dev (LLVM af1f73e9e9, Swift 93d98f8eec)
2.  While type-checking statement at [/Users/hamishknight/Desktop/Stochastic Projects/newnew/newnew/main.swift:225:1 - line:225:3] RangeText="s."
3.  While type-checking expression at [/Users/hamishknight/Desktop/Stochastic Projects/newnew/newnew/main.swift:225:1 - line:225:3] RangeText="s."
0  swift                    0x0000000108bbe215 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x0000000108bbd218 llvm::sys::RunSignalHandlers() + 248
2  swift                    0x0000000108bbe818 SignalHandler(int) + 264
3  libsystem_platform.dylib 0x00007fff649e3b5d _sigtramp + 29
4  swift                    0x000000010a22ebf0 cmark_strbuf__initbuf + 131435
5  libsystem_c.dylib        0x00007fff648a36a6 abort + 127
6  libsystem_c.dylib        0x00007fff6486c20d basename_r + 0
7  swift                    0x0000000108ec7e43 swift::constraints::SubscriptMisuseFailure::diagnoseAsError() (.cold.2) + 35
8  swift                    0x00000001057b34fa swift::constraints::SubscriptMisuseFailure::diagnoseAsError() + 730
9  swift                    0x00000001057a85e8 swift::constraints::UseSubscriptOperator::diagnose(swift::Expr*, bool) const + 104
10 swift                    0x000000010571385b swift::constraints::ConstraintSystem::applySolutionFixes(swift::Expr*, swift::constraints::Solution const&)::$_12::operator()(swift::Expr*) const + 203
11 swift                    0x00000001057136d0 swift::constraints::ConstraintSystem::applySolutionFixes(swift::Expr*, swift::constraints::Solution const&) + 544
12 swift                    0x000000010571398d swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::Expr*, swift::Type, bool, bool) + 141
13 swift                    0x000000010584f333 swift::TypeChecker::typeCheckExpressionImpl(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener&, swift::constraints::ConstraintSystem*) + 1107
14 swift                    0x000000010584eecf swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) + 63
15 swift                    0x00000001058e81d6 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 614
16 swift                    0x00000001058e72f8 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 136
17 swift                    0x00000001058e73ee swift::TypeChecker::typeCheckTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 174
18 swift                    0x0000000105907862 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int) + 914
19 swift                    0x00000001050bd266 swift::CompilerInstance::parseAndTypeCheckMainFileUpTo(swift::SourceFile::ASTStage_t, swift::PersistentParserState&, swift::DelayedParsingCallbacks*, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>) + 502
20 swift                    0x00000001050bc076 swift::CompilerInstance::parseAndCheckTypesUpTo(swift::CompilerInstance::ImplicitImports const&, swift::SourceFile::ASTStage_t) + 342
21 swift                    0x00000001050bba1a swift::CompilerInstance::performSemaUpTo(swift::SourceFile::ASTStage_t) + 618
22 swift                    0x0000000104eab912 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 1730
23 swift                    0x0000000104eaa271 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3025
24 swift                    0x0000000104e537c9 main + 729
25 libdyld.dylib            0x00007fff647fe3d5 start + 1
fish: './swift -frontend -typecheck /U…' terminated by signal SIGABRT (Abort)
@belkadan
Copy link
Contributor

cc @xedin

@theblixguy
Copy link
Collaborator

Hmm the problem is that its expecting a CallExpr (so `s.subscript(0)`) but in this case, we just have:

unresolved_dot_expr type='<null>' field 'subscript' function_ref=single
    (declref_expr type='<null>' decl=test.(file).s@/Users/suyashsrijan/Desktop/test.swift:5:5 function_ref=unapplied))

so it crashes when trying to find the parent expression. Maybe we should take into consideration that there might not be a parent here and just use the `unresolved_dot_expr` to emit the diagnostic. We'll also have to tweak the fix-it locs otherwise it will replace the entire `s.subscript` with [], where as we just want to append it.

There's another case where if we do `s.subscript = 10` then it causes the same crash, so in this case the fix-it would then have to remove the `=` and add the [], so the code ends up being `s.subscript[10]`. Not sure if its worth covering all these edge cases with special fix-its.

@gregomni
Copy link
Collaborator

Done in #25892

@hamishknight
Copy link
Collaborator Author

Thanks Greg!

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

5 participants