Skip to content

Commit 87beea3

Browse files
committed
[ConstraintSystem] Use binding as a source of type variable assignment
Since `binding` has all of the required information now it's possible to use its `locator` as a source of type variable assignment (`Bind` constraint) in `TypeVariableBinding::attempt` which helps to improve diagnostics.
1 parent 382013f commit 87beea3

File tree

7 files changed

+18
-20
lines changed

7 files changed

+18
-20
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -982,9 +982,7 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
982982
type = type->reconstituteSugar(/*recursive=*/false);
983983
}
984984

985-
// FIXME: We want the locator that indicates where the binding came
986-
// from.
987-
cs.addConstraint(ConstraintKind::Bind, TypeVar, type, locator);
985+
cs.addConstraint(ConstraintKind::Bind, TypeVar, type, Binding.getLocator());
988986

989987
// If this was from a defaultable binding note that.
990988
if (Binding.isDefaultableBinding()) {

test/Constraints/function.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ sr590(())
8585
sr590((1, 2))
8686

8787
// SR-2657: Poor diagnostics when function arguments should be '@escaping'.
88-
private class SR2657BlockClass<T> { // expected-note 3 {{generic parameters are always considered '@escaping'}}
88+
private class SR2657BlockClass<T> { // expected-note 4 {{generic parameters are always considered '@escaping'}}
8989
let f: T
9090
init(f: T) { self.f = f }
9191
}
@@ -94,7 +94,7 @@ func takesAny(_: Any) {}
9494

9595
func foo(block: () -> (), other: () -> Int) {
9696
let _ = SR2657BlockClass(f: block)
97-
// expected-error@-1 {{converting non-escaping value to 'T' may allow it to escape}}
97+
// expected-error@-1 {{converting non-escaping parameter 'block' to generic parameter 'T' may allow it to escape}}
9898
let _ = SR2657BlockClass<()->()>(f: block)
9999
// expected-error@-1 {{converting non-escaping parameter 'block' to generic parameter 'T' may allow it to escape}}
100100
let _: SR2657BlockClass<()->()> = SR2657BlockClass(f: block)

test/Constraints/suspicious_bit_casts.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
func escapeByBitCast(f: () -> ()) -> () -> () {
44
return unsafeBitCast(f, to: (() -> ()).self)
5-
// expected-error@-1 {{converting non-escaping value to 'T' may allow it to escape}}
5+
// expected-error@-1 {{converting non-escaping parameter 'f' to generic parameter 'T' may allow it to escape}}
66
}
77

88
func changeFnRep(f: @escaping () -> ()) -> @convention(block) () -> () {

test/Sema/type_join.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protocol FakeExpressibleByFloatLiteral {}
3131
protocol FakeBinaryFloatingPoint : FakeFloatingPoint, FakeExpressibleByFloatLiteral {}
3232

3333
func expectEqualType<T>(_: T.Type, _: T.Type) {}
34-
func commonSupertype<T>(_: T, _: T) -> T {}
34+
func commonSupertype<T>(_: T, _: T) -> T {} // expected-note 2 {{generic parameters are always considered '@escaping'}}
3535

3636
expectEqualType(Builtin.type_join(Int.self, Int.self), Int.self)
3737
expectEqualType(Builtin.type_join_meta(D.self, C.self), C.self)
@@ -90,9 +90,9 @@ func joinFunctions(
9090
) {
9191
_ = commonSupertype(escaping, escaping)
9292
_ = commonSupertype(nonescaping, escaping)
93-
// expected-error@-1 {{converting non-escaping value to 'T' may allow it to escape}}
93+
// expected-error@-1 {{converting non-escaping parameter 'nonescaping' to generic parameter 'T' may allow it to escape}}
9494
_ = commonSupertype(escaping, nonescaping)
95-
// expected-error@-1 {{converting non-escaping value to 'T' may allow it to escape}}
95+
// expected-error@-1 {{converting non-escaping parameter 'nonescaping' to generic parameter 'T' may allow it to escape}}
9696
let x: Int = 1
9797
// FIXME: We emit these diagnostics here because we refuse to allow
9898
// Any to be inferred for the generic type. That's pretty

test/attr/attr_autoclosure.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ func passAutoClosureToEnumCase(_ fn: @autoclosure () -> Int) {
183183
func rdar_20591571() {
184184
func foo(_ g: @autoclosure () -> Int) {
185185
typealias G = ()->Int
186-
let _ = unsafeBitCast(g, to: G.self) // expected-error {{converting non-escaping value to 'T' may allow it to escape}}
186+
let _ = unsafeBitCast(g, to: G.self) // expected-error {{converting non-escaping parameter 'g' to generic parameter 'T' may allow it to escape}}
187187
}
188188

189-
func id<T>(_: T) -> T {}
189+
func id<T>(_: T) -> T {} // expected-note {{eneric parameters are always considered '@escaping'}}
190190
func same<T>(_: T, _: T) {}
191191
// expected-note@-1 2 {{generic parameters are always considered '@escaping'}}
192192

@@ -198,7 +198,7 @@ func rdar_20591571() {
198198
var _ = efn
199199
let _ = efn
200200

201-
_ = id(fn) // expected-error {{converting non-escaping value to 'T' may allow it to escape}}
201+
_ = id(fn) // expected-error {{converting non-escaping parameter 'fn' to generic parameter 'T' may allow it to escape}}
202202
_ = same(fn, { 3 }) // expected-error {{converting non-escaping parameter 'fn' to generic parameter 'T' may allow it to escape}}
203203
_ = same({ 3 }, fn) // expected-error {{converting non-escaping parameter 'fn' to generic parameter 'T' may allow it to escape}}
204204

@@ -251,15 +251,15 @@ func overloaded_autoclj(_: @autoclosure () -> Int) {}
251251

252252
func autoclosure_param_returning_func_type() {
253253
func foo(_ fn: @autoclosure () -> (() -> Int)) {}
254-
func generic_foo<T>(_ fn: @autoclosure () -> T) {}
254+
func generic_foo<T>(_ fn: @autoclosure () -> T) {} // expected-note {{generic parameters are always considered '@escaping'}}
255255

256256
func bar_1(_ fn: @autoclosure @escaping () -> Int) { foo(fn) } // Ok
257257
func bar_2(_ fn: @autoclosure () -> Int) { foo(fn) } // expected-note {{parameter 'fn' is implicitly non-escaping}}
258258
// expected-error@-1 {{passing non-escaping parameter 'fn' to function expecting an @escaping closure}}
259259
func baz_1(_ fn: @autoclosure @escaping () -> Int) { generic_foo(fn) } // Ok (T is inferred as () -> Int)
260260
func baz_2(_ fn: @autoclosure @escaping () -> Int) { generic_foo(fn()) } // Ok (T is inferred as Int)
261261
func baz_3(_ fn: @autoclosure () -> Int) { generic_foo(fn) } // Fails because fn is not marked as @escaping
262-
// expected-error@-1 {{converting non-escaping value to 'T' may allow it to escape}}
262+
// expected-error@-1 {{converting non-escaping parameter 'fn' to generic parameter 'T' may allow it to escape}}
263263

264264
// Let's make sure using `fn` as value works fine in presence of overloading
265265
func biz_1(_ fn: @autoclosure @escaping () -> Int) { overloaded_autoclj(fn) } // Ok

test/attr/attr_noescape.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func takesGenericClosure<T>(_ a : Int, _ fn : @noescape () -> T) {} // expected-
1111

1212
var globalAny: Any = 0
1313

14-
func assignToGlobal<T>(_ t: T) {
14+
func assignToGlobal<T>(_ t: T) { // expected-note {{generic parameters are always considered '@escaping'}}
1515
globalAny = t
1616
}
1717

@@ -23,7 +23,7 @@ func takesVariadic(_ fns: () -> Int...) {
2323
doesEscape(fns[0]) // Okay - variadic-of-function parameters are escaping
2424
}
2525

26-
func takesNoEscapeClosure(_ fn : () -> Int) {
26+
func takesNoEscapeClosure(_ fn : () -> Int) { // expected-note 2 {{parameter 'fn' is implicitly non-escaping}} {{34-34=@escaping }}
2727
// expected-note@-1 5{{parameter 'fn' is implicitly non-escaping}} {{34-34=@escaping }}
2828
takesNoEscapeClosure { 4 } // ok
2929

@@ -36,14 +36,14 @@ func takesNoEscapeClosure(_ fn : () -> Int) {
3636
takesGenericClosure(4, fn) // ok
3737
takesGenericClosure(4) { fn() } // ok.
3838

39-
_ = [fn] // expected-error {{converting non-escaping value to 'Element' may allow it to escape}}
39+
_ = [fn] // expected-error {{using non-escaping parameter 'fn' in a context expecting an @escaping closure}}
4040
_ = [doesEscape(fn)] // expected-error {{passing non-escaping parameter 'fn' to function expecting an @escaping closure}}
41-
_ = [1 : fn] // expected-error {{converting non-escaping value to 'Value' may allow it to escape}}
41+
_ = [1 : fn] // expected-error {{using non-escaping parameter 'fn' in a context expecting an @escaping closure}}
4242
_ = [1 : doesEscape(fn)] // expected-error {{passing non-escaping parameter 'fn' to function expecting an @escaping closure}}
4343
_ = "\(doesEscape(fn))" // expected-error {{passing non-escaping parameter 'fn' to function expecting an @escaping closure}}
4444
_ = "\(takesArray([fn]))" // expected-error {{using non-escaping parameter 'fn' in a context expecting an @escaping closure}}
4545

46-
assignToGlobal(fn) // expected-error {{converting non-escaping value to 'T' may allow it to escape}}
46+
assignToGlobal(fn) // expected-error {{converting non-escaping parameter 'fn' to generic parameter 'T' may allow it to escape}}
4747
assignToGlobal((fn, fn)) // expected-error {{converting non-escaping value to 'T' may allow it to escape}}
4848
}
4949

test/expr/primary/keypath/keypath-objc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class SR_10146_3 {
160160
}
161161

162162
func doNotCrash_1(_ obj: AnyObject, _ kp: KeyPath<AnyObject, Int>) {
163-
let _ = obj[keyPath: \.abc] // expected-error 2{{the root type of a Swift key path cannot be 'AnyObject'}}
163+
let _ = obj[keyPath: \.abc] // expected-error {{the root type of a Swift key path cannot be 'AnyObject'}}
164164
let _ = obj[keyPath: kp] // expected-error {{the root type of a Swift key path cannot be 'AnyObject'}}
165165
}
166166
}

0 commit comments

Comments
 (0)