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 in pack iteration #71921

Closed
Tracked by #72153
MaxDesiatov opened this issue Feb 27, 2024 · 3 comments · Fixed by #74414
Closed
Tracked by #72153

Compiler crash in pack iteration #71921

MaxDesiatov opened this issue Feb 27, 2024 · 3 comments · Fixed by #74414
Labels
assertion failure Bug → crash: An assertion failure bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. closures Feature: closures compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software expressions Feature: expressions generics Feature: generic declarations and types pack expansions Feature → expressions: Pack expansion expressions parameter packs Feature → generics: Parameter packs swift 6.0 type checker Area → compiler: Semantic analysis verifier

Comments

@MaxDesiatov
Copy link
Contributor

MaxDesiatov commented Feb 27, 2024

Description

Simple pack iteration snippet crashes the compiler.

Reproduction

protocol Signal {
    mutating func process() -> Float
}

struct Mixer<each Source: Signal> {
    var sources: (repeat each Source)

    mutating func process() -> Float {
        var result: Float = 0

        self.sources = (repeat ({
            var signal = $0
            result += signal.process()
            return signal
        }(each sources)))

        return result
    }
}

Stack dump

1.	Apple Swift version 5.11-dev (LLVM 87ace14daa2139a, Swift 8bb684628593220)
2.	Compiling with the current language version
3.	While walking into 'Mixer' (at repro.swift:5:1)
4.	While walking into body of 'process()' (at repro.swift:8:14)
5.	While verifying VarDecl '$0' (at repro.swift:11:33)
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           0x0000000109d02888 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109d01054 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109d02ed0 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x0000000183cf9a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000183cc9cc0 pthread_kill + 288
5  libsystem_c.dylib        0x0000000183bd5a40 abort + 180
6  swift-frontend           0x000000010a1ca420 (anonymous namespace)::Verifier::verifyChecked(swift::AbstractStorageDecl*) (.cold.1) + 0
7  swift-frontend           0x0000000105ec385c (anonymous namespace)::Verifier::verifyChecked(swift::VarDecl*) + 748
8  swift-frontend           0x0000000105eb1e84 (anonymous namespace)::Verifier::walkToDeclPost(swift::Decl*) + 2792
9  swift-frontend           0x0000000105ec5250 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 312
10 swift-frontend           0x0000000105ec5968 (anonymous namespace)::Traversal::visitClosureExpr(swift::ClosureExpr*) + 104
11 swift-frontend           0x0000000105ec4978 (anonymous namespace)::Traversal::doIt(swift::Expr*) + 676
12 swift-frontend           0x0000000105ec6778 (anonymous namespace)::Traversal::visitApplyExpr(swift::ApplyExpr*) + 40
13 swift-frontend           0x0000000105ec47b0 (anonymous namespace)::Traversal::doIt(swift::Expr*) + 220
14 swift-frontend           0x0000000105ec4784 (anonymous namespace)::Traversal::doIt(swift::Expr*) + 176
15 swift-frontend           0x0000000105ec47c0 (anonymous namespace)::Traversal::doIt(swift::Expr*) + 236
16 swift-frontend           0x0000000105ec572c (anonymous namespace)::Traversal::visitTupleExpr(swift::TupleExpr*) + 88
17 swift-frontend           0x0000000105ec485c (anonymous namespace)::Traversal::doIt(swift::Expr*) + 392
18 swift-frontend           0x0000000105ec6058 (anonymous namespace)::Traversal::visitAssignExpr(swift::AssignExpr*) + 60
19 swift-frontend           0x0000000105ec48f4 (anonymous namespace)::Traversal::doIt(swift::Expr*) + 544
20 swift-frontend           0x0000000105ec6b64 swift::Stmt* llvm::function_ref<swift::Stmt* (swift::Stmt*)>::callback_fn<(anonymous namespace)::Traversal::doIt(swift::Stmt*)::'lambda'(swift::Stmt*)>(long, swift::Stmt*) + 380
21 swift-frontend           0x0000000105ec6984 swift::Stmt* (anonymous namespace)::Traversal::traverse<swift::Stmt>(swift::ASTWalker::PreWalkResult<swift::Stmt*>, llvm::function_ref<swift::Stmt* (swift::Stmt*)>, llvm::function_ref<swift::ASTWalker::PostWalkResult<swift::Stmt*> (swift::Stmt*)>) + 84
22 swift-frontend           0x0000000105ec89c8 (anonymous namespace)::Traversal::visitAbstractFunctionDecl(swift::AbstractFunctionDecl*) + 660
23 swift-frontend           0x0000000105ec77b8 (anonymous namespace)::Traversal::visit(swift::Decl*) + 156
24 swift-frontend           0x0000000105ec5238 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 288
25 swift-frontend           0x0000000105ec85c0 (anonymous namespace)::Traversal::visitNominalTypeDecl(swift::NominalTypeDecl*) + 480
26 swift-frontend           0x0000000105ec77a8 (anonymous namespace)::Traversal::visit(swift::Decl*) + 140
27 swift-frontend           0x0000000105ec5238 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 288
28 swift-frontend           0x0000000105ec510c swift::Decl::walk(swift::ASTWalker&) + 32
29 swift-frontend           0x000000010609d538 swift::SourceFile::walk(swift::ASTWalker&) + 248
30 swift-frontend           0x0000000105eaeed0 swift::verify(swift::SourceFile&) + 96
31 swift-frontend           0x00000001061c04e4 swift::TypeCheckSourceFileRequest::cacheResult(std::__1::tuple<>) const + 76
32 swift-frontend           0x0000000105bbc950 swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 44
33 swift-frontend           0x0000000104af3b64 bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_7>(long, swift::SourceFile&) + 16
34 swift-frontend           0x0000000104aed98c swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) + 76
35 swift-frontend           0x0000000104aed920 swift::CompilerInstance::performSema() + 76
36 swift-frontend           0x00000001048ca160 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 60
37 swift-frontend           0x00000001048bbaf4 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 708
38 swift-frontend           0x00000001048bab28 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2292
39 swift-frontend           0x0000000104709768 swift::mainEntry(int, char const**) + 3096
40 dyld                     0x00000001839490e0 start + 2360

