Skip to content

[TypeChecker] NFC: Add additional test-cases for @autoclosure changes #20500

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions test/attr/attr_autoclosure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,38 @@ func passNonThrowingToThrowingAC(_ fn: @autoclosure () -> Int) {
func passThrowingToThrowingAC(_ fn: @autoclosure () throws -> Int) {
takesThrowingAutoclosure(fn)
}

// rdar://problem/20591571 - Various type inference problems with @autoclosure
func rdar_20591571() {
func foo(_ g: @autoclosure () -> Int) {
typealias G = ()->Int
let _ = unsafeBitCast(g, to: G.self) // expected-error {{converting non-escaping value to 'T' may allow it to escape}}
}

func id<T>(_: T) -> T {}
func same<T>(_: T, _: T) {}

func takesAnAutoclosure(_ fn: @autoclosure () -> Int, _ efn: @escaping @autoclosure () -> Int) {
// expected-note@-1 2{{parameter 'fn' is implicitly non-escaping}}

var _ = fn // expected-error {{non-escaping parameter 'fn' may only be called}}
let _ = fn // expected-error {{non-escaping parameter 'fn' may only be called}}

var _ = efn
let _ = efn

_ = id(fn) // expected-error {{converting non-escaping value to 'T' may allow it to escape}}
_ = same(fn, { 3 }) // expected-error {{converting non-escaping value to 'T' may allow it to escape}}
_ = same({ 3 }, fn) // expected-error {{converting non-escaping value to 'T' may allow it to escape}}

withoutActuallyEscaping(fn) { _ in } // Ok
withoutActuallyEscaping(fn) { (_: () -> Int) in } // Ok
}
}

// rdar://problem/30906031 - [SR-4188]: withoutActuallyEscaping doesn't accept an @autoclosure argument
func rdar_30906031(in arr: [Int], fn: @autoclosure () -> Int) -> Bool {
return withoutActuallyEscaping(fn) { escapableF in // Ok
arr.lazy.filter { $0 >= escapableF() }.isEmpty
}
}