Skip to content

[Parser] Fix the fixit for protocol compositions' syntax change (3.0) #4486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/Parse/ParseType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ ParserResult<TypeRepr> Parser::parseTypeIdentifierOrTypeComposition() {
// Skip until we hit the '>'.
RAngleLoc = skipUntilGreaterInTypeList(/*protocolComposition=*/true);
}

auto composition = ProtocolCompositionTypeRepr::create(
Context, Protocols, ProtocolLoc, {LAngleLoc, RAngleLoc});

Expand All @@ -470,6 +470,16 @@ ParserResult<TypeRepr> Parser::parseTypeIdentifierOrTypeComposition() {
replacement += extractText(*Begin);
}

// Copy trailing content after '>' to the replacement string.
// FIXME: lexer should smartly separate '>' and trailing contents like '?'.
StringRef TrailingContent = L->getTokenAt(RAngleLoc).getRange().str().
substr(1);
if (!TrailingContent.empty()) {
replacement.insert(replacement.begin(), '(');
replacement += ")";
replacement += TrailingContent;
}

// Replace 'protocol<T1, T2>' with 'T1 & T2'
diagnose(ProtocolLoc,
Protocols.size() > 1 ? diag::deprecated_protocol_composition
Expand Down
5 changes: 5 additions & 0 deletions test/FixCode/fixits-apply.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,8 @@ func testBoolValue(a : BoolFoo) {
guard a {}
if a as BoolFoo {}
}

protocol P1 {}
protocol P2 {}
var a : protocol<P1, P2>?
var a2 : protocol<P1>= 17
5 changes: 5 additions & 0 deletions test/FixCode/fixits-apply.swift.result
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,8 @@ func testBoolValue(a : BoolFoo) {
guard a.boolValue {}
if (a as BoolFoo).boolValue {}
}

protocol P1 {}
protocol P2 {}
var a : (P1 & P2)?
var a2 : (P1)= 17 as! P1
4 changes: 2 additions & 2 deletions test/type/protocol_composition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func testEquality() {
let x4 : (_ : P2) -> ()
x3 = x4
_ = x3

// Empty protocol-conformance types are empty.
let x5 : (_ : Any) -> ()
let x6 : (_ : Any2) -> ()
Expand Down Expand Up @@ -123,7 +123,7 @@ func testConversion() {
}

// Test the parser's splitting of >= into > and =.
var x : protocol<P5>= 17 // expected-warning {{'protocol<...>' composition syntax is deprecated and not needed here}} {{9-22=P5}}
var x : protocol<P5>= 17 // expected-warning {{'protocol<...>' composition syntax is deprecated and not needed here}} {{9-22=(P5)=}}

typealias A = protocol<> // expected-warning {{'protocol<>' syntax is deprecated; use 'Any' instead}} {{15-25=Any}}
typealias B1 = protocol<P1,P2> // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}} {{16-31=P1 & P2}}
Expand Down