Skip to content

Invalid cast when throwing error in public init #74832

@finnvoor

Description

@finnvoor

Description

Had trouble tracking this down as it only happens in very specific cases, but the compiler crashes if you have a public class with a public init that throws an error. This only happens if the error has a stored property (most types trigger this, Double, Int, String, but not Array/Dictionary)

Reproduction

// test.swift

public struct Foo: Error, @unchecked Sendable {
    let foo: Int
}

public class Bar {
    public init() throws(Foo) {}
}

swiftc -O -wmo -enable-experimental-feature Embedded -c test.swift

Stack dump

Assertion failed: (castIsValid(op, S, Ty) && "Invalid cast!"), function Create, file Instructions.cpp, line 3427.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-06-28-a.xctoolchain/usr/bin/swift-frontend -frontend -c /Users/finnvoorhees/Downloads/swifttest/Bar.swift -target arm64-apple-macosx15.0 -Xllvm -aarch64-use-tbi -disable-objc-interop -color-diagnostics -O -enable-experimental-feature Embedded -empty-abi-descriptor -Xcc -working-directory -Xcc /Users/finnvoorhees/Downloads/swifttest -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-06-28-a.xctoolchain/usr/lib/swift -module-name Bar -in-process-plugin-server-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-06-28-a.xctoolchain/usr/lib/swift/host/libSwiftInProcPluginServer.dylib -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-06-28-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-06-28-a.xctoolchain/usr/local/lib/swift/host/plugins -enable-default-cmo -o /Users/finnvoorhees/Downloads/swifttest/Bar.o
1.	Apple Swift version 6.0-dev (LLVM 6b6d1cefaf4b4cc, Swift dd13e2f08336eab)
2.	Compiling with effective version 5.10
3.	While evaluating request IRGenRequest(IR Generation for module Bar)
4.	While emitting IR SIL function "@$s3BarAACAByAA3FooVYKcfC".
 for 'init()' (at /Users/finnvoorhees/Downloads/swifttest/Bar.swift:6:12)
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           0x00000001061dcb7c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001061db348 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001061dd1c4 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x0000000193a72184 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000193a3bf70 pthread_kill + 288
5  libsystem_c.dylib        0x0000000193948908 abort + 128
6  libsystem_c.dylib        0x0000000193947c1c err + 0
7  swift-frontend           0x00000001074f1c84 llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::BasicBlock*) (.cold.1) + 0
8  swift-frontend           0x0000000105f21338 llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::Instruction*) + 716
9  swift-frontend           0x0000000100e756a4 llvm::IRBuilderBase::CreateCast(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&) + 92
10 swift-frontend           0x00000001011485ac swift::irgen::IRGenFunction::emitScalarReturn(swift::SILType, swift::SILType, swift::irgen::Explosion&, bool, bool, swift::SILType)::$_10::operator()(llvm::Type*, llvm::Value*) const + 196
11 swift-frontend           0x0000000101148294 swift::irgen::IRGenFunction::emitScalarReturn(swift::SILType, swift::SILType, swift::irgen::Explosion&, bool, bool, swift::SILType) + 1636
12 swift-frontend           0x00000001013111ac (anonymous namespace)::IRGenSILFunction::visitSILBasicBlock(swift::SILBasicBlock*) + 64664
13 swift-frontend           0x0000000101300244 (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 9632
14 swift-frontend           0x00000001012fd710 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 1528
15 swift-frontend           0x000000010119b42c swift::irgen::IRGenerator::emitGlobalTopLevel(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>> const&) + 752
16 swift-frontend           0x00000001012aeef4 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 2172
17 swift-frontend           0x00000001012fcc0c swift::GeneratedModule swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::callDerived<0ul>(swift::Evaluator&, std::__1::integer_sequence<unsigned long, 0ul>) const + 200
18 swift-frontend           0x00000001012b7828 swift::IRGenRequest::OutputType swift::Evaluator::getResultUncached<swift::IRGenRequest, swift::IRGenRequest::OutputType swift::evaluateOrFatal<swift::IRGenRequest>(swift::Evaluator&, swift::IRGenRequest)::'lambda'()>(swift::IRGenRequest const&, swift::IRGenRequest::OutputType swift::evaluateOrFatal<swift::IRGenRequest>(swift::Evaluator&, swift::IRGenRequest)::'lambda'()) + 212
19 swift-frontend           0x00000001012affc4 swift::performIRGeneration(swift::ModuleDecl*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>, llvm::GlobalVariable**) + 1416
20 swift-frontend           0x0000000100db42cc generateIR(swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, llvm::GlobalVariable*&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>) + 272
21 swift-frontend           0x0000000100db0c80 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 1416
22 swift-frontend           0x0000000100db0310 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1228
23 swift-frontend           0x0000000100dbc140 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
24 swift-frontend           0x0000000100db1fb8 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 708
25 swift-frontend           0x0000000100db168c swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2344
26 swift-frontend           0x0000000100bbacc4 swift::mainEntry(int, char const**) + 3096
27 dyld                     0x00000001936bb274 start + 2840
./test.sh: line 1: 24215 Abort trap: 6           /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-06-28-a.xctoolchain/usr/bin/swiftc -O -wmo -enable-experimental-feature Embedded -c Bar.swift

Expected behavior

No issues building

Environment

June 28 Nightly
Apple Swift version 6.0-dev (LLVM 6b6d1cefaf4b4cc, Swift dd13e2f)
Target: arm64-apple-macosx15.0

Additional information

@rauhul
@kubamracek

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.crashBug: A crash, i.e., an abnormal termination of softwaretriage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions