Skip to content

Commit 62aabec

Browse files
authored
Merge pull request swiftlang#36866 from ahoppen/pr/serialize-internal-closure-name
[Serialization] Serialize internal closure labels
2 parents 0712b42 + 380db63 commit 62aabec

File tree

10 files changed

+130
-18
lines changed

10 files changed

+130
-18
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ struct PrintOptions {
510510
result.PrintDocumentationComments = true;
511511
result.PrintRegularClangComments = true;
512512
result.PrintLongAttrsOnSeparateLines = true;
513+
result.AlwaysTryPrintParameterLabels = true;
513514
return result;
514515
}
515516

@@ -655,6 +656,7 @@ struct PrintOptions {
655656
PO.ShouldQualifyNestedDeclarations = QualifyNestedDeclarations::TypesOnly;
656657
PO.PrintParameterSpecifiers = true;
657658
PO.SkipImplicit = true;
659+
PO.AlwaysTryPrintParameterLabels = true;
658660
return PO;
659661
}
660662
};

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
135135
if (printFullConvention)
136136
result.PrintFunctionRepresentationAttrs =
137137
PrintOptions::FunctionRepresentationMode::Full;
138+
result.AlwaysTryPrintParameterLabels = true;
138139
result.PrintSPIs = printSPIs;
139140

140141
// We should print __consuming, __owned, etc for the module interface file.

lib/Serialization/Deserialization.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5248,12 +5248,13 @@ class TypeDeserializer {
52485248
break;
52495249

52505250
IdentifierID labelID;
5251+
IdentifierID internalLabelID;
52515252
TypeID typeID;
52525253
bool isVariadic, isAutoClosure, isNonEphemeral, isNoDerivative;
52535254
unsigned rawOwnership;
52545255
decls_block::FunctionParamLayout::readRecord(
5255-
scratch, labelID, typeID, isVariadic, isAutoClosure, isNonEphemeral,
5256-
rawOwnership, isNoDerivative);
5256+
scratch, labelID, internalLabelID, typeID, isVariadic, isAutoClosure,
5257+
isNonEphemeral, rawOwnership, isNoDerivative);
52575258

52585259
auto ownership =
52595260
getActualValueOwnership((serialization::ValueOwnership)rawOwnership);
@@ -5267,7 +5268,8 @@ class TypeDeserializer {
52675268
params.emplace_back(paramTy.get(), MF.getIdentifier(labelID),
52685269
ParameterTypeFlags(isVariadic, isAutoClosure,
52695270
isNonEphemeral, *ownership,
5270-
isNoDerivative));
5271+
isNoDerivative),
5272+
MF.getIdentifier(internalLabelID));
52715273
}
52725274

