Skip to content

Commit f8c737b

Browse files
author
Abdul Ajetunmobi
committed
SR-13976: Improve compiler error message: "partial application of ‘mutating’ method is not allowed”
1 parent 20f9aff commit f8c737b

File tree

12 files changed

+52
-48
lines changed

12 files changed

+52
-48
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3700,20 +3700,20 @@ NOTE(ambiguous_because_of_trailing_closure,none,
37003700
// Cannot capture inout-ness of a parameter
37013701
// Partial application of foreign functions not supported
37023702
ERROR(partial_application_of_function_invalid,none,
3703-
"partial application of %select{"
3703+
"cannot reference %select{"
37043704
"'mutating' method|"
37053705
"'super.init' initializer chain|"
37063706
"'self.init' initializer delegation|"
37073707
"'super' instance method with metatype base"
3708-
"}0 is not allowed",
3708+
"}0 as function value",
37093709
(unsigned))
37103710
WARNING(partial_application_of_function_invalid_swift4,none,
3711-
"partial application of %select{"
3711+
"cannot reference %select{"
37123712
"'mutating' method|"
37133713
"'super.init' initializer chain|"
37143714
"'self.init' initializer delegation|"
37153715
"'super' instance method with metatype base"
3716-
"}0 is not allowed; calling the function has undefined behavior and will "
3716+
"}0 as function value; calling the function has undefined behavior and will "
37173717
"be an error in future Swift versions",
37183718
(unsigned))
37193719

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4189,6 +4189,9 @@ bool PartialApplicationFailure::diagnoseAsError() {
41894189
kind = RefKind::SuperMethod;
41904190
}
41914191

4192+
/* TODO(diagnostics): SR-15250,
4193+
Add a "did you mean to call it?" note with a fix-it for inserting '()'
4194+
if function type has no params or all have a default value. */
41924195
auto diagnostic = CompatibilityWarning
41934196
? diag::partial_application_of_function_invalid_swift4
41944197
: diag::partial_application_of_function_invalid;

test/Constraints/construction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,15 @@ func sr_10837() {
241241
convenience init(foo: Int = 42) {
242242
self.init(value:)(foo) // Ok
243243
self.init(value:)
244-
// expected-error@-1 {{partial application of 'self.init' initializer delegation is not allowed}}
244+
// expected-error@-1 {{cannot reference 'self.init' initializer delegation as function value}}
245245
}
246246
}
247247

248248
class C : A {
249249
override init(bar: Int) {
250250
super.init(bar:)(bar) // Ok
251251
super.init(bar:)
252-
// expected-error@-1 {{partial application of 'super.init' initializer chain is not allowed}}
252+
// expected-error@-1 {{cannot reference 'super.init' initializer chain as function value}}
253253
}
254254
}
255255
}

test/Constraints/members.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func g0(_: (inout X) -> (Float) -> ()) {}
2828
_ = x.f0(i)
2929
x.f0(i).f1(i)
3030

31-
g0(X.f1) // expected-error{{partial application of 'mutating' method}}
31+
g0(X.f1) // expected-error{{cannot reference 'mutating' method as function value}}
3232

