Skip to content

Commit c543838

Browse files
committed
Sema: Rewrite partial applications into closures
When a method is called with fewer than two parameter lists, transform it into a fully-applied call by wrapping it in a closure. Eg, Foo.bar => { self in { args... self.bar(args...) } } foo.bar => { self in { args... self.bar(args...) } }(self) super.bar => { args... in super.bar(args...) } With this change, SILGen only ever sees fully-applied calls, which will allow ripping out some code. This new way of doing curry thunks fixes a long-standing bug where unbound references to protocol methods did not work. This is because such a reference must open the existential *inside* the closure, after 'self' has been applied, whereas the old SILGen implementation of curry thunks really wanted the type of the method reference to match the opened type of the method. A follow-up cleanup will remove the SILGen curry thunk implementation. Fixes rdar://21289579 and https://bugs.swift.org/browse/SR-75.
1 parent 4caa6d7 commit c543838

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+837
-823
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ Swift Next
4646
// OK, <U where U: Equatable> has broader visibility than <T where T == Int>
4747
override func foo() where U: Equatable { ... }
4848
}
49+
50+
* [SR-75][]:
51+
52+
Unapplied references to protocol methods methods are now supported. Previously this
53+
only worked for methods defined in structs, enums and classes.
54+
55+
```swift
56+
protocol Cat {
57+
func play(catToy: Toy)
58+
}
59+
60+
let fn = Cat.play
61+
fn(myCat)(myToy)
4962
```
5063

5164
* [SE-0266][]:
@@ -7939,6 +7952,7 @@ Swift 1.0
79397952
[SE-0267]: <https://github.com/apple/swift-evolution/blob/master/proposals/0267-where-on-contextually-generic.md>
79407953
[SE-0269]: <https://github.com/apple/swift-evolution/blob/master/proposals/0269-implicit-self-explicit-capture.md>
79417954

7955+
[SR-75]: <https://bugs.swift.org/browse/SR-75>
79427956
[SR-106]: <https://bugs.swift.org/browse/SR-106>
79437957
[SR-419]: <https://bugs.swift.org/browse/SR-419>
79447958
[SR-631]: <https://bugs.swift.org/browse/SR-631>

lib/SIL/SILDeclRef.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,11 @@ bool SILDeclRef::isTransparent() const {
450450
if (isStoredPropertyInitializer())
451451
return true;
452452

453-
if (hasAutoClosureExpr())
454-
return true;
453+
if (hasAutoClosureExpr()) {
454+
auto *ace = getAutoClosureExpr();
455+
if (ace->getThunkKind() == AutoClosureExpr::Kind::None)
456+
return true;
457+
}
455458

456459
if (hasDecl()) {
457460
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(getDecl()))

lib/SIL/SILProfiler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ static bool isUnmapped(ASTNode N) {
6868
LLVM_DEBUG(llvm::dbgs() << "Skipping ASTNode: implicit closure expr\n");
6969
return true;
7070
}
71+
72+
if (isa<AutoClosureExpr>(CE) &&
73+
cast<AutoClosureExpr>(CE)->getThunkKind() != AutoClosureExpr::Kind::None) {
74+
LLVM_DEBUG(llvm::dbgs() << "Skipping ASTNode: curry thunk expr\n");
75+
return true;
76+
}
7177
}
7278

7379
// Map all other kinds of expressions.

0 commit comments

Comments
 (0)