Skip to content

Commit 79f9840

Browse files
authored
Merge pull request #3112 from nkcsgexi/master
2 parents c955037 + b378e38 commit 79f9840

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,6 +3477,33 @@ bool FailureDiagnosis::diagnoseContextualConversionError() {
34773477
return true;
34783478
}
34793479

3480+
// When converting from T to [T] or UnsafePointer<T>, we can offer fixit to wrap
3481+
// the expr with brackets.
3482+
if (auto *contextDecl = contextualType->getAnyNominal()) {
3483+
if (contextDecl == CS->TC.Context.getArrayDecl()) {
3484+
SmallVector<Type, 4> scratch;
3485+
for (Type arg : contextualType->getAllGenericArgs(scratch)) {
3486+
if (arg->isEqual(exprType)) {
3487+
diagnose(expr->getLoc(), diagID, exprType, contextualType).
3488+
fixItInsert(expr->getStartLoc(), "[").fixItInsert(
3489+
Lexer::getLocForEndOfToken(CS->TC.Context.SourceMgr,
3490+
expr->getEndLoc()), "]");
3491+
return true;
3492+
}
3493+
}
3494+
} else if (contextDecl == CS->TC.Context.getUnsafePointerDecl() ||
3495+
contextDecl == CS->TC.Context.getUnsafeMutablePointerDecl()) {
3496+
SmallVector<Type, 4> scratch;
3497+
for (Type arg : contextualType->getAllGenericArgs(scratch)) {
3498+
if (arg->isEqual(exprType)) {
3499+
diagnose(expr->getLoc(), diagID, exprType, contextualType).
3500+
fixItInsert(expr->getStartLoc(), "&");
3501+
return true;
3502+
}
3503+
}
3504+
}
3505+
}
3506+
34803507
// When complaining about conversion to a protocol type, complain about
34813508
// conformance instead of "conversion".
34823509
if (contextualType->is<ProtocolType>() ||

test/Sema/diag_type_conversion.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-parse-verify-swift
2+
3+
func foo1(_ a: [Int]) {}
4+
func foo2(_ a : UnsafePointer<Int>) {}
5+
func foo4(_ a : UnsafeMutablePointer<Int>) {}
6+
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=&}}
12+
}

0 commit comments

Comments
 (0)