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

compiler crash: optional chaining on ~Copyable type #75999

Open
mayoff opened this issue Aug 20, 2024 · 0 comments
Open

compiler crash: optional chaining on ~Copyable type #75999

mayoff opened this issue Aug 20, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software expressions Feature: expressions noncopyable struct/enum Feature → declarations: Noncopyable value type declarations type checker Area → compiler: Semantic analysis

Comments

@mayoff
Copy link

mayoff commented Aug 20, 2024

Description

I tried to use optional chaining on an Optional<NonCopyable>, where struct NonCopyable: ~Copyable. The compiler crashed.

Reproduction

Put this in nc.swift:

struct NonCopyable: ~Copyable {
    var shared: Self { .init() }
}

func f() {
    _ = (Optional<NonCopyable>.none)?.shared
}

Then compile it:

~/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swiftc -c nc.swift

Stack dump

Assertion failed: (Ptr && "Cannot dereference a null Type!"), function operator->, file Type.h, line 229.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /Users/mayoff/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/mayoff/t/nc.swift -target arm64-apple-macosx14.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -empty-abi-descriptor -Xcc -working-directory -Xcc /Users/mayoff/t -resource-dir /Users/mayoff/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/lib/swift -module-name nc -in-process-plugin-server-path /Users/mayoff/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/lib/swift/host/libSwiftInProcPluginServer.dylib -plugin-path /Users/mayoff/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Users/mayoff/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/local/lib/swift/host/plugins -o /Users/mayoff/t/nc.o
1.	Apple Swift version 6.0-dev (LLVM 6c1a2ac10cabb71, Swift 54908861448c8e8)
2.	Compiling with effective version 5.10
3.	While evaluating request TypeCheckSourceFileRequest(source_file "/Users/mayoff/t/nc.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(nc.(file).f()@/Users/mayoff/t/nc.swift:6:6)
5.	While type-checking statement at [/Users/mayoff/t/nc.swift:6:10 - line:8:1] RangeText="{
    _ = (Optional<NonCopyable>.none)?.shared
"
6.	While type-checking expression at [/Users/mayoff/t/nc.swift:7:5 - line:7:39] RangeText="_ = (Optional<NonCopyable>.none)?."
7.	While type-checking-target starting at /Users/mayoff/t/nc.swift:7:5
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000106197b08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001061962d4 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000106198150 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x0000000183e62584 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000183e31c20 pthread_kill + 288
5  libsystem_c.dylib        0x0000000183d3ea30 abort + 180
6  libsystem_c.dylib        0x0000000183d3dd20 err + 0
7  swift-frontend           0x000000010662cd04 swift::findSyntacticErrorForConsume(swift::ModuleDecl*, swift::SourceLoc, swift::Expr*) (.cold.4) + 0
8  swift-frontend           0x0000000101edf440 swift::findSyntacticErrorForConsume(swift::ModuleDecl*, swift::SourceLoc, swift::Expr*) + 1064
9  swift-frontend           0x0000000101fa28b0 swift::canAddExplicitConsume(swift::ModuleDecl*, swift::Expr*) + 92
10 swift-frontend           0x0000000101d6d788 (anonymous namespace)::ExprRewriter::diagnoseOptionalInjection(swift::InjectIntoOptionalExpr*, swift::constraints::ConstraintLocatorBuilder) + 196
11 swift-frontend           0x0000000101d4c2a8 (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 2836
12 swift-frontend           0x0000000101d526bc (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 14592
13 swift-frontend           0x000000010230ca14 (anonymous namespace)::Traversal::doIt(swift::Expr*) + 988
14 swift-frontend           0x000000010230ded0 (anonymous namespace)::Traversal::visitAssignExpr(swift::AssignExpr*) + 60
15 swift-frontend           0x000000010230c818 (anonymous namespace)::Traversal::doIt(swift::Expr*) + 480
16 swift-frontend           0x000000010230c62c swift::Expr::walk(swift::ASTWalker&) + 32
17 swift-frontend           0x0000000101d49cd8 (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 596
18 swift-frontend           0x0000000101d496fc swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 620
19 swift-frontend           0x0000000101f9bb44 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 392
20 swift-frontend           0x0000000101f9b964 swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 184
21 swift-frontend           0x0000000101f9b838 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::constraints::ContextualTypeInfo, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 92
22 swift-frontend           0x0000000102074778 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 264
23 swift-frontend           0x00000001020780b8 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 160
24 swift-frontend           0x00000001020765c0 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 136
25 swift-frontend           0x0000000102075d14 (anonymous namespace)::StmtChecker::typeCheckBody(swift::BraceStmt*&) + 32
26 swift-frontend           0x0000000102075a9c swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1360
27 swift-frontend           0x000000010241ca1c swift::TypeCheckFunctionBodyRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()>(swift::TypeCheckFunctionBodyRequest const&, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()) + 240
28 swift-frontend           0x0000000102386510 swift::AbstractFunctionDecl::getTypecheckedBody() const + 120
29 swift-frontend           0x00000001024ca8a4 swift::SourceFile::typeCheckDelayedFunctions() + 104
30 swift-frontend           0x00000001020b6210 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 256
31 swift-frontend           0x00000001020b7c74 swift::TypeCheckSourceFileRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()>(swift::TypeCheckSourceFileRequest const&, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()) + 232
32 swift-frontend           0x00000001020b60e8 swift::performTypeChecking(swift::SourceFile&) + 84
33 swift-frontend           0x0000000100f28e90 bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_8>(long, swift::SourceFile&) + 16
34 swift-frontend           0x0000000100f22058 swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) + 76
35 swift-frontend           0x0000000100f21fec swift::CompilerInstance::performSema() + 76
36 swift-frontend           0x0000000100ce80a4 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 60
37 swift-frontend           0x0000000100cddca4 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 716
38 swift-frontend           0x0000000100cdd370 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2304
39 swift-frontend           0x0000000100acad98 swift::mainEntry(int, char const**) + 3096
40 dyld                     0x0000000183aa7154 start + 2476

Expected behavior

I expected the compiler to not crash. Not sure what else there is to say…

Environment

Apple Swift version 6.0-dev (LLVM 6c1a2ac10cabb71, Swift 54908861448c8e8)
Target: arm64-apple-macosx14.0

I used the “main” Swift development snapshot of 2024-08-20: swift-DEVELOPMENT-SNAPSHOT-2024-08-19-a.xctoolchain

It also crashes with the toolchains of Xcode 16.0 beta 4 and Xcode 16.1 beta 1, but does not crash with the toolchain of Xcode-16.0 beta 3.

Additional information

Changing the ? to ! makes the compiler not crash.

@mayoff mayoff added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Aug 20, 2024
@xedin xedin added compiler The Swift compiler itself type checker Area → compiler: Semantic analysis expressions Feature: expressions noncopyable struct/enum Feature → declarations: Noncopyable value type declarations and removed triage needed This issue needs more specific labels labels Aug 20, 2024
kavon added a commit to kavon/swift that referenced this issue Sep 23, 2024
Since this function is being called from the constraint solver now, we
need to generalize the way it obtains the Type of an Expression, as the
expression itself may not know its own type, only the solver does.

resolves rdar://134371893 / swiftlang#75999
kavon added a commit to kavon/swift that referenced this issue Sep 24, 2024
Since this function is being called from the constraint solver now, we
need to generalize the way it obtains the Type of an Expression, as the
expression itself may not know its own type, only the solver does.

resolves rdar://134371893 / swiftlang#75999
kavon added a commit to kavon/swift that referenced this issue Sep 24, 2024
Since this function is being called from the constraint solver now, we
need to generalize the way it obtains the Type of an Expression, as the
expression itself may not know its own type, only the solver does.

resolves rdar://134371893 / swiftlang#75999
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 itself crash Bug: A crash, i.e., an abnormal termination of software expressions Feature: expressions noncopyable struct/enum Feature → declarations: Noncopyable value type declarations type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants