You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
//----------------------------------------------------------------------------
The text was updated successfully, but these errors were encountered:
Environment
Default toolchain of Xcode 11.3.1 (11C504)
and
Swift Development Snapshot 2020-01-21
Additional Detail from JIRA
md5: 5ea63af8ee144e06c5700866a9dfedb3
duplicates:
Issue Description:
The text was updated successfully, but these errors were encountered: