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

Crash when trying to directly iterate over the return value of a function taking a closure #74545

Open
msklywenn opened this issue Jun 19, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software expressions Feature: expressions type checker Area → compiler: Semantic analysis

Comments

@msklywenn
Copy link

Description

Running the given code crashes the compiler

Reproduction

func Map<Source, Destination>(_ transform: @escaping (Source) -> Destination) -> any Sequence<Destination> {
        return []
}

for i in Map({$0 + 1}) {
        print(i)
}

Stack dump

error: compile command failed due to signal 6 (use -v to see invocation)
Unhandled coercion:
(type_variable_type id=2)
(struct_type decl=Swift.(file).Int)
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file Bug.swift -target arm64-apple-macosx14.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -color-diagnostics -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name Bug -disable-clang-spi -target-sdk-version 14.5 -target-sdk-name macosx14.5 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /var/folders/6c/_9x490kd5n1bym6_4yw_2hqh0000gn/T/TemporaryDirectory.KsBRsO/Bug-1.o
1.	Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
2.	Compiling with the current language version
3.	While evaluating request TypeCheckSourceFileRequest(source_file "Bug.swift")
4.	While type-checking statement at [Bug.swift:5:1 - line:7:1] RangeText="for i in Map({$0 + 1}) {
	print(i)
"
5.	While type-checking statement at [Bug.swift:5:1 - line:7:1] RangeText="for i in Map({$0 + 1}) {
	print(i)
"
6.	While type-checking-for-each statement at [Bug.swift:5:1 - line:7:1] RangeText="for i in Map({$0 + 1}) {
	print(i)
"
7.	While type-checking-target starting at Bug.swift:5:1
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           0x0000000107f63f3c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000107f630f8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000107f64544 SignalHandler(int) + 360
3  libsystem_platform.dylib 0x000000019f8ab584 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000019f87ac20 pthread_kill + 288
5  libsystem_c.dylib        0x000000019f787a30 abort + 180
6  swift-frontend           0x00000001038c3224 (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 9076
7  swift-frontend           0x00000001038eb18c buildOpaqueElementConversion((anonymous namespace)::ExprRewriter&, swift::SourceRange, swift::Type, swift::Type, bool, swift::constraints::ConstraintLocatorBuilder, unsigned int) + 608
8  swift-frontend           0x00000001038ef468 (anonymous namespace)::ExprRewriter::coerceExistential(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 1860
9  swift-frontend           0x00000001038c2804 (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 6484
10 swift-frontend           0x00000001038c0dd4 swift::constraints::Solution::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocator*) + 176
11 swift-frontend           0x00000001038eb748 applySolutionToInitialization(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget, swift::Expr*, std::__1::function<llvm::Optional<swift::constraints::SyntacticElementTarget> (swift::constraints::SyntacticElementTarget)>) + 764
12 swift-frontend           0x00000001038bfe88 (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 5060
13 swift-frontend           0x00000001038ee028 llvm::Optional<swift::constraints::SyntacticElementTarget> llvm::function_ref<llvm::Optional<swift::constraints::SyntacticElementTarget> (swift::constraints::SyntacticElementTarget)>::callback_fn<(anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget)::$_34>(long, swift::constraints::SyntacticElementTarget) + 76
14 swift-frontend           0x00000001038ec07c applySolutionToForEachStmt(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget, llvm::function_ref<llvm::Optional<swift::constraints::SyntacticElementTarget> (swift::constraints::SyntacticElementTarget)>) + 512
15 swift-frontend           0x00000001038bf358 (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 2196
16 swift-frontend           0x00000001038be5d0 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 6984
17 swift-frontend           0x0000000103b83b94 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 660
18 swift-frontend           0x0000000103b85430 swift::TypeChecker::typeCheckForEachBinding(swift::DeclContext*, swift::ForEachStmt*) + 428
19 swift-frontend           0x0000000103c963f4 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 760
20 swift-frontend           0x0000000103c95fc4 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::Stmt>(swift::Stmt*&) + 316
21 swift-frontend           0x0000000103c91008 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 104
22 swift-frontend           0x0000000103c96224 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 296
23 swift-frontend           0x0000000103c93a6c bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 316
24 swift-frontend           0x0000000103c93c40 swift::TypeChecker::typeCheckTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 140
25 swift-frontend           0x0000000103ce0498 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 472
26 swift-frontend           0x0000000103ce6988 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 664
27 swift-frontend           0x0000000103ce0240 swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 228
28 swift-frontend           0x0000000102a70854 swift::CompilerInstance::performSema() + 196
29 swift-frontend           0x0000000102865634 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1352
30 swift-frontend           0x00000001028636d0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4968
31 swift-frontend           0x00000001027f2e8c swift::mainEntry(int, char const**) + 2612
32 dyld                     0x000000019f4f20e0 start + 2360
error: fatalError


### Expected behavior

No crash...

### 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

The crash goes away if I store the result of the Map call into a variable and then pass that variable to the for loop.
@msklywenn msklywenn 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 Jun 19, 2024
@hborla hborla added type checker Area → compiler: Semantic analysis expressions Feature: expressions and removed triage needed This issue needs more specific labels 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. crash Bug: A crash, i.e., an abnormal termination of software expressions Feature: expressions type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants