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-10083] When Swift can't determine the return type of a closure, misleading and incorrect errors are shown instead #52485

Open
swift-ci opened this issue Mar 11, 2019 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10083
Radar None
Original Reporter twof (JIRA User)
Type Bug

Attachment: Download

Environment

macOS 10.14
Swift 4.2/Swift 5 beta toolchain

In order to run the zipped project, run `swift run` inside its directory. Dependencies will be fetched and compiled.

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee None
Priority Medium

md5: c12fee4b6ba2c0734b38fdf25e653786

Issue Description:

Given the following code which can be found in `main.swift` in the attached project

import Vapor

let app = try Application()
let router = try app.make(Router.self)

router.get("users") { req in
    let id = 1

    return "User #\(id)"
}

try app.run()
{\code}

The following error is thrown upon `swift run`

error: type 'PathComponentsRepresentable' does not conform to protocol 'ExpressibleByStringLiteral'
router.get("users") { req in
^
{\code}

While that error is technically correct, that's not the problem here. `router.get` is defined as

public func get<T>(_ path: PathComponentsRepresentable..., use closure: @escaping (Request) throws -> T) -> Route<Responder>
        where T: ResponseEncodable
{\code}

and `String` conforms to `PathComponentsRepresentable`. The above code runs fine if it's changed to

import Vapor

let app = try Application()
let router = try app.make(Router.self)

router.get("users") { req -> String in // CHANGE HERE: Explicit return type added
let id = 1

return "User #(id)"
}

try app.run()
{\code}

@swift-ci
Copy link
Collaborator Author

Comment by Alex Reilly (JIRA)

Reported in part by adtrevor (JIRA User)

@belkadan
Copy link
Contributor

cc @xedin

@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 diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants