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-12109] Invalid "call can throw"-error related to default argument of closure type #54545

Closed
jepers opened this issue Jan 29, 2020 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@jepers
Copy link

jepers commented Jan 29, 2020

Previous ID SR-12109
Radar rdar://problem/59132830
Original Reporter @jepers
Type Bug
Status Resolved
Resolution Duplicate
Environment

Default toolchain of Xcode 11.3.1 (11C504)
and
Swift Development Snapshot 2020-01-21

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 5ea63af8ee144e06c5700866a9dfedb3

duplicates:

  • SR-1534 Error: Call is to rethrows function, but a defaulted argument function can throw

Issue Description:

//----------------------------------------------------------------------------
// Demonstration of (what I guess must be) an invalid error.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// I have only tested this with the default toolchain of
// Xcode 11.3.1 (11C504) and with dev snapshot 2020-01-21.
//----------------------------------------------------------------------------


func sort<E: Comparable>(
    _ a: inout E,
    _ b: inout E,
    by areInOrder: (E, E) throws -> Bool = (<)
) rethrows
{
    if try areInOrder(a, b) == false { swap(&a, &b) }
}

// NOTE: In Xcode, if we jump-to-definition on the default
// value for `areInOrder`, ie (<), then we are taken to this
// non-throwing method requirement of Comparable:
// static func < (lhs: Self, rhs: Self) -> Bool


func demo() {
    var p, q: Int
    for _ in 0 ..< 3 {
        repeat {
            (p, q) = (Int.random(in: 1 ... 6), Int.random(in: 1 ... 6))
        } while p == q
        print("    random:", p, q)
        sort(&p, &q, by: <) // <--- INVALID(?) ERROR: "Call can throw, but it
                     //      is not marked with `try` and the error
                     //      is not handled"
                     // WORKAROUND: Explicitly set the default (<), ie:
                     // change sort(&p, &q) to sort(&p, &q, by: <)
        print(" ascending:", p, q)
        sort(&p, &q, by: >) // <-- Compiles
        print("descending:", p, q, "\n")
    }
}
demo()


//----------------------------------------------------------------------------
// Compiling this (as is, without the workaround):
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// $ swiftc --version
// Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)
// Target: x86_64-apple-darwin19.2.0
// $ swiftc test.swift
// test.swift:32:9: error: call can throw, but it is not marked with 'try'
// and the error is not handled
//         sort(&p, &q) // <--- INVALID(?) ERROR: "Call can throw, but it
//         ^
// test.swift:32:9: note: call is to 'rethrows' function, but a defaulted
// argument function can throw
//         sort(&p, &q) // <--- INVALID(?) ERROR: "Call can throw, but it
//         ^
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// I see the note ("but a defaulted argument function can throw".) but I think
// it should not apply in the demonstrated context where the Comparable type
// is known to be `Int` and its (<) is known to *not* be throwable. I can see
// how it could apply in contexts where the type is unknown though, like this:
// func foo<C: Comparable>(_ a: C, _ b: C) {
//    sort(&a, &b)
// }
//----------------------------------------------------------------------------


//----------------------------------------------------------------------------
// Compiling (with workaround, ie adding `by: <`) and running:
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// $ swiftc test.swift && ./test
//     random: 2 4
//  ascending: 2 4
// descending: 4 2 
// 
//     random: 6 1
//  ascending: 1 6
// descending: 6 1 
// 
//     random: 3 4
//  ascending: 3 4
// descending: 4 3 
//----------------------------------------------------------------------------
@beccadax
Copy link
Contributor

beccadax commented Feb 4, 2020

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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
Projects
None yet
Development

No branches or pull requests

2 participants