3333
_ = x.f0(x.f2(1))
3434
_ = x.f0(1).f2(i)
@@ -74,7 +74,7 @@ struct GZ<T> {
7474

7575
var z = Z(i: 0)
7676
var getI = z.getI
77-
var incI = z.incI // expected-error{{partial application of 'mutating'}}
77+
var incI = z.incI // expected-error{{cannot reference 'mutating' method as function value}}
7878
var zi = z.getI()
7979
var zcurried1 = z.curried
8080
var zcurried2 = z.curried(0)

test/Constraints/mutating_members.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@ struct Foo {
66
mutating func boom() {}
77
}
88

9-
let x = Foo.boom // expected-error{{partial application of 'mutating' method}}
9+
let x = Foo.boom // expected-error{{cannot reference 'mutating' method as function value}}
1010
var y = Foo()
11-
let z0 = y.boom // expected-error{{partial application of 'mutating' method}}
12-
let z1 = Foo.boom(&y) // expected-error{{partial application of 'mutating' method}}
11+
let z0 = y.boom // expected-error{{cannot reference 'mutating' method as function value}}
12+
let z1 = Foo.boom(&y) // expected-error{{cannot reference 'mutating' method as function value}}
1313

1414
func fromLocalContext() -> (inout Foo) -> () -> () {
15-
return Foo.boom // expected-error{{partial application of 'mutating' method}}
15+
return Foo.boom // expected-error{{cannot reference 'mutating' method as function value}}
1616
}
1717
func fromLocalContext2(x: inout Foo, y: Bool) -> () -> () {
1818
if y {
19-
return x.boom // expected-error{{partial application of 'mutating' method}}
19+
return x.boom // expected-error{{cannot reference 'mutating' method as function value}}
2020
} else {
21-
return Foo.boom(&x) // expected-error{{partial application of 'mutating' method}}
21+
return Foo.boom(&x) // expected-error{{cannot reference 'mutating' method as function value}}
2222
}
2323
}
2424

2525
func bar() -> P.Type { fatalError() }
2626
func bar() -> Foo.Type { fatalError() }
2727

28-
_ = bar().boom // expected-error{{partial application of 'mutating' method}}
29-
_ = bar().boom(&y) // expected-error{{partial application of 'mutating' method}}
30-
_ = bar().boom(&y)() // expected-error{{partial application of 'mutating' method}}
28+
_ = bar().boom // expected-error{{cannot reference 'mutating' method as function value}}
29+
_ = bar().boom(&y) // expected-error{{cannot reference 'mutating' method as function value}}
30+
_ = bar().boom(&y)() // expected-error{{cannot reference 'mutating' method as function value}}
3131

3232
func foo(_ foo: Foo.Type) {
33-
_ = foo.boom // expected-error{{partial application of 'mutating' method}}
34-
_ = foo.boom(&y) // expected-error{{partial application of 'mutating' method}}
35-
_ = foo.boom(&y)() // expected-error{{partial application of 'mutating' method}}
33+
_ = foo.boom // expected-error{{cannot reference 'mutating' method as function value}}
34+
_ = foo.boom(&y) // expected-error{{cannot reference 'mutating' method as function value}}
35+
_ = foo.boom(&y)() // expected-error{{cannot reference 'mutating' method as function value}}
3636
}

test/Constraints/mutating_members_compat.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@ struct Foo {
66
mutating func boom() {}
77
}
88

9-
let x = Foo.boom // expected-warning{{partial application of 'mutating' method}}
9+
let x = Foo.boom // expected-warning{{cannot reference 'mutating' method as function value; calling the function has undefined behavior and will be an error in future Swift versions}}
1010
var y = Foo()
11-
let z0 = y.boom // expected-error{{partial application of 'mutating' method}}
12-
let z1 = Foo.boom(&y) // expected-error{{partial application of 'mutating' method}}
11+
let z0 = y.boom // expected-error{{cannot reference 'mutating' method as function value}}
12+
let z1 = Foo.boom(&y) // expected-error{{cannot reference 'mutating' method as function value}}
1313

1414
func fromLocalContext() -> (inout Foo) -> () -> () {
15-
return Foo.boom // expected-warning{{partial application of 'mutating' method}}
15+
return Foo.boom // expected-warning{{cannot reference 'mutating' method as function value; calling the function has undefined behavior and will be an error in future Swift versions}}
1616
}
1717
func fromLocalContext2(x: inout Foo, y: Bool) -> () -> () {
1818
if y {
19-
return x.boom // expected-error{{partial application of 'mutating' method}}
19+
return x.boom // expected-error{{cannot reference 'mutating' method as function value}}
2020
} else {
21-
return Foo.boom(&x) // expected-error{{partial application of 'mutating' method}}
21+
return Foo.boom(&x) // expected-error{{cannot reference 'mutating' method as function value}}
2222
}
2323
}
2424

2525
func bar() -> P.Type { fatalError() }
2626
func bar() -> Foo.Type { fatalError() }
2727

28-
_ = bar().boom // expected-warning{{partial application of 'mutating' method}}
29-
_ = bar().boom(&y) // expected-error{{partial application of 'mutating' method}}
30-
_ = bar().boom(&y)() // expected-error{{partial application of 'mutating' method}}
28+
_ = bar().boom // expected-warning{{cannot reference 'mutating' method as function value; calling the function has undefined behavior and will be an error in future Swift versions}}
29+
_ = bar().boom(&y) // expected-error{{cannot reference 'mutating' method as function value}}
30+
_ = bar().boom(&y)() // expected-error{{cannot reference 'mutating' method as function value}}
3131

3232
func foo(_ foo: Foo.Type) {
33-
_ = foo.boom // expected-warning{{partial application of 'mutating' method}}
34-
_ = foo.boom(&y) // expected-error{{partial application of 'mutating' method}}
35-
_ = foo.boom(&y)() // expected-error{{partial application of 'mutating' method}}
33+
_ = foo.boom // expected-warning{{cannot reference 'mutating' method as function value; calling the function has undefined behavior and will be an error in future Swift versions}}
34+
_ = foo.boom(&y) // expected-error{{cannot reference 'mutating' method as function value}}
35+
_ = foo.boom(&y)() // expected-error{{cannot reference 'mutating' method as function value}}
3636
}

test/Constraints/protocols.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func generic<T: P>(_ t: T) {
136136
let _: (T) -> (Int) -> () = id(T.bar)
137137
let _: (Int) -> () = id(T.bar(t))
138138

139-
_ = t.mut // expected-error{{partial application of 'mutating' method is not allowed}}
139+
_ = t.mut // expected-error{{cannot reference 'mutating' method as function value}}
140140
_ = t.tum // expected-error{{static member 'tum' cannot be used on instance of type 'T'}}
141141
}
142142

@@ -167,7 +167,7 @@ func existential(_ p: P) {
167167
var p = p
168168
// Fully applied mutating method
169169
p.mut(1)
170-
_ = p.mut // expected-error{{partial application of 'mutating' method is not allowed}}
170+
_ = p.mut // expected-error{{cannot reference 'mutating' method as function value}}
171171

172172
// Instance member of existential)
173173
let _: (Int) -> () = id(p.bar)
@@ -212,7 +212,7 @@ func staticExistential(_ p: P.Type, pp: P.Protocol) {
212212
// Instance member of existential metatype -- not allowed
213213
_ = p.bar // expected-error{{instance member 'bar' cannot be used on type 'P'}}
214214
_ = p.mut // expected-error{{instance member 'mut' cannot be used on type 'P'}}
215-
// expected-error@-1 {{partial application of 'mutating' method is not allowed}}
215+
// expected-error@-1 {{cannot reference 'mutating' method as function value}}
216216

217217
// Static member of metatype -- not allowed
218218
_ = pp.tum // expected-error{{static member 'tum' cannot be used on protocol metatype 'P.Protocol'}}

test/NameLookup/name_lookup.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,23 +288,24 @@ class ThisDerived1 : ThisBase1 {
288288
self.Type // expected-error {{type 'ThisDerived1' has no member 'Type'}}
289289
}
290290

291+
// FIXME(SR-15250): Partial application diagnostic is applied incorrectly for some test cases.
291292
class func staticTestSuper1() {
292293
super.baseInstanceVar = 42 // expected-error {{member 'baseInstanceVar' cannot be used on type 'ThisBase1'}}
293294
super.baseProp = 42 // expected-error {{member 'baseProp' cannot be used on type 'ThisBase1'}}
294295
super.baseFunc0() // expected-error {{instance member 'baseFunc0' cannot be used on type 'ThisBase1'}}
295-
// expected-error@-1 {{partial application of 'super' instance method with metatype base is not allowed}}
296-
super.baseFunc0(ThisBase1())() // expected-error {{partial application of 'super' instance method with metatype base is not allowed}}
296+
// expected-error@-1 {{cannot reference 'super' instance method with metatype base as function value}}
297+
super.baseFunc0(ThisBase1())() // expected-error {{cannot reference 'super' instance method with metatype base as function value}}
297298
super.baseFunc1(42) // expected-error {{instance member 'baseFunc1' cannot be used on type 'ThisBase1'}}
298-
// expected-error@-1 {{partial application of 'super' instance method with metatype base is not allowed}}
299-
super.baseFunc1(ThisBase1())(42) // expected-error {{partial application of 'super' instance method with metatype base is not allowed}}
299+
// expected-error@-1 {{cannot reference 'super' instance method with metatype base as function value}}
300+
super.baseFunc1(ThisBase1())(42) // expected-error {{cannot reference 'super' instance method with metatype base as function value}}
300301
super[0] = 42.0 // expected-error {{instance member 'subscript' cannot be used on type 'ThisBase1'}}
301302
super.baseStaticVar = 42
302303
super.baseStaticProp = 42
303304
super.baseStaticFunc0()
304305

305306
super.baseExtProp = 42 // expected-error {{member 'baseExtProp' cannot be used on type 'ThisBase1'}}
306307
super.baseExtFunc0() // expected-error {{instance member 'baseExtFunc0' cannot be used on type 'ThisBase1'}}
307-
// expected-error@-1 {{partial application of 'super' instance method with metatype base is not allowed}}
308+
// expected-error@-1 {{cannot reference 'super' instance method with metatype base as function value}}
308309
super.baseExtStaticVar = 42 // expected-error {{instance member 'baseExtStaticVar' cannot be used on type 'ThisBase1'}}
309310
super.baseExtStaticProp = 42 // expected-error {{member 'baseExtStaticProp' cannot be used on type 'ThisBase1'}}
310311
super.baseExtStaticFunc0()

test/Parse/super.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class D : B {
2121
}
2222

2323
override init(x:Int) {
24-
let _: () -> B = super.init // expected-error {{partial application of 'super.init' initializer chain is not allowed}}
24+
let _: () -> B = super.init // expected-error {{cannot reference 'super.init' initializer chain as function value}}
2525
}
2626

2727
convenience init(y:Int) {
28-
let _: () -> D = self.init // expected-error {{partial application of 'self.init' initializer delegation is not allowed}}
28+
let _: () -> D = self.init // expected-error {{cannot reference 'self.init' initializer delegation as function value}}
2929
}
3030

3131
init(z: Int) {

test/expr/capture/inout.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ struct C {
1313
}
1414

1515
var c = C()
16-
let x = c.f // expected-error{{partial application of 'mutating' method is not allowed}}
16+
let x = c.f // expected-error{{cannot reference 'mutating' method as function value}}

test/expr/postfix/dot/init_ref_delegation.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class Z5 : Z4 {
125125
// Ill-formed initialization: failure to call initializer.
126126
class Z6 {
127127
convenience init() {
128-
var _ : () -> Z6 = self.init // expected-error{{partial application of 'self.init' initializer delegation is not allowed}}
128+
var _ : () -> Z6 = self.init // expected-error{{cannot reference 'self.init' initializer delegation as function value}}
129129
}
130130

131131
init(other: Z6) { }
@@ -171,15 +171,15 @@ struct S {
171171
init() {
172172
let _ = S.init()
173173
self.init()
174-
let _ = self.init // expected-error{{partial application of 'self.init' initializer delegation is not allowed}}
174+
let _ = self.init // expected-error{{cannot reference 'self.init' initializer delegation as function value}}
175175
}
176176
}
177177

178178
class C {
179179
convenience init() { // expected-note 11 {{selected non-required initializer 'init()'}}
180180
self.init()
181181
let _: C = self.init() // expected-error{{cannot convert value of type '()' to specified type 'C'}}
182-
let _: () -> C = self.init // expected-error{{partial application of 'self.init' initializer delegation is not allowed}}
182+
let _: () -> C = self.init // expected-error{{cannot reference 'self.init' initializer delegation as function value}}
183183
}
184184

185185
init(x: Int) {} // expected-note 11 {{selected non-required initializer 'init(x:)'}}
@@ -191,7 +191,7 @@ class D: C {
191191
override init(x: Int) {
192192
super.init(x: x)
193193
let _: C = super.init() // expected-error{{cannot convert value of type '()' to specified type 'C'}}
194-
let _: () -> C = super.init // expected-error{{partial application of 'super.init' initializer chain is not allowed}}
194+
let _: () -> C = super.init // expected-error{{cannot reference 'super.init' initializer chain as function value}}
195195
}
196196

197197
func foo() {

test/type/self.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ do {
355355
let _: Self = super.property // expected-error {{cannot convert value of type 'D' to specified type 'Self'}}
356356
let _: Self = super[] // expected-error {{cannot convert value of type 'D' to specified type 'Self'}}
357357
let _: () -> Self = super.method
358-
// expected-error@-1 {{partial application of 'super' instance method with metatype base is not allowed}}
358+
// expected-error@-1 {{cannot reference 'super' instance method with metatype base as function value}}
359359
// expected-error@-2 {{cannot convert value of type '(C) -> () -> D' to specified type '() -> Self'}}
360360
}
361361
}

0 commit comments

Comments
 (0)