Skip to content

Commit 9e54917

Browse files
authored
Merge pull request #58402 from xedin/issue-58389
[Diagnostics] Improve diagnostic message for extraneous `&`
2 parents e0b24e9 + a0d1325 commit 9e54917

File tree

11 files changed

+40
-40
lines changed

11 files changed

+40
-40
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ ERROR(missing_address_of_yield,none,
12061206
"yielding mutable value of type %0 requires explicit '&'",
12071207
(Type))
12081208
ERROR(extraneous_address_of,none,
1209-
"use of extraneous '&'",
1209+
"'&' may only be used to pass an argument to inout parameter",
12101210
())
12111211
ERROR(extra_address_of,none,
12121212
"'&' used with non-inout argument of type %0",

test/Constraints/diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ func testTypeSugar(_ a : Int) {
453453

454454
// <rdar://problem/21974772> SegFault in FailureDiagnosis::visitInOutExpr
455455
func r21974772(_ y : Int) {
456-
let x = &(1.0 + y) // expected-error {{use of extraneous '&'}}
456+
let x = &(1.0 + y) // expected-error {{'&' may only be used to pass an argument to inout parameter}}
457457
}
458458

459459
// <rdar://problem/22020088> QoI: missing member diagnostic on optional gives worse error message than existential/bound generic/etc

test/Constraints/lvalues.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ func testInOut(_ arg: inout Int) {
159159
}
160160

161161
// Don't infer inout types.
162-
var ir = &i // expected-error {{use of extraneous '&'}}
163-
var ir2 = ((&i)) // expected-error {{use of extraneous '&'}}
162+
var ir = &i // expected-error {{'&' may only be used to pass an argument to inout parameter}}
163+
var ir2 = ((&i)) // expected-error {{'&' may only be used to pass an argument to inout parameter}}
164164

165165
// <rdar://problem/17133089>
166166
func takeArrayRef(_ x: inout Array<String>) { }
@@ -279,10 +279,10 @@ func look_through_parens_when_checking_inout() {
279279
func modifyPoint(source: inout Point) {}
280280

281281
var point = Point(x: 0, y: 0)
282-
modifyPoint((&point)) // expected-error {{use of extraneous '&}} {{16-17=(}} {{15-16=&}}
283-
modifyPoint(((&point))) // expected-error {{use of extraneous '&}} {{17-18=(}} {{15-16=&}}
284-
modifyPoint(source: (&point)) // expected-error {{use of extraneous '&}} {{24-25=(}} {{23-24=&}}
285-
modifyPoint(source: ((&point))) // expected-error {{use of extraneous '&}} {{25-26=(}} {{23-24=&}}
286-
modifyPoint((&point), 0) // expected-error {{use of extraneous '&}} {{16-17=(}} {{15-16=&}}
287-
modifyPoint((&point), msg: "") // expected-error {{use of extraneous '&}} {{16-17=(}} {{15-16=&}}
282+
modifyPoint((&point)) // expected-error {{'&' may only be used to pass an argument to inout parameter}} {{16-17=(}} {{15-16=&}}
283+
modifyPoint(((&point))) // expected-error {{'&' may only be used to pass an argument to inout parameter}} {{17-18=(}} {{15-16=&}}
284+
modifyPoint(source: (&point)) // expected-error {{'&' may only be used to pass an argument to inout parameter}} {{24-25=(}} {{23-24=&}}
285+
modifyPoint(source: ((&point))) // expected-error {{'&' may only be used to pass an argument to inout parameter}} {{25-26=(}} {{23-24=&}}
286+
modifyPoint((&point), 0) // expected-error {{'&' may only be used to pass an argument to inout parameter}} {{16-17=(}} {{15-16=&}}
287+
modifyPoint((&point), msg: "") // expected-error {{'&' may only be used to pass an argument to inout parameter}} {{16-17=(}} {{15-16=&}}
288288
}

test/Constraints/optional.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,17 +470,17 @@ func rdar75146811() {
470470
var arr: [Double]! = []
471471

472472
test(&arr) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
473-
test((&arr)) // expected-error {{use of extraneous '&'}}
473+
test((&arr)) // expected-error {{'&' may only be used to pass an argument to inout parameter}}
474474
// expected-error@-1 {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
475475
test(&(arr)) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
476476

477477
test_tuple(&arr, x: 0) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
478-
test_tuple((&arr), x: 0) // expected-error {{use of extraneous '&'}}
478+
test_tuple((&arr), x: 0) // expected-error {{'&' may only be used to pass an argument to inout parameter}}
479479
// expected-error@-1 {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
480480
test_tuple(&(arr), x: 0) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
481481

482482
test_named(x: &arr) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
483-
test_named(x: (&arr)) // expected-error {{use of extraneous '&'}}
483+
test_named(x: (&arr)) // expected-error {{'&' may only be used to pass an argument to inout parameter}}
484484
// expected-error@-1 {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
485485
test_named(x: &(arr)) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
486486
}

test/Parse/pointer_conversion.swift.gyb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ func mutablePointerArguments(_ p: UnsafeMutablePointer<Int>,
6565
takesMutableArrayPointer(&ii)
6666

6767
// We don't allow these conversions outside of function arguments.
68-
var x: UnsafeMutablePointer<Int> = &i // expected-error {{use of extraneous '&'}}
69-
x = &ii // expected-error {{use of extraneous '&'}}
68+
var x: UnsafeMutablePointer<Int> = &i // expected-error {{'&' may only be used to pass an argument to inout parameter}}
69+
x = &ii // expected-error {{'&' may only be used to pass an argument to inout parameter}}
7070
_ = x
7171
}
7272

@@ -97,9 +97,9 @@ func mutableVoidPointerArguments(_ p: UnsafeMutablePointer<Int>,
9797
takesMutableVoidPointer(ff) // expected-error{{cannot convert value of type '[Int]' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
9898

9999
// We don't allow these conversions outside of function arguments.
100-
var x: UnsafeMutableRawPointer = &i // expected-error {{use of extraneous '&'}}
100+
var x: UnsafeMutableRawPointer = &i // expected-error {{'&' may only be used to pass an argument to inout parameter}}
101101
x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Int>' to type 'UnsafeMutableRawPointer'}}
102-
x = &ii // expected-error {{use of extraneous '&'}}
102+
x = &ii // expected-error {{'&' may only be used to pass an argument to inout parameter}}
103103
_ = x
104104
}
105105

@@ -132,9 +132,9 @@ func mutableRawPointerArguments(_ p: UnsafeMutablePointer<Int>,
132132
takesMutableRawPointer(ff) // expected-error{{cannot convert value of type '[Int]' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
133133

134134
// We don't allow these conversions outside of function arguments.
135-
var x: UnsafeMutableRawPointer = &i // expected-error {{use of extraneous '&'}}
135+
var x: UnsafeMutableRawPointer = &i // expected-error {{'&' may only be used to pass an argument to inout parameter}}
136136
x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Int>' to type 'UnsafeMutableRawPointer'}}
137-
x = &ii //expected-error {{use of extraneous '&'}}
137+
x = &ii //expected-error {{'&' may only be used to pass an argument to inout parameter}}
138138
_ = x
139139
}
140140

@@ -167,7 +167,7 @@ func constPointerArguments(_ p: UnsafeMutablePointer<Int>,
167167
// expected-error@-1 3 {{cannot convert value of type 'Double' to expected element type 'Int'}}
168168

169169
// We don't allow these conversions outside of function arguments.
170-
var x: UnsafePointer<Int> = &i // expected-error {{use of extraneous '&'}}
170+
var x: UnsafePointer<Int> = &i // expected-error {{'&' may only be used to pass an argument to inout parameter}}
171171
x = ii // expected-error{{cannot assign value of type '[Int]' to type 'UnsafePointer<Int>'}}
172172
x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Int>' to type 'UnsafePointer<Int>'}}
173173
}
@@ -201,7 +201,7 @@ func constVoidPointerArguments(_ p: UnsafeMutablePointer<Int>,
201201
takesConstVoidPointer([0.0, 1.0, 2.0])
202202

203203
// We don't allow these conversions outside of function arguments.
204-
var x: UnsafeRawPointer = &i // expected-error {{use of extraneous '&'}}
204+
var x: UnsafeRawPointer = &i // expected-error {{'&' may only be used to pass an argument to inout parameter}}
205205
x = ii // expected-error{{cannot assign value of type '[Int]' to type 'UnsafeRawPointer'}}
206206
x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Int>' to type 'UnsafeRawPointer'}}
207207
x = fp // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Float>' to type 'UnsafeRawPointer'}}
@@ -241,7 +241,7 @@ func constRawPointerArguments(_ p: UnsafeMutablePointer<Int>,
241241
takesConstRawPointer([0.0, 1.0, 2.0])
242242

243243
// We don't allow these conversions outside of function arguments.
244-
var x: UnsafeRawPointer = &i // expected-error {{use of extraneous '&'}}
244+
var x: UnsafeRawPointer = &i // expected-error {{'&' may only be used to pass an argument to inout parameter}}
245245
x = ii // expected-error{{cannot assign value of type '[Int]' to type 'UnsafeRawPointer'}}
246246
x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Int>' to type 'UnsafeRawPointer'}}
247247
x = fp // expected-error{{cannot assign value of type 'UnsafeMutablePointer<Float>' to type 'UnsafeRawPointer'}}

test/Parse/pointer_conversion_objc.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@ func autoreleasingPointerArguments(p: UnsafeMutablePointer<Int>,
7272
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('[C]' and 'C') are expected to be equal}}
7373
takesAutoreleasingPointer(&dd) // expected-error{{cannot convert value of type 'AutoreleasingUnsafeMutablePointer<[D]>' to expected argument type 'AutoreleasingUnsafeMutablePointer<C>'}}
7474
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('[D]' and 'C') are expected to be equal}}
75-
let _: AutoreleasingUnsafeMutablePointer<C> = &c // expected-error {{use of extraneous '&'}}
75+
let _: AutoreleasingUnsafeMutablePointer<C> = &c // expected-error {{'&' may only be used to pass an argument to inout parameter}}
7676
}