Expected behavior

This code compiles or an actionable diagnostic message is emitted

Environment

Apple Swift version 5.11-dev (LLVM 87ace14daa2139a, Swift 8bb6846)
Target: arm64-apple-macosx14.0

Additional information

No response

@MaxDesiatov MaxDesiatov added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. generics Feature: generic declarations and types crash Bug: A crash, i.e., an abnormal termination of software parameter packs Feature → generics: Parameter packs labels Feb 27, 2024
@MaxDesiatov MaxDesiatov changed the title Compiler crashes in pack iteration Compiler crash in pack iteration Feb 27, 2024
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself verifier assertion failure Bug → crash: An assertion failure swift 6.0 pack iteration Feature → statements → for-in: Pack iteration type checker Area → compiler: Semantic analysis closures Feature: closures expressions Feature: expressions pack expansions Feature → expressions: Pack expansion expressions labels Mar 7, 2024
@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Mar 7, 2024

Reduced:

func test<each Source>(sources: (repeat each Source)) {
  let _ = (repeat { return $0 }(each sources)) // crash
  let _ = (repeat { (s: each Source) in return s }(each sources)) // error: pack reference 'Source' requires expansion using keyword 'repeat'
}

Similar to #66917.


@xedin I assume closure expansion patterns are still underway. Do you think it reasonable to fold this into #69282?

@simanerush simanerush removed the pack iteration Feature → statements → for-in: Pack iteration label Mar 7, 2024
@xedin
Copy link
Contributor

xedin commented Mar 7, 2024

I think it’s reasonable, thank you!

@slavapestov
Copy link
Contributor

After #74020 the only remaining issue here is the var capture whose type contains a pack element. I forgot to implement that. Should be straightforward.

slavapestov added a commit to slavapestov/swift that referenced this issue Jun 14, 2024
A mutable 'var' becomes a SILBoxType, and we need to plumb the
correct generic signature through here too.

Fixes swiftlang#71921.
slavapestov added a commit to slavapestov/swift that referenced this issue Jun 15, 2024
A mutable 'var' becomes a SILBoxType, and we need to plumb the
correct generic signature through here too.

Fixes swiftlang#71921.
slavapestov added a commit to slavapestov/swift that referenced this issue Jun 18, 2024
A mutable 'var' becomes a SILBoxType, and we need to plumb the
correct generic signature through here too.

Fixes swiftlang#71921.
slavapestov added a commit to slavapestov/swift that referenced this issue Jun 18, 2024
A mutable 'var' becomes a SILBoxType, and we need to plumb the
correct generic signature through here too.

Fixes swiftlang#71921.
meg-gupta pushed a commit to meg-gupta/swift that referenced this issue Jun 20, 2024
A mutable 'var' becomes a SILBoxType, and we need to plumb the
correct generic signature through here too.

Fixes swiftlang#71921.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assertion failure Bug → crash: An assertion failure bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. closures Feature: closures compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software expressions Feature: expressions generics Feature: generic declarations and types pack expansions Feature → expressions: Pack expansion expressions parameter packs Feature → generics: Parameter packs swift 6.0 type checker Area → compiler: Semantic analysis verifier
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants