Skip to content

Commit 72ebe8d

Browse files
committed
[Parse] Attach 'inout' to Swift 2 unparenthesized function params. (#4241)
This syntax is no longer permitted, but we need to parse it like Swift 2 did in order to produce the fix-it that matches Swift 2 behavior. rdar://problem/26681485 (cherry picked from commit 294bbd8)
1 parent 611c6be commit 72ebe8d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/Parse/ParsePattern.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,29 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
278278
status |= type;
279279
param.Type = type.getPtrOrNull();
280280

281+
if (param.SpecifierKind == ParsedParameter::InOut) {
282+
if (auto *fnTR = dyn_cast_or_null<FunctionTypeRepr>(param.Type)) {
283+
// If the input to the function isn't parenthesized, apply the inout
284+
// to the first (only) parameter, as we would in Swift 2. (This
285+
// syntax is deprecated in Swift 3.)
286+
TypeRepr *argsTR = fnTR->getArgsTypeRepr();
287+
if (!isa<TupleTypeRepr>(argsTR)) {
288+
auto *newArgsTR =
289+
new (Context) InOutTypeRepr(argsTR, param.LetVarInOutLoc);
290+
auto *newTR =
291+
new (Context) FunctionTypeRepr(fnTR->getGenericParams(),
292+
newArgsTR,
293+
fnTR->getThrowsLoc(),
294+
fnTR->getArrowLoc(),
295+
fnTR->getResultTypeRepr());
296+
newTR->setGenericSignature(fnTR->getGenericSignature());
297+
param.Type = newTR;
298+
param.SpecifierKind = ParsedParameter::Let;
299+
param.LetVarInOutLoc = SourceLoc();
300+
}
301+
}
302+
}
303+
281304
// If we didn't parse a type, then we already diagnosed that the type
282305
// was invalid. Remember that.
283306
if (type.isParseError() && !type.hasCodeCompletion())

test/type/types.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ class r21949448 {
158158

159159
// SE-0066 - Standardize function type argument syntax to require parentheses
160160
let _ : Int -> Float // expected-error {{single argument function types require parentheses}} {{9-9=(}} {{12-12=)}}
161+
let _ : inout Int -> Float // expected-error {{single argument function types require parentheses}} {{9-9=(}} {{18-18=)}}
162+
func testNoParenFunction(x: Int -> Float) {} // expected-error {{single argument function types require parentheses}} {{29-29=(}} {{32-32=)}}
163+
func testNoParenFunction(x: inout Int -> Float) {} // expected-error {{single argument function types require parentheses}} {{29-29=(}} {{38-38=)}}
161164

162165
func foo1(a : UnsafePointer<Void>) {} // expected-warning {{UnsafePointer<Void> has been replaced by UnsafeRawPointer}}{{15-34=UnsafeRawPointer}}
163166
func foo2(a : UnsafeMutablePointer<()>) {} // expected-warning {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}}{{15-39=UnsafeMutableRawPointer}}

0 commit comments

Comments
 (0)