test/Parse/recovery.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ func test23550816(ss: [String], s: String) {
816816
// <rdar://problem/23719432> [practicalswift] Compiler crashes on &(Int:_)
817817
func test23719432() {
818818
var x = 42
819-
&(Int:x) // expected-error {{use of extraneous '&'}}
819+
&(Int:x) // expected-error {{'&' may only be used to pass an argument to inout parameter}}
820820
}
821821

822822
// <rdar://problem/19911096> QoI: terrible recovery when using '·' for an operator

test/Sema/diag_invalid_interpolation_4_2.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ _ = "\(y: &x)"
1010
// expected-note@-3 {{remove 'y' label to keep current behavior}}
1111

1212
_ = "\(x, y: &x)"
13-
// expected-error@-1 {{use of extraneous '&'}}
13+
// expected-error@-1 {{'&' may only be used to pass an argument to inout parameter}}
1414
// expected-warning@-2 {{interpolating multiple values will not form a tuple in Swift 5}}
1515
// expected-note@-3 {{insert parentheses to keep current behavior}}

test/TypeCoercion/overload_noncall.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func test_inout() {
5252
x = accept_XY(&xy);
5353

5454
x = xy
55-
x = &xy; // expected-error {{use of extraneous '&'}}
55+
x = &xy; // expected-error {{'&' may only be used to pass an argument to inout parameter}}
5656
accept_Z(&xy); // expected-error{{cannot convert value of type 'X' to expected argument type 'Z'}}
5757
}
5858

test/decl/func/default-values.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ func inoutFuncWithDefaultArg1(x: inout Int = 1) {} // expected-error {{cannot pr
132132
func inoutFuncWithDefaultArg2(x: inout Int = bLiteral) {} // expected-error {{cannot provide default value to inout parameter 'x'}}
133133
func inoutFuncWithDefaultArg3(x: inout Int = aLiteral) {} // expected-error {{cannot provide default value to inout parameter 'x'}}
134134
func inoutFuncWithDefaultArg4(x: inout Int = &aLiteral) {} // expected-error {{cannot provide default value to inout parameter 'x'}}
135-
// expected-error@-1 {{use of extraneous '&'}}
135+
// expected-error@-1 {{'&' may only be used to pass an argument to inout parameter}}
136136

137137
func inoutFuncWithDefaultArg5(x: inout Int = &bLiteral) {} // expected-error {{cannot provide default value to inout parameter 'x'}}
138-
// expected-error@-1 {{use of extraneous '&'}}
138+
// expected-error@-1 {{'&' may only be used to pass an argument to inout parameter}}
139139

140140
func inoutFuncWithDefaultArg6(x: inout Int = #file) {} // expected-error {{cannot provide default value to inout parameter 'x'}}
141141
// expected-error@-1 {{default argument value of type 'String' cannot be converted to type 'Int'}}

0 commit comments

Comments
 (0)