Skip to content

Commit 202779d

Browse files
committed
[QoI] Don't enclose single type identifier with parens
in fixit for deprecated protocol composition with trailing characters. protocol<>? => Any? protocol<P1>? => P1? protocol<P1, P2>? => (P1 & P2)?
1 parent 5693003 commit 202779d

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

lib/Parse/ParseType.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,10 @@ ParserResult<TypeRepr> Parser::parseTypeIdentifierOrTypeComposition() {
468468
StringRef TrailingContent = L->getTokenAt(RAngleLoc).getRange().str().
469469
substr(1);
470470
if (!TrailingContent.empty()) {
471-
replacement.insert(replacement.begin(), '(');
472-
replacement += ")";
471+
if (Protocols.size() > 1) {
472+
replacement.insert(replacement.begin(), '(');
473+
replacement += ")";
474+
}
473475
replacement += TrailingContent;
474476
}
475477

test/FixCode/fixits-apply.swift.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,4 @@ func testBoolValue(a : BoolFoo) {
308308
protocol P1 {}
309309
protocol P2 {}
310310
var a : (P1 & P2)?
311-
var a2 : (P1)= 17 as! P1
311+
var a2 : P1= 17 as! P1

test/type/protocol_composition.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ func testConversion() {
123123
}
124124

125125
// Test the parser's splitting of >= into > and =.
126-
var x : protocol<P5>= 17 // expected-warning {{'protocol<...>' composition syntax is deprecated and not needed here}} {{9-22=(P5)=}}
126+
var x : protocol<P5>= 17 // expected-warning {{'protocol<...>' composition syntax is deprecated and not needed here}} {{9-22=P5=}}
127+
var y : protocol<P5, P7>= 17 // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}} {{9-26=(P5 & P7)=}}
127128

128-
typealias A = protocol<> // expected-warning {{'protocol<>' syntax is deprecated; use 'Any' instead}} {{15-25=Any}}
129+
typealias A1 = protocol<> // expected-warning {{'protocol<>' syntax is deprecated; use 'Any' instead}} {{16-26=Any}}
130+
typealias A2 = protocol<>? // expected-warning {{'protocol<>' syntax is deprecated; use 'Any' instead}} {{16-27=Any?}}
129131
typealias B1 = protocol<P1,P2> // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}} {{16-31=P1 & P2}}
130132
typealias B2 = protocol<P1, P2> // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}} {{16-32=P1 & P2}}
131133
typealias B3 = protocol<P1 ,P2> // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}} {{16-32=P1 & P2}}

0 commit comments

Comments
 (0)