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

[SR-12277] Swift compiler ignores {{return}} statement #54705

Open
weissi opened this issue Feb 26, 2020 · 2 comments
Open

[SR-12277] Swift compiler ignores {{return}} statement #54705

weissi opened this issue Feb 26, 2020 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@weissi
Copy link
Member

weissi commented Feb 26, 2020

Previous ID SR-12277
Radar rdar://problem/52204414
Original Reporter @weissi
Type Bug

Attachment: Download

Environment
$ jw-docker-swift-master swift -version
docker.io/swiftlang/swift:nightly-master-bionic
Swift version 5.2-dev (LLVM 4cd92ac2f9, Swift 79716efbee)
Target: x86_64-unknown-linux-gnu
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 7120d42547df677b3aefa8e54450a1ab

Issue Description:

In this code

import NIO
struct Thing {}

func bar(eventLoop: EventLoop, process: @escaping (Thing) -> EventLoopFuture<Void>) -> EventLoopFuture<Void> {
    func doIt(thingsLeft: ArraySlice<Thing>) -> EventLoopFuture<Void> {
        var thingsLeft = thingsLeft
        guard let first = thingsLeft.popFirst() else {
            return eventLoop.makeSucceededFuture(())
        }

        return process(first).flatMap { [thingsLeft] in
            return eventLoop.scheduleTask(in: .milliseconds(100)) {
                return doIt(thingsLeft: thingsLeft)
            }.futureResult /* <=== actual bug: .flatMap { $0 } missing */
        }
    }
    return doIt(thingsLeft: [][...])
}

the Swift compiler actually complains with

/tmp/test_package_ugk1IN/Sources/TestApp/main.swift:14:24: warning: result of call to 'doIt(thingsLeft:)' is unused
                return doIt(thingsLeft: thingsLeft)
                       ^   ~~~~~~~~~~~~~~~~~~~~~~~~

so if I interpret that correctly, despite the fact that doIt returns EventLoopFuture<Void> it decided to ignore that value and make the closure return Void !?

repro package attached.

Repro that doesn't depend on NIO

enum Time {
    case milliseconds(Int)
}
struct Thing {}
struct Scheduled<T> {
    let futureResult: EventLoopFuture<T>
}
struct EventLoop {
    func makeSucceededFuture<T>(_ v: T) -> EventLoopFuture<T> {
        fatalError()
    }
    func scheduleTask<T>(in t: Time, _ body: @escaping () -> T) -> Scheduled<T> {
        fatalError()
    }
}
struct EventLoopFuture<T> {
    func flatMap<V>(_ body: @escaping (T) -> EventLoopFuture<V>) -> EventLoopFuture<V> {
        fatalError()
    }
}


func bar(eventLoop: EventLoop, process: @escaping (Thing) -> EventLoopFuture<Void>) -> EventLoopFuture<Void> {
    func doIt(thingsLeft: ArraySlice<Thing>) -> EventLoopFuture<Void> {
        var thingsLeft = thingsLeft
        guard let first = thingsLeft.popFirst() else {
            return eventLoop.makeSucceededFuture(())
        }

        return process(first).flatMap { [thingsLeft] in
            return eventLoop.scheduleTask(in: .milliseconds(100)) {
                return doIt(thingsLeft: thingsLeft)
            }.futureResult
        }
    }
    return doIt(thingsLeft: [][...])
}
@weissi
Copy link
Member Author

weissi commented Feb 26, 2020

@swift-ci create

@xedin
Copy link
Member

xedin commented Feb 28, 2020

Thanks for reporting! I think the problem here that we still add a default `Void` type even though closure has an explicit return...

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants