Skip to content

Commit 0958f67

Browse files
committed
Fix <rdar://problem/22058555> QoI: unhelpful type error "cannot invoke 'withCString' with an argument list of type '((_) -> _)'"
The original issue has long since been fixed, but we were now producing: error: cannot subscript a value of type 'UnsafePointer<Int8>' which is pretty obviously wrong. The problem is that when ranking subscript decl candidates, CSDiags was using TC.isConvertibleTo to evaluate whether the actual base type is compatible with the base type of a subscript decl. This was failing when the base was generic, because the logic isn't opening archetypes. Instead of incorrectly deciding that they are incompatible, just decide we don't know if an archetype is present. This allows us to generate good errors in situation like this.
1 parent 1c592a7 commit 0958f67

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3334,6 +3334,8 @@ bool FailureDiagnosis::visitSubscriptExpr(SubscriptExpr *SE) {
33343334
auto instanceTy =
33353335
getter->getImplicitSelfDecl()->getType()->getInOutObjectType();
33363336
if (!isUnresolvedOrTypeVarType(baseType) &&
3337+
// TODO: We're not handling archetypes well here.
3338+
!instanceTy->hasArchetype() &&
33373339
!CS->TC.isConvertibleTo(baseType, instanceTy, CS->DC)) {
33383340
selfConstraint = CC_SelfMismatch;
33393341
}

test/Constraints/diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,14 +638,14 @@ _ = -UnaryOp() // expected-error {{unary operator '-' cannot be applied to an op
638638
// <rdar://problem/23433271> Swift compiler segfault in failure diagnosis
639639
func f23433271(x : UnsafePointer<Int>) {}
640640
func segfault23433271(a : UnsafeMutablePointer<Void>) {
641-
f23433271(a[0]) // expected-error {{cannot subscript a value of type 'UnsafeMutablePointer<Void>' (aka 'UnsafeMutablePointer<()>')}}
641+
f23433271(a[0]) // expected-error {{cannot convert value of type 'Void' (aka '()') to expected argument type 'UnsafePointer<Int>'}}
642642
}
643643

644644
// <rdar://problem/22058555> crash in cs diags in withCString
645645
func r22058555() {
646646
var firstChar: UInt8 = 0
647647
"abc".withCString { chars in
648-
firstChar = chars[0] // expected-error {{cannot subscript a value of type 'UnsafePointer<Int8>'}}
648+
firstChar = chars[0] // expected-error {{cannot assign value of type 'Int8' to type 'UInt8'}}
649649
}
650650
}
651651

0 commit comments

Comments
 (0)