52735275
if (!isGeneric) {

lib/Serialization/ModuleFormat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5656
/// describe what change you made. The content of this comment isn't important;
5757
/// it just ensures a conflict if two people change the module format.
5858
/// Don't worry about adhering to the 80-column limit for this line.
59-
const uint16_t SWIFTMODULE_VERSION_MINOR = 607; // async / throws property decls
59+
const uint16_t SWIFTMODULE_VERSION_MINOR = 608; // internal parameter labels
6060

6161
/// A standard hash seed used for all string hashes in a serialized module.
6262
///
@@ -1007,6 +1007,7 @@ namespace decls_block {
10071007
using FunctionParamLayout = BCRecordLayout<
10081008
FUNCTION_PARAM,
10091009
IdentifierIDField, // name
1010+
IdentifierIDField, // internal label
10101011
TypeIDField, // type
10111012
BCFixed<1>, // vararg?
10121013
BCFixed<1>, // autoclosure?

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4345,6 +4345,7 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
43454345
FunctionParamLayout::emitRecord(
43464346
S.Out, S.ScratchRecord, abbrCode,
43474347
S.addDeclBaseNameRef(param.getLabel()),
4348+
S.addDeclBaseNameRef(param.getInternalLabel()),
43484349
S.addTypeRef(param.getPlainType()), paramFlags.isVariadic(),
43494350
paramFlags.isAutoClosure(), paramFlags.isNonEphemeral(), rawOwnership,
43504351
paramFlags.isNoDerivative());

test/IDE/complete_from_stdlib.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func testArchetypeReplacement2<BAR : Equatable>(_ a: [BAR]) {
8989
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super/IsSystem: min({#by: (Equatable, Equatable) throws -> Bool##(Equatable, Equatable) throws -> Bool#})[' rethrows'][#Equatable?#]{{; name=.+}}
9090
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super/IsSystem: max({#by: (Equatable, Equatable) throws -> Bool##(Equatable, Equatable) throws -> Bool#})[' rethrows'][#Equatable?#]{{; name=.+}}
9191
// FIXME: The following should include 'partialResult' as local parameter name: "(nextPartialResult): (_ partialResult: Result, Equatable)"
92-
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super/IsSystem: reduce({#(initialResult): Result#}, {#(nextPartialResult): (Result, Equatable) throws -> Result##(Result, Equatable) throws -> Result#})[' rethrows'][#Result#]{{; name=.+}}
92+
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super/IsSystem: reduce({#(initialResult): Result#}, {#(nextPartialResult): (Result, Equatable) throws -> Result##(_ partialResult: Result, Equatable) throws -> Result#})[' rethrows'][#Result#]{{; name=.+}}
9393
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super/IsSystem: dropFirst({#(k): Int#})[#ArraySlice<Equatable>#]{{; name=.+}}
9494
// FIXME: restore Decl[InstanceMethod]/Super: flatMap({#(transform): (Equatable) throws -> Sequence##(Equatable) throws -> Sequence#})[' rethrows'][#[IteratorProtocol.Element]#]{{; name=.+}}
9595

test/IDE/print_ast_tc_decls.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
// RUN: %target-swift-ide-test(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -swift-version 4 -skip-deinit=false -print-ast-typechecked -source-filename %s -F %S/Inputs/mock-sdk -function-definitions=false -prefer-type-repr=false -print-implicit-attrs=true -enable-objc-interop -disable-objc-attr-requires-foundation-module > %t.printed.txt
1414
// RUN: %FileCheck %s -check-prefix=PASS_COMMON -strict-whitespace < %t.printed.txt
15-
// RUN: %FileCheck %s -check-prefix=PASS_PRINT_AST -strict-whitespace < %t.printed.txt
15+
// RUN: %FileCheck %s -check-prefixes=PASS_PRINT_AST,PASS_PRINT_AST_TYPE -strict-whitespace < %t.printed.txt
1616
// RUN: %FileCheck %s -check-prefix=PASS_RW_PROP_GET_SET -strict-whitespace < %t.printed.txt
1717
// RUN: %FileCheck %s -check-prefix=PASS_2200 -strict-whitespace < %t.printed.txt
1818
// RUN: %FileCheck %s -check-prefix=PASS_2500 -strict-whitespace < %t.printed.txt
@@ -23,7 +23,7 @@
2323
//
2424
// RUN: %target-swift-ide-test(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -swift-version 4 -skip-deinit=false -print-ast-typechecked -source-filename %s -F %S/Inputs/mock-sdk -function-definitions=false -prefer-type-repr=true -print-implicit-attrs=true -enable-objc-interop -disable-objc-attr-requires-foundation-module > %t.printed.txt
2525
// RUN: %FileCheck %s -check-prefix=PASS_COMMON -strict-whitespace < %t.printed.txt
26-
// RUN: %FileCheck %s -check-prefix=PASS_PRINT_AST -strict-whitespace < %t.printed.txt
26+
// RUN: %FileCheck %s -check-prefixes=PASS_PRINT_AST,PASS_PRINT_AST_TYPEREPR -strict-whitespace < %t.printed.txt
2727
// RUN: %FileCheck %s -check-prefix=PASS_RW_PROP_GET_SET -strict-whitespace < %t.printed.txt
2828
// RUN: %FileCheck %s -check-prefix=PASS_2200 -strict-whitespace < %t.printed.txt
2929
// RUN: %FileCheck %s -check-prefix=PASS_2500 -strict-whitespace < %t.printed.txt
@@ -1343,9 +1343,16 @@ public func ParamAttrs5(a : (@escaping () -> ()) -> ()) {
13431343
// PASS_PRINT_AST: public typealias ParamAttrs6 = (@autoclosure () -> ()) -> ()
13441344
public typealias ParamAttrs6 = (@autoclosure () -> ()) -> ()
13451345

1346-
// PASS_PRINT_AST: public var ParamAttrs7: (@escaping () -> ()) -> ()
1346+
// The following type only has the internal paramter name inferred from the
1347+
// closure on the right-hand side of `=`. Thus, it is only part of the `Type`
1348+
// and not part of the `TypeRepr`.
1349+
// PASS_PRINT_AST_TYPE: public var ParamAttrs7: (_ f: @escaping () -> ()) -> ()
1350+
// PASS_PRINT_AST_TYPEREPR: public var ParamAttrs7: (@escaping () -> ()) -> ()
13471351
public var ParamAttrs7: (@escaping () -> ()) -> () = { f in f() }
13481352

1353+
// PASS_PRINT_AST: public var ParamAttrs8: (_ f: @escaping () -> ()) -> ()
1354+
public var ParamAttrs8: (_ f: @escaping () -> ()) -> () = { f in f() }
1355+
13491356
// Setter
13501357
// PASS_PRINT_AST: class FooClassComputed {
13511358
class FooClassComputed {

test/IDE/print_types.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ typealias MyInt = Int
99
// FULL: TypeAliasDecl '''MyInt''' swift_ide_test.MyInt.Type{{$}}
1010

1111
func testVariableTypes(_ param: Int, param2: inout Double) {
12-
// CHECK: FuncDecl '''testVariableTypes''' (Int, inout Double) -> (){{$}}
13-
// FULL: FuncDecl '''testVariableTypes''' (Swift.Int, inout Swift.Double) -> (){{$}}
12+
// CHECK: FuncDecl '''testVariableTypes''' (_ param: Int, param2: inout Double) -> (){{$}}
13+
// FULL: FuncDecl '''testVariableTypes''' (_ param: Swift.Int, param2: inout Swift.Double) -> (){{$}}
1414

1515
var a1 = 42
1616
// CHECK: VarDecl '''a1''' Int{{$}}
@@ -92,16 +92,16 @@ func testFuncType6() -> (Int, Int) {}
9292
// FULL: FuncDecl '''testFuncType6''' () -> (Swift.Int, Swift.Int){{$}}
9393

9494
func testFuncType7(_ a: Int, withFloat b: Float) {}
95-
// CHECK: FuncDecl '''testFuncType7''' (Int, Float) -> (){{$}}
96-
// FULL: FuncDecl '''testFuncType7''' (Swift.Int, Swift.Float) -> (){{$}}
95+
// CHECK: FuncDecl '''testFuncType7''' (_ a: Int, withFloat: Float) -> (){{$}}
96+
// FULL: FuncDecl '''testFuncType7''' (_ a: Swift.Int, withFloat: Swift.Float) -> (){{$}}
9797

9898
func testVariadicFuncType(_ a: Int, b: Float...) {}
99-
// CHECK: FuncDecl '''testVariadicFuncType''' (Int, Float...) -> (){{$}}
100-
// FULL: FuncDecl '''testVariadicFuncType''' (Swift.Int, Swift.Float...) -> (){{$}}
99+
// CHECK: FuncDecl '''testVariadicFuncType''' (_ a: Int, b: Float...) -> (){{$}}
100+
// FULL: FuncDecl '''testVariadicFuncType''' (_ a: Swift.Int, b: Swift.Float...) -> (){{$}}
101101

102102
func testCurriedFuncType1(_ a: Int) -> (_ b: Float) -> () {}
103-
// CHECK: FuncDecl '''testCurriedFuncType1''' (Int) -> (Float) -> (){{$}}
104-
// FULL: FuncDecl '''testCurriedFuncType1''' (Swift.Int) -> (Swift.Float) -> (){{$}}
103+
// CHECK: FuncDecl '''testCurriedFuncType1''' (_ a: Int) -> (_ b: Float) -> (){{$}}
104+
// FULL: FuncDecl '''testCurriedFuncType1''' (_ a: Swift.Int) -> (_ b: Swift.Float) -> (){{$}}
105105

106106
protocol FooProtocol {}
107107
protocol BarProtocol {}
@@ -110,8 +110,8 @@ protocol QuxProtocol { associatedtype Qux }
110110
struct GenericStruct<A, B : FooProtocol> {}
111111

112112
func testInGenericFunc1<A, B : FooProtocol, C : FooProtocol & BarProtocol>(_ a: A, b: B, c: C) {
113-
// CHECK: FuncDecl '''testInGenericFunc1''' <A, B, C where B : FooProtocol, C : BarProtocol, C : FooProtocol> (A, b: B, c: C) -> (){{$}}
114-
// FULL: FuncDecl '''testInGenericFunc1''' <A, B, C where B : swift_ide_test.FooProtocol, C : swift_ide_test.BarProtocol, C : swift_ide_test.FooProtocol> (A, b: B, c: C) -> (){{$}}
113+
// CHECK: FuncDecl '''testInGenericFunc1''' <A, B, C where B : FooProtocol, C : BarProtocol, C : FooProtocol> (_ a: A, b: B, c: C) -> (){{$}}
114+
// FULL: FuncDecl '''testInGenericFunc1''' <A, B, C where B : swift_ide_test.FooProtocol, C : swift_ide_test.BarProtocol, C : swift_ide_test.FooProtocol> (_ a: A, b: B, c: C) -> (){{$}}
115115

116116
var a1 = a
117117
_ = a1; a1 = a
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t.module-cache)
3+
//
4+
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path %t/label.swiftinterface -enable-library-evolution -module-cache-path %t.module-cache %s
5+
// RUN: %FileCheck --check-prefix=SWIFT_INTERFACE %s < %t/label.swiftinterface
6+
// RUN: %sourcekitd-test -req=doc-info -module label -- -I %t -target %target-triple -module-cache-path %t.module-cache > %t.response
7+
// RUN: %diff -u %s.response %t.response
8+
//
9+
// RUN: %empty-directory(%t)
10+
// RUN: %empty-directory(%t.module-cache)
11+
// RUN: %target-swift-frontend -emit-module %s -module-name label -emit-module-path %t/label.swiftmodule -module-cache-path %t.module-cache
12+
// RUN: %sourcekitd-test -req=doc-info -module label -- -I %t -target %target-triple -module-cache-path %t.module-cache> %t.response
13+
// RUN: %diff -u %s.response %t.response
14+
15+
public func foo(_ callback: (_ myInternalParam: Int) -> Void) {}
16+
17+
// SWIFT_INTERFACE: import Swift
18+
// SWIFT_INTERFACE: public func foo(_ callback: (_ myInternalParam: Swift.Int) -> Swift.Void)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import SwiftOnoneSupport
2+
3+
func foo(_ callback: (_ myInternalParam: Int) -> Void)
4+
5+
6+
[
7+
{
8+
key.kind: source.lang.swift.syntaxtype.keyword,
9+
key.offset: 0,
10+
key.length: 6
11+
},
12+
{
13+
key.kind: source.lang.swift.syntaxtype.identifier,
14+
key.offset: 7,
15+
key.length: 17
16+
},
17+
{
18+
key.kind: source.lang.swift.syntaxtype.keyword,
19+
key.offset: 26,
20+
key.length: 4
21+
},
22+
{
23+
key.kind: source.lang.swift.syntaxtype.identifier,
24+
key.offset: 31,
25+
key.length: 3
26+
},
27+
{
28+
key.kind: source.lang.swift.syntaxtype.argument,
29+
key.offset: 35,
30+
key.length: 1
31+
},
32+
{
33+
key.kind: source.lang.swift.syntaxtype.parameter,
34+
key.offset: 37,
35+
key.length: 8
36+
},
37+
{
38+
key.kind: source.lang.swift.syntaxtype.keyword,
39+
key.offset: 48,
40+
key.length: 1
41+
},
42+
{
43+
key.kind: source.lang.swift.syntaxtype.identifier,
44+
key.offset: 50,
45+
key.length: 15
46+
},
47+
{
48+
key.kind: source.lang.swift.ref.struct,
49+
key.name: "Int",
50+
key.usr: "s:Si",
51+
key.offset: 67,
52+
key.length: 3
53+
},
54+
{
55+
key.kind: source.lang.swift.ref.typealias,
56+
key.name: "Void",
57+
key.usr: "s:s4Voida",
58+
key.offset: 75,
59+
key.length: 4
60+
}
61+
]
62+
[
63+
{
64+
key.kind: source.lang.swift.decl.function.free,
65+
key.name: "foo(_:)",
66+
key.usr: "s:5label3fooyyySiXEF",
67+
key.offset: 26,
68+
key.length: 54,
69+
key.fully_annotated_decl: "<decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>foo</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>_</decl.var.parameter.argument_label> <decl.var.parameter.name>callback</decl.var.parameter.name>: <decl.var.parameter.type>(<decl.var.parameter>_ <decl.var.parameter.name>myInternalParam</decl.var.parameter.name>: <decl.var.parameter.type><ref.struct usr=\"s:Si\">Int</ref.struct></decl.var.parameter.type></decl.var.parameter>) -&gt; <decl.function.returntype><ref.typealias usr=\"s:s4Voida\">Void</ref.typealias></decl.function.returntype></decl.var.parameter.type></decl.var.parameter>)</decl.function.free>",
70+
key.entities: [
71+
{
72+
key.kind: source.lang.swift.decl.var.local,
73+
key.keyword: "_",
74+
key.name: "callback",
75+
key.offset: 47,
76+
key.length: 32
77+
}
78+
]
79+
}
80+
]

0 commit comments

Comments
 (0)