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 with value and type pack when used in a method vs free function #73211

Closed
PopFlamingo opened this issue Apr 23, 2024 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. class Feature → type declarations: Class declarations compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software generics Feature: generic declarations and types methods Feature → functions: methods (member functions) parameter packs Feature → generics: Parameter packs SILGen Area → compiler: The SIL generation stage swift 5.10

Comments

@PopFlamingo
Copy link

Description

This simple example demonstrates a compiler crash when a function is defined as a method instead of a free function.

Reproduction

class Foo {
    init() {}
    func unchanged<each T>(_ value: ()->(repeat each T)) -> (repeat each T) {
        return (repeat (each value()))
    }
}

func unchanged<each T>(_ value: ()->(repeat each T)) -> (repeat each T) {
    return (repeat (each value()))
}
let foo = Foo()



print(foo.unchanged { "xyz" }) // Command SwiftCompile failed with a nonzero exit code + Command SwiftEmitModule failed with a nonzero exit code
// But this compiles fine: print(unchanged { "xyz" })

Stack dump

0  swift-frontend           0x0000000107ccff3c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000107ccf0f8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000107cd0544 SignalHandler(int) + 360
3  libsystem_platform.dylib 0x000000018d52b584 _sigtramp + 56
4  swift-frontend           0x0000000102bdbdcc swift::Lowering::ResultPlanBuilder::buildForPackExpansion(llvm::Optional<llvm::ArrayRef<swift::Lowering::Initialization*>>, swift::Lowering::AbstractionPattern, swift::ArrayRefView<swift::TupleTypeElt, swift::CanType, swift::getCanTupleEltType(swift::TupleTypeElt const&), false>) + 1080
5  swift-frontend           0x0000000102bdbdcc swift::Lowering::ResultPlanBuilder::buildForPackExpansion(llvm::Optional<llvm::ArrayRef<swift::Lowering::Initialization*>>, swift::Lowering::AbstractionPattern, swift::ArrayRefView<swift::TupleTypeElt, swift::CanType, swift::getCanTupleEltType(swift::TupleTypeElt const&), false>) + 1080
6  swift-frontend           0x0000000102be0900 void llvm::function_ref<void (swift::Lowering::TupleElementGenerator&)>::callback_fn<(anonymous namespace)::TupleInitializationResultPlan::TupleInitializationResultPlan(swift::Lowering::ResultPlanBuilder&, swift::Lowering::Initialization*, swift::Lowering::AbstractionPattern, swift::CanType, bool)::'lambda'(swift::Lowering::TupleElementGenerator&)>(long, swift::Lowering::TupleElementGenerator&) + 236
7  swift-frontend           0x0000000103452404 swift::Lowering::AbstractionPattern::forEachTupleElement(swift::CanType, llvm::function_ref<void (swift::Lowering::TupleElementGenerator&)>) const + 188
8  swift-frontend           0x0000000102bdb11c swift::Lowering::ResultPlanBuilder::buildForTuple(swift::Lowering::Initialization*, swift::Lowering::AbstractionPattern, swift::CanType) + 1300
9  swift-frontend           0x0000000102bdab04 swift::Lowering::ResultPlanBuilder::buildTopLevelResult(swift::Lowering::Initialization*, swift::SILLocation) + 2592
10 swift-frontend           0x0000000102c01c90 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 3848
11 swift-frontend           0x0000000102bff7bc swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 3060
12 swift-frontend           0x0000000102c6af2c swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 40
13 swift-frontend           0x0000000102c90864 swift::Lowering::ManagedValue llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SGFContext)>::callback_fn<(anonymous namespace)::RValueEmitter::visitErasureExpr(swift::ErasureExpr*, swift::Lowering::SGFContext)::$_10>(long, swift::Lowering::SGFContext) + 64
14 swift-frontend           0x0000000102c4f428 void llvm::function_ref<void (swift::SILValue)>::callback_fn<swift::Lowering::SILGenFunction::emitExistentialErasure(swift::SILLocation, swift::CanType, swift::Lowering::TypeLowering const&, swift::Lowering::TypeLowering const&, llvm::ArrayRef<swift::ProtocolConformanceRef>, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SGFContext)>, bool)::$_6>(long, swift::SILValue) + 308
15 swift-frontend           0x0000000102c2c4a0 swift::Lowering::SILGenBuilder::bufferForExpr(swift::SILLocation, swift::SILType, swift::Lowering::TypeLowering const&, swift::Lowering::SGFContext, llvm::function_ref<void (swift::SILValue)>) + 136
16 swift-frontend           0x0000000102c4ad70 swift::Lowering::SILGenFunction::emitExistentialErasure(swift::SILLocation, swift::CanType, swift::Lowering::TypeLowering const&, swift::Lowering::TypeLowering const&, llvm::ArrayRef<swift::ProtocolConformanceRef>, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SGFContext)>, bool) + 1132
17 swift-frontend           0x0000000102c84480 (anonymous namespace)::RValueEmitter::visitErasureExpr(swift::ErasureExpr*, swift::Lowering::SGFContext) + 732
18 swift-frontend           0x0000000102c682cc swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional<swift::SILLocation>) + 128
19 swift-frontend           0x0000000102bd55ec swift::Lowering::ArgumentSource::forwardInto(swift::Lowering::SILGenFunction&, swift::Lowering::Initialization*) && + 76
20 swift-frontend           0x0000000102c8dee4 (anonymous namespace)::RValueEmitter::visitCollectionExpr(swift::CollectionExpr*, swift::Lowering::SGFContext) + 1916
21 swift-frontend           0x0000000102c6af2c swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 40
22 swift-frontend           0x0000000102c0ec04 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern, llvm::Optional<swift::AnyFunctionType::Param>) + 4684
23 swift-frontend           0x0000000102bfceb8 (anonymous namespace)::ArgEmitter::emitSingleArg(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern, llvm::Optional<swift::AnyFunctionType::Param>) + 224
24 swift-frontend           0x0000000102c0d6a0 (anonymous namespace)::ArgEmitter::emitPreparedArgs(swift::Lowering::PreparedArguments&&, swift::Lowering::AbstractionPattern) + 260
25 swift-frontend           0x0000000102c19fb4 (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<(anonymous namespace)::DelayedArgument>&, swift::ForeignInfo const&) && + 672
26 swift-frontend           0x0000000102c199cc (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, swift::ForeignInfo const&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&) + 1000
27 swift-frontend           0x0000000102c01d68 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 4064
28 swift-frontend           0x0000000102bff7bc swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 3060
29 swift-frontend           0x0000000102c6b618 swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 892
30 swift-frontend           0x0000000102d0f970 swift::ASTVisitor<swift::Lowering::SILGenTopLevel, void, void, void, void, void, void>::visit(swift::Decl*) + 1860
31 swift-frontend           0x0000000102d0e5e0 swift::Lowering::SILGenModule::emitEntryPoint(swift::SourceFile*, swift::SILFunction*) + 1180
32 swift-frontend           0x0000000102bf2a48 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 1400
33 swift-frontend           0x0000000102d01f1c swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 196
34 swift-frontend           0x0000000102bf5f44 llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 584
35 swift-frontend           0x00000001025cd408 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 548
36 swift-frontend           0x00000001025d1694 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1448
37 swift-frontend           0x00000001025cf6d0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4968
38 swift-frontend           0x000000010255ee8c swift::mainEntry(int, char const**) + 2612
39 dyld                     0x000000018d1720e0 start + 2360

