swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.6k stars 10.37k forks source link

Invalid conditional downcast in type-casting pattern in switch crashes frontend #77633

Open ADKaster opened 2 days ago

ADKaster commented 2 days ago

Description

I wanted to check if an Int

  1. fit inside a UInt16
  2. was a lead or trailing surrogate

The compiler didn't like the as? I tried to use to check this.

Reproduction

#if false
func foo(val: Int) -> UnicodeScalar {
    return switch val {
        case let c as? Unicode.UTF16.CodeUnit where Unicode.UTF16.isSurrogate(c):
            UnicodeScalar(c)
        default:
            UnicodeScalar(0xFFFD)
    }
}
#endif

func bar(val: Int) {
    let x = { () in switch val {
        case let c as? Unicode.UTF16.CodeUnit where Unicode.UTF16.isSurrogate(c):
            UnicodeScalar(c)
        default:
            UnicodeScalar(0xFFFD)
    }
    }()

    return x
}

Both of these forms cause the crash

Stack dump

error: compile command failed due to signal 6 (use -v to see invocation)
Libraries/LibWeb/HTML/Parser/test.swift:16:20: error: cannot conditionally downcast in a type-casting pattern
14 | func bar(val: Int) {
15 |     let x = { () in switch val {
16 |         case let c as? Unicode.UTF16.CodeUnit where Unicode.UTF16.isSurrogate(c):
   |                    `- error: cannot conditionally downcast in a type-casting pattern
17 |             UnicodeScalar(c)
18 |         default:

swift-frontend: /home/build-user/swift/include/swift/Sema/ConstraintSystem.h:3129: swift::Type swift::constraints::ConstraintSystem::getType(swift::ASTNode) const: Assertion `hasType(node) && "Expected type to have been set!"' failed.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: /home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend -frontend -c -primary-file Libraries/LibWeb/HTML/Parser/test.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -color-diagnostics -empty-abi-descriptor -resource-dir /home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/lib/swift -module-name test -in-process-plugin-server-path /home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/lib/swift/host/libSwiftInProcPluginServer.so -plugin-path /home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/lib/swift/host/plugins -plugin-path /home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/local/lib/swift/host/plugins -parse-as-library -o /tmp/TemporaryDirectory.nZxF0h/test-1.o
1.  Swift version 6.1-dev (LLVM 5c0d283c37132bf, Swift b5af518638c9a16)
2.  Compiling with effective version 5.10
3.  While evaluating request TypeCheckSourceFileRequest(source_file "Libraries/LibWeb/HTML/Parser/test.swift")
4.  While evaluating request TypeCheckFunctionBodyRequest(test.(file).bar(val:)@Libraries/LibWeb/HTML/Parser/test.swift:14:6)
5.  While type-checking statement at [Libraries/LibWeb/HTML/Parser/test.swift:14:20 - line:24:1] RangeText="{
    let x = { () in switch val {
        case let c as? Unicode.UTF16.CodeUnit where Unicode.UTF16.isSurrogate(c):
            UnicodeScalar(c)
        default:
            UnicodeScalar(0xFFFD)
    }
    }()

    return x
"
6.  While type-checking declaration 0x5dc7072fed48 (at Libraries/LibWeb/HTML/Parser/test.swift:15:5)
7.  While evaluating request PatternBindingEntryRequest((unknown decl)@Libraries/LibWeb/HTML/Parser/test.swift:15:5, 0)
8.  While type-checking expression at [Libraries/LibWeb/HTML/Parser/test.swift:15:13 - line:21:7] RangeText="{ () in switch val {
        case let c as? Unicode.UTF16.CodeUnit where Unicode.UTF16.isSurrogate(c):
            UnicodeScalar(c)
        default:
            UnicodeScalar(0xFFFD)
    }
    }("
9.  While type-checking-target starting at Libraries/LibWeb/HTML/Parser/test.swift:15:13
 #0 0x00005dc701a121d7 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x85091d7)
 #1 0x00005dc701a0fd8e llvm::sys::RunSignalHandlers() (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x8506d8e)
 #2 0x00005dc701a1287a SignalHandler(int) Signals.cpp:0:0
 #3 0x00007c2fd4445320 (/lib/x86_64-linux-gnu/libc.so.6+0x45320)
 #4 0x00007c2fd449eb1c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x00007c2fd449eb1c __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #6 0x00007c2fd449eb1c pthread_kill ./nptl/pthread_kill.c:89:10
 #7 0x00007c2fd444526e raise ./signal/../sysdeps/posix/raise.c:27:6
 #8 0x00007c2fd44288ff abort ./stdlib/abort.c:81:7
 #9 0x00007c2fd442881b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#10 0x00007c2fd443b507 (/lib/x86_64-linux-gnu/libc.so.6+0x3b507)
#11 0x00005dc6fb7fb08c swift::constraints::ConstraintSystem::getType(swift::ASTNode) const crtstuff.c:0:0
#12 0x00005dc6fbc1c525 swift::ASTVisitor<(anonymous namespace)::SyntacticElementConstraintGenerator, void, void, void, void, void, void>::visit(swift::Stmt*) CSSyntacticElement.cpp:0:0
#13 0x00005dc6fbc1fb3a swift::constraints::ConstraintSystem::simplifySyntacticElementConstraint(swift::ASTNode, swift::constraints::ContextualTypeInfo, bool, swift::optionset::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x2716b3a)
#14 0x00005dc6fb85b1db swift::constraints::ConstraintSystem::simplifyConstraint(swift::constraints::Constraint const&) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x23521db)
#15 0x00005dc6fb87e194 swift::constraints::ConjunctionElement::attempt(swift::constraints::ConstraintSystem&) const (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x2375194)
#16 0x00005dc6fb892d0a swift::constraints::ConjunctionStep::attempt(swift::constraints::ConjunctionElement const&) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x2389d0a)
#17 0x00005dc6fb8948cb swift::constraints::BindingStep<swift::constraints::ConjunctionElementProducer>::take(bool) crtstuff.c:0:0
#18 0x00005dc6fb893018 swift::constraints::ConjunctionStep::resume(bool) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x238a018)
#19 0x00005dc6fb87714c swift::constraints::ConstraintSystem::solveImpl(llvm::SmallVectorImpl<swift::constraints::Solution>&) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x236e14c)
#20 0x00005dc6fb879b2c swift::constraints::ConstraintSystem::solve(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::FreeTypeVariableBinding) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x2370b2c)
#21 0x00005dc6fb87944d swift::constraints::ConstraintSystem::solveImpl(swift::constraints::SyntacticElementTarget&, swift::FreeTypeVariableBinding) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x237044d)
#22 0x00005dc6fb878adc swift::constraints::ConstraintSystem::solve(swift::constraints::SyntacticElementTarget&, swift::FreeTypeVariableBinding) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x236fadc)
#23 0x00005dc6fba3344c swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::DiagnosticTransaction*) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x252a44c)
#24 0x00005dc6fba332be swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::DiagnosticTransaction*) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x252a2be)
#25 0x00005dc6fba35036 swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*, swift::Type, swift::PatternBindingDecl*, unsigned int, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x252c036)
#26 0x00005dc6fba3532f swift::TypeChecker::typeCheckPatternBinding(swift::PatternBindingDecl*, unsigned int, swift::Type, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x252c32f)
#27 0x00005dc6fbb4d5de swift::PatternBindingEntryRequest::evaluate(swift::Evaluator&, swift::PatternBindingDecl*, unsigned int) const (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x26445de)
#28 0x00005dc6fc0163b8 swift::PatternBindingEntryRequest::OutputType swift::Evaluator::getResultUncached<swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType swift::evaluateOrDefault<swift::PatternBindingEntryRequest>(swift::Evaluator&, swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType)::'lambda'()>(swift::PatternBindingEntryRequest const&, swift::PatternBindingEntryRequest::OutputType swift::evaluateOrDefault<swift::PatternBindingEntryRequest>(swift::Evaluator&, swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType)::'lambda'()) crtstuff.c:0:0
#29 0x00005dc6fbfd82d7 swift::PatternBindingDecl::getCheckedPatternBindingEntry(unsigned int) const (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x2acf2d7)
#30 0x00005dc6fba86e9d (anonymous namespace)::DeclChecker::visit(swift::Decl*) TypeCheckDeclPrimary.cpp:0:0
#31 0x00005dc6fba863a4 swift::TypeChecker::typeCheckDecl(swift::Decl*) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x257d3a4)
#32 0x00005dc6fbb4150c swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) TypeCheckStmt.cpp:0:0
#33 0x00005dc6fbb3ef9c bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) TypeCheckStmt.cpp:0:0
#34 0x00005dc6fbb3e533 (anonymous namespace)::StmtChecker::typeCheckBody(swift::BraceStmt*&) TypeCheckStmt.cpp:0:0
#35 0x00005dc6fbb3dd30 swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x2634d30)
#36 0x00005dc6fc09f7f2 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'()) crtstuff.c:0:0
#37 0x00005dc6fbff3549 swift::AbstractFunctionDecl::getTypecheckedBody() const (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x2aea549)
#38 0x00005dc6fc17213b swift::SourceFile::typeCheckDelayedFunctions() (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x2c6913b)
#39 0x00005dc6fbb90eb2 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x2687eb2)
#40 0x00005dc6fbb9334b 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'()) crtstuff.c:0:0
#41 0x00005dc6fbb90d95 swift::performTypeChecking(swift::SourceFile&) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x2687d95)
#42 0x00005dc6fa70b379 bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_8>(long, swift::SourceFile&) Frontend.cpp:0:0
#43 0x00005dc6fa70207a swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x11f907a)
#44 0x00005dc6fa702017 swift::CompilerInstance::performSema() (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0x11f9017)
#45 0x00005dc6fa3b4e72 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) FrontendTool.cpp:0:0
#46 0x00005dc6fa3a6ea0 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) FrontendTool.cpp:0:0
#47 0x00005dc6fa3a5bda swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0xe9cbda)
#48 0x00005dc6fa17d80e swift::mainEntry(int, char const**) (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0xc7480e)
#49 0x00007c2fd442a1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#50 0x00007c2fd442a28b call_init ./csu/../csu/libc-start.c:128:20
#51 0x00007c2fd442a28b __libc_start_main ./csu/../csu/libc-start.c:347:5
#52 0x00005dc6fa17c8a5 _start (/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2024-11-09/usr/bin/swift-frontend+0xc738a5)

Expected behavior

The compile errors at the top alone, without a crash dump below.

Environment

Swift version 6.1-dev (LLVM 5c0d283c37132bf, Swift b5af518638c9a16) Target: x86_64-unknown-linux-gnu

Additional information

The full test case was like so, with a slightly different stack trace:

https://gist.github.com/ADKaster/3dc5de4169d21f41c4679154ac2f1376