Skip to content

Commit f727ec0

Browse files
committed
[Fixit] Suggested by Jordan, check immutability before adding ampersand.
1 parent 8d4bc74 commit f727ec0

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

Diff for: lib/Sema/CSDiag.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3495,7 +3495,7 @@ bool FailureDiagnosis::diagnoseContextualConversionError() {
34953495
contextDecl == CS->TC.Context.getUnsafeMutablePointerDecl()) {
34963496
SmallVector<Type, 4> scratch;
34973497
for (Type arg : contextualType->getAllGenericArgs(scratch)) {
3498-
if (arg->isEqual(exprType)) {
3498+
if (arg->isEqual(exprType) && expr->getType()->isLValueType()) {
34993499
diagnose(expr->getLoc(), diagID, exprType, contextualType).
35003500
fixItInsert(expr->getStartLoc(), "&");
35013501
return true;

Diff for: test/Sema/diag_type_conversion.swift

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ func foo1(_ a: [Int]) {}
44
func foo2(_ a : UnsafePointer<Int>) {}
55
func foo4(_ a : UnsafeMutablePointer<Int>) {}
66
func foo3 () {
7-
foo1(1) // expected-error {{cannot convert value of type 'Int' to expected argument type '[Int]'}} {{8-8=[}} {{9-9=]}}
8-
foo2(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafePointer<Int>'}} {{8-8=&}}
9-
var j = 3
10-
foo2(j) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafePointer<Int>'}} {{8-8=&}}
11-
foo4(j) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafeMutablePointer<Int>'}} {{8-8=&}}
7+
let j = 3
8+
foo2(j) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafePointer<Int>'}} {{none}}
9+
foo4(j) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafeMutablePointer<Int>'}} {{none}}
10+
11+
var i = 3
12+
foo2(i) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafePointer<Int>'}} {{8-8=&}}
13+
foo4(i) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafeMutablePointer<Int>'}} {{8-8=&}}
14+
15+
foo2(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafePointer<Int>'}} {{none}}
16+
foo4(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafeMutablePointer<Int>'}} {{none}}
1217
}

0 commit comments

Comments
 (0)