Expected behavior

Both should be compiling fine

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Additional information

I suppose (although not sure at all) it may be related to #73209 as it follows the same pattern where a method causes an issue and not a free function, but #73209 causes a runtime crash whereas the present issue causes a compiler crash, and both issues also have slightly different code.

@PopFlamingo PopFlamingo 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 Apr 23, 2024
@PopFlamingo PopFlamingo changed the title Compiler crash with value and type pack when used as method vs free function Compiler crash with value and type pack when used in a method vs free function Apr 23, 2024
@hborla hborla added parameter packs Feature → generics: Parameter packs and removed triage needed This issue needs more specific labels labels Jul 14, 2024
@hborla
Copy link
Member

hborla commented Jul 14, 2024

This is fixed in 6.0 and on main.

@hborla hborla closed this as completed Jul 14, 2024
@AnthonyLatsis AnthonyLatsis added generics Feature: generic declarations and types compiler The Swift compiler itself SILGen Area → compiler: The SIL generation stage class Feature → type declarations: Class declarations methods Feature → functions: methods (member functions) swift 5.10 labels Jul 14, 2024
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. class Feature → type declarations: Class declarations compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software generics Feature: generic declarations and types methods Feature → functions: methods (member functions) parameter packs Feature → generics: Parameter packs SILGen Area → compiler: The SIL generation stage swift 5.10
Projects
None yet
Development

No branches or pull requests

3 participants