Skip to content

Commit f19e6cb

Browse files
committed
[Fixit] Suggested by Dmitri, adding & instead of [] to bridge T and UnsafePointer<T>.
1 parent 3a6baaf commit f19e6cb

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,8 +3480,7 @@ bool FailureDiagnosis::diagnoseContextualConversionError() {
34803480
// When converting from T to [T] or UnsafePointer<T>, we can offer fixit to wrap
34813481
// the expr with brackets.
34823482
if (auto *contextDecl = contextualType->getAnyNominal()) {
3483-
if (contextDecl == CS->TC.Context.getArrayDecl() ||
3484-
contextDecl == CS->TC.Context.getUnsafePointerDecl()) {
3483+
if (contextDecl == CS->TC.Context.getArrayDecl()) {
34853484
SmallVector<Type, 4> scratch;
34863485
for (Type arg : contextualType->getAllGenericArgs(scratch)) {
34873486
if (arg->isEqual(exprType)) {
@@ -3492,6 +3491,15 @@ bool FailureDiagnosis::diagnoseContextualConversionError() {
34923491
return true;
34933492
}
34943493
}
3494+
} else if (contextDecl == CS->TC.Context.getUnsafePointerDecl()) {
3495+
SmallVector<Type, 4> scratch;
3496+
for (Type arg : contextualType->getAllGenericArgs(scratch)) {
3497+
if (arg->isEqual(exprType)) {
3498+
diagnose(expr->getLoc(), diagID, exprType, contextualType).
3499+
fixItInsert(expr->getStartLoc(), "&");
3500+
return true;
3501+
}
3502+
}
34953503
}
34963504
}
34973505

test/Sema/diag_type_conversion.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ func foo1(_ a: [Int]) {}
44
func foo2(_ a : UnsafePointer<Int>) {}
55
func foo3 () {
66
foo1(1) // expected-error {{cannot convert value of type 'Int' to expected argument type '[Int]'}} {{8-8=[}} {{9-9=]}}
7-
foo2(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafePointer<Int>'}} {{8-8=[}} {{9-9=]}}
7+
foo2(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafePointer<Int>'}} {{8-8=&}}
8+
var j = 3
9+
foo2(j) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafePointer<Int>'}} {{8-8=&}}
810
}

0 commit comments

Comments
 (0)