Skip to content

Commit a160f0f

Browse files
committed
[Parse] Improve diagnostic for deprecated protocol<...> syntax
* Don't emit deprecated warnings for incomplete protocol<...>. E.g: `func fn(x: protocol<P) {}` * Different message for single protocol compostion. E.g: `typealias MyError = protocol<Error>`
1 parent bc09af3 commit a160f0f

File tree

5 files changed

+27
-23
lines changed

5 files changed

+27
-23
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,8 @@ ERROR(disallowed_protocol_composition,PointsToFirstBadToken,
655655

656656
WARNING(deprecated_protocol_composition,none,
657657
"'protocol<...>' composition syntax is deprecated; join the protocols using '&'", ())
658+
WARNING(deprecated_protocol_composition_single,none,
659+
"'protocol<...>' composition syntax is deprecated and not needed here", ())
658660
WARNING(deprecated_any_composition,none,
659661
"'protocol<>' syntax is deprecated; use 'Any' instead", ())
660662

lib/Parse/ParseType.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -458,17 +458,23 @@ ParserResult<TypeRepr> Parser::parseTypeIdentifierOrTypeComposition() {
458458
RAngleLoc = skipUntilGreaterInTypeList(/*protocolComposition=*/true);
459459
}
460460

461-
// Replace 'protocol<T1, T2>' with 'T1 & T2'
462-
auto Diag = diagnose(ProtocolLoc, diag::deprecated_protocol_composition);
463461
auto composition = ProtocolCompositionTypeRepr::create(
464462
Context, Protocols, ProtocolLoc, {LAngleLoc, RAngleLoc});
465463

466-
Diag.highlight({ProtocolLoc, RAngleLoc});
467-
for (auto Comma : Commas) // remove commas and '<'
468-
Diag.fixItReplace(Comma, " &");
469-
Diag
470-
.fixItRemove({ProtocolLoc, LAngleLoc}) // remove 'protocol<'
471-
.fixItRemove(RAngleLoc); // remove '>'
464+
if (Status.isSuccess()) {
465+
// Only if we have complete protocol<...> construct, diagnose deprecated.
466+
auto Diag = diagnose(ProtocolLoc,
467+
Commas.size() > 0 ? diag::deprecated_protocol_composition
468+
: diag::deprecated_protocol_composition_single);
469+
Diag.highlight({ProtocolLoc, RAngleLoc});
470+
471+
// Replace 'protocol<T1, T2>' with 'T1 & T2'
472+
for (auto Comma : Commas) // remove commas and '<'
473+
Diag.fixItReplace(Comma, " &");
474+
Diag
475+
.fixItRemove({ProtocolLoc, LAngleLoc}) // remove 'protocol<'
476+
.fixItRemove(RAngleLoc); // remove '>'
477+
}
472478

473479
return makeParserResult(Status, composition);
474480
}

test/Parse/recovery.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ struct ErrorTypeInVarDecl7 {
281281
}
282282

283283
struct ErrorTypeInVarDecl8 {
284-
var v1 : protocol<FooProtocol // expected-error {{expected '>' to complete protocol composition type}} expected-note {{to match this opening '<'}} expected-warning{{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}}
284+
var v1 : protocol<FooProtocol // expected-error {{expected '>' to complete protocol composition type}} expected-note {{to match this opening '<'}}
285285
var v2 : Int
286286
}
287287

@@ -291,25 +291,22 @@ struct ErrorTypeInVarDecl9 {
291291
}
292292

293293
struct ErrorTypeInVarDecl10 {
294-
var v1 : protocol<FooProtocol // expected-error {{expected '>' to complete protocol composition type}} expected-note {{to match this opening '<'}} expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}}
294+
var v1 : protocol<FooProtocol // expected-error {{expected '>' to complete protocol composition type}} expected-note {{to match this opening '<'}}
295295
var v2 : Int
296296
}
297297

298298
struct ErrorTypeInVarDecl11 {
299-
var v1 : protocol<FooProtocol, // expected-error {{expected identifier for type name}} expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}}
299+
var v1 : protocol<FooProtocol, // expected-error {{expected identifier for type name}}
300300
var v2 : Int
301301
}
302302

303303
func ErrorTypeInPattern1(_: protocol<) { } // expected-error {{expected identifier for type name}}
304-
// expected-warning @-1 {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}}
305-
func ErrorTypeInPattern2(_: protocol<F) { } // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}}
306-
// expected-error@-1 {{expected '>' to complete protocol composition type}}
307-
// expected-note@-2 {{to match this opening '<'}}
308-
// expected-error@-3 {{use of undeclared type 'F'}}
304+
func ErrorTypeInPattern2(_: protocol<F) { } // expected-error {{expected '>' to complete protocol composition type}}
305+
// expected-note@-1 {{to match this opening '<'}}
306+
// expected-error@-2 {{use of undeclared type 'F'}}
309307

310308
func ErrorTypeInPattern3(_: protocol<F,) { } // expected-error {{expected identifier for type name}}
311309
// expected-error@-1 {{use of undeclared type 'F'}}
312-
// expected-warning@-2 {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}}
313310

314311
struct ErrorTypeInVarDecl12 {
315312
var v1 : FooProtocol & // expected-error{{expected identifier for type name}}

test/expr/expressions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func test_lambda() {
230230

231231
func test_lambda2() {
232232
{ () -> protocol<Int> in
233-
// expected-warning @-1 {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}}
233+
// expected-warning @-1 {{'protocol<...>' composition syntax is deprecated and not needed here}} {{11-20=}} {{23-24=}}
234234
// expected-error @-2 {{non-protocol type 'Int' cannot be used within a protocol composition}}
235235
// expected-warning @-3 {{result of call is unused}}
236236
return 1

test/type/protocol_composition.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,21 @@ func testConversion() {
111111
accept_manyPrintable(sp)
112112

113113
// Conversions among existential types.
114-
var x2 : protocol<SuperREPLPrintable, FooProtocol> // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}}
114+
var x2 : protocol<SuperREPLPrintable, FooProtocol> // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}} {{12-21=}} {{39-40= &}} {{52-53=}}
115115
x2 = x // expected-error{{value of type 'FooProtocol & REPLPrintable' does not conform to 'FooProtocol & SuperREPLPrintable' in assignment}}
116116
x = x2
117117

118118
// Subtyping
119119
var _ : () -> FooProtocol & SuperREPLPrintable = return_superPrintable
120120

121121
// FIXME: closures make ABI conversions explicit. rdar://problem/19517003
122-
var _ : () -> protocol<FooProtocol, REPLPrintable> = { return_superPrintable() } // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}}
122+
var _ : () -> protocol<FooProtocol, REPLPrintable> = { return_superPrintable() } // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}} {{17-26=}} {{37-38= &}} {{52-53=}}
123123
}
124124

125125
// Test the parser's splitting of >= into > and =.
126-
var x : protocol<P5>= 17 // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}}
126+
var x : protocol<P5>= 17 // expected-warning {{'protocol<...>' composition syntax is deprecated and not needed here}} {{9-18=}} {{20-22=}}
127127

128128
typealias A = protocol<> // expected-warning {{'protocol<>' syntax is deprecated; use 'Any' instead}} {{15-25=Any}}
129129
typealias B = protocol<P1, P2> // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}} {{15-24=}} {{26-27= &}} {{30-31=}}
130130

131-
132-
131+
typealias C = protocol<P1> // expected-warning {{'protocol<...>' composition syntax is deprecated and not needed here}} {{15-24=}} {{26-27=}}

0 commit comments

Comments
 (0)