Skip to content

Commit cd1b6ed

Browse files
committed
[CS] Fix logic determining parens for non-optional weak diagnostic
We were previously checking the optional type, which would never say that parens were needed. Unwrap the type before checking, ensuring we correctly handle e.g protocol compositions.
1 parent 61156e1 commit cd1b6ed

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9124,7 +9124,7 @@ bool InvalidWeakAttributeUse::diagnoseAsError() {
91249124
ReferenceOwnership::Weak, varType);
91259125

91269126
auto typeRange = var->getTypeSourceRangeForDiagnostics();
9127-
if (varType->hasSimpleTypeRepr()) {
9127+
if (varType->lookThroughSingleOptionalType()->hasSimpleTypeRepr()) {
91289128
diagnostic.fixItInsertAfter(typeRange.End, "?");
91299129
} else {
91309130
diagnostic.fixItInsert(typeRange.Start, "(")

test/attr/attributes.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ weak var weak16 : Class!
191191

192192
@weak var weak17 : Class? // expected-error {{'weak' is a declaration modifier, not an attribute}} {{1-2=}}
193193

194+
class SomeClass {}
195+
protocol SomeProtocol {}
196+
_ = {
197+
// Make sure the fix-it here includes the parens
198+
weak var x: SomeClass & SomeProtocol // expected-error {{'weak' variable should have optional type '(any SomeClass & SomeProtocol)?'}} {{15-15=(}} {{39-39=)?}}
199+
}
194200

195201
@_exported var exportVar: Int // expected-error {{@_exported may only be used on 'import' declarations}}{{1-12=}}
196202
@_exported func exportFunc() {} // expected-error {{@_exported may only be used on 'import' declarations}}{{1-12=}}

0 commit comments

Comments
 (0)