Skip to content

Commit 224613a

Browse files
committed
[noescape-by-default] Expand useful diagnostics to more cases
Extends the more useful diagnostics for non-escaping function parameters used in @escaping contexts to apply to all functions that are equivalent modulo ExtInfo.
1 parent 9cf4f04 commit 224613a

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3613,8 +3613,8 @@ static bool tryDiagnoseNonEscapingParameterToEscaping(Expr *expr, Type srcType,
36133613
if (!srcFT || !destFT || !srcFT->isNoEscape() || destFT->isNoEscape())
36143614
return false;
36153615

3616-
// Function types must be equivalent modulo @escaping
3617-
if (destFT != srcFT->withExtInfo(srcFT->getExtInfo().withNoEscape(false)))
3616+
// Function types must be equivalent modulo @escaping, @convention, etc.
3617+
if (destFT != srcFT->withExtInfo(destFT->getExtInfo()))
36183618
return false;
36193619

36203620
// Pick a specific diagnostic for the specific use

test/attr/attr_autoclosure.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func overloadedEach<P: P2>(_ source: P, _ closure: @escaping () -> ()) {
5555
struct S : P2 {
5656
typealias Element = Int
5757
func each(_ closure: @autoclosure () -> ()) {
58-
overloadedEach(self, closure) // expected-error {{invalid conversion from non-escaping function of type '@autoclosure () -> ()' to potentially escaping function type '() -> ()'}}
58+
// expected-note@-1{{parameter 'closure' is implicitly non-escaping because it was declared @autoclosure}}
59+
60+
overloadedEach(self, closure) // expected-error {{passing non-escaping parameter 'closure' to function expecting an @escaping closure}}
5961
}
6062
}
6163

@@ -87,7 +89,9 @@ class Sub : Super {
8789
func func12_sink(_ x: @escaping () -> Int) { }
8890

8991
func func12a(_ x: @autoclosure () -> Int) {
90-
func12_sink(x) // expected-error{{invalid conversion from non-escaping function of type '@autoclosure () -> Int' to potentially escaping function type '() -> Int'}}
92+
// expected-note@-1{{parameter 'x' is implicitly non-escaping because it was declared @autoclosure}}
93+
94+
func12_sink(x) // expected-error {{passing non-escaping parameter 'x' to function expecting an @escaping closure}}
9195
}
9296
func func12b(_ x: @autoclosure(escaping) () -> Int) {
9397
func12_sink(x)

test/attr/attr_escaping.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@ struct StoresClosure {
3838
}
3939
}
4040

41+
func takesEscapingBlock(_ fn: @escaping @convention(block) () -> Void) {
42+
fn()
43+
}
44+
func callEscapingWithNoEscapeBlock(_ fn: () -> Void) {
45+
// expected-note@-1{{parameter 'fn' is implicitly non-escaping}} {{42-42=@escaping }}
46+
47+
takesEscapingBlock(fn) // expected-error{{passing non-escaping parameter 'fn' to function expecting an @escaping closure}}
48+
}

0 commit comments

Comments
